2
0

♻️ Normalize data

This commit is contained in:
Baptiste Arnaud
2022-01-06 09:40:56 +01:00
parent 6c1e0fd345
commit 9fa4c7dffa
114 changed files with 1545 additions and 1632 deletions

View File

@ -1,8 +1,8 @@
import { parseNewTypebot } from 'bot-engine'
import { User } from 'db'
import { Prisma, User } from 'db'
import prisma from 'libs/prisma'
import { NextApiRequest, NextApiResponse } from 'next'
import { getSession } from 'next-auth/react'
import { parseNewTypebot } from 'services/typebots'
import { methodNotAllowed } from 'utils'
const handler = async (req: NextApiRequest, res: NextApiResponse) => {
@ -26,7 +26,10 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
if (req.method === 'POST') {
const data = JSON.parse(req.body)
const typebot = await prisma.typebot.create({
data: parseNewTypebot({ ownerId: user.id, ...data }),
data: parseNewTypebot({
ownerId: user.id,
...data,
}) as Prisma.TypebotUncheckedCreateInput,
})
return res.send(typebot)
}

View File

@ -1,4 +1,4 @@
import { PublicTypebot } from 'bot-engine'
import { PublicTypebot } from 'models'
import { User } from 'db'
import prisma from 'libs/prisma'
import { NextApiRequest, NextApiResponse } from 'next'
@ -24,14 +24,14 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
const answersCounts: { blockId: string; totalAnswers: number }[] =
await Promise.all(
(typebot.publishedTypebot as PublicTypebot).blocks.map(
async (block) => {
const totalAnswers = await prisma.answer.count({
where: { blockId: block.id },
})
return { blockId: block.id, totalAnswers }
}
)
(
typebot.publishedTypebot as unknown as PublicTypebot
).blocks.allIds.map(async (blockId) => {
const totalAnswers = await prisma.answer.count({
where: { blockId },
})
return { blockId, totalAnswers }
})
)
return res.status(200).send({ answersCounts })
}

View File

@ -1,6 +1,6 @@
import { Stats } from 'bot-engine'
import { User } from 'db'
import prisma from 'libs/prisma'
import { Stats } from 'models'
import { NextApiRequest, NextApiResponse } from 'next'
import { getSession } from 'next-auth/react'
import { methodNotAllowed } from 'utils'