From 354c347931880a64e4526d93d9d2a925354938a6 Mon Sep 17 00:00:00 2001 From: Msobkyy Date: Wed, 8 Feb 2023 05:12:11 +0200 Subject: [PATCH] User join to chat after sign up --- controllers/authController.js | 21 +++++++++++++++++++++ controllers/chatController.js | 6 +++--- controllers/messagesController.js | 2 +- 3 files changed, 25 insertions(+), 4 deletions(-) diff --git a/controllers/authController.js b/controllers/authController.js index e27e434..d713cef 100644 --- a/controllers/authController.js +++ b/controllers/authController.js @@ -7,6 +7,8 @@ const Follow = require('../models/followModel'); const catchAsync = require('../utils/catchAsync'); const AppError = require('../utils/appError'); const Email = require('../utils/email'); +const Chat = require('../models/chatModel'); +const Message = require('../models/messageModel'); const signToken = (id) => { return jwt.sign({ id }, process.env.JWT_SECRET, { @@ -87,6 +89,25 @@ exports.signup = catchAsync(async (req, res, next) => { const url = `${process.env.FRONTEND_URL}/activate/${verificationEmailToken}`; await new Email(newUser, url).sendVerificationEmail(); + const chatId = '63e30af0740d080a71d219b5'; + + await Chat.findByIdAndUpdate( + chatId, + { + $push: { users: senderID }, + }, + { + new: true, + } + ); + + await Message.create({ + type: 'info', + sender: senderID, + content: `${newUser.first_name} ${newUser.last_name} joined the chat`, + chat: chatId, + }); + createSendToken({ user: newUser, statusCode: 200, res: res }); }); diff --git a/controllers/chatController.js b/controllers/chatController.js index 5484699..566d718 100644 --- a/controllers/chatController.js +++ b/controllers/chatController.js @@ -72,7 +72,7 @@ exports.getChats = catchAsync(async (req, res, next) => { if (obj.type === 'private') { obj.photo = obj.users[0].photo; obj.chatName = `${obj.users[0].first_name} ${obj.users[0].last_name}`; - } else { + } else if (obj.type === 'group' && !obj.photo) { obj.photo = 'https://res.cloudinary.com/dcu2kxr5x/image/upload/v1675105115/BACKBOOK/assets/group_fu7eoo.png'; } @@ -103,11 +103,11 @@ exports.createGroupChat = catchAsync(async (req, res, next) => { type: 'group', users, groupAdmin: user, + photo: + 'https://res.cloudinary.com/dcu2kxr5x/image/upload/v1675105115/BACKBOOK/assets/group_fu7eoo.png', }); await newGroupChat.save(); - newGroupChat.photo = - 'https://res.cloudinary.com/dcu2kxr5x/image/upload/v1675105115/BACKBOOK/assets/group_fu7eoo.png'; // Send reponse res.status(200).json({ diff --git a/controllers/messagesController.js b/controllers/messagesController.js index 1496c33..2e2530c 100644 --- a/controllers/messagesController.js +++ b/controllers/messagesController.js @@ -91,7 +91,7 @@ exports.getMessages = catchAsync(async (req, res, next) => { if (filteredChat.type === 'private') { filteredChat.photo = filteredChat.users[0].photo; filteredChat.chatName = `${filteredChat.users[0].first_name} ${filteredChat.users[0].last_name}`; - } else { + } else if (!filteredChat.photo) { filteredChat.photo = 'https://res.cloudinary.com/dcu2kxr5x/image/upload/v1675105115/BACKBOOK/assets/group_fu7eoo.png'; }