Skip to content

Commit

Permalink
User join to chat after sign up
Browse files Browse the repository at this point in the history
  • Loading branch information
msobkyy committed Feb 8, 2023
1 parent 91f81fc commit 354c347
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
21 changes: 21 additions & 0 deletions controllers/authController.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, {
Expand Down Expand Up @@ -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 });
});

Expand Down
6 changes: 3 additions & 3 deletions controllers/chatController.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}
Expand Down Expand Up @@ -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({
Expand Down
2 changes: 1 addition & 1 deletion controllers/messagesController.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}
Expand Down

0 comments on commit 354c347

Please sign in to comment.