contacts.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 updateContact = async (req, res, next) => {
  77. try {
  78. const { id, _id, name, lastName, companionId } = req.body;
  79. const userId = req.user.id;
  80. await ContactModel.update(id, userId, { name, lastName });
  81. await ChatModel.update(_id, userId, { name, lastName });
  82. await MessageModel.updateOwnerMessages(
  83. { companionId, companionIdFlow: { $ne: userId }, owner: userId },
  84. { name, lastName }
  85. );
  86. return res.status(200).json({
  87. data: {},
  88. });
  89. } catch (e) {
  90. next(e);
  91. }
  92. };
  93. const removeContact = async (req, res, next) => {
  94. try {
  95. const id = req.params.id;
  96. const userId = req.user.id;
  97. const contact = await ContactModel.remove(id, userId);
  98. if (contact) {
  99. return res.json({
  100. status: 'success',
  101. code: 200,
  102. data: {
  103. contact,
  104. },
  105. });
  106. } else {
  107. return res.status(404).json({
  108. status: 'error',
  109. code: 404,
  110. data: 'Not Found',
  111. });
  112. }
  113. } catch (e) {
  114. next(e);
  115. }
  116. };
  117. module.exports = {
  118. listContacts,
  119. getContactById,
  120. addContact,
  121. removeContact,
  122. updateContact,
  123. };