2
0

♻️ Introduce typebot v6 with events (#1013)

Closes #885
This commit is contained in:
Baptiste Arnaud
2023-11-08 15:34:16 +01:00
committed by GitHub
parent 68e4fc71fb
commit 35300eaf34
634 changed files with 58971 additions and 31449 deletions

View File

@@ -7,7 +7,7 @@ import {
WorkspaceRole,
} from '@typebot.io/prisma'
import { createId } from '@paralleldrive/cuid2'
import { Typebot, Webhook } from '@typebot.io/schemas'
import { Typebot, TypebotV6, Webhook } from '@typebot.io/schemas'
import { readFileSync } from 'fs'
import { proWorkspaceId, userId } from './databaseSetup'
import {
@@ -74,20 +74,24 @@ export const importTypebotInDatabase = async (
path: string,
updates?: Partial<Typebot>
) => {
const typebot: Typebot = {
...JSON.parse(readFileSync(path).toString()),
const typebotFile = JSON.parse(readFileSync(path).toString())
const typebot = {
events: null,
...typebotFile,
workspaceId: proWorkspaceId,
...updates,
version: '3',
}
await prisma.typebot.create({
data: parseCreateTypebot(typebot),
})
return prisma.publicTypebot.create({
data: parseTypebotToPublicTypebot(
updates?.id ? `${updates?.id}-public` : 'publicBot',
typebot
),
data: {
...parseTypebotToPublicTypebot(
updates?.id ? `${updates?.id}-public` : 'publicBot',
typebot
),
events: typebot.events === null ? Prisma.DbNull : typebot.events,
},
})
}
@@ -165,7 +169,7 @@ export const createWebhook = async (
})
}
export const createTypebots = async (partialTypebots: Partial<Typebot>[]) => {
export const createTypebots = async (partialTypebots: Partial<TypebotV6>[]) => {
const typebotsWithId = partialTypebots.map((typebot) => {
const typebotId = typebot.id ?? createId()
return {
@@ -178,9 +182,9 @@ export const createTypebots = async (partialTypebots: Partial<Typebot>[]) => {
data: typebotsWithId.map(parseTestTypebot).map(parseCreateTypebot),
})
return prisma.publicTypebot.createMany({
data: typebotsWithId.map((t) =>
parseTypebotToPublicTypebot(t.publicId, parseTestTypebot(t))
),
data: typebotsWithId.map((t) => ({
...parseTypebotToPublicTypebot(t.publicId, parseTestTypebot(t)),
})) as any,
})
}
@@ -193,7 +197,11 @@ export const updateTypebot = async (
})
return prisma.publicTypebot.updateMany({
where: { typebotId: partialTypebot.id },
data: partialTypebot,
data: {
...partialTypebot,
events:
partialTypebot.events === null ? Prisma.DbNull : partialTypebot.events,
},
})
}
@@ -213,6 +221,7 @@ export const parseCreateTypebot = (typebot: Typebot) => ({
typebot.resultsTablePreferences === null
? Prisma.DbNull
: typebot.resultsTablePreferences,
events: typebot.events === null ? Prisma.DbNull : typebot.events,
})
const parseUpdateTypebot = (typebot: Partial<Typebot>) => ({
@@ -221,4 +230,5 @@ const parseUpdateTypebot = (typebot: Partial<Typebot>) => ({
typebot.resultsTablePreferences === null
? Prisma.DbNull
: typebot.resultsTablePreferences,
events: typebot.events === null ? Prisma.DbNull : typebot.events,
})

View File

@@ -1,64 +1,78 @@
import { createId } from '@paralleldrive/cuid2'
import {
Block,
defaultChoiceInputOptions,
defaultSettings,
defaultTheme,
InputBlockType,
ItemType,
BlockV5,
BlockV6,
Group,
PublicTypebot,
Typebot,
TypebotV6,
} from '@typebot.io/schemas'
import { isDefined } from '../utils'
import { proWorkspaceId } from './databaseSetup'
import { InputBlockType } from '@typebot.io/schemas/features/blocks/inputs/constants'
import { EventType } from '@typebot.io/schemas/features/events/constants'
export const parseTestTypebot = (
partialTypebot: Partial<Typebot>
): Typebot => ({
id: createId(),
version: '3',
workspaceId: proWorkspaceId,
folderId: null,
name: 'My typebot',
theme: defaultTheme,
settings: defaultSettings({ isBrandingEnabled: true }),
publicId: null,
updatedAt: new Date(),
createdAt: new Date(),
customDomain: null,
icon: null,
selectedThemeTemplateId: null,
isArchived: false,
isClosed: false,
resultsTablePreferences: null,
whatsAppCredentialsId: null,
variables: [{ id: 'var1', name: 'var1' }],
...partialTypebot,
edges: [
{
id: 'edge1',
from: { groupId: 'group0', blockId: 'block0' },
to: { groupId: 'group1' },
},
],
groups: [
{
id: 'group0',
title: 'Group #0',
blocks: [
{
id: 'block0',
type: 'start',
groupId: 'group0',
label: 'Start',
outgoingEdgeId: 'edge1',
},
],
graphCoordinates: { x: 0, y: 0 },
},
...(partialTypebot.groups ?? []),
],
})
export const parseTestTypebot = (partialTypebot: Partial<Typebot>): Typebot => {
const version = partialTypebot.version ?? ('3' as any)
return {
id: createId(),
version,
workspaceId: proWorkspaceId,
folderId: null,
name: 'My typebot',
theme: {},
settings: {},
publicId: null,
updatedAt: new Date(),
createdAt: new Date(),
customDomain: null,
icon: null,
selectedThemeTemplateId: null,
isArchived: false,
isClosed: false,
resultsTablePreferences: null,
whatsAppCredentialsId: null,
events:
version === '6'
? [
{
id: 'group1',
type: EventType.START,
graphCoordinates: { x: 0, y: 0 },
outgoingEdgeId: 'edge1',
},
]
: null,
variables: [{ id: 'var1', name: 'var1' }],
...partialTypebot,
edges: [
{
id: 'edge1',
from: { blockId: 'block0' },
to: { groupId: 'group1' },
},
],
groups: (version === '6'
? partialTypebot.groups ?? []
: [
{
id: 'group0',
title: 'Group #0',
blocks: [
{
id: 'block0',
type: 'start',
label: 'Start',
outgoingEdgeId: 'edge1',
},
],
graphCoordinates: { x: 0, y: 0 },
},
...(partialTypebot.groups ?? []),
]) as any[],
}
}
export const parseTypebotToPublicTypebot = (
id: string,
@@ -72,6 +86,7 @@ export const parseTypebotToPublicTypebot = (
settings: typebot.settings,
variables: typebot.variables,
edges: typebot.edges,
events: typebot.events,
})
type Options = {
@@ -79,9 +94,9 @@ type Options = {
}
export const parseDefaultGroupWithBlock = (
block: Partial<Block>,
block: Partial<BlockV6>,
options?: Options
): Pick<Typebot, 'groups'> => ({
): Pick<TypebotV6, 'groups'> => ({
groups: [
{
graphCoordinates: { x: 200, y: 200 },
@@ -96,19 +111,17 @@ export const parseDefaultGroupWithBlock = (
{
id: 'item1',
blockId: 'block1',
type: ItemType.BUTTON,
content: 'Go',
},
],
options: defaultChoiceInputOptions,
options: {},
}
: undefined,
{
id: 'block2',
groupId: 'group1',
...block,
} as Block,
].filter(isDefined) as Block[],
} as BlockV5,
].filter(isDefined) as BlockV6[],
title: 'Group #1',
},
],