🐛 (billing) Fix webhook calls when workspace was deleted
This commit is contained in:
@ -77,7 +77,7 @@ export const OnboardingPage = () => {
|
|||||||
if (isOtherCategory)
|
if (isOtherCategory)
|
||||||
setOnboardingReplies((prev) => ({
|
setOnboardingReplies((prev) => ({
|
||||||
...prev,
|
...prev,
|
||||||
categories: prev.onboardingCategories
|
onboardingCategories: prev.onboardingCategories
|
||||||
? [...prev.onboardingCategories, answer.message]
|
? [...prev.onboardingCategories, answer.message]
|
||||||
: [answer.message],
|
: [answer.message],
|
||||||
}))
|
}))
|
||||||
|
@ -3,6 +3,9 @@ import { authenticatedProcedure } from '@/helpers/server/trpc'
|
|||||||
import { z } from 'zod'
|
import { z } from 'zod'
|
||||||
import { isAdminWriteWorkspaceForbidden } from '../helpers/isAdminWriteWorkspaceForbidden'
|
import { isAdminWriteWorkspaceForbidden } from '../helpers/isAdminWriteWorkspaceForbidden'
|
||||||
import { TRPCError } from '@trpc/server'
|
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
|
export const deleteWorkspace = authenticatedProcedure
|
||||||
.meta({
|
.meta({
|
||||||
@ -38,9 +41,23 @@ export const deleteWorkspace = authenticatedProcedure
|
|||||||
throw new TRPCError({ code: 'NOT_FOUND', message: 'No workspaces found' })
|
throw new TRPCError({ code: 'NOT_FOUND', message: 'No workspaces found' })
|
||||||
|
|
||||||
await prisma.workspace.deleteMany({
|
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 {
|
return {
|
||||||
message: 'Workspace deleted',
|
message: 'Workspace deleted',
|
||||||
}
|
}
|
||||||
|
@ -234,6 +234,17 @@ const webhookHandler = async (req: NextApiRequest, res: NextApiResponse) => {
|
|||||||
outstandingInvoices.data.filter(
|
outstandingInvoices.data.filter(
|
||||||
(invoice) => invoice.amount_due > prices['PRO'] * 100
|
(invoice) => invoice.amount_due > prices['PRO'] * 100
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const workspaceExist =
|
||||||
|
(await prisma.workspace.count({
|
||||||
|
where: {
|
||||||
|
stripeId: subscription.customer as string,
|
||||||
|
},
|
||||||
|
})) > 0
|
||||||
|
|
||||||
|
if (!workspaceExist)
|
||||||
|
return res.send({ message: 'Workspace not found, skipping...' })
|
||||||
|
|
||||||
const workspace = await prisma.workspace.update({
|
const workspace = await prisma.workspace.update({
|
||||||
where: {
|
where: {
|
||||||
stripeId: subscription.customer as string,
|
stripeId: subscription.customer as string,
|
||||||
|
Reference in New Issue
Block a user