🐛 (billing) Fix webhook calls when workspace was deleted
This commit is contained in:
@@ -3,6 +3,9 @@ import { authenticatedProcedure } from '@/helpers/server/trpc'
|
||||
import { z } from 'zod'
|
||||
import { isAdminWriteWorkspaceForbidden } from '../helpers/isAdminWriteWorkspaceForbidden'
|
||||
import { TRPCError } from '@trpc/server'
|
||||
import { isNotEmpty } from '@typebot.io/lib/utils'
|
||||
import Stripe from 'stripe'
|
||||
import { env } from '@typebot.io/env'
|
||||
|
||||
export const deleteWorkspace = authenticatedProcedure
|
||||
.meta({
|
||||
@@ -38,9 +41,23 @@ export const deleteWorkspace = authenticatedProcedure
|
||||
throw new TRPCError({ code: 'NOT_FOUND', message: 'No workspaces found' })
|
||||
|
||||
await prisma.workspace.deleteMany({
|
||||
where: { members: { some: { userId: user.id } }, id: workspaceId },
|
||||
where: { id: workspaceId },
|
||||
})
|
||||
|
||||
if (isNotEmpty(workspace.stripeId) && env.STRIPE_SECRET_KEY) {
|
||||
const stripe = new Stripe(env.STRIPE_SECRET_KEY, {
|
||||
apiVersion: '2022-11-15',
|
||||
})
|
||||
|
||||
const subscriptions = await stripe.subscriptions.list({
|
||||
customer: workspace.stripeId,
|
||||
})
|
||||
|
||||
for (const subscription of subscriptions.data) {
|
||||
await stripe.subscriptions.cancel(subscription.id)
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
message: 'Workspace deleted',
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user