2
0
Files
bot/packages/models/features/telemetry.ts
Baptiste Arnaud 9ca17e4e0b 📈 Add telemetry webhook
Closes #357
2023-03-14 14:20:21 +01:00

90 lines
1.8 KiB
TypeScript

import { Plan } from 'db'
import { z } from 'zod'
const userEvent = z.object({
userId: z.string(),
})
const workspaceEvent = userEvent.merge(
z.object({
workspaceId: z.string(),
})
)
const typebotEvent = workspaceEvent.merge(
z.object({
typebotId: z.string(),
})
)
const workspaceCreatedEventSchema = workspaceEvent.merge(
z.object({
name: z.literal('Workspace created'),
data: z.object({
name: z.string().optional(),
plan: z.nativeEnum(Plan),
}),
})
)
const userCreatedEventSchema = userEvent.merge(
z.object({
name: z.literal('User created'),
data: z.object({
email: z.string(),
name: z.string().optional(),
}),
})
)
const typebotCreatedEventSchema = typebotEvent.merge(
z.object({
name: z.literal('Typebot created'),
data: z.object({
name: z.string(),
template: z.string().optional(),
}),
})
)
const publishedTypebotEventSchema = typebotEvent.merge(
z.object({
name: z.literal('Typebot published'),
data: z.object({
name: z.string(),
isFirstPublish: z.literal(true).optional(),
}),
})
)
const subscriptionUpdatedEventSchema = workspaceEvent.merge(
z.object({
name: z.literal('Subscription updated'),
data: z.object({
plan: z.nativeEnum(Plan),
additionalChatsIndex: z.number(),
additionalStorageIndex: z.number(),
}),
})
)
const newResultsCollectedEventSchema = typebotEvent.merge(
z.object({
name: z.literal('New results collected'),
data: z.object({
total: z.number(),
}),
})
)
export const eventSchema = z.discriminatedUnion('name', [
workspaceCreatedEventSchema,
userCreatedEventSchema,
typebotCreatedEventSchema,
publishedTypebotEventSchema,
subscriptionUpdatedEventSchema,
newResultsCollectedEventSchema,
])
export type TelemetryEvent = z.infer<typeof eventSchema>