2023-02-17 16:19:39 +01:00
|
|
|
import prisma from '@/lib/prisma'
|
|
|
|
|
import { authenticatedProcedure } from '@/utils/server/trpc'
|
|
|
|
|
import { TRPCError } from '@trpc/server'
|
2023-03-15 08:35:16 +01:00
|
|
|
import { WorkspaceRole } from '@typebot.io/prisma'
|
2023-02-17 16:19:39 +01:00
|
|
|
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-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({
|
|
|
|
|
subscription: subscriptionSchema,
|
|
|
|
|
})
|
|
|
|
|
)
|
|
|
|
|
.query(async ({ input: { workspaceId }, ctx: { user } }) => {
|
|
|
|
|
if (
|
|
|
|
|
!process.env.STRIPE_SECRET_KEY ||
|
|
|
|
|
!process.env.STRIPE_ADDITIONAL_CHATS_PRICE_ID ||
|
|
|
|
|
!process.env.STRIPE_ADDITIONAL_STORAGE_PRICE_ID
|
|
|
|
|
)
|
|
|
|
|
throw new TRPCError({
|
|
|
|
|
code: 'INTERNAL_SERVER_ERROR',
|
|
|
|
|
message: 'Stripe environment variables are missing',
|
|
|
|
|
})
|
|
|
|
|
const workspace = await prisma.workspace.findFirst({
|
|
|
|
|
where: {
|
|
|
|
|
id: workspaceId,
|
|
|
|
|
members: { some: { userId: user.id, role: WorkspaceRole.ADMIN } },
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
if (!workspace?.stripeId)
|
|
|
|
|
throw new TRPCError({
|
|
|
|
|
code: 'NOT_FOUND',
|
|
|
|
|
message: 'Workspace not found',
|
|
|
|
|
})
|
|
|
|
|
const stripe = new Stripe(process.env.STRIPE_SECRET_KEY, {
|
|
|
|
|
apiVersion: '2022-11-15',
|
|
|
|
|
})
|
|
|
|
|
const subscriptions = await stripe.subscriptions.list({
|
|
|
|
|
customer: workspace.stripeId,
|
|
|
|
|
limit: 1,
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
const subscription = subscriptions?.data.shift()
|
|
|
|
|
|
|
|
|
|
if (!subscription)
|
|
|
|
|
throw new TRPCError({
|
|
|
|
|
code: 'NOT_FOUND',
|
|
|
|
|
message: 'Subscription not found',
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
subscription: {
|
|
|
|
|
additionalChatsIndex:
|
|
|
|
|
subscription?.items.data.find(
|
|
|
|
|
(item) =>
|
|
|
|
|
item.price.id === process.env.STRIPE_ADDITIONAL_CHATS_PRICE_ID
|
|
|
|
|
)?.quantity ?? 0,
|
|
|
|
|
additionalStorageIndex:
|
|
|
|
|
subscription.items.data.find(
|
|
|
|
|
(item) =>
|
|
|
|
|
item.price.id === process.env.STRIPE_ADDITIONAL_STORAGE_PRICE_ID
|
|
|
|
|
)?.quantity ?? 0,
|
|
|
|
|
currency: subscription.currency as 'usd' | 'eur',
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
})
|