Skip to content

Commit

Permalink
update(group-join): add member details in response
Browse files Browse the repository at this point in the history
  • Loading branch information
tirthajyoti-ghosh committed Aug 22, 2024
1 parent cf3c805 commit 2bbaa1f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
15 changes: 9 additions & 6 deletions src/group/group.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,26 +126,29 @@ export class GroupService {
const decodedGroupId = this.jwtService.verify(hashedGroupId);
const groupId = decrypt(decodedGroupId.groupId);


const group = await this.groupModel.findById(groupId).exec();

if (!group) {
throw new NotFoundException('Group not found');
}

const { members } = group;
// Check if the user is already a member of the group

if (members.includes(userId)) {
throw new BadRequestException('User already a member of the group');
}

// Add the user to the group's members array
members.push(userId);

// Save the updated group
await group.save();
const [, memberDetails] = await Promise.all([
group.save(),
this.userService.findUsersByIds(members),
]);

return group; // Or some other meaningful response
return {
...group.toObject(),
members: memberDetails,
};
}

async getAllUserGroups(userId) {
Expand Down
4 changes: 4 additions & 0 deletions src/users/users.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,4 +129,8 @@ export class UsersService {
async editName(id, name) {
return await this.userModel.findByIdAndUpdate(id, { name: name }).exec();
}

async findUsersByIds(userIds: string[], projection = {}) {
return await this.userModel.find({ _id: { $in: userIds } }, projection).exec();
}
}

0 comments on commit 2bbaa1f

Please sign in to comment.