Skip to content

Commit 6f939aa

Browse files
stripe downgrade
1 parent 1e3a538 commit 6f939aa

File tree

1 file changed

+101
-5
lines changed

1 file changed

+101
-5
lines changed

packages/api/src/router/stripe/webhook.ts

Lines changed: 101 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,15 @@ import type Stripe from "stripe";
33
import { z } from "zod";
44

55
import { Events, setupAnalytics } from "@openstatus/analytics";
6-
import { eq } from "@openstatus/db";
6+
import { and, asc, eq, isNull } from "@openstatus/db";
77
import {
8+
invitation,
9+
monitor,
10+
notification,
11+
page,
812
selectWorkspaceSchema,
913
user,
14+
usersToWorkspaces,
1015
workspace,
1116
} from "@openstatus/db/src/schema";
1217

@@ -213,19 +218,110 @@ export const webhookRouter = createTRPCRouter({
213218
subscriptionId: null,
214219
plan: "free",
215220
paidUntil: null,
221+
limits: JSON.stringify(getLimits("free")),
216222
})
217223
.where(eq(workspace.stripeId, customerId))
218224
.returning();
219225

220-
const customer = await stripe.customers.retrieve(customerId);
221-
222-
if (!_workspace) {
226+
if (!_workspace.length) {
223227
throw new TRPCError({
224228
code: "BAD_REQUEST",
225229
message: "Workspace not found",
226230
});
227231
}
228232

233+
const workspaceId = _workspace[0].id;
234+
235+
const activeMonitors = await opts.ctx.db
236+
.select({ id: monitor.id })
237+
.from(monitor)
238+
.where(
239+
and(
240+
eq(monitor.workspaceId, workspaceId),
241+
eq(monitor.active, true),
242+
isNull(monitor.deletedAt),
243+
),
244+
)
245+
.orderBy(asc(monitor.createdAt));
246+
247+
for (const m of activeMonitors.slice(1)) {
248+
await opts.ctx.db
249+
.update(monitor)
250+
.set({ active: false })
251+
.where(eq(monitor.id, m.id))
252+
.run();
253+
}
254+
255+
const statusPages = await opts.ctx.db
256+
.select({ id: page.id })
257+
.from(page)
258+
.where(eq(page.workspaceId, workspaceId))
259+
.orderBy(asc(page.createdAt));
260+
261+
for (const p of statusPages.slice(1)) {
262+
await opts.ctx.db.delete(page).where(eq(page.id, p.id)).run();
263+
}
264+
265+
if (statusPages.length > 0) {
266+
await opts.ctx.db
267+
.update(page)
268+
.set({
269+
customDomain: "",
270+
password: null,
271+
accessType: "public",
272+
authEmailDomains: null,
273+
})
274+
.where(eq(page.id, statusPages[0].id))
275+
.run();
276+
}
277+
278+
const notifications = await opts.ctx.db
279+
.select({ id: notification.id, provider: notification.provider })
280+
.from(notification)
281+
.where(eq(notification.workspaceId, workspaceId))
282+
.orderBy(asc(notification.createdAt));
283+
284+
const keepNotification =
285+
notifications.find((n) => n.provider === "email") ?? notifications[0];
286+
287+
for (const n of notifications.filter(
288+
(n) => n.id !== keepNotification?.id,
289+
)) {
290+
await opts.ctx.db
291+
.delete(notification)
292+
.where(eq(notification.id, n.id))
293+
.run();
294+
}
295+
296+
// Remove all non-owner members from the workspace
297+
await opts.ctx.db
298+
.delete(usersToWorkspaces)
299+
.where(
300+
and(
301+
eq(usersToWorkspaces.workspaceId, workspaceId),
302+
eq(usersToWorkspaces.role, "member"),
303+
),
304+
)
305+
.run();
306+
307+
await opts.ctx.db
308+
.delete(usersToWorkspaces)
309+
.where(
310+
and(
311+
eq(usersToWorkspaces.workspaceId, workspaceId),
312+
eq(usersToWorkspaces.role, "admin"),
313+
),
314+
)
315+
.run();
316+
317+
// Remove all pending invitations for the workspace
318+
await opts.ctx.db
319+
.delete(invitation)
320+
.where(eq(invitation.workspaceId, workspaceId))
321+
.run();
322+
323+
const customer = await stripe.customers.retrieve(customerId);
324+
229325
if (!customer.deleted && customer.email) {
230326
const userResult = await opts.ctx.db
231327
.select()
@@ -237,7 +333,7 @@ export const webhookRouter = createTRPCRouter({
237333
const analytics = await setupAnalytics({
238334
userId: `usr_${userResult.id}`,
239335
email: customer.email || undefined,
240-
workspaceId: String(_workspace[0].id),
336+
workspaceId: String(workspaceId),
241337
plan: "free",
242338
});
243339
await analytics.track(Events.DowngradeWorkspace);

0 commit comments

Comments
 (0)