BREAKING CHANGE: Stripe environment variables simplified. Check out the new configs to adapt your existing system. Closes #906 <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ### Summary by CodeRabbit **New Features:** - Introduced a usage-based billing system, providing more flexibility and options for users. - Integrated with Stripe for a smoother and more secure payment process. - Enhanced the user interface with improvements to the billing, workspace, and pricing pages for a more intuitive experience. **Improvements:** - Simplified the billing logic, removing additional chats and yearly billing for a more streamlined user experience. - Updated email notifications to keep users informed about their usage and limits. - Improved pricing and currency formatting for better clarity and understanding. **Testing:** - Updated tests and specifications to ensure the reliability of new features and improvements. **Note:** These changes aim to provide a more flexible and user-friendly billing system, with clearer pricing and improved notifications. Users should find the new system more intuitive and easier to navigate. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
109 lines
2.9 KiB
TypeScript
109 lines
2.9 KiB
TypeScript
import prisma from '@typebot.io/lib/prisma'
|
|
import { authenticatedProcedure } from '@/helpers/server/trpc'
|
|
import { TRPCError } from '@trpc/server'
|
|
import { z } from 'zod'
|
|
import { isReadWorkspaceFobidden } from '@/features/workspace/helpers/isReadWorkspaceFobidden'
|
|
import { env } from '@typebot.io/env'
|
|
import Stripe from 'stripe'
|
|
|
|
export const getUsage = authenticatedProcedure
|
|
.meta({
|
|
openapi: {
|
|
method: 'GET',
|
|
path: '/billing/usage',
|
|
protect: true,
|
|
summary: 'Get current plan usage',
|
|
tags: ['Billing'],
|
|
},
|
|
})
|
|
.input(
|
|
z.object({
|
|
workspaceId: z.string(),
|
|
})
|
|
)
|
|
.output(z.object({ totalChatsUsed: z.number(), resetsAt: z.date() }))
|
|
.query(async ({ input: { workspaceId }, ctx: { user } }) => {
|
|
const workspace = await prisma.workspace.findFirst({
|
|
where: {
|
|
id: workspaceId,
|
|
},
|
|
select: {
|
|
stripeId: true,
|
|
plan: true,
|
|
members: {
|
|
select: {
|
|
userId: true,
|
|
},
|
|
},
|
|
typebots: {
|
|
select: { id: true },
|
|
},
|
|
},
|
|
})
|
|
if (!workspace || isReadWorkspaceFobidden(workspace, user))
|
|
throw new TRPCError({
|
|
code: 'NOT_FOUND',
|
|
message: 'Workspace not found',
|
|
})
|
|
|
|
if (
|
|
!env.STRIPE_SECRET_KEY ||
|
|
!workspace.stripeId ||
|
|
(workspace.plan !== 'STARTER' && workspace.plan !== 'PRO')
|
|
) {
|
|
const now = new Date()
|
|
const firstDayOfMonth = new Date(now.getFullYear(), now.getMonth(), 1)
|
|
|
|
const totalChatsUsed = await prisma.result.count({
|
|
where: {
|
|
typebotId: { in: workspace.typebots.map((typebot) => typebot.id) },
|
|
hasStarted: true,
|
|
createdAt: {
|
|
gte: firstDayOfMonth,
|
|
},
|
|
},
|
|
})
|
|
|
|
const firstDayOfNextMonth = new Date(
|
|
firstDayOfMonth.getFullYear(),
|
|
firstDayOfMonth.getMonth() + 1,
|
|
1
|
|
)
|
|
return { totalChatsUsed, resetsAt: firstDayOfNextMonth }
|
|
}
|
|
|
|
const stripe = new Stripe(env.STRIPE_SECRET_KEY, {
|
|
apiVersion: '2022-11-15',
|
|
})
|
|
|
|
const subscriptions = await stripe.subscriptions.list({
|
|
customer: workspace.stripeId,
|
|
})
|
|
|
|
const currentSubscription = subscriptions.data
|
|
.filter((sub) => ['past_due', 'active'].includes(sub.status))
|
|
.sort((a, b) => a.created - b.created)
|
|
.shift()
|
|
|
|
if (!currentSubscription)
|
|
throw new TRPCError({
|
|
code: 'INTERNAL_SERVER_ERROR',
|
|
message: `No subscription found on workspace: ${workspaceId}`,
|
|
})
|
|
|
|
const totalChatsUsed = await prisma.result.count({
|
|
where: {
|
|
typebotId: { in: workspace.typebots.map((typebot) => typebot.id) },
|
|
hasStarted: true,
|
|
createdAt: {
|
|
gte: new Date(currentSubscription.current_period_start * 1000),
|
|
},
|
|
},
|
|
})
|
|
|
|
return {
|
|
totalChatsUsed,
|
|
resetsAt: new Date(currentSubscription.current_period_end * 1000),
|
|
}
|
|
})
|