Skip to content

Commit 4c9b82f

Browse files
committed
fix: updated the downgrade flow for restricting workspaces
1 parent 17258e5 commit 4c9b82f

File tree

3 files changed

+45
-1
lines changed

3 files changed

+45
-1
lines changed

src/modules/billing/repositories/stripe-subscription.repository.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ export class StripeSubscriptionRepository {
303303
}
304304
const updateQuery: Record<string, any> = {};
305305
// Add workspaces if provided
306-
if (workspaces && workspaces.length > 0) {
306+
if (workspaces) {
307307
updateQuery["downgrade.workspaces"] = { $each: workspaces };
308308
}
309309
// Add users if provided

src/modules/billing/services/downgrade.service.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,4 +290,38 @@ export class DownGradeService {
290290
console.log(error);
291291
}
292292
}
293+
294+
async validateWorkspaceRestrictions(
295+
teamDetails: Team,
296+
teamID: string,
297+
newPlan: PlanName,
298+
): Promise<void> {
299+
try {
300+
const planData =
301+
await this.stripeSubscriptionRepository.findPlanByName(newPlan);
302+
if (!teamDetails || !Array.isArray(teamDetails.workspaces)) {
303+
return;
304+
}
305+
// Get only unrestricted workspaces
306+
// A workspace is unrestricted if isRestricted is not present OR isRestricted === false
307+
const unrestrictedWorkspaces = teamDetails.workspaces.filter(
308+
(w) => w?.isRestricted !== true,
309+
);
310+
const unRestrictedCount = unrestrictedWorkspaces.length;
311+
const limit = planData?.limits?.workspacesPerHub?.value ?? 0;
312+
// Only act when unrestricted workspaces exceed the plan limit
313+
if (unRestrictedCount > limit) {
314+
// Get excess workspaces (those beyond the limit)
315+
const idsToRestrict = unrestrictedWorkspaces
316+
.slice(limit) // Take all workspaces after the limit
317+
.map((w) => w.id.toString());
318+
await this.restrictTeamWorkspace(teamID, idsToRestrict);
319+
for (const wid of idsToRestrict) {
320+
await this.restrictWorkspace(wid);
321+
}
322+
}
323+
} catch (error) {
324+
console.log("Error in validateWorkspaceRestrictions:", error);
325+
}
326+
}
293327
}

src/modules/billing/services/stripe-subscription.service.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -590,6 +590,16 @@ export class StripeSubscriptionService {
590590
newPlan,
591591
new Date(),
592592
);
593+
if (
594+
!team?.downgrade?.workspaces ||
595+
team?.downgrade?.workspaces.length === 0
596+
) {
597+
await this.downgradeService.validateWorkspaceRestrictions(
598+
team,
599+
metadata.hubId,
600+
newPlan,
601+
);
602+
}
593603
await this.stripeSubscriptionRepo.removeDowngradeDetails(
594604
metadata.hubId,
595605
);

0 commit comments

Comments
 (0)