2
0

(billing) Automatic usage-based billing (#924)

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 -->
This commit is contained in:
Baptiste Arnaud
2023-10-17 08:03:30 +02:00
committed by GitHub
parent a8c2deb258
commit 797751b418
55 changed files with 1589 additions and 1497 deletions

View File

@ -23,7 +23,6 @@ export const findPublicTypebot = ({ publicId }: Props) =>
select: {
id: true,
plan: true,
additionalChatsIndex: true,
customChatsLimit: true,
isQuarantined: true,
isSuspended: true,

View File

@ -1,21 +1,15 @@
import React, { ComponentProps } from 'react'
import {
Mjml,
MjmlBody,
MjmlSection,
MjmlColumn,
MjmlSpacer,
} from '@faire/mjml-react'
import { ComponentProps } from 'react'
import { Mjml, MjmlBody, MjmlSection, MjmlColumn } from '@faire/mjml-react'
import { render } from '@faire/mjml-react/utils/render'
import { Button, Head, HeroImage, Text } from '../components'
import { Head, HeroImage, Text } from '../components'
import { parseNumberWithCommas } from '@typebot.io/lib'
import { SendMailOptions } from 'nodemailer'
import { sendEmail } from '../sendEmail'
type AlmostReachedChatsLimitEmailProps = {
workspaceName: string
usagePercent: number
chatsLimit: number
url: string
}
const now = new Date()
@ -27,9 +21,9 @@ const readableResetDate = firstDayOfNextMonth
.join(' ')
export const AlmostReachedChatsLimitEmail = ({
workspaceName,
usagePercent,
chatsLimit,
url,
}: AlmostReachedChatsLimitEmailProps) => {
const readableChatsLimit = parseNumberWithCommas(chatsLimit)
@ -46,18 +40,22 @@ export const AlmostReachedChatsLimitEmail = ({
<MjmlColumn>
<Text>Your bots are chatting a lot. That&apos;s amazing. 💙</Text>
<Text>
This means you&apos;ve almost reached your monthly chats limit.
You currently reached {usagePercent}% of {readableChatsLimit}{' '}
Your workspace <strong>{workspaceName}</strong> has used{' '}
{usagePercent}% of the included chats this month. Once you hit{' '}
{readableChatsLimit} chats, you will pay as you go for additional
chats.
</Text>
<Text>This limit will be reset on {readableResetDate}.</Text>
<Text fontWeight="800">
Upon this limit your bots will still continue to chat, but we ask
you kindly to upgrade your monthly chats limit.
<Text>
Your progress can be monitored on your workspace dashboard
settings. Check out the{' '}
<a href="https://typebot.io/pricing">pricing page</a> for
information about the pay as you go tiers.
</Text>
<Text>
As a reminder, your billing cycle ends on {readableResetDate}. If
you&apos;d like to learn more about the Enterprise plan for an
annual commitment, reach out to .
</Text>
<MjmlSpacer height="24px" />
<Button link={url}>Upgrade workspace</Button>
</MjmlColumn>
</MjmlSection>
</MjmlBody>

18
packages/env/env.ts vendored
View File

@ -140,20 +140,10 @@ const stripeEnv = {
server: {
STRIPE_SECRET_KEY: z.string().min(1).optional(),
STRIPE_WEBHOOK_SECRET: z.string().min(1).optional(),
STRIPE_STARTER_PRODUCT_ID: z.string().min(1).optional(),
STRIPE_STARTER_MONTHLY_PRICE_ID: z.string().min(1).optional(),
STRIPE_STARTER_YEARLY_PRICE_ID: z.string().min(1).optional(),
STRIPE_STARTER_CHATS_MONTHLY_PRICE_ID: z.string().min(1).optional(),
STRIPE_STARTER_CHATS_YEARLY_PRICE_ID: z.string().min(1).optional(),
STRIPE_STARTER_STORAGE_MONTHLY_PRICE_ID: z.string().min(1).optional(),
STRIPE_STARTER_STORAGE_YEARLY_PRICE_ID: z.string().min(1).optional(),
STRIPE_PRO_PRODUCT_ID: z.string().min(1).optional(),
STRIPE_PRO_MONTHLY_PRICE_ID: z.string().min(1).optional(),
STRIPE_PRO_YEARLY_PRICE_ID: z.string().min(1).optional(),
STRIPE_PRO_CHATS_MONTHLY_PRICE_ID: z.string().min(1).optional(),
STRIPE_PRO_CHATS_YEARLY_PRICE_ID: z.string().min(1).optional(),
STRIPE_PRO_STORAGE_MONTHLY_PRICE_ID: z.string().min(1).optional(),
STRIPE_PRO_STORAGE_YEARLY_PRICE_ID: z.string().min(1).optional(),
STRIPE_STARTER_PRICE_ID: z.string().min(1).optional(),
STRIPE_STARTER_CHATS_PRICE_ID: z.string().min(1).optional(),
STRIPE_PRO_PRICE_ID: z.string().min(1).optional(),
STRIPE_PRO_CHATS_PRICE_ID: z.string().min(1).optional(),
},
client: {
NEXT_PUBLIC_STRIPE_PUBLIC_KEY: z.string().min(1).optional(),

View File

@ -1,33 +0,0 @@
import { env } from '@typebot.io/env'
import { Plan } from '@typebot.io/prisma'
export const priceIds = {
[Plan.STARTER]: {
base: {
monthly: env.STRIPE_STARTER_MONTHLY_PRICE_ID,
yearly: env.STRIPE_STARTER_YEARLY_PRICE_ID,
},
chats: {
monthly: env.STRIPE_STARTER_CHATS_MONTHLY_PRICE_ID,
yearly: env.STRIPE_STARTER_CHATS_YEARLY_PRICE_ID,
},
storage: {
monthly: env.STRIPE_STARTER_STORAGE_MONTHLY_PRICE_ID,
yearly: env.STRIPE_STARTER_STORAGE_YEARLY_PRICE_ID,
},
},
[Plan.PRO]: {
base: {
monthly: env.STRIPE_PRO_MONTHLY_PRICE_ID,
yearly: env.STRIPE_PRO_YEARLY_PRICE_ID,
},
chats: {
monthly: env.STRIPE_PRO_CHATS_MONTHLY_PRICE_ID,
yearly: env.STRIPE_PRO_CHATS_YEARLY_PRICE_ID,
},
storage: {
monthly: env.STRIPE_PRO_STORAGE_MONTHLY_PRICE_ID,
yearly: env.STRIPE_PRO_STORAGE_YEARLY_PRICE_ID,
},
},
}

View File

@ -0,0 +1,167 @@
import { Plan } from '@typebot.io/prisma'
import type { Stripe } from 'stripe'
export const prices = {
[Plan.STARTER]: 39,
[Plan.PRO]: 89,
} as const
export const chatsLimits = {
[Plan.FREE]: 200,
[Plan.STARTER]: 2000,
[Plan.PRO]: 10000,
} as const
export const seatsLimits = {
[Plan.FREE]: 1,
[Plan.OFFERED]: 1,
[Plan.STARTER]: 2,
[Plan.PRO]: 5,
[Plan.LIFETIME]: 8,
} as const
export const starterChatTiers = [
{
up_to: 2000,
flat_amount: 0,
},
{
up_to: 2500,
flat_amount: 1000,
},
{
up_to: 3000,
flat_amount: 1000,
},
{
up_to: 3500,
flat_amount: 1000,
},
{
up_to: 4000,
flat_amount: 1000,
},
{
up_to: 'inf',
unit_amount: 2,
},
] satisfies Stripe.PriceCreateParams.Tier[]
export const proChatTiers = [
{
up_to: 10000,
flat_amount: 0,
},
{
up_to: 15000,
flat_amount: 5000,
},
{
up_to: 20000,
flat_amount: 4500,
},
{
up_to: 30000,
flat_amount: 8500,
},
{
up_to: 40000,
flat_amount: 8000,
},
{
up_to: 50000,
flat_amount: 7500,
},
{
up_to: 60000,
flat_amount: 7225,
},
{
up_to: 70000,
flat_amount: 7000,
},
{
up_to: 80000,
flat_amount: 6800,
},
{
up_to: 90000,
flat_amount: 6600,
},
{
up_to: 100000,
flat_amount: 6400,
},
{
up_to: 120000,
flat_amount: 12400,
},
{
up_to: 140000,
flat_amount: 12000,
},
{
up_to: 160000,
flat_amount: 11800,
},
{
up_to: 180000,
flat_amount: 11600,
},
{
up_to: 200000,
flat_amount: 11400,
},
{
up_to: 300000,
flat_amount: 55000,
},
{
up_to: 400000,
flat_amount: 53000,
},
{
up_to: 500000,
flat_amount: 51000,
},
{
up_to: 600000,
flat_amount: 50000,
},
{
up_to: 700000,
flat_amount: 49000,
},
{
up_to: 800000,
flat_amount: 48000,
},
{
up_to: 900000,
flat_amount: 47000,
},
{
up_to: 1000000,
flat_amount: 46000,
},
{
up_to: 1200000,
flat_amount: 91400,
},
{
up_to: 1400000,
flat_amount: 90800,
},
{
up_to: 1600000,
flat_amount: 90000,
},
{
up_to: 1800000,
flat_amount: 89400,
},
{
up_to: 'inf',
unit_amount_decimal: '0.442',
},
] satisfies Stripe.PriceCreateParams.Tier[]

View File

@ -0,0 +1,21 @@
import { guessIfUserIsEuropean } from './guessIfUserIsEuropean'
type FormatPriceParams = {
currency?: 'eur' | 'usd'
maxFractionDigits?: number
}
export const formatPrice = (
price: number,
{ currency, maxFractionDigits = 0 }: FormatPriceParams = {
maxFractionDigits: 0,
}
) => {
const isEuropean = guessIfUserIsEuropean()
const formatter = new Intl.NumberFormat(isEuropean ? 'fr-FR' : 'en-US', {
style: 'currency',
currency: currency?.toUpperCase() ?? (isEuropean ? 'EUR' : 'USD'),
maximumFractionDigits: maxFractionDigits,
})
return formatter.format(price)
}

View File

@ -0,0 +1,19 @@
import { Plan } from '@typebot.io/prisma'
import { chatsLimits } from './constants'
import { Workspace } from '@typebot.io/schemas'
export const getChatsLimit = ({
plan,
customChatsLimit,
}: Pick<Workspace, 'plan'> & {
customChatsLimit?: Workspace['customChatsLimit']
}) => {
if (
plan === Plan.UNLIMITED ||
plan === Plan.LIFETIME ||
plan === Plan.OFFERED
)
return -1
if (plan === Plan.CUSTOM) return customChatsLimit ?? -1
return chatsLimits[plan]
}

View File

@ -0,0 +1,12 @@
import { Workspace } from '@typebot.io/schemas'
import { seatsLimits } from './constants'
import { Plan } from '@typebot.io/prisma'
export const getSeatsLimit = ({
plan,
customSeatsLimit,
}: Pick<Workspace, 'plan' | 'customSeatsLimit'>) => {
if (plan === Plan.UNLIMITED) return -1
if (plan === Plan.CUSTOM) return customSeatsLimit ? customSeatsLimit : -1
return seatsLimits[plan]
}

View File

@ -0,0 +1,56 @@
const europeanUnionCountryCodes = [
'AT',
'BE',
'BG',
'CY',
'CZ',
'DE',
'DK',
'EE',
'ES',
'FI',
'FR',
'GR',
'HR',
'HU',
'IE',
'IT',
'LT',
'LU',
'LV',
'MT',
'NL',
'PL',
'PT',
'RO',
'SE',
'SI',
'SK',
]
const europeanUnionExclusiveLanguageCodes = [
'fr',
'de',
'it',
'el',
'pl',
'fi',
'nl',
'hr',
'cs',
'hu',
'ro',
'sl',
'sv',
'bg',
]
export const guessIfUserIsEuropean = () => {
if (typeof window === 'undefined') return false
return window.navigator.languages.some((language) => {
const [languageCode, countryCode] = language.split('-')
return countryCode
? europeanUnionCountryCodes.includes(countryCode)
: europeanUnionExclusiveLanguageCodes.includes(languageCode)
})
}

View File

@ -27,6 +27,7 @@
"@udecode/plate-common": "^21.1.5",
"got": "12.6.0",
"minio": "7.1.3",
"remark-slate": "^1.8.6"
"remark-slate": "^1.8.6",
"stripe": "12.13.0"
}
}

View File

@ -1,177 +0,0 @@
import type { Workspace } from '@typebot.io/prisma'
import { Plan } from '@typebot.io/prisma'
const infinity = -1
export const prices = {
[Plan.STARTER]: 39,
[Plan.PRO]: 89,
} as const
export const chatsLimit = {
[Plan.FREE]: { totalIncluded: 200 },
[Plan.STARTER]: {
graduatedPrice: [
{ totalIncluded: 2000, price: 0 },
{
totalIncluded: 2500,
price: 10,
},
{
totalIncluded: 3000,
price: 20,
},
{
totalIncluded: 3500,
price: 30,
},
],
},
[Plan.PRO]: {
graduatedPrice: [
{ totalIncluded: 10000, price: 0 },
{ totalIncluded: 15000, price: 50 },
{ totalIncluded: 25000, price: 150 },
{ totalIncluded: 50000, price: 400 },
],
},
[Plan.CUSTOM]: {
totalIncluded: 2000,
increaseStep: {
amount: 500,
price: 10,
},
},
[Plan.OFFERED]: { totalIncluded: infinity },
[Plan.LIFETIME]: { totalIncluded: infinity },
[Plan.UNLIMITED]: { totalIncluded: infinity },
} as const
export const seatsLimit = {
[Plan.FREE]: { totalIncluded: 1 },
[Plan.STARTER]: {
totalIncluded: 2,
},
[Plan.PRO]: {
totalIncluded: 5,
},
[Plan.CUSTOM]: {
totalIncluded: 2,
},
[Plan.OFFERED]: { totalIncluded: 2 },
[Plan.LIFETIME]: { totalIncluded: 8 },
[Plan.UNLIMITED]: { totalIncluded: infinity },
} as const
export const getChatsLimit = ({
plan,
additionalChatsIndex,
customChatsLimit,
}: Pick<Workspace, 'additionalChatsIndex' | 'plan' | 'customChatsLimit'>) => {
if (customChatsLimit) return customChatsLimit
const totalIncluded =
plan === Plan.STARTER || plan === Plan.PRO
? chatsLimit[plan].graduatedPrice[additionalChatsIndex].totalIncluded
: chatsLimit[plan].totalIncluded
return totalIncluded
}
export const getSeatsLimit = ({
plan,
customSeatsLimit,
}: Pick<Workspace, 'plan' | 'customSeatsLimit'>) => {
if (customSeatsLimit) return customSeatsLimit
return seatsLimit[plan].totalIncluded
}
export const isSeatsLimitReached = ({
existingMembersAndInvitationsCount,
plan,
customSeatsLimit,
}: {
existingMembersAndInvitationsCount: number
} & Pick<Workspace, 'plan' | 'customSeatsLimit'>) => {
const seatsLimit = getSeatsLimit({ plan, customSeatsLimit })
return (
seatsLimit !== infinity && seatsLimit <= existingMembersAndInvitationsCount
)
}
export const computePrice = (
plan: Plan,
selectedTotalChatsIndex: number,
frequency: 'monthly' | 'yearly'
) => {
if (plan !== Plan.STARTER && plan !== Plan.PRO) return
const price =
prices[plan] +
chatsLimit[plan].graduatedPrice[selectedTotalChatsIndex].price
return frequency === 'monthly' ? price : price - price * 0.16
}
const europeanUnionCountryCodes = [
'AT',
'BE',
'BG',
'CY',
'CZ',
'DE',
'DK',
'EE',
'ES',
'FI',
'FR',
'GR',
'HR',
'HU',
'IE',
'IT',
'LT',
'LU',
'LV',
'MT',
'NL',
'PL',
'PT',
'RO',
'SE',
'SI',
'SK',
]
const europeanUnionExclusiveLanguageCodes = [
'fr',
'de',
'it',
'el',
'pl',
'fi',
'nl',
'hr',
'cs',
'hu',
'ro',
'sl',
'sv',
'bg',
]
export const guessIfUserIsEuropean = () => {
if (typeof window === 'undefined') return false
return window.navigator.languages.some((language) => {
const [languageCode, countryCode] = language.split('-')
return countryCode
? europeanUnionCountryCodes.includes(countryCode)
: europeanUnionExclusiveLanguageCodes.includes(languageCode)
})
}
export const formatPrice = (price: number, currency?: 'eur' | 'usd') => {
const isEuropean = guessIfUserIsEuropean()
const formatter = new Intl.NumberFormat(isEuropean ? 'fr-FR' : 'en-US', {
style: 'currency',
currency: currency?.toUpperCase() ?? (isEuropean ? 'EUR' : 'USD'),
maximumFractionDigits: 0, // (causes 2500.99 to be printed as $2,501)
})
return formatter.format(price)
}

View File

@ -1,7 +1,10 @@
import { z } from 'zod'
export const subscriptionSchema = z.object({
isYearly: z.boolean(),
currentBillingPeriod: z.object({
start: z.date(),
end: z.date(),
}),
currency: z.enum(['eur', 'usd']),
cancelDate: z.date().optional(),
status: z.enum(['active', 'past_due']),

View File

@ -62,7 +62,6 @@ const subscriptionUpdatedEventSchema = workspaceEvent.merge(
name: z.literal('Subscription updated'),
data: z.object({
plan: z.nativeEnum(Plan),
additionalChatsIndex: z.number(),
}),
})
)

View File

@ -4,25 +4,26 @@ import {
PrismaClient,
WorkspaceRole,
} from '@typebot.io/prisma'
import { isDefined } from '@typebot.io/lib'
import { getChatsLimit } from '@typebot.io/lib/pricing'
import { isDefined, isEmpty } from '@typebot.io/lib'
import { getChatsLimit } from '@typebot.io/lib/billing/getChatsLimit'
import { getUsage } from '@typebot.io/lib/api/getUsage'
import { promptAndSetEnvironment } from './utils'
import { Workspace } from '@typebot.io/schemas'
import { sendAlmostReachedChatsLimitEmail } from '@typebot.io/emails/src/emails/AlmostReachedChatsLimitEmail'
import { sendReachedChatsLimitEmail } from '@typebot.io/emails/src/emails/ReachedChatsLimitEmail'
import { TelemetryEvent } from '@typebot.io/schemas/features/telemetry'
import { sendTelemetryEvents } from '@typebot.io/lib/telemetry/sendTelemetryEvent'
import Stripe from 'stripe'
import { createId } from '@paralleldrive/cuid2'
const prisma = new PrismaClient()
const LIMIT_EMAIL_TRIGGER_PERCENT = 0.8
const LIMIT_EMAIL_TRIGGER_PERCENT = 0.75
type WorkspaceForDigest = Pick<
Workspace,
| 'id'
| 'plan'
| 'name'
| 'customChatsLimit'
| 'additionalChatsIndex'
| 'isQuarantined'
| 'chatsLimitFirstEmailSentAt'
| 'chatsLimitSecondEmailSentAt'
@ -32,12 +33,40 @@ type WorkspaceForDigest = Pick<
})[]
}
export const sendTotalResultsDigest = async () => {
type ResultWithWorkspace = {
userId: string
workspace: {
id: string
typebots: {
id: string
}[]
members: {
user: {
id: string
email: string | null
}
role: WorkspaceRole
}[]
additionalStorageIndex: number
customChatsLimit: number | null
customStorageLimit: number | null
plan: Plan
isQuarantined: boolean
stripeId: string | null
}
typebotId: string
totalResultsYesterday: number
isFirstOfKind: true | undefined
}
export const checkAndReportChatsUsage = async () => {
await promptAndSetEnvironment('production')
console.log('Get collected results from the last hour...')
const hourAgo = new Date(Date.now() - 1000 * 60 * 60)
const zeroedMinutesHour = new Date()
zeroedMinutesHour.setUTCMinutes(0, 0, 0)
const hourAgo = new Date(zeroedMinutesHour.getTime() - 1000 * 60 * 60)
const results = await prisma.result.groupBy({
by: ['typebotId'],
@ -47,6 +76,7 @@ export const sendTotalResultsDigest = async () => {
where: {
hasStarted: true,
createdAt: {
lt: zeroedMinutesHour,
gte: hourAgo,
},
},
@ -69,11 +99,11 @@ export const sendTotalResultsDigest = async () => {
},
select: {
id: true,
name: true,
typebots: { select: { id: true } },
members: {
select: { user: { select: { id: true, email: true } }, role: true },
},
additionalChatsIndex: true,
additionalStorageIndex: true,
customChatsLimit: true,
customStorageLimit: true,
@ -81,6 +111,7 @@ export const sendTotalResultsDigest = async () => {
isQuarantined: true,
chatsLimitFirstEmailSentAt: true,
chatsLimitSecondEmailSentAt: true,
stripeId: true,
},
})
@ -110,9 +141,27 @@ export const sendTotalResultsDigest = async () => {
.map((result) => result.workspace)
)
console.log(`Send ${events.length} auto quarantine events...`)
await reportUsageToStripe(resultsWithWorkspaces)
await sendTelemetryEvents(events)
const newResultsCollectedEvents = resultsWithWorkspaces.map(
(result) =>
({
name: 'New results collected',
userId: result.userId,
workspaceId: result.workspace.id,
typebotId: result.typebotId,
data: {
total: result.totalResultsYesterday,
isFirstOfKind: result.isFirstOfKind,
},
} satisfies TelemetryEvent)
)
console.log(
`Send ${newResultsCollectedEvents.length} new results events and ${events.length} auto quarantine events...`
)
await sendTelemetryEvents(events.concat(newResultsCollectedEvents))
}
const sendAlertIfLimitReached = async (
@ -144,7 +193,7 @@ const sendAlertIfLimitReached = async (
to,
usagePercent: Math.round((totalChatsUsed / chatsLimit) * 100),
chatsLimit,
url: `https://app.typebot.io/typebots?workspaceId=${workspace.id}`,
workspaceName: workspace.name,
})
await prisma.workspace.updateMany({
where: { id: workspace.id },
@ -155,32 +204,7 @@ const sendAlertIfLimitReached = async (
}
}
if (
chatsLimit > 0 &&
totalChatsUsed >= chatsLimit &&
!workspace.chatsLimitSecondEmailSentAt
) {
const to = workspace.members
.filter((member) => member.role === WorkspaceRole.ADMIN)
.map((member) => member.user.email)
.filter(isDefined)
try {
console.log(`Send reached chats limit email to ${to.join(', ')}...`)
await sendReachedChatsLimitEmail({
to,
chatsLimit,
url: `https://app.typebot.io/typebots?workspaceId=${workspace.id}`,
})
await prisma.workspace.updateMany({
where: { id: workspace.id },
data: { chatsLimitSecondEmailSentAt: new Date() },
})
} catch (err) {
console.error(err)
}
}
if (totalChatsUsed > chatsLimit * 3 && workspace.plan === Plan.FREE) {
if (totalChatsUsed > chatsLimit * 1.5 && workspace.plan === Plan.FREE) {
console.log(`Automatically quarantine workspace ${workspace.id}...`)
await prisma.workspace.updateMany({
where: { id: workspace.id },
@ -207,4 +231,67 @@ const sendAlertIfLimitReached = async (
return events
}
sendTotalResultsDigest().then()
const reportUsageToStripe = async (
resultsWithWorkspaces: (Pick<ResultWithWorkspace, 'totalResultsYesterday'> & {
workspace: Pick<
ResultWithWorkspace['workspace'],
'id' | 'plan' | 'stripeId'
>
})[]
) => {
if (isEmpty(process.env.STRIPE_SECRET_KEY))
throw new Error('Missing STRIPE_SECRET_KEY env variable')
const stripe = new Stripe(process.env.STRIPE_SECRET_KEY, {
apiVersion: '2022-11-15',
})
for (const result of resultsWithWorkspaces.filter(
(result) =>
result.workspace.plan === 'STARTER' || result.workspace.plan === 'PRO'
)) {
if (!result.workspace.stripeId)
throw new Error(
`Found paid workspace without a stripeId: ${result.workspace.stripeId}`
)
const subscriptions = await stripe.subscriptions.list({
customer: result.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 Error(
`Found paid workspace without a subscription: ${result.workspace.stripeId}`
)
const subscriptionItem = currentSubscription.items.data.find(
(item) =>
item.price.id === process.env.STRIPE_STARTER_CHATS_PRICE_ID ||
item.price.id === process.env.STRIPE_PRO_CHATS_PRICE_ID
)
if (!subscriptionItem)
throw new Error(
`Could not find subscription item for workspace ${result.workspace.id}`
)
const idempotencyKey = createId()
await stripe.subscriptionItems.createUsageRecord(
subscriptionItem.id,
{
quantity: result.totalResultsYesterday,
timestamp: 'now',
},
{
idempotencyKey,
}
)
}
}
checkAndReportChatsUsage().then()

View File

@ -180,17 +180,12 @@ const resetBillingProps = async () => {
{
chatsLimitFirstEmailSentAt: { not: null },
},
{
storageLimitFirstEmailSentAt: { not: null },
},
],
},
data: {
isQuarantined: false,
chatsLimitFirstEmailSentAt: null,
storageLimitFirstEmailSentAt: null,
chatsLimitSecondEmailSentAt: null,
storageLimitSecondEmailSentAt: null,
},
})
console.log(`Resetted ${count} workspaces.`)

View File

@ -0,0 +1,50 @@
import Stripe from 'stripe'
import { promptAndSetEnvironment } from './utils'
import {
proChatTiers,
starterChatTiers,
} from '@typebot.io/lib/billing/constants'
const chatsProductId = 'prod_MVXtq5sATQzIcM'
const createChatsPrices = async () => {
await promptAndSetEnvironment()
const stripe = new Stripe(process.env.STRIPE_SECRET_KEY!, {
apiVersion: '2022-11-15',
})
await stripe.prices.create({
currency: 'usd',
billing_scheme: 'tiered',
recurring: { interval: 'month', usage_type: 'metered' },
tiers: starterChatTiers,
tiers_mode: 'volume',
tax_behavior: 'exclusive',
product: chatsProductId,
currency_options: {
eur: {
tax_behavior: 'exclusive',
tiers: starterChatTiers,
},
},
})
await stripe.prices.create({
currency: 'usd',
billing_scheme: 'tiered',
recurring: { interval: 'month', usage_type: 'metered' },
tiers: proChatTiers,
tiers_mode: 'volume',
tax_behavior: 'exclusive',
product: chatsProductId,
currency_options: {
eur: {
tax_behavior: 'exclusive',
tiers: proChatTiers,
},
},
})
}
createChatsPrices()

View File

@ -39,7 +39,6 @@ const inspectUser = async () => {
user: { email: { not: response.email } },
},
},
additionalChatsIndex: true,
additionalStorageIndex: true,
typebots: {
orderBy: {
@ -82,10 +81,6 @@ const inspectUser = async () => {
console.log(' - Name:', workspace.workspace.name)
console.log(' Plan:', workspace.workspace.plan)
console.log(' Members:', workspace.workspace.members.length + 1)
console.log(
' Additional chats:',
workspace.workspace.additionalChatsIndex
)
console.log(
' Additional storage:',
workspace.workspace.additionalStorageIndex

View File

@ -0,0 +1,278 @@
import { PrismaClient } from '@typebot.io/prisma'
import { promptAndSetEnvironment } from './utils'
import { Stripe } from 'stripe'
import { createId } from '@paralleldrive/cuid2'
import { writeFileSync } from 'fs'
const migrateSubscriptionsToUsageBased = async () => {
await promptAndSetEnvironment()
const prisma = new PrismaClient()
if (
!process.env.STRIPE_STARTER_CHATS_PRICE_ID ||
!process.env.STRIPE_PRO_CHATS_PRICE_ID ||
!process.env.STRIPE_SECRET_KEY ||
!process.env.STRIPE_STARTER_PRICE_ID ||
!process.env.STRIPE_PRO_PRICE_ID
)
throw new Error('Missing some env variables')
const {
starterChatsPriceId,
proChatsPriceId,
secretKey,
starterPriceId,
proPriceId,
starterYearlyPriceId,
proYearlyPriceId,
} = {
starterChatsPriceId: process.env.STRIPE_STARTER_CHATS_PRICE_ID,
proChatsPriceId: process.env.STRIPE_PRO_CHATS_PRICE_ID,
secretKey: process.env.STRIPE_SECRET_KEY,
starterPriceId: process.env.STRIPE_STARTER_PRICE_ID,
proPriceId: process.env.STRIPE_PRO_PRICE_ID,
starterYearlyPriceId: process.env.STRIPE_STARTER_YEARLY_PRICE_ID,
proYearlyPriceId: process.env.STRIPE_PRO_YEARLY_PRICE_ID,
}
const workspacesWithPaidPlan = await prisma.workspace.findMany({
where: {
plan: {
in: ['PRO', 'STARTER'],
},
isSuspended: false,
},
select: {
plan: true,
name: true,
id: true,
stripeId: true,
isQuarantined: true,
members: {
select: {
user: {
select: { email: true },
},
},
},
},
})
writeFileSync(
'./workspacesWithPaidPlan.json',
JSON.stringify(workspacesWithPaidPlan, null, 2)
)
const stripe = new Stripe(secretKey, {
apiVersion: '2022-11-15',
})
const todayMidnight = new Date()
todayMidnight.setUTCHours(0, 0, 0, 0)
const failedWorkspaces = []
const workspacesWithoutSubscription = []
const workspacesWithoutStripeId = []
let i = 0
for (const workspace of workspacesWithPaidPlan) {
i += 1
console.log(
`(${i} / ${workspacesWithPaidPlan.length})`,
'Migrating workspace:',
workspace.id,
workspace.name,
workspace.stripeId,
JSON.stringify(workspace.members.map((member) => member.user.email))
)
if (!workspace.stripeId) {
console.log('No stripe ID, skipping...')
workspacesWithoutStripeId.push(workspace)
writeFileSync(
'./workspacesWithoutStripeId.json',
JSON.stringify(workspacesWithoutStripeId, null, 2)
)
continue
}
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) {
console.log('No current subscription in workspace:', workspace.id)
workspacesWithoutSubscription.push(workspace)
writeFileSync(
'./workspacesWithoutSubscription.json',
JSON.stringify(workspacesWithoutSubscription)
)
continue
}
if (
currentSubscription.items.data.find(
(item) =>
item.price.id === starterChatsPriceId ||
item.price.id === proChatsPriceId
)
) {
console.log(
'Already migrated to usage based billing for workspace. Skipping...'
)
continue
}
if (
!currentSubscription.items.data.find(
(item) =>
item.price.id === starterPriceId ||
item.price.id === proPriceId ||
item.price.id === starterYearlyPriceId ||
item.price.id === proYearlyPriceId
)
) {
console.log(
'Could not find STARTER or PRO plan in items for workspace:',
workspace.id
)
failedWorkspaces.push(workspace)
writeFileSync(
'./failedWorkspaces.json',
JSON.stringify(failedWorkspaces, null, 2)
)
continue
}
const newSubscription = await stripe.subscriptions.update(
currentSubscription.id,
{
items: [
...currentSubscription.items.data.flatMap<Stripe.SubscriptionUpdateParams.Item>(
(item) => {
if (
item.price.id === starterPriceId ||
item.price.id === proPriceId
)
return {
id: item.id,
price: item.price.id,
quantity: item.quantity,
}
if (
item.price.id === starterYearlyPriceId ||
item.price.id === proYearlyPriceId
)
return [
{
id: item.id,
price: item.price.id,
quantity: item.quantity,
deleted: true,
},
{
price:
workspace.plan === 'STARTER'
? starterPriceId
: proPriceId,
quantity: 1,
},
]
return {
id: item.id,
price: item.price.id,
quantity: item.quantity,
deleted: true,
}
}
),
{
price:
workspace.plan === 'STARTER'
? starterChatsPriceId
: proChatsPriceId,
},
],
}
)
const totalResults = await prisma.result.count({
where: {
typebot: { workspaceId: workspace.id },
hasStarted: true,
createdAt: {
gte: new Date(newSubscription.current_period_start * 1000),
lt: todayMidnight,
},
},
})
if (workspace.plan === 'STARTER' && totalResults >= 4000) {
console.log(
'Workspace has more than 4000 chats, automatically upgrading to PRO plan'
)
const currentPlanItemId = newSubscription?.items.data.find((item) =>
[starterPriceId, proPriceId].includes(item.price.id)
)?.id
if (!currentPlanItemId)
throw new Error(`Could not find current plan item ID for workspace`)
await stripe.subscriptions.update(newSubscription.id, {
items: [
{
id: currentPlanItemId,
price: proPriceId,
quantity: 1,
},
],
})
await prisma.workspace.update({
where: { id: workspace.id },
data: {
plan: 'PRO',
},
})
}
const subscriptionItem = newSubscription.items.data.find(
(item) =>
item.price.id === starterChatsPriceId ||
item.price.id === proChatsPriceId
)
if (!subscriptionItem)
throw new Error(
`Could not find subscription item for workspace ${workspace.id}`
)
const idempotencyKey = createId()
console.log('Reporting total results:', totalResults)
await stripe.subscriptionItems.createUsageRecord(
subscriptionItem.id,
{
quantity: totalResults,
timestamp: 'now',
},
{
idempotencyKey,
}
)
if (workspace.isQuarantined) {
await prisma.workspace.update({
where: { id: workspace.id },
data: {
isQuarantined: false,
},
})
}
}
}
migrateSubscriptionsToUsageBased()

View File

@ -13,9 +13,11 @@
"db:bulkUpdate": "tsx bulkUpdate.ts",
"db:fixTypebots": "tsx fixTypebots.ts",
"telemetry:sendTotalResultsDigest": "tsx sendTotalResultsDigest.ts",
"sendAlertEmails": "tsx sendAlertEmails.ts",
"checkAndReportChatsUsage": "tsx checkAndReportChatsUsage.ts",
"inspectUser": "tsx inspectUser.ts",
"checkSubscriptionsStatus": "tsx checkSubscriptionsStatus.ts"
"checkSubscriptionsStatus": "tsx checkSubscriptionsStatus.ts",
"createChatsPrices": "tsx createChatsPrices.ts",
"migrateSubscriptionsToUsageBased": "tsx migrateSubscriptionsToUsageBased.ts"
},
"devDependencies": {
"@typebot.io/emails": "workspace:*",
@ -31,5 +33,8 @@
"tsx": "3.12.7",
"typescript": "5.1.6",
"zod": "3.21.4"
},
"dependencies": {
"@paralleldrive/cuid2": "2.2.1"
}
}

View File

@ -1,200 +0,0 @@
import {
MemberInWorkspace,
PrismaClient,
WorkspaceRole,
} from '@typebot.io/prisma'
import { isDefined } from '@typebot.io/lib'
import { getChatsLimit } from '@typebot.io/lib/pricing'
import { promptAndSetEnvironment } from './utils'
import { TelemetryEvent } from '@typebot.io/schemas/features/telemetry'
import { sendTelemetryEvents } from '@typebot.io/lib/telemetry/sendTelemetryEvent'
import { Workspace } from '@typebot.io/schemas'
const prisma = new PrismaClient()
type WorkspaceForDigest = Pick<
Workspace,
| 'id'
| 'plan'
| 'customChatsLimit'
| 'customStorageLimit'
| 'additionalChatsIndex'
| 'additionalStorageIndex'
| 'isQuarantined'
> & {
members: (Pick<MemberInWorkspace, 'role'> & {
user: { id: string; email: string | null }
})[]
}
export const sendTotalResultsDigest = async () => {
await promptAndSetEnvironment('production')
console.log("Generating total results yesterday's digest...")
const todayMidnight = new Date()
todayMidnight.setUTCHours(0, 0, 0, 0)
const yesterday = new Date(todayMidnight)
yesterday.setDate(yesterday.getDate() - 1)
const results = await prisma.result.groupBy({
by: ['typebotId'],
_count: {
_all: true,
},
where: {
hasStarted: true,
createdAt: {
gte: yesterday,
lt: todayMidnight,
},
},
})
console.log(
`Found ${results.reduce(
(total, result) => total + result._count._all,
0
)} results collected yesterday.`
)
const workspaces = await prisma.workspace.findMany({
where: {
typebots: {
some: {
id: { in: results.map((result) => result.typebotId) },
},
},
},
select: {
id: true,
typebots: { select: { id: true } },
members: {
select: { user: { select: { id: true, email: true } }, role: true },
},
additionalChatsIndex: true,
additionalStorageIndex: true,
customChatsLimit: true,
customStorageLimit: true,
plan: true,
isQuarantined: true,
},
})
const resultsWithWorkspaces = results
.flatMap((result) => {
const workspace = workspaces.find((workspace) =>
workspace.typebots.some((typebot) => typebot.id === result.typebotId)
)
if (!workspace) return
return workspace.members
.filter((member) => member.role !== WorkspaceRole.GUEST)
.map((member, memberIndex) => ({
userId: member.user.id,
workspace: workspace,
typebotId: result.typebotId,
totalResultsYesterday: result._count._all,
isFirstOfKind: memberIndex === 0 ? (true as const) : undefined,
}))
})
.filter(isDefined)
console.log('Computing workspaces limits...')
const workspaceLimitReachedEvents = await sendAlertIfLimitReached(
resultsWithWorkspaces
.filter((result) => result.isFirstOfKind)
.map((result) => result.workspace)
)
const newResultsCollectedEvents = resultsWithWorkspaces.map(
(result) =>
({
name: 'New results collected',
userId: result.userId,
workspaceId: result.workspace.id,
typebotId: result.typebotId,
data: {
total: result.totalResultsYesterday,
isFirstOfKind: result.isFirstOfKind,
},
} satisfies TelemetryEvent)
)
await sendTelemetryEvents(
workspaceLimitReachedEvents.concat(newResultsCollectedEvents)
)
console.log(
`Sent ${workspaceLimitReachedEvents.length} workspace limit reached events.`
)
console.log(
`Sent ${newResultsCollectedEvents.length} new results collected events.`
)
}
const sendAlertIfLimitReached = async (
workspaces: WorkspaceForDigest[]
): Promise<TelemetryEvent[]> => {
const events: TelemetryEvent[] = []
const taggedWorkspaces: string[] = []
for (const workspace of workspaces) {
if (taggedWorkspaces.includes(workspace.id) || workspace.isQuarantined)
continue
taggedWorkspaces.push(workspace.id)
const { totalChatsUsed } = await getUsage(workspace.id)
const chatsLimit = getChatsLimit(workspace)
if (chatsLimit > 0 && totalChatsUsed >= chatsLimit) {
events.push(
...workspace.members
.filter((member) => member.role === WorkspaceRole.ADMIN)
.map(
(member) =>
({
name: 'Workspace limit reached',
userId: member.user.id,
workspaceId: workspace.id,
data: {
totalChatsUsed,
chatsLimit,
},
} satisfies TelemetryEvent)
)
)
continue
}
}
return events
}
const getUsage = async (workspaceId: string) => {
const now = new Date()
const firstDayOfMonth = new Date(now.getFullYear(), now.getMonth(), 1)
const firstDayOfNextMonth = new Date(now.getFullYear(), now.getMonth() + 1, 1)
const typebots = await prisma.typebot.findMany({
where: {
workspace: {
id: workspaceId,
},
},
select: { id: true },
})
const [totalChatsUsed] = await Promise.all([
prisma.result.count({
where: {
typebotId: { in: typebots.map((typebot) => typebot.id) },
hasStarted: true,
createdAt: {
gte: firstDayOfMonth,
lt: firstDayOfNextMonth,
},
},
}),
])
return {
totalChatsUsed,
}
}
sendTotalResultsDigest().then()