feat(cloud): handle cancelled subscriptions
This commit is contained in:
@@ -287,13 +287,40 @@ export const initAuth = (db: dbClient) => {
|
|||||||
|
|
||||||
if (workspace?.id) {
|
if (workspace?.id) {
|
||||||
await memberRepo.unpauseAllMembers(db, workspace.id);
|
await memberRepo.unpauseAllMembers(db, workspace.id);
|
||||||
|
|
||||||
console.log(
|
|
||||||
`Unpausing all members for workspace ${workspace.id}`,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
onSubscriptionCancel: async ({
|
||||||
|
subscription,
|
||||||
|
cancellationDetails,
|
||||||
|
}) => {
|
||||||
|
await triggerWorkflow(
|
||||||
|
db,
|
||||||
|
"subscription-canceled",
|
||||||
|
subscription,
|
||||||
|
cancellationDetails,
|
||||||
|
);
|
||||||
|
|
||||||
|
// for cancelled subscriptions, we need to pause all members and set their workspace plan to free
|
||||||
|
const workspace = await workspaceRepo.getByPublicId(
|
||||||
|
db,
|
||||||
|
subscription.referenceId,
|
||||||
|
);
|
||||||
|
|
||||||
|
if (workspace?.id) {
|
||||||
|
await memberRepo.pauseAllMembers(db, workspace.id);
|
||||||
|
await workspaceRepo.update(db, subscription.referenceId, {
|
||||||
|
plan: "free",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
},
|
||||||
|
onSubscriptionUpdate: async ({ subscription }) => {
|
||||||
|
await triggerWorkflow(
|
||||||
|
db,
|
||||||
|
"subscription-updated",
|
||||||
|
subscription,
|
||||||
|
);
|
||||||
|
},
|
||||||
},
|
},
|
||||||
}),
|
}),
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -121,3 +121,15 @@ export const unpauseAllMembers = async (db: dbClient, workspaceId: number) => {
|
|||||||
),
|
),
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const pauseAllMembers = async (db: dbClient, workspaceId: number) => {
|
||||||
|
await db
|
||||||
|
.update(workspaceMembers)
|
||||||
|
.set({ status: "paused" })
|
||||||
|
.where(
|
||||||
|
and(
|
||||||
|
eq(workspaceMembers.workspaceId, workspaceId),
|
||||||
|
eq(workspaceMembers.status, "active"),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user