2022-02-20 10:45:55 +01:00
|
|
|
import {
|
2022-04-12 10:00:54 -05:00
|
|
|
CredentialsType,
|
2022-02-20 10:45:55 +01:00
|
|
|
defaultSettings,
|
|
|
|
defaultTheme,
|
|
|
|
PublicTypebot,
|
2022-04-12 10:00:54 -05:00
|
|
|
SmtpCredentialsData,
|
2022-06-11 07:27:38 +02:00
|
|
|
Block,
|
2022-02-20 10:45:55 +01:00
|
|
|
Typebot,
|
2022-04-19 14:10:22 -07:00
|
|
|
Webhook,
|
2022-02-20 10:45:55 +01:00
|
|
|
} from 'models'
|
2022-09-17 16:37:33 +02:00
|
|
|
import { GraphNavigation, Plan, PrismaClient, WorkspaceRole } from 'db'
|
2022-02-20 10:45:55 +01:00
|
|
|
import { readFileSync } from 'fs'
|
2022-09-20 19:15:47 +02:00
|
|
|
import { injectFakeResults } from 'utils'
|
2022-09-18 09:46:42 +02:00
|
|
|
import { encrypt } from 'utils/api'
|
2022-02-20 10:45:55 +01:00
|
|
|
|
|
|
|
const prisma = new PrismaClient()
|
|
|
|
|
2022-09-17 16:37:33 +02:00
|
|
|
const userId = 'userId'
|
|
|
|
export const freeWorkspaceId = 'freeWorkspace'
|
|
|
|
export const starterWorkspaceId = 'starterWorkspace'
|
2022-09-24 08:58:23 +02:00
|
|
|
export const limitTestWorkspaceId = 'limitTestWorkspace'
|
|
|
|
export const apiToken = 'jirowjgrwGREHE'
|
2022-05-13 15:22:44 -07:00
|
|
|
|
2022-03-01 07:13:09 +01:00
|
|
|
export const teardownDatabase = async () => {
|
2022-09-17 16:37:33 +02:00
|
|
|
await prisma.workspace.deleteMany({
|
|
|
|
where: {
|
|
|
|
members: {
|
|
|
|
some: { userId },
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
await prisma.user.deleteMany({
|
|
|
|
where: { id: userId },
|
|
|
|
})
|
|
|
|
return prisma.webhook.deleteMany()
|
|
|
|
}
|
|
|
|
|
|
|
|
export const setupDatabase = async () => {
|
|
|
|
await createWorkspaces()
|
|
|
|
await createUser()
|
2022-02-20 10:45:55 +01:00
|
|
|
}
|
|
|
|
|
2022-09-17 16:37:33 +02:00
|
|
|
export const createWorkspaces = async () =>
|
|
|
|
prisma.workspace.createMany({
|
|
|
|
data: [
|
|
|
|
{
|
|
|
|
id: freeWorkspaceId,
|
|
|
|
name: 'Free workspace',
|
|
|
|
plan: Plan.FREE,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: starterWorkspaceId,
|
|
|
|
name: 'Starter workspace',
|
|
|
|
plan: Plan.STARTER,
|
|
|
|
},
|
2022-09-24 08:58:23 +02:00
|
|
|
{
|
|
|
|
id: limitTestWorkspaceId,
|
|
|
|
name: 'Limit test workspace',
|
|
|
|
plan: Plan.FREE,
|
|
|
|
},
|
2022-09-17 16:37:33 +02:00
|
|
|
],
|
|
|
|
})
|
2022-02-20 10:45:55 +01:00
|
|
|
|
2022-09-17 16:37:33 +02:00
|
|
|
export const createUser = async () => {
|
|
|
|
await prisma.user.create({
|
2022-02-21 11:46:56 +01:00
|
|
|
data: {
|
2022-09-17 16:37:33 +02:00
|
|
|
id: userId,
|
2022-02-21 11:46:56 +01:00
|
|
|
email: 'user@email.com',
|
2022-09-17 16:37:33 +02:00
|
|
|
name: 'John Doe',
|
|
|
|
graphNavigation: GraphNavigation.TRACKPAD,
|
|
|
|
apiTokens: {
|
|
|
|
createMany: {
|
|
|
|
data: [
|
|
|
|
{
|
2022-09-24 08:58:23 +02:00
|
|
|
name: 'Token',
|
|
|
|
token: apiToken,
|
2022-09-17 16:37:33 +02:00
|
|
|
createdAt: new Date(2022, 1, 1),
|
|
|
|
},
|
|
|
|
],
|
2022-05-13 15:22:44 -07:00
|
|
|
},
|
|
|
|
},
|
2022-02-21 11:46:56 +01:00
|
|
|
},
|
2022-02-20 10:45:55 +01:00
|
|
|
})
|
2022-09-17 16:37:33 +02:00
|
|
|
await prisma.memberInWorkspace.createMany({
|
|
|
|
data: [
|
|
|
|
{ role: WorkspaceRole.ADMIN, userId, workspaceId: freeWorkspaceId },
|
|
|
|
{ role: WorkspaceRole.ADMIN, userId, workspaceId: starterWorkspaceId },
|
2022-09-24 08:58:23 +02:00
|
|
|
{ role: WorkspaceRole.ADMIN, userId, workspaceId: limitTestWorkspaceId },
|
2022-09-17 16:37:33 +02:00
|
|
|
],
|
|
|
|
})
|
|
|
|
}
|
2022-02-20 10:45:55 +01:00
|
|
|
|
2022-04-19 14:10:22 -07:00
|
|
|
export const createWebhook = (typebotId: string, webhook?: Partial<Webhook>) =>
|
2022-03-01 07:13:09 +01:00
|
|
|
prisma.webhook.create({
|
|
|
|
data: {
|
|
|
|
id: 'webhook1',
|
2022-04-19 14:10:22 -07:00
|
|
|
typebotId,
|
2022-03-01 07:13:09 +01:00
|
|
|
method: 'GET',
|
2022-04-19 14:10:22 -07:00
|
|
|
...webhook,
|
2022-03-01 07:13:09 +01:00
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2022-02-20 10:45:55 +01:00
|
|
|
export const createTypebots = async (partialTypebots: Partial<Typebot>[]) => {
|
|
|
|
await prisma.typebot.createMany({
|
2022-09-17 16:37:33 +02:00
|
|
|
data: partialTypebots.map(parseTestTypebot),
|
2022-02-20 10:45:55 +01:00
|
|
|
})
|
|
|
|
return prisma.publicTypebot.createMany({
|
|
|
|
data: partialTypebots.map((t) =>
|
|
|
|
parseTypebotToPublicTypebot(t.id + '-published', parseTestTypebot(t))
|
2022-09-17 16:37:33 +02:00
|
|
|
),
|
2022-02-20 10:45:55 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-05-14 11:53:59 -07:00
|
|
|
export const updateTypebot = async (
|
|
|
|
partialTypebot: Partial<Typebot> & { id: string }
|
|
|
|
) => {
|
|
|
|
await prisma.typebot.updateMany({
|
|
|
|
where: { id: partialTypebot.id },
|
|
|
|
data: partialTypebot,
|
|
|
|
})
|
|
|
|
return prisma.publicTypebot.updateMany({
|
|
|
|
where: { typebotId: partialTypebot.id },
|
|
|
|
data: partialTypebot,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-02-20 10:45:55 +01:00
|
|
|
const parseTypebotToPublicTypebot = (
|
|
|
|
id: string,
|
|
|
|
typebot: Typebot
|
|
|
|
): PublicTypebot => ({
|
|
|
|
id,
|
2022-06-11 07:27:38 +02:00
|
|
|
groups: typebot.groups,
|
2022-02-20 10:45:55 +01:00
|
|
|
typebotId: typebot.id,
|
|
|
|
theme: typebot.theme,
|
|
|
|
settings: typebot.settings,
|
|
|
|
variables: typebot.variables,
|
|
|
|
edges: typebot.edges,
|
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-09-17 16:37:33 +02:00
|
|
|
workspaceId: freeWorkspaceId,
|
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',
|
2022-06-11 07:27:38 +02:00
|
|
|
from: { groupId: 'group0', blockId: 'block0' },
|
|
|
|
to: { groupId: 'group1' },
|
2022-02-20 10:45:55 +01:00
|
|
|
},
|
|
|
|
],
|
2022-06-11 07:27:38 +02:00
|
|
|
groups: [
|
2022-02-20 10:45:55 +01:00
|
|
|
{
|
2022-06-11 07:27:38 +02:00
|
|
|
id: 'group0',
|
|
|
|
title: 'Group #0',
|
|
|
|
blocks: [
|
2022-02-20 10:45:55 +01:00
|
|
|
{
|
2022-06-11 07:27:38 +02:00
|
|
|
id: 'block0',
|
2022-02-20 10:45:55 +01:00
|
|
|
type: 'start',
|
2022-06-11 07:27:38 +02:00
|
|
|
groupId: 'group0',
|
2022-02-20 10:45:55 +01:00
|
|
|
label: 'Start',
|
|
|
|
outgoingEdgeId: 'edge1',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
graphCoordinates: { x: 0, y: 0 },
|
|
|
|
},
|
2022-06-11 07:27:38 +02:00
|
|
|
...(partialTypebot.groups ?? []),
|
2022-02-20 10:45:55 +01:00
|
|
|
],
|
|
|
|
})
|
|
|
|
|
2022-06-11 07:27:38 +02:00
|
|
|
export const parseDefaultGroupWithBlock = (
|
|
|
|
block: Partial<Block>
|
|
|
|
): Pick<Typebot, 'groups'> => ({
|
|
|
|
groups: [
|
2022-02-20 10:45:55 +01:00
|
|
|
{
|
|
|
|
graphCoordinates: { x: 200, y: 200 },
|
2022-06-11 07:27:38 +02:00
|
|
|
id: 'group1',
|
|
|
|
blocks: [
|
2022-02-20 10:45:55 +01:00
|
|
|
{
|
2022-06-11 07:27:38 +02:00
|
|
|
id: 'block1',
|
|
|
|
groupId: 'group1',
|
|
|
|
...block,
|
|
|
|
} as Block,
|
2022-02-20 10:45:55 +01:00
|
|
|
],
|
2022-06-11 07:27:38 +02:00
|
|
|
title: 'Group #1',
|
2022-02-20 10:45:55 +01:00
|
|
|
},
|
|
|
|
],
|
|
|
|
})
|
|
|
|
|
|
|
|
export const importTypebotInDatabase = async (
|
|
|
|
path: string,
|
|
|
|
updates?: Partial<Typebot>
|
|
|
|
) => {
|
2022-05-14 16:52:05 -07:00
|
|
|
const typebot: Typebot = {
|
2022-02-20 10:45:55 +01:00
|
|
|
...JSON.parse(readFileSync(path).toString()),
|
2022-09-17 16:37:33 +02:00
|
|
|
workspaceId: starterWorkspaceId,
|
2022-09-24 08:58:23 +02:00
|
|
|
...updates,
|
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
|
|
|
|
2022-09-20 19:15:47 +02:00
|
|
|
export const createResults = injectFakeResults(prisma)
|
2022-04-12 10:00:54 -05:00
|
|
|
|
|
|
|
export const createSmtpCredentials = (
|
|
|
|
id: string,
|
|
|
|
smtpData: SmtpCredentialsData
|
|
|
|
) => {
|
|
|
|
const { encryptedData, iv } = encrypt(smtpData)
|
|
|
|
return prisma.credentials.create({
|
|
|
|
data: {
|
|
|
|
id,
|
|
|
|
data: encryptedData,
|
|
|
|
iv,
|
|
|
|
name: smtpData.from.email as string,
|
|
|
|
type: CredentialsType.SMTP,
|
2022-09-17 16:37:33 +02:00
|
|
|
workspaceId: freeWorkspaceId,
|
2022-04-12 10:00:54 -05:00
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|