-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathleaveGroup.js
37 lines (31 loc) · 1.29 KB
/
leaveGroup.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
exports = async function leaveGroup(groupId) {
if (!groupId)
return { error: { message: 'Please provide which group to leave.' }};
const db = context.services.get('mongodb-atlas').db('findourdevices');
const realmUser = context.user;
const userId = BSON.ObjectId(realmUser.id);
groupId = BSON.ObjectId(groupId);
try {
const groupDoc = await db.collection('Group').findOne({ _id: groupId });
if (!groupDoc?._id)
return { error: { message: 'The group does not exist.' }};
// Stringify the ObjectIds when comparing as the references themselves may differ
if (groupDoc.ownerId.toString() === userId.toString())
return { error: { message: 'Group owners must remove the group in order to leave it.' }};
// Remove the GroupMember from the Group's 'members' array
await db.collection('Group').updateOne(
{ _id: groupId },
{ $pull: { members: { userId } } }
);
// Remove the User's GroupMembership from it's "groups" array
await db.collection('User').updateOne(
{ _id: userId },
{ $pull: { groups: { groupId } } }
);
return { success: true };
}
catch (err) {
console.error('Error leaving the group: ', err.message);
return { error: { message: err.message || 'There was an error leaving the group.' } };
}
};