contacts.js 2.5 KB

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