2022-02-20 10:45:55 +01:00
|
|
|
import {
|
|
|
|
defaultSettings,
|
|
|
|
defaultTheme,
|
|
|
|
PublicTypebot,
|
|
|
|
Step,
|
|
|
|
Typebot,
|
|
|
|
} from 'models'
|
2022-02-21 11:46:56 +01:00
|
|
|
import { PrismaClient } from 'db'
|
2022-02-20 10:45:55 +01:00
|
|
|
import { readFileSync } from 'fs'
|
|
|
|
|
|
|
|
const prisma = new PrismaClient()
|
|
|
|
|
2022-03-01 07:13:09 +01:00
|
|
|
export const teardownDatabase = async () => {
|
2022-02-21 11:46:56 +01:00
|
|
|
try {
|
2022-03-01 07:13:09 +01:00
|
|
|
await prisma.user.delete({
|
2022-03-09 15:12:00 +01:00
|
|
|
where: { id: 'proUser' },
|
2022-02-21 11:46:56 +01:00
|
|
|
})
|
2022-03-09 15:12:00 +01:00
|
|
|
} catch (err) {
|
|
|
|
console.error(err)
|
|
|
|
}
|
2022-03-01 07:13:09 +01:00
|
|
|
return
|
2022-02-20 10:45:55 +01:00
|
|
|
}
|
|
|
|
|
2022-02-21 11:46:56 +01:00
|
|
|
export const setupDatabase = () => createUser()
|
2022-02-20 10:45:55 +01:00
|
|
|
|
2022-02-21 11:46:56 +01:00
|
|
|
export const createUser = () =>
|
|
|
|
prisma.user.create({
|
|
|
|
data: {
|
2022-03-09 15:12:00 +01:00
|
|
|
id: 'proUser',
|
2022-02-21 11:46:56 +01:00
|
|
|
email: 'user@email.com',
|
|
|
|
name: 'User',
|
|
|
|
apiToken: 'userToken',
|
|
|
|
},
|
2022-02-20 10:45:55 +01:00
|
|
|
})
|
|
|
|
|
2022-03-01 07:13:09 +01:00
|
|
|
export const createWebhook = (typebotId: string) =>
|
|
|
|
prisma.webhook.create({
|
|
|
|
data: {
|
|
|
|
id: 'webhook1',
|
|
|
|
typebotId: typebotId,
|
|
|
|
method: 'GET',
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2022-02-20 10:45:55 +01:00
|
|
|
export const createTypebots = async (partialTypebots: Partial<Typebot>[]) => {
|
|
|
|
await prisma.typebot.createMany({
|
|
|
|
data: partialTypebots.map(parseTestTypebot) as any[],
|
|
|
|
})
|
|
|
|
return prisma.publicTypebot.createMany({
|
|
|
|
data: partialTypebots.map((t) =>
|
|
|
|
parseTypebotToPublicTypebot(t.id + '-published', parseTestTypebot(t))
|
|
|
|
) as any[],
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
const parseTypebotToPublicTypebot = (
|
|
|
|
id: string,
|
|
|
|
typebot: Typebot
|
|
|
|
): PublicTypebot => ({
|
|
|
|
id,
|
|
|
|
name: typebot.name,
|
2022-03-01 07:13:09 +01:00
|
|
|
blocks: typebot.blocks,
|
2022-02-20 10:45:55 +01:00
|
|
|
typebotId: typebot.id,
|
|
|
|
theme: typebot.theme,
|
|
|
|
settings: typebot.settings,
|
|
|
|
publicId: typebot.publicId,
|
|
|
|
variables: typebot.variables,
|
|
|
|
edges: typebot.edges,
|
|
|
|
customDomain: null,
|
2022-03-01 07:13:09 +01:00
|
|
|
createdAt: typebot.createdAt,
|
|
|
|
updatedAt: typebot.updatedAt,
|
2022-02-20 10:45:55 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
const parseTestTypebot = (partialTypebot: Partial<Typebot>): Typebot => ({
|
|
|
|
id: partialTypebot.id ?? 'typebot',
|
|
|
|
folderId: null,
|
|
|
|
name: 'My typebot',
|
2022-03-09 15:12:00 +01:00
|
|
|
ownerId: 'proUser',
|
2022-04-01 16:28:09 +02:00
|
|
|
icon: null,
|
2022-02-20 10:45:55 +01:00
|
|
|
theme: defaultTheme,
|
|
|
|
settings: defaultSettings,
|
|
|
|
publicId: partialTypebot.id + '-public',
|
|
|
|
publishedTypebotId: null,
|
2022-03-09 15:12:00 +01:00
|
|
|
updatedAt: new Date().toISOString(),
|
|
|
|
createdAt: new Date().toISOString(),
|
2022-02-20 10:45:55 +01:00
|
|
|
customDomain: null,
|
|
|
|
variables: [{ id: 'var1', name: 'var1' }],
|
|
|
|
...partialTypebot,
|
|
|
|
edges: [
|
|
|
|
{
|
|
|
|
id: 'edge1',
|
|
|
|
from: { blockId: 'block0', stepId: 'step0' },
|
|
|
|
to: { blockId: 'block1' },
|
|
|
|
},
|
|
|
|
],
|
|
|
|
blocks: [
|
|
|
|
{
|
|
|
|
id: 'block0',
|
|
|
|
title: 'Block #0',
|
|
|
|
steps: [
|
|
|
|
{
|
|
|
|
id: 'step0',
|
|
|
|
type: 'start',
|
|
|
|
blockId: 'block0',
|
|
|
|
label: 'Start',
|
|
|
|
outgoingEdgeId: 'edge1',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
graphCoordinates: { x: 0, y: 0 },
|
|
|
|
},
|
|
|
|
...(partialTypebot.blocks ?? []),
|
|
|
|
],
|
|
|
|
})
|
|
|
|
|
|
|
|
export const parseDefaultBlockWithStep = (
|
|
|
|
step: Partial<Step>
|
|
|
|
): Pick<Typebot, 'blocks'> => ({
|
|
|
|
blocks: [
|
|
|
|
{
|
|
|
|
graphCoordinates: { x: 200, y: 200 },
|
|
|
|
id: 'block1',
|
|
|
|
steps: [
|
|
|
|
{
|
|
|
|
id: 'step1',
|
|
|
|
blockId: 'block1',
|
|
|
|
...step,
|
|
|
|
} as Step,
|
|
|
|
],
|
|
|
|
title: 'Block #1',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
})
|
|
|
|
|
|
|
|
export const importTypebotInDatabase = async (
|
|
|
|
path: string,
|
|
|
|
updates?: Partial<Typebot>
|
|
|
|
) => {
|
|
|
|
const typebot: any = {
|
|
|
|
...JSON.parse(readFileSync(path).toString()),
|
|
|
|
...updates,
|
2022-03-09 15:12:00 +01:00
|
|
|
ownerId: 'proUser',
|
2022-02-20 10:45:55 +01:00
|
|
|
}
|
|
|
|
await prisma.typebot.create({
|
|
|
|
data: typebot,
|
|
|
|
})
|
|
|
|
return prisma.publicTypebot.create({
|
|
|
|
data: parseTypebotToPublicTypebot(
|
|
|
|
updates?.id ? `${updates?.id}-public` : 'publicBot',
|
|
|
|
typebot
|
|
|
|
),
|
|
|
|
})
|
|
|
|
}
|
2022-02-21 15:51:40 +01:00
|
|
|
|
|
|
|
export const createResults = async ({ typebotId }: { typebotId: string }) => {
|
|
|
|
await prisma.result.deleteMany()
|
|
|
|
await prisma.result.createMany({
|
|
|
|
data: [
|
|
|
|
...Array.from(Array(200)).map((_, idx) => {
|
|
|
|
const today = new Date()
|
|
|
|
const rand = Math.random()
|
|
|
|
return {
|
|
|
|
id: `result${idx}`,
|
|
|
|
typebotId,
|
|
|
|
createdAt: new Date(
|
|
|
|
today.setTime(today.getTime() + 1000 * 60 * 60 * 24 * idx)
|
|
|
|
),
|
|
|
|
isCompleted: rand > 0.5,
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
],
|
|
|
|
})
|
|
|
|
return createAnswers()
|
|
|
|
}
|
|
|
|
|
|
|
|
const createAnswers = () => {
|
|
|
|
return prisma.answer.createMany({
|
|
|
|
data: [
|
|
|
|
...Array.from(Array(200)).map((_, idx) => ({
|
|
|
|
resultId: `result${idx}`,
|
|
|
|
content: `content${idx}`,
|
|
|
|
stepId: 'step1',
|
|
|
|
blockId: 'block1',
|
|
|
|
})),
|
|
|
|
],
|
|
|
|
})
|
|
|
|
}
|