@@ -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