Closes #1039 <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - **New Features** - Workspaces now include additional status indicator: `isPastDue`. - New pages for handling workspaces that are past due. Meaning, an invoice is unpaid. - **Bug Fixes** - Fixed issues with workspace status checks and redirections for suspended workspaces. - **Refactor** - Refactored workspace-related API functions to accommodate new status fields. - Improved permission checks for reading and writing typebots based on workspace status. - **Chores** - Database schema updated to include `isPastDue` field for workspaces. - Implemented new webhook event handling for subscription and invoice updates. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
135 lines
2.9 KiB
TypeScript
135 lines
2.9 KiB
TypeScript
import { Plan } from '@typebot.io/prisma'
|
|
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),
|
|
}),
|
|
})
|
|
)
|
|
|
|
const subscriptionAutoUpdatedEventSchema = workspaceEvent.merge(
|
|
z.object({
|
|
name: z.literal('Subscription automatically updated'),
|
|
data: z.object({
|
|
plan: z.nativeEnum(Plan),
|
|
}),
|
|
})
|
|
)
|
|
|
|
const newResultsCollectedEventSchema = typebotEvent.merge(
|
|
z.object({
|
|
name: z.literal('New results collected'),
|
|
data: z.object({
|
|
total: z.number(),
|
|
isFirstOfKind: z.literal(true).optional(),
|
|
}),
|
|
})
|
|
)
|
|
|
|
const workspaceLimitReachedEventSchema = workspaceEvent.merge(
|
|
z.object({
|
|
name: z.literal('Workspace limit reached'),
|
|
data: z.object({
|
|
chatsLimit: z.number(),
|
|
totalChatsUsed: z.number(),
|
|
}),
|
|
})
|
|
)
|
|
|
|
const workspaceAutoQuarantinedEventSchema = workspaceEvent.merge(
|
|
z.object({
|
|
name: z.literal('Workspace automatically quarantined'),
|
|
data: z.object({
|
|
chatsLimit: z.number(),
|
|
totalChatsUsed: z.number(),
|
|
}),
|
|
})
|
|
)
|
|
|
|
export const workspacePastDueEventSchema = workspaceEvent.merge(
|
|
z.object({
|
|
name: z.literal('Workspace past due'),
|
|
})
|
|
)
|
|
|
|
export const workspaceNotPastDueEventSchema = workspaceEvent.merge(
|
|
z.object({
|
|
name: z.literal('Workspace past due status removed'),
|
|
})
|
|
)
|
|
|
|
export const eventSchema = z.discriminatedUnion('name', [
|
|
workspaceCreatedEventSchema,
|
|
userCreatedEventSchema,
|
|
typebotCreatedEventSchema,
|
|
publishedTypebotEventSchema,
|
|
subscriptionUpdatedEventSchema,
|
|
newResultsCollectedEventSchema,
|
|
workspaceLimitReachedEventSchema,
|
|
workspaceAutoQuarantinedEventSchema,
|
|
subscriptionAutoUpdatedEventSchema,
|
|
workspacePastDueEventSchema,
|
|
workspaceNotPastDueEventSchema,
|
|
])
|
|
|
|
export type TelemetryEvent = z.infer<typeof eventSchema>
|