Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
philipperolet committed Nov 20, 2023
1 parent ac13488 commit 08500ec
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion front/pages/api/stripe/webhook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
sendReactivateSubscriptionEmail,
} from "@app/lib/email";
import { ReturnedAPIErrorType } from "@app/lib/error";
import { Plan, Subscription, Workspace } from "@app/lib/models";
import { Membership, Plan, Subscription, Workspace } from "@app/lib/models";
import { PlanInvitation } from "@app/lib/models/plan";
import { generateModelSId } from "@app/lib/utils";
import logger from "@app/logger/logger";
Expand Down Expand Up @@ -368,6 +368,7 @@ async function handler(
status: "ended",
endDate: new Date(),
});
await revokeUsersForDowngrade(activeSubscription.workspace.sId);
await sendOpsEmail(activeSubscription.workspace.sId);
} else {
logger.warn(
Expand All @@ -394,4 +395,21 @@ async function handler(
}
}

/**
* Remove everybody except the most tenured admin
* @param workspaceId
*/
async function revokeUsersForDowngrade(workspaceId: string) {
const memberships = await Membership.findAll({
where: { workspaceId },
order: [["createdAt", "ASC"]],
});
const adminMemberships = memberships.filter((m) => m.role === "admin");
// the first membership with role admin will not be revoked
adminMemberships.shift();
const nonAdminMemberships = memberships.filter((m) => m.role !== "admin");
for (const membership of [...adminMemberships, ...nonAdminMemberships]) {
await membership.update({ role: "revoked" });
}
}
export default withLogging(handler);

0 comments on commit 08500ec

Please sign in to comment.