contacts.js 2.5 KB

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