chats.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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 { total, limit, page, chats } = await ChatModel.getList(
  8. userId,
  9. req.query
  10. );
  11. const messages = await MessageModel.getLastMessagesList(userId);
  12. const lastOnline = await chats.reduce(async (acc, { companionId }) => {
  13. const { _id, online, updatedAt } = await UserModel.findById(companionId);
  14. acc.push({ _id, online, updatedAt });
  15. return acc;
  16. }, []);
  17. const lastMessages = await messages.reduce(
  18. (acc, { message, companionId, createdAt, updatedAt }) => {
  19. const obj = {
  20. message,
  21. companionId,
  22. createdAt,
  23. updatedAt,
  24. };
  25. if (acc.length === 0) {
  26. acc.push(obj);
  27. return acc;
  28. }
  29. if (acc.some((el) => el.companionId === companionId)) {
  30. const i = acc.findIndex((el) => {
  31. return el.companionId === companionId;
  32. });
  33. acc[i] = obj;
  34. return acc;
  35. }
  36. acc.push(obj);
  37. return acc;
  38. },
  39. []
  40. );
  41. return res.json({
  42. status: 'success',
  43. code: 200,
  44. data: {
  45. total,
  46. limit,
  47. page,
  48. chats,
  49. lastMessages,
  50. lastOnline,
  51. },
  52. });
  53. } catch (e) {
  54. next(e);
  55. }
  56. };
  57. const startChat = async (req, res, next) => {
  58. try {
  59. const id = req.body.id;
  60. const user = req.user;
  61. const userId = user.id;
  62. const companion = await UserModel.findById(id);
  63. const isUser = await ChatModel.getByField(id, userId);
  64. const isCompanion = await ChatModel.getByField(userId, id);
  65. const { name, lastName, avatarUrl, color, online, number } = companion;
  66. const {
  67. name: Name,
  68. lastName: LastName,
  69. avatarUrl: AvatarUrl,
  70. color: Color,
  71. online: Online,
  72. number: Number,
  73. } = user;
  74. if (companion && (isUser || isCompanion)) {
  75. await ChatModel.update(isUser._id, {
  76. name,
  77. lastName,
  78. avatarUrl,
  79. color,
  80. online,
  81. number,
  82. });
  83. await ChatModel.update(isCompanion._id, {
  84. name: Name,
  85. lastName: LastName,
  86. avatarUrl: AvatarUrl,
  87. color: Color,
  88. online: Online,
  89. number: Number,
  90. });
  91. const updatedChat = await ChatModel.getByField(id, userId);
  92. return res.status(200).json({
  93. status: 'success',
  94. code: 200,
  95. data: updatedChat,
  96. });
  97. }
  98. if (companion && !isUser && !isCompanion) {
  99. const newChat = await ChatModel.add({
  100. name,
  101. lastName,
  102. avatarUrl,
  103. color,
  104. online,
  105. number,
  106. companionId: id,
  107. owner: userId,
  108. });
  109. await ChatModel.add({
  110. name: Name,
  111. lastName: LastName,
  112. avatarUrl: AvatarUrl,
  113. color: Color,
  114. online: Online,
  115. number: Number,
  116. companionId: userId,
  117. owner: id,
  118. });
  119. return res.status(201).json({
  120. status: 'success',
  121. code: 201,
  122. data: newChat,
  123. });
  124. }
  125. } catch (e) {
  126. next(e);
  127. }
  128. };
  129. const muteChat = async (req, res, next) => {
  130. try {
  131. const id = req.body.id;
  132. const userId = req.user.id;
  133. const isChat = await ChatModel.getByField(id, userId);
  134. if (isChat) {
  135. const { _id, mute } = isChat;
  136. await ChatModel.update(_id, { mute: !mute });
  137. return res.status(200).json({
  138. status: 'success',
  139. code: 200,
  140. data: {},
  141. });
  142. }
  143. } catch (e) {
  144. next(e);
  145. }
  146. };
  147. const seenChat = async (req, res, next) => {
  148. try {
  149. const id = req.body.id;
  150. const userId = req.user.id;
  151. const isChat = await ChatModel.getByField(id, userId);
  152. const isMessage = await MessageModel.getList(userId, {});
  153. if (isChat && isMessage) {
  154. const { _id } = isChat;
  155. const { total } = isMessage;
  156. await ChatModel.update(_id, { seen: total });
  157. return res.status(200).json({
  158. status: 'success',
  159. code: 200,
  160. data: {},
  161. });
  162. }
  163. } catch (e) {
  164. next(e);
  165. }
  166. };
  167. const chatById = async (req, res, next) => {
  168. try {
  169. const userId = req.user.id;
  170. const companionId = req.params.companionId;
  171. const { online } = await UserModel.findById(companionId);
  172. const isChat = await ChatModel.getByField(companionId, userId);
  173. if (isChat) {
  174. const {
  175. name,
  176. lastName,
  177. avatarUrl,
  178. color,
  179. mute,
  180. seen,
  181. total,
  182. number,
  183. _id,
  184. companionId,
  185. owner,
  186. createdAt,
  187. updatedAt,
  188. __v,
  189. } = isChat;
  190. return res.json({
  191. status: 'success',
  192. code: 200,
  193. data: {
  194. name,
  195. lastName,
  196. avatarUrl,
  197. color,
  198. online,
  199. mute,
  200. seen,
  201. total,
  202. number,
  203. _id,
  204. companionId,
  205. owner,
  206. createdAt,
  207. updatedAt,
  208. __v,
  209. },
  210. });
  211. }
  212. } catch (e) {
  213. next(e);
  214. }
  215. };
  216. module.exports = {
  217. listChats,
  218. startChat,
  219. muteChat,
  220. seenChat,
  221. chatById,
  222. };