contacts.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. const ContactModel = require('../model/contact');
  2. const UserModel = require('../model/user');
  3. const ChatModel = require('../model/chat');
  4. const MessageModel = require('../model/message');
  5. const listContacts = async (req, res, next) => {
  6. try {
  7. const userId = req.user.id;
  8. const contacts = await ContactModel.getList({ owner: userId }, req.query);
  9. return res.json({
  10. status: 'success',
  11. code: 200,
  12. data: {
  13. ...contacts,
  14. },
  15. });
  16. } catch (e) {
  17. next(e);
  18. }
  19. };
  20. const getContactById = async (req, res, next) => {
  21. try {
  22. const id = req.params.id;
  23. const userId = req.user.id;
  24. const contact = await ContactModel.getById(id, userId);
  25. if (contact)
  26. return res.json({
  27. status: 'success',
  28. code: 200,
  29. data: {
  30. contact,
  31. },
  32. });
  33. return res.status(404).json({
  34. status: 'error',
  35. code: 404,
  36. data: 'Not Found',
  37. });
  38. } catch (e) {
  39. next(e);
  40. }
  41. };
  42. const addContact = async (req, res, next) => {
  43. try {
  44. const userId = req.user.id;
  45. const userNumber = req.user.number;
  46. const number = req.body.number;
  47. const user = await UserModel.findByNumber(number);
  48. const isExist = await ContactModel.getByField(number, userId);
  49. if (user && userNumber !== number && !isExist) {
  50. const { name, lastName, country, avatarUrl, color, _id } = user;
  51. const newContact = await ContactModel.add({
  52. companionId: _id,
  53. name,
  54. lastName,
  55. number,
  56. country,
  57. avatarUrl,
  58. color,
  59. owner: userId,
  60. });
  61. return res.status(201).json({
  62. status: 'success',
  63. code: 201,
  64. data: newContact,
  65. });
  66. }
  67. return res.status(404).json({
  68. status: 'error',
  69. code: 404,
  70. data: 'Not Found',
  71. });
  72. } catch (e) {
  73. next(e);
  74. }
  75. };
  76. const pinContact = async (req, res, next) => {
  77. try {
  78. const { id, pinned } = req.body;
  79. const userId = req.user.id;
  80. await ContactModel.update(id, userId, { pinned });
  81. return res.status(200).json({
  82. status: 'success',
  83. code: 200,
  84. data: {},
  85. });
  86. } catch (e) {
  87. next(e);
  88. }
  89. };
  90. const updateContact = async (req, res, next) => {
  91. try {
  92. const { id, _id, name, lastName, companionId } = req.body;
  93. const userId = req.user.id;
  94. const userChat = await ChatModel.getByField(companionId, userId);
  95. await ContactModel.update(id, userId, { name, lastName });
  96. await ChatModel.update(_id, userId, { name, lastName });
  97. await MessageModel.updateOwnerMessages(
  98. { companionId, companionIdFlow: { $ne: userId }, owner: userId },
  99. { name, lastName }
  100. );
  101. await MessageModel.updateOwnerMessages(
  102. {
  103. companionId,
  104. replyName: { $eq: userChat.name },
  105. replyLastName: { $eq: userChat.lastName },
  106. owner: userId,
  107. },
  108. { replyName: name, replyLastName: lastName }
  109. );
  110. return res.status(200).json({
  111. data: {},
  112. });
  113. } catch (e) {
  114. next(e);
  115. }
  116. };
  117. const removeContact = async (req, res, next) => {
  118. try {
  119. const id = req.params.id;
  120. const userId = req.user.id;
  121. const contact = await ContactModel.remove(id, userId);
  122. if (contact) {
  123. return res.json({
  124. status: 'success',
  125. code: 200,
  126. data: {
  127. contact,
  128. },
  129. });
  130. } else {
  131. return res.status(404).json({
  132. status: 'error',
  133. code: 404,
  134. data: 'Not Found',
  135. });
  136. }
  137. } catch (e) {
  138. next(e);
  139. }
  140. };
  141. module.exports = {
  142. listContacts,
  143. getContactById,
  144. addContact,
  145. removeContact,
  146. pinContact,
  147. updateContact,
  148. };