user.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. const UserModel = require('../model/user');
  2. const ChatModel = require('../model/chat');
  3. const MessageModel = require('../model/message');
  4. const ContactModel = require('../model/contact');
  5. const fs = require('fs').promises;
  6. const Jimp = require('jimp');
  7. const jwt = require('jsonwebtoken');
  8. const createFolderIsExist = require('../helpers/create-directory');
  9. require('dotenv').config();
  10. const s3 = require('../helpers/aws');
  11. const client = require('../helpers/twilio');
  12. const phoneToken = require('generate-sms-verification-code');
  13. const SECRET_KEY = process.env.JWT_SECRET;
  14. const AWS_BUCKET_NAME = process.env.AWS_BUCKET_NAME;
  15. const createNewUser = async (req, res, next) => {
  16. try {
  17. const code = phoneToken(4, { type: 'number' });
  18. const color = `#${Math.floor(Math.random() * 16777215).toString(16)}`;
  19. const { number, country } = req.body;
  20. const isExist = await UserModel.findByNumber(number);
  21. if (isExist) {
  22. await UserModel.updateUser(isExist._id, { code });
  23. } else {
  24. await UserModel.createUser({ number, country, color, code });
  25. }
  26. client.messages.create({
  27. body: `${code}`,
  28. to: `${number}`,
  29. from: '+18305875860',
  30. });
  31. return res.status(201).json({
  32. status: 'success',
  33. code: 201,
  34. data: String(code),
  35. });
  36. } catch (e) {
  37. next(e);
  38. }
  39. };
  40. const logIn = async (req, res, next) => {
  41. try {
  42. const { number, code } = req.body;
  43. const user = await UserModel.findByNumber(number);
  44. if (!user || user.code !== code)
  45. return res.status(401).json({
  46. status: 'error',
  47. code: 401,
  48. data: 'UNAUTHORIZED',
  49. message: 'Invalid credentials',
  50. });
  51. const id = user._id;
  52. const payload = { id };
  53. const token = jwt.sign(payload, SECRET_KEY, { expiresIn: '24h' });
  54. let registered = true;
  55. if (!user.originalName || !user.originalLastName) registered = false;
  56. const online = true;
  57. await UserModel.updateUser(id, { token, code: '', online });
  58. await ChatModel.updateCompanionsChat(id, { online });
  59. return res.status(200).json({
  60. status: 'success',
  61. code: 200,
  62. data: {
  63. token,
  64. registered,
  65. },
  66. });
  67. } catch (e) {
  68. next(e);
  69. }
  70. };
  71. const logOut = async (req, res, next) => {
  72. try {
  73. const id = req.user.id;
  74. const user = await UserModel.findById(id);
  75. const online = new Date();
  76. if (!user)
  77. return res.status(401).json({
  78. status: 'error',
  79. code: 401,
  80. data: 'UNAUTHORIZED',
  81. message: 'Invalid credentials',
  82. });
  83. await UserModel.updateUser(id, { token: null, online });
  84. await ChatModel.updateCompanionsChat(id, { online });
  85. return res.status(204).json({});
  86. } catch (e) {
  87. next(e);
  88. }
  89. };
  90. const online = async (req, res, next) => {
  91. try {
  92. const id = req.user.id;
  93. const user = await UserModel.findById(id);
  94. const online = new Date();
  95. if (!user)
  96. return res.status(401).json({
  97. status: 'error',
  98. code: 401,
  99. data: 'UNAUTHORIZED',
  100. message: 'Invalid credentials',
  101. });
  102. await ChatModel.updateCompanionsChat(id, { online });
  103. await UserModel.updateUser(id, { online });
  104. return res.status(204).json({});
  105. } catch (e) {
  106. next(e);
  107. }
  108. };
  109. const getCurrent = async (req, res, next) => {
  110. try {
  111. const user = req.user;
  112. if (!user)
  113. return res.status(401).json({
  114. status: 'error',
  115. code: 401,
  116. data: 'UNAUTHORIZED',
  117. message: 'Invalid credentials',
  118. });
  119. const id = req.user.id;
  120. const online = true;
  121. await UserModel.updateUser(id, { online });
  122. await ChatModel.updateCompanionsChat(id, { online });
  123. const updatedUser = await UserModel.findById(id);
  124. return res.status(200).json({
  125. status: 'success',
  126. code: 200,
  127. data: updatedUser,
  128. });
  129. } catch (e) {
  130. next(e);
  131. }
  132. };
  133. const updateCredentials = async (req, res, next) => {
  134. try {
  135. const user = req.user;
  136. const { id, token } = user;
  137. const { name, lastName, originalName, originalLastName } = req.body;
  138. await UserModel.updateUser(id, req.body);
  139. await ChatModel.updateCompanionsChat(id, {
  140. originalName,
  141. originalLastName,
  142. });
  143. await MessageModel.updateOwnerMessages(
  144. { companionIdFlow: { $eq: id }, owner: id },
  145. { name, lastName }
  146. );
  147. await MessageModel.updateOwnerMessages(
  148. {
  149. replyName: { $eq: user.name },
  150. replyLastName: { $eq: user.lastName },
  151. owner: id,
  152. },
  153. { replyName: name, replyLastName: lastName }
  154. );
  155. await MessageModel.updateOwnerMessages(
  156. {
  157. companionIdForwardToAndFrom: { $eq: user._id },
  158. forwardName: { $eq: user.name },
  159. forwardLastName: { $eq: user.lastName },
  160. },
  161. { forwardName: name, forwardLastName: lastName }
  162. );
  163. await MessageModel.updateOwnerMessages(
  164. {
  165. number: { $eq: user.number },
  166. forwardName: { $eq: user.name },
  167. forwardLastName: { $eq: user.lastName },
  168. },
  169. { forwardName: name, forwardLastName: lastName }
  170. );
  171. return res.status(200).json({
  172. data: {
  173. token,
  174. },
  175. });
  176. } catch (e) {
  177. next(e);
  178. }
  179. };
  180. const updateUser = async (req, res, next) => {
  181. try {
  182. const { id } = req.user;
  183. await UserModel.updateUser(id, req.body);
  184. const socketId = req.body.socketId;
  185. return res.status(200).json({
  186. data: {},
  187. });
  188. } catch (e) {
  189. next(e);
  190. }
  191. };
  192. const removeAvatar = async (req, res, next) => {
  193. try {
  194. const { id, avatarsArr, number } = req.user;
  195. const index = Number(req.params.index);
  196. const toDelete = avatarsArr[index];
  197. let filteredAvatars;
  198. let avatarUrl;
  199. if (index === 0 && avatarsArr.length === 1) {
  200. filteredAvatars = [];
  201. avatarUrl = null;
  202. } else if (index >= 0 && avatarsArr.length > 1) {
  203. filteredAvatars = [...avatarsArr].filter(
  204. ({ avatarUrl }) => avatarUrl !== toDelete.avatarUrl
  205. );
  206. avatarUrl = filteredAvatars[0].avatarUrl;
  207. }
  208. const params = {
  209. Bucket: AWS_BUCKET_NAME,
  210. Key: `${toDelete.avatarUrl}`,
  211. };
  212. s3.deleteObject(params, async function (err, _data) {
  213. if (err) throw err;
  214. });
  215. await UserModel.updateUser(id, {
  216. avatarUrl,
  217. avatarsArr: filteredAvatars,
  218. });
  219. await ChatModel.updateCompanionsChat(id, {
  220. avatarUrl,
  221. avatarsArr: filteredAvatars,
  222. });
  223. await ContactModel.updateCompanionsContact(number, { avatarUrl });
  224. await MessageModel.updateOwnerMessages(
  225. { companionIdFlow: { $eq: id } },
  226. { avatarUrl }
  227. );
  228. return res.status(200).json({
  229. data: {},
  230. });
  231. } catch (e) {
  232. next(e);
  233. }
  234. };
  235. const updateAvatar = async (req, res, next) => {
  236. try {
  237. const userId = req.user.id;
  238. const userNumber = req.user.number;
  239. const pathToFile = req.file.path;
  240. const originalName = req.file.originalname;
  241. const avatarUrl = `${Math.round(
  242. Date.now() / 1000
  243. )}${userId}${originalName}`;
  244. const cropImg = await Jimp.read(pathToFile);
  245. await cropImg
  246. .autocrop()
  247. .cover(
  248. 250,
  249. 250,
  250. Jimp.HORIZONTAL_ALIGN_CENTER | Jimp.VERTICAL_ALIGN_MIDDLE
  251. )
  252. .writeAsync(pathToFile);
  253. const fileContent = await fs.readFile(pathToFile);
  254. const params = {
  255. Bucket: AWS_BUCKET_NAME,
  256. Key: `${avatarUrl}`,
  257. Body: fileContent,
  258. };
  259. s3.upload(params, async (err, _data) => {
  260. if (err) throw err;
  261. fs.unlink(pathToFile);
  262. });
  263. const avatarsArr = [
  264. { avatarUrl, updatedAt: new Date() },
  265. ...req.user.avatarsArr,
  266. ];
  267. await UserModel.updateUser(userId, { avatarUrl, avatarsArr });
  268. await ChatModel.updateCompanionsChat(userId, { avatarUrl, avatarsArr });
  269. await ContactModel.updateCompanionsContact(userNumber, { avatarUrl });
  270. await MessageModel.updateOwnerMessages(
  271. { companionIdFlow: { $eq: userId } },
  272. { avatarUrl }
  273. );
  274. return res.status(200).json({
  275. status: 'success',
  276. code: 200,
  277. data: {},
  278. });
  279. } catch (e) {
  280. next(e);
  281. }
  282. };
  283. module.exports = {
  284. createNewUser,
  285. logIn,
  286. logOut,
  287. online,
  288. getCurrent,
  289. updateCredentials,
  290. updateUser,
  291. removeAvatar,
  292. updateAvatar,
  293. };