2022-09-17 16:37:33 +02:00
|
|
|
import { PrismaClient } from 'db'
|
2022-09-24 08:58:23 +02:00
|
|
|
import cuid from 'cuid'
|
2022-09-17 16:37:33 +02:00
|
|
|
|
|
|
|
type CreateFakeResultsProps = {
|
|
|
|
typebotId: string
|
|
|
|
count: number
|
2022-09-24 08:58:23 +02:00
|
|
|
customResultIdPrefix?: string
|
2022-09-17 16:37:33 +02:00
|
|
|
isChronological?: boolean
|
|
|
|
fakeStorage?: number
|
|
|
|
}
|
|
|
|
|
2022-09-20 19:15:47 +02:00
|
|
|
export const injectFakeResults =
|
2022-09-17 16:37:33 +02:00
|
|
|
(prisma: PrismaClient) =>
|
|
|
|
async ({
|
|
|
|
count,
|
2022-09-24 08:58:23 +02:00
|
|
|
customResultIdPrefix,
|
2022-09-17 16:37:33 +02:00
|
|
|
typebotId,
|
2022-09-24 08:58:23 +02:00
|
|
|
isChronological,
|
2022-09-17 16:37:33 +02:00
|
|
|
fakeStorage,
|
|
|
|
}: CreateFakeResultsProps) => {
|
2022-09-24 08:58:23 +02:00
|
|
|
const resultIdPrefix = customResultIdPrefix ?? cuid()
|
2022-09-17 16:37:33 +02:00
|
|
|
await prisma.result.createMany({
|
|
|
|
data: [
|
|
|
|
...Array.from(Array(count)).map((_, idx) => {
|
|
|
|
const today = new Date()
|
|
|
|
const rand = Math.random()
|
|
|
|
return {
|
2022-09-24 08:58:23 +02:00
|
|
|
id: `${resultIdPrefix}-result${idx}`,
|
2022-09-17 16:37:33 +02:00
|
|
|
typebotId,
|
|
|
|
createdAt: isChronological
|
|
|
|
? new Date(
|
|
|
|
today.setTime(today.getTime() + 1000 * 60 * 60 * 24 * idx)
|
|
|
|
)
|
|
|
|
: new Date(),
|
|
|
|
isCompleted: rand > 0.5,
|
|
|
|
hasStarted: true,
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
],
|
|
|
|
})
|
2022-09-24 08:58:23 +02:00
|
|
|
return createAnswers(prisma)({ fakeStorage, resultIdPrefix, count })
|
2022-09-17 16:37:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const createAnswers =
|
|
|
|
(prisma: PrismaClient) =>
|
|
|
|
({
|
|
|
|
count,
|
2022-09-24 08:58:23 +02:00
|
|
|
resultIdPrefix,
|
2022-09-17 16:37:33 +02:00
|
|
|
fakeStorage,
|
2022-09-24 08:58:23 +02:00
|
|
|
}: { resultIdPrefix: string } & Pick<
|
|
|
|
CreateFakeResultsProps,
|
|
|
|
'fakeStorage' | 'count'
|
|
|
|
>) => {
|
2022-09-17 16:37:33 +02:00
|
|
|
return prisma.answer.createMany({
|
|
|
|
data: [
|
|
|
|
...Array.from(Array(count)).map((_, idx) => ({
|
2022-09-24 08:58:23 +02:00
|
|
|
resultId: `${resultIdPrefix}-result${idx}`,
|
2022-09-17 16:37:33 +02:00
|
|
|
content: `content${idx}`,
|
|
|
|
blockId: 'block1',
|
|
|
|
groupId: 'block1',
|
|
|
|
storageUsed: fakeStorage ? Math.round(fakeStorage / count) : null,
|
|
|
|
})),
|
|
|
|
],
|
|
|
|
})
|
|
|
|
}
|