chats.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. const ChatModel = require('../model/chat');
  2. const UserModel = require('../model/user');
  3. const MessageModel = require('../model/message');
  4. const listChats = async (req, res, next) => {
  5. try {
  6. const userId = req.user.id;
  7. const chats = await ChatModel.getList(userId, req.query);
  8. const messages = await MessageModel.getLastMessagesList(userId);
  9. const lastMessages = await messages.reduce(
  10. (acc, { message, companionId }) => {
  11. const obj = { message, companionId };
  12. if (acc.length === 0) {
  13. acc.push(obj);
  14. return acc;
  15. }
  16. if (acc.some((el) => el.companionId === companionId)) {
  17. const i = acc.findIndex((el) => {
  18. return el.companionId === companionId;
  19. });
  20. acc[i] = obj;
  21. return acc;
  22. }
  23. acc.push(obj);
  24. return acc;
  25. },
  26. []
  27. );
  28. return res.json({
  29. status: 'success',
  30. code: 200,
  31. data: {
  32. ...chats,
  33. lastMessages,
  34. },
  35. });
  36. } catch (e) {
  37. next(e);
  38. }
  39. };
  40. const startChat = async (req, res, next) => {
  41. try {
  42. const id = req.body.id;
  43. const user = req.user;
  44. const userId = user.id;
  45. const companion = await UserModel.findById(id);
  46. const isUser = await ChatModel.getByField(id, userId);
  47. const isCompanion = await ChatModel.getByField(userId, id);
  48. const { name, lastName, avatarUrl, color, online, number } = companion;
  49. const {
  50. name: Name,
  51. lastName: LastName,
  52. avatarUrl: AvatarUrl,
  53. color: Color,
  54. online: Online,
  55. number: Number,
  56. } = user;
  57. if (companion && (isUser || isCompanion)) {
  58. await ChatModel.update(isUser._id, {
  59. name,
  60. lastName,
  61. avatarUrl,
  62. color,
  63. online,
  64. number,
  65. });
  66. await ChatModel.update(isCompanion._id, {
  67. name: Name,
  68. lastName: LastName,
  69. avatarUrl: AvatarUrl,
  70. color: Color,
  71. online: Online,
  72. number: Number,
  73. });
  74. const updatedChat = await ChatModel.getByField(id, userId);
  75. return res.status(200).json({
  76. status: 'success',
  77. code: 200,
  78. data: updatedChat,
  79. });
  80. }
  81. if (companion && !isUser && !isCompanion) {
  82. const newChat = await ChatModel.add({
  83. name,
  84. lastName,
  85. avatarUrl,
  86. color,
  87. online,
  88. number,
  89. companionId: id,
  90. owner: userId,
  91. });
  92. await ChatModel.add({
  93. name: Name,
  94. lastName: LastName,
  95. avatarUrl: AvatarUrl,
  96. color: Color,
  97. online: Online,
  98. number: Number,
  99. companionId: userId,
  100. owner: id,
  101. });
  102. return res.status(201).json({
  103. status: 'success',
  104. code: 201,
  105. data: newChat,
  106. });
  107. }
  108. } catch (e) {
  109. next(e);
  110. }
  111. };
  112. module.exports = {
  113. listChats,
  114. startChat,
  115. };