♻️ Normalize data
This commit is contained in:
@ -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)
|
||||
}
|
||||
|
@ -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 })
|
||||
}
|
||||
|
@ -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'
|
||||
|
Reference in New Issue
Block a user