Skip to content

Commit 157d43c

Browse files
author
Rajat Saxena
committed
Bug fix: Last admin leaving the community
1 parent 8108321 commit 157d43c

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

apps/web/config/strings.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,8 @@ export const responses = {
123123
profile_incomplete: "Complete your profile to join this community",
124124
cannot_reject_member_with_active_subscription:
125125
"Cannot reject a member with an active subscription",
126+
cannot_leave_community_last_admin:
127+
"Last manager cannot leave the community",
126128

127129
// api responses
128130
digital_download_no_files:

apps/web/graphql/communities/logic.ts

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1508,10 +1508,6 @@ export async function leaveCommunity({
15081508
}): Promise<boolean> {
15091509
checkIfAuthenticated(ctx);
15101510

1511-
// const community = await CommunityModel.findOne<InternalCommunity>({
1512-
// domain: ctx.subdomain._id,
1513-
// communityId: id,
1514-
// });
15151511
const community = await CommunityModel.findOne<InternalCommunity>(
15161512
getCommunityQuery(ctx, id),
15171513
);
@@ -1532,6 +1528,33 @@ export async function leaveCommunity({
15321528
return true;
15331529
}
15341530

1531+
if (checkPermission(ctx.user.permissions, [permissions.manageCommunity])) {
1532+
const otherMembers = await MembershipModel.find(
1533+
{
1534+
domain: ctx.subdomain._id,
1535+
entityId: id,
1536+
entityType: Constants.MembershipEntityType.COMMUNITY,
1537+
status: Constants.MembershipStatus.ACTIVE,
1538+
userId: { $ne: ctx.user.userId },
1539+
},
1540+
{
1541+
_id: 0,
1542+
userId: 1,
1543+
},
1544+
).lean();
1545+
1546+
const otherMembersWithManageCommunityPermission =
1547+
await UserModel.countDocuments({
1548+
domain: ctx.subdomain._id,
1549+
userId: { $in: otherMembers.map((m) => m.userId) },
1550+
permissions: permissions.manageCommunity,
1551+
});
1552+
1553+
if (otherMembersWithManageCommunityPermission === 0) {
1554+
throw new Error(responses.cannot_leave_community_last_admin);
1555+
}
1556+
}
1557+
15351558
if (member.subscriptionId) {
15361559
const paymentMethod = await getPaymentMethodFromSettings(
15371560
ctx.subdomain.settings,

0 commit comments

Comments
 (0)