Skip to content
This repository has been archived by the owner on Apr 19, 2023. It is now read-only.

Commit

Permalink
✨ Add single membership logic to leave
Browse files Browse the repository at this point in the history
  • Loading branch information
AnandChowdhary committed Oct 4, 2019
1 parent d1f2cf7 commit ceb04ab
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 10 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "staart-manager",
"version": "1.1.39",
"version": "1.1.40",
"main": "index.js",
"repository": "[email protected]:AnandChowdhary/staart.git",
"author": "Anand Chowdhary <[email protected]>",
Expand Down Expand Up @@ -147,5 +147,5 @@
"setup"
],
"snyk": true,
"staart-version": "1.1.39"
"staart-version": "1.1.40"
}
4 changes: 0 additions & 4 deletions src/crud/organization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -592,10 +592,6 @@ export const deleteOrganizationMembership = async (
organizationId: string,
id: string
) => {
// Check if there's only one member in this team
const members = await getOrganizationMemberships(organizationId);
if (members && members.data && members.length === 1)
throw new Error(ErrorCode.CANNOT_DELETE_SOLE_MEMBER);
const membershipDetails = await getOrganizationMembership(organizationId, id);
if (membershipDetails.id)
deleteItemFromCache(
Expand Down
8 changes: 5 additions & 3 deletions src/rest/membership.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,18 @@ export const deleteMembershipForUser = async (
if (!membership || !membership.id)
throw new Error(ErrorCode.MEMBERSHIP_NOT_FOUND);
if (await can(tokenUserId, Authorizations.DELETE, "membership", membership)) {
const organizationMembers = await getOrganizationMembers(
membership.organizationId
);
if (membership.role == MembershipRole.OWNER) {
const organizationMembers = await getOrganizationMembers(
membership.organizationId
);
const currentMembers = organizationMembers.filter(
member => member.role == MembershipRole.OWNER
);
if (currentMembers.length < 2)
throw new Error(ErrorCode.CANNOT_DELETE_SOLE_OWNER);
}
if (organizationMembers.length === 1)
throw new Error(ErrorCode.CANNOT_DELETE_SOLE_MEMBER);
trackEvent(
{
userId: membershipId,
Expand Down
7 changes: 6 additions & 1 deletion src/rest/organization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -615,8 +615,13 @@ export const deleteOrganizationMembershipForUser = async (
"organization",
organizationId
)
)
) {
// Check if there's only one member in this team
const members = await getOrganizationMemberships(organizationId);
if (members && members.data && members.length === 1)
throw new Error(ErrorCode.CANNOT_DELETE_SOLE_MEMBER);
return await deleteOrganizationMembership(organizationId, membershipId);
}
throw new Error(ErrorCode.INSUFFICIENT_PERMISSION);
};

Expand Down

0 comments on commit ceb04ab

Please sign in to comment.