12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- const ChatModel = require('../model/chat');
- const UserModel = require('../model/user');
- const listChats = async (req, res, next) => {
- try {
- const userId = req.user.id;
- const chats = await ChatModel.getList(userId, req.query);
- return res.json({
- status: 'success',
- code: 200,
- data: {
- ...chats,
- },
- });
- } catch (e) {
- next(e);
- }
- };
- const startChat = async (req, res, next) => {
- try {
- const id = req.body.id;
- const user = req.user;
- const userId = user.id;
- const companion = await UserModel.findById(id);
- const isUser = await ChatModel.getByField(id, userId);
- const isCompanion = await ChatModel.getByField(userId, id);
- const { name, lastName, avatarUrl, color, online, number } = companion;
- const {
- name: Name,
- lastName: LastName,
- avatarUrl: AvatarUrl,
- color: Color,
- online: Online,
- number: Number,
- } = user;
- if (companion && (isUser || isCompanion)) {
- await ChatModel.update(isUser._id, {
- name,
- lastName,
- avatarUrl,
- color,
- online,
- number,
- });
- await ChatModel.update(isCompanion._id, {
- name: Name,
- lastName: LastName,
- avatarUrl: AvatarUrl,
- color: Color,
- online: Online,
- number: Number,
- });
- const updatedChat = await ChatModel.getByField(id, userId);
- return res.status(200).json({
- status: 'success',
- code: 200,
- data: updatedChat,
- });
- }
- if (companion && !isUser && !isCompanion) {
- const newChat = await ChatModel.add({
- name,
- lastName,
- avatarUrl,
- color,
- online,
- number,
- companionId: id,
- owner: userId,
- });
- await ChatModel.add({
- name: Name,
- lastName: LastName,
- avatarUrl: AvatarUrl,
- color: Color,
- online: Online,
- number: Number,
- companionId: userId,
- owner: id,
- });
- return res.status(201).json({
- status: 'success',
- code: 201,
- data: newChat,
- });
- }
- } catch (e) {
- next(e);
- }
- };
- module.exports = {
- listChats,
- startChat,
- };
|