2023-02-17 16:19:39 +01:00
|
|
|
import prisma from '@/lib/prisma'
|
2023-03-15 11:51:30 +01:00
|
|
|
import { authenticatedProcedure } from '@/helpers/server/trpc'
|
2023-02-17 16:19:39 +01:00
|
|
|
import { TRPCError } from '@trpc/server'
|
|
|
|
|
import Stripe from 'stripe'
|
|
|
|
|
import { z } from 'zod'
|
2023-03-15 08:35:16 +01:00
|
|
|
import { subscriptionSchema } from '@typebot.io/schemas/features/billing/subscription'
|
2023-04-13 11:39:10 +02:00
|
|
|
import { priceIds } from '@typebot.io/lib/pricing'
|
2023-08-22 10:29:00 +02:00
|
|
|
import { isReadWorkspaceFobidden } from '@/features/workspace/helpers/isReadWorkspaceFobidden'
|
2023-02-17 16:19:39 +01:00
|
|
|
|
|
|
|
|
export const getSubscription = authenticatedProcedure
|
|
|
|
|
.meta({
|
|
|
|
|
openapi: {
|
|
|
|
|
method: 'GET',
|
|
|
|
|
path: '/billing/subscription',
|
|
|
|
|
protect: true,
|
|
|
|
|
summary: 'List invoices',
|
|
|
|
|
tags: ['Billing'],
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
.input(
|
|
|
|
|
z.object({
|
|
|
|
|
workspaceId: z.string(),
|
|
|
|
|
})
|
|
|
|
|
)
|
|
|
|
|
.output(
|
|
|
|
|
z.object({
|
2023-04-13 11:39:10 +02:00
|
|
|
subscription: subscriptionSchema.or(z.null()),
|
2023-02-17 16:19:39 +01:00
|
|
|
})
|
|
|
|
|
)
|
|
|
|
|
.query(async ({ input: { workspaceId }, ctx: { user } }) => {
|
2023-04-13 11:39:10 +02:00
|
|
|
if (!process.env.STRIPE_SECRET_KEY)
|
2023-02-17 16:19:39 +01:00
|
|
|
throw new TRPCError({
|
|
|
|
|
code: 'INTERNAL_SERVER_ERROR',
|
|
|
|
|
message: 'Stripe environment variables are missing',
|
|
|
|
|
})
|
|
|
|
|
const workspace = await prisma.workspace.findFirst({
|
|
|
|
|
where: {
|
|
|
|
|
id: workspaceId,
|
2023-08-22 10:29:00 +02:00
|
|
|
},
|
|
|
|
|
select: {
|
|
|
|
|
stripeId: true,
|
|
|
|
|
members: {
|
|
|
|
|
select: {
|
|
|
|
|
userId: true,
|
|
|
|
|
},
|
|
|
|
|
},
|
2023-02-17 16:19:39 +01:00
|
|
|
},
|
|
|
|
|
})
|
2023-08-22 10:29:00 +02:00
|
|
|
if (!workspace || isReadWorkspaceFobidden(workspace, user))
|
|
|
|
|
throw new TRPCError({
|
|
|
|
|
code: 'NOT_FOUND',
|
|
|
|
|
message: 'Workspace not found',
|
|
|
|
|
})
|
2023-02-17 16:19:39 +01:00
|
|
|
if (!workspace?.stripeId)
|
2023-04-13 11:39:10 +02:00
|
|
|
return {
|
|
|
|
|
subscription: null,
|
|
|
|
|
}
|
2023-02-17 16:19:39 +01:00
|
|
|
const stripe = new Stripe(process.env.STRIPE_SECRET_KEY, {
|
|
|
|
|
apiVersion: '2022-11-15',
|
|
|
|
|
})
|
|
|
|
|
const subscriptions = await stripe.subscriptions.list({
|
|
|
|
|
customer: workspace.stripeId,
|
|
|
|
|
limit: 1,
|
2023-05-05 11:02:59 -04:00
|
|
|
status: 'active',
|
2023-02-17 16:19:39 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
const subscription = subscriptions?.data.shift()
|
|
|
|
|
|
|
|
|
|
if (!subscription)
|
2023-04-13 11:39:10 +02:00
|
|
|
return {
|
|
|
|
|
subscription: null,
|
|
|
|
|
}
|
2023-02-17 16:19:39 +01:00
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
subscription: {
|
2023-04-13 11:39:10 +02:00
|
|
|
isYearly: subscription.items.data.some((item) => {
|
|
|
|
|
return (
|
|
|
|
|
priceIds.STARTER.chats.yearly === item.price.id ||
|
|
|
|
|
priceIds.STARTER.storage.yearly === item.price.id ||
|
|
|
|
|
priceIds.PRO.chats.yearly === item.price.id ||
|
|
|
|
|
priceIds.PRO.storage.yearly === item.price.id
|
|
|
|
|
)
|
|
|
|
|
}),
|
2023-02-17 16:19:39 +01:00
|
|
|
currency: subscription.currency as 'usd' | 'eur',
|
2023-04-13 11:39:10 +02:00
|
|
|
cancelDate: subscription.cancel_at
|
|
|
|
|
? new Date(subscription.cancel_at * 1000)
|
|
|
|
|
: undefined,
|
2023-02-17 16:19:39 +01:00
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
})
|
2023-04-13 11:39:10 +02:00
|
|
|
|
|
|
|
|
export const chatPriceIds = [priceIds.STARTER.chats.monthly]
|
|
|
|
|
.concat(priceIds.STARTER.chats.yearly)
|
|
|
|
|
.concat(priceIds.PRO.chats.monthly)
|
|
|
|
|
.concat(priceIds.PRO.chats.yearly)
|
|
|
|
|
|
|
|
|
|
export const storagePriceIds = [priceIds.STARTER.storage.monthly]
|
|
|
|
|
.concat(priceIds.STARTER.storage.yearly)
|
|
|
|
|
.concat(priceIds.PRO.storage.monthly)
|
|
|
|
|
.concat(priceIds.PRO.storage.yearly)
|