contacts.js 2.5 KB

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