@@ -1,3 +1,4 @@
|
||||
import React from 'react'
|
||||
import { IMjmlImageProps, MjmlImage } from '@faire/mjml-react'
|
||||
import { borderBase } from '../theme'
|
||||
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import React from 'react'
|
||||
import { MjmlText, IMjmlTextProps } from '@faire/mjml-react'
|
||||
import { leadingRelaxed, textBase } from '../theme'
|
||||
|
||||
|
||||
64
packages/emails/src/emails/ReachedChatsLimitEmail.tsx
Normal file
64
packages/emails/src/emails/ReachedChatsLimitEmail.tsx
Normal file
@@ -0,0 +1,64 @@
|
||||
import React, { ComponentProps } from 'react'
|
||||
import {
|
||||
Mjml,
|
||||
MjmlBody,
|
||||
MjmlSection,
|
||||
MjmlColumn,
|
||||
MjmlSpacer,
|
||||
} from '@faire/mjml-react'
|
||||
import { render } from '@faire/mjml-react/utils/render'
|
||||
import { Button, Head, HeroImage, Text } from '../components'
|
||||
import { parseNumberWithCommas } from '@typebot.io/lib'
|
||||
import { SendMailOptions } from 'nodemailer'
|
||||
import { sendEmail } from '../sendEmail'
|
||||
|
||||
type ReachedChatsLimitEmailProps = {
|
||||
chatsLimit: number
|
||||
url: string
|
||||
}
|
||||
|
||||
export const ReachedChatsLimitEmail = ({
|
||||
chatsLimit,
|
||||
url,
|
||||
}: ReachedChatsLimitEmailProps) => {
|
||||
const readableChatsLimit = parseNumberWithCommas(chatsLimit)
|
||||
|
||||
return (
|
||||
<Mjml>
|
||||
<Head />
|
||||
<MjmlBody width={600}>
|
||||
<MjmlSection padding="0">
|
||||
<MjmlColumn>
|
||||
<HeroImage src="https://typebot.s3.fr-par.scw.cloud/public/assets/actionRequiredEmailBanner.png" />
|
||||
</MjmlColumn>
|
||||
</MjmlSection>
|
||||
<MjmlSection padding="0 24px" cssClass="smooth">
|
||||
<MjmlColumn>
|
||||
<Text>
|
||||
It just happened, you've reached your monthly{' '}
|
||||
{readableChatsLimit} chats limit 😮
|
||||
</Text>
|
||||
<Text>
|
||||
If you'd like your bots to continue chatting with your users
|
||||
this month, then you need to upgrade your plan. 🚀
|
||||
</Text>
|
||||
|
||||
<MjmlSpacer height="24px" />
|
||||
<Button link={url}>Upgrade workspace</Button>
|
||||
</MjmlColumn>
|
||||
</MjmlSection>
|
||||
</MjmlBody>
|
||||
</Mjml>
|
||||
)
|
||||
}
|
||||
|
||||
export const sendReachedChatsLimitEmail = ({
|
||||
to,
|
||||
...props
|
||||
}: Pick<SendMailOptions, 'to'> &
|
||||
ComponentProps<typeof ReachedChatsLimitEmail>) =>
|
||||
sendEmail({
|
||||
to,
|
||||
subject: "You've reached your chats limit",
|
||||
html: render(<ReachedChatsLimitEmail {...props} />).html,
|
||||
})
|
||||
@@ -3,6 +3,7 @@ import { TelemetryEvent } from '@typebot.io/schemas/features/telemetry'
|
||||
import { isEmpty, isNotEmpty } from '../utils'
|
||||
|
||||
export const sendTelemetryEvents = async (events: TelemetryEvent[]) => {
|
||||
if (events.length === 0) return { message: 'No events to send' }
|
||||
if (isEmpty(process.env.TELEMETRY_WEBHOOK_URL))
|
||||
return { message: 'Telemetry not enabled' }
|
||||
|
||||
|
||||
@@ -90,6 +90,16 @@ const workspaceLimitReachedEventSchema = workspaceEvent.merge(
|
||||
})
|
||||
)
|
||||
|
||||
const workspaceAutoQuarantinedEventSchema = workspaceEvent.merge(
|
||||
z.object({
|
||||
name: z.literal('Workspace automatically quarantined'),
|
||||
data: z.object({
|
||||
chatsLimit: z.number(),
|
||||
totalChatsUsed: z.number(),
|
||||
}),
|
||||
})
|
||||
)
|
||||
|
||||
export const eventSchema = z.discriminatedUnion('name', [
|
||||
workspaceCreatedEventSchema,
|
||||
userCreatedEventSchema,
|
||||
@@ -98,6 +108,7 @@ export const eventSchema = z.discriminatedUnion('name', [
|
||||
subscriptionUpdatedEventSchema,
|
||||
newResultsCollectedEventSchema,
|
||||
workspaceLimitReachedEventSchema,
|
||||
workspaceAutoQuarantinedEventSchema,
|
||||
])
|
||||
|
||||
export type TelemetryEvent = z.infer<typeof eventSchema>
|
||||
|
||||
@@ -1,8 +1,2 @@
|
||||
DATABASE_URL=postgresql://postgres:typebot@localhost:5432/typebot
|
||||
ENCRYPTION_SECRET=
|
||||
|
||||
# For setCustomPlan
|
||||
STRIPE_SECRET_KEY=
|
||||
STRIPE_SUBSCRIPTION_ID=
|
||||
STRIPE_PRODUCT_ID=
|
||||
WORKSPACE_ID=
|
||||
@@ -12,21 +12,22 @@
|
||||
"db:setCustomPlan": "tsx setCustomPlan.ts",
|
||||
"db:bulkUpdate": "tsx bulkUpdate.ts",
|
||||
"db:fixTypebots": "tsx fixTypebots.ts",
|
||||
"telemetry:sendTotalResultsDigest": "tsx sendTotalResultsDigest.ts"
|
||||
"telemetry:sendTotalResultsDigest": "tsx sendTotalResultsDigest.ts",
|
||||
"sendAlertEmails": "tsx sendAlertEmails.ts"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@typebot.io/emails": "workspace:*",
|
||||
"@typebot.io/lib": "workspace:*",
|
||||
"@typebot.io/prisma": "workspace:*",
|
||||
"@typebot.io/schemas": "workspace:*",
|
||||
"@types/node": "20.2.3",
|
||||
"@types/prompts": "2.4.4",
|
||||
"@typebot.io/prisma": "workspace:*",
|
||||
"deep-object-diff": "1.1.9",
|
||||
"@typebot.io/emails": "workspace:*",
|
||||
"got": "12.6.0",
|
||||
"@typebot.io/schemas": "workspace:*",
|
||||
"prompts": "2.4.2",
|
||||
"stripe": "12.6.0",
|
||||
"tsx": "3.12.7",
|
||||
"typescript": "5.0.4",
|
||||
"@typebot.io/lib": "workspace:*",
|
||||
"zod": "3.21.4"
|
||||
}
|
||||
}
|
||||
|
||||
247
packages/scripts/sendAlertEmails.ts
Normal file
247
packages/scripts/sendAlertEmails.ts
Normal file
@@ -0,0 +1,247 @@
|
||||
import {
|
||||
MemberInWorkspace,
|
||||
Plan,
|
||||
PrismaClient,
|
||||
WorkspaceRole,
|
||||
} from '@typebot.io/prisma'
|
||||
import { isDefined } from '@typebot.io/lib'
|
||||
import { getChatsLimit } from '@typebot.io/lib/pricing'
|
||||
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'
|
||||
|
||||
const prisma = new PrismaClient()
|
||||
const LIMIT_EMAIL_TRIGGER_PERCENT = 0.8
|
||||
|
||||
type WorkspaceForDigest = Pick<
|
||||
Workspace,
|
||||
| 'id'
|
||||
| 'plan'
|
||||
| 'customChatsLimit'
|
||||
| 'additionalChatsIndex'
|
||||
| 'isQuarantined'
|
||||
| 'chatsLimitFirstEmailSentAt'
|
||||
| 'chatsLimitSecondEmailSentAt'
|
||||
> & {
|
||||
members: (Pick<MemberInWorkspace, 'role'> & {
|
||||
user: { id: string; email: string | null }
|
||||
})[]
|
||||
}
|
||||
|
||||
export const sendTotalResultsDigest = async () => {
|
||||
await promptAndSetEnvironment('production')
|
||||
|
||||
console.log('Get collected results from the last hour...')
|
||||
|
||||
const hourAgo = new Date(Date.now() - 1000 * 60 * 60)
|
||||
|
||||
const results = await prisma.result.groupBy({
|
||||
by: ['typebotId'],
|
||||
_count: {
|
||||
_all: true,
|
||||
},
|
||||
where: {
|
||||
hasStarted: true,
|
||||
createdAt: {
|
||||
gte: hourAgo,
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
console.log(
|
||||
`Found ${results.reduce(
|
||||
(total, result) => total + result._count._all,
|
||||
0
|
||||
)} results collected for the last hour.`
|
||||
)
|
||||
|
||||
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,
|
||||
chatsLimitFirstEmailSentAt: true,
|
||||
chatsLimitSecondEmailSentAt: 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('Check limits...')
|
||||
|
||||
const events = await sendAlertIfLimitReached(
|
||||
resultsWithWorkspaces
|
||||
.filter((result) => result.isFirstOfKind)
|
||||
.map((result) => result.workspace)
|
||||
)
|
||||
|
||||
console.log(`Send ${events.length} auto quarantine events...`)
|
||||
|
||||
await sendTelemetryEvents(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 * LIMIT_EMAIL_TRIGGER_PERCENT &&
|
||||
totalChatsUsed < chatsLimit &&
|
||||
!workspace.chatsLimitFirstEmailSentAt
|
||||
) {
|
||||
const to = workspace.members
|
||||
.map((member) => member.user.email)
|
||||
.filter(isDefined)
|
||||
console.log(
|
||||
`Send almost reached chats limit email to ${to.join(', ')}...`
|
||||
)
|
||||
await sendAlmostReachedChatsLimitEmail({
|
||||
to: workspace.members
|
||||
.map((member) => member.user.email)
|
||||
.filter(isDefined),
|
||||
usagePercent: Math.round((totalChatsUsed / chatsLimit) * 100),
|
||||
chatsLimit,
|
||||
url: `https://app.typebot.io/typebots?workspaceId=${workspace.id}`,
|
||||
})
|
||||
await prisma.workspace.update({
|
||||
where: { id: workspace.id },
|
||||
data: { chatsLimitFirstEmailSentAt: new Date() },
|
||||
})
|
||||
}
|
||||
|
||||
if (
|
||||
chatsLimit > 0 &&
|
||||
totalChatsUsed >= chatsLimit &&
|
||||
!workspace.chatsLimitSecondEmailSentAt
|
||||
) {
|
||||
const to = workspace.members
|
||||
.map((member) => member.user.email)
|
||||
.filter(isDefined)
|
||||
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.update({
|
||||
where: { id: workspace.id },
|
||||
data: { chatsLimitSecondEmailSentAt: new Date() },
|
||||
})
|
||||
}
|
||||
|
||||
if (totalChatsUsed > chatsLimit * 3 && workspace.plan === Plan.FREE) {
|
||||
console.log(`Automatically quarantine workspace ${workspace.id}...`)
|
||||
await prisma.workspace.update({
|
||||
where: { id: workspace.id },
|
||||
data: { isQuarantined: true },
|
||||
})
|
||||
events.push(
|
||||
...workspace.members
|
||||
.filter((member) => member.role === WorkspaceRole.ADMIN)
|
||||
.map(
|
||||
(member) =>
|
||||
({
|
||||
name: 'Workspace automatically quarantined',
|
||||
userId: member.user.id,
|
||||
workspaceId: workspace.id,
|
||||
data: {
|
||||
totalChatsUsed,
|
||||
chatsLimit,
|
||||
},
|
||||
} satisfies TelemetryEvent)
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
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,
|
||||
{
|
||||
_sum: { storageUsed: totalStorageUsed },
|
||||
},
|
||||
] = await Promise.all([
|
||||
prisma.result.count({
|
||||
where: {
|
||||
typebotId: { in: typebots.map((typebot) => typebot.id) },
|
||||
hasStarted: true,
|
||||
createdAt: {
|
||||
gte: firstDayOfMonth,
|
||||
lt: firstDayOfNextMonth,
|
||||
},
|
||||
},
|
||||
}),
|
||||
prisma.answer.aggregate({
|
||||
where: {
|
||||
storageUsed: { gt: 0 },
|
||||
result: {
|
||||
typebotId: { in: typebots.map((typebot) => typebot.id) },
|
||||
},
|
||||
},
|
||||
_sum: { storageUsed: true },
|
||||
}),
|
||||
])
|
||||
|
||||
return {
|
||||
totalChatsUsed,
|
||||
totalStorageUsed: totalStorageUsed ?? 0,
|
||||
}
|
||||
}
|
||||
|
||||
sendTotalResultsDigest().then()
|
||||
@@ -11,7 +11,6 @@ import { sendTelemetryEvents } from '@typebot.io/lib/telemetry/sendTelemetryEven
|
||||
import { Workspace } from '@typebot.io/schemas'
|
||||
|
||||
const prisma = new PrismaClient()
|
||||
const LIMIT_EMAIL_TRIGGER_PERCENT = 0.8
|
||||
|
||||
type WorkspaceForDigest = Pick<
|
||||
Workspace,
|
||||
@@ -170,18 +169,6 @@ const sendAlertIfLimitReached = async (
|
||||
)
|
||||
continue
|
||||
}
|
||||
// if (
|
||||
// chatsLimit > 0 &&
|
||||
// totalChatsUsed >= chatsLimit * LIMIT_EMAIL_TRIGGER_PERCENT
|
||||
// )
|
||||
// await sendAlmostReachedChatsLimitEmail({
|
||||
// to: workspace.members
|
||||
// .map((member) => member.user.email)
|
||||
// .filter(isDefined),
|
||||
// usagePercent: Math.round((totalChatsUsed / chatsLimit) * 100),
|
||||
// chatsLimit,
|
||||
// url: `https://app.typebot.io/typebots?workspaceId=${workspace.id}`,
|
||||
// })
|
||||
}
|
||||
return events
|
||||
}
|
||||
|
||||
@@ -1,86 +0,0 @@
|
||||
import { Plan, PrismaClient } from '@typebot.io/prisma'
|
||||
import Stripe from 'stripe'
|
||||
import { promptAndSetEnvironment } from './utils'
|
||||
|
||||
const setCustomPlan = async () => {
|
||||
await promptAndSetEnvironment()
|
||||
const prisma = new PrismaClient()
|
||||
if (
|
||||
!process.env.STRIPE_SECRET_KEY ||
|
||||
!process.env.STRIPE_PRODUCT_ID ||
|
||||
!process.env.STRIPE_SUBSCRIPTION_ID ||
|
||||
!process.env.WORKSPACE_ID
|
||||
)
|
||||
throw Error(
|
||||
'STRIPE_SECRET_KEY or STRIPE_SUBSCRIPTION_ID or STRIPE_PRODUCT_ID or process.env.WORKSPACE_ID var is missing'
|
||||
)
|
||||
const stripe = new Stripe(process.env.STRIPE_SECRET_KEY, {
|
||||
apiVersion: '2022-11-15',
|
||||
})
|
||||
|
||||
const claimablePlan = await prisma.claimableCustomPlan.findFirst({
|
||||
where: { workspaceId: process.env.WORKSPACE_ID, claimedAt: null },
|
||||
})
|
||||
|
||||
if (!claimablePlan) throw Error('No claimable plan found')
|
||||
|
||||
console.log('Claimable plan found')
|
||||
|
||||
const { items: existingItems } = await stripe.subscriptions.retrieve(
|
||||
process.env.STRIPE_SUBSCRIPTION_ID
|
||||
)
|
||||
if (existingItems.data.length === 0) return
|
||||
|
||||
const planItem = existingItems.data.find(
|
||||
(item) => item.plan.product === process.env.STRIPE_PRODUCT_ID
|
||||
)
|
||||
|
||||
if (!planItem) throw Error("Couldn't find plan item")
|
||||
|
||||
console.log('Updating subscription...')
|
||||
|
||||
await stripe.subscriptions.update(process.env.STRIPE_SUBSCRIPTION_ID, {
|
||||
items: [
|
||||
{
|
||||
id: planItem.id,
|
||||
price_data: {
|
||||
currency: 'usd',
|
||||
tax_behavior: 'exclusive',
|
||||
recurring: { interval: 'month' },
|
||||
product: process.env.STRIPE_PRODUCT_ID,
|
||||
unit_amount: claimablePlan.price * 100,
|
||||
},
|
||||
},
|
||||
...existingItems.data
|
||||
.filter((item) => item.plan.product !== process.env.STRIPE_PRODUCT_ID)
|
||||
.map((item) => ({ id: item.id, deleted: true })),
|
||||
],
|
||||
})
|
||||
|
||||
console.log('Subscription updated!')
|
||||
|
||||
console.log('Updating workspace...')
|
||||
|
||||
await prisma.workspace.update({
|
||||
where: { id: process.env.WORKSPACE_ID },
|
||||
data: {
|
||||
plan: Plan.CUSTOM,
|
||||
customChatsLimit: claimablePlan.chatsLimit,
|
||||
customSeatsLimit: claimablePlan.seatsLimit,
|
||||
customStorageLimit: claimablePlan.storageLimit,
|
||||
},
|
||||
})
|
||||
|
||||
console.log('Workspace updated!')
|
||||
|
||||
console.log('Updating claimable plan...')
|
||||
|
||||
await prisma.claimableCustomPlan.update({
|
||||
where: { id: claimablePlan.id },
|
||||
data: { claimedAt: new Date() },
|
||||
})
|
||||
|
||||
console.log('Claimable plan updated!')
|
||||
}
|
||||
|
||||
setCustomPlan()
|
||||
Reference in New Issue
Block a user