2
0

feat(results): Add logs in results

This commit is contained in:
Baptiste Arnaud
2022-03-01 11:40:22 +01:00
parent 4630512b8b
commit ebf92b5536
27 changed files with 408 additions and 120 deletions

View File

@ -1,23 +0,0 @@
import { withSentry } from '@sentry/nextjs'
import prisma from 'libs/prisma'
import { VariableWithValue } from 'models'
import { NextApiRequest, NextApiResponse } from 'next'
import { methodNotAllowed } from 'utils'
const handler = async (req: NextApiRequest, res: NextApiResponse) => {
if (req.method === 'POST') {
const resultData = (
typeof req.body === 'string' ? JSON.parse(req.body) : req.body
) as {
typebotId: string
prefilledVariables: VariableWithValue[]
}
const result = await prisma.result.create({
data: { ...resultData, isCompleted: false },
})
return res.send(result)
}
return methodNotAllowed(res)
}
export default withSentry(handler)

View File

@ -1,5 +1,5 @@
import prisma from 'libs/prisma'
import { ResultWithAnswers, Typebot } from 'models'
import { ResultWithAnswers, Typebot, VariableWithValue } from 'models'
import { NextApiRequest, NextApiResponse } from 'next'
import { authenticateUser } from 'services/api/utils'
import { methodNotAllowed, parseAnswers } from 'utils'
@ -14,16 +14,30 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
})
if (!typebot) return res.status(400).send({ message: 'Typebot not found' })
const limit = Number(req.query.limit)
console.log(limit, typebot.id)
const results = (await prisma.result.findMany({
where: { typebotId: typebot.id },
orderBy: { createdAt: 'desc' },
take: limit,
include: { answers: true },
})) as unknown as ResultWithAnswers[]
res.send({
console.log(results)
return res.send({
results: results.map(parseAnswers(typebot as unknown as Typebot)),
})
}
if (req.method === 'POST') {
const typebotId = req.query.typebotId as string
const resultData = (
typeof req.body === 'string' ? JSON.parse(req.body) : req.body
) as {
prefilledVariables: VariableWithValue[]
}
const result = await prisma.result.create({
data: { ...resultData, typebotId, isCompleted: false },
})
return res.send(result)
}
methodNotAllowed(res)
}

View File

@ -8,9 +8,9 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
const data = (
typeof req.body === 'string' ? JSON.parse(req.body) : req.body
) as { isCompleted: true }
const id = req.query.id.toString()
const resultId = req.query.resultId as string
const result = await prisma.result.update({
where: { id },
where: { id: resultId },
data,
})
return res.send(result)

View File

@ -0,0 +1,20 @@
import { withSentry } from '@sentry/nextjs'
import { Log } from 'db'
import prisma from 'libs/prisma'
import { NextApiRequest, NextApiResponse } from 'next'
import { badRequest, methodNotAllowed } from 'utils'
const handler = async (req: NextApiRequest, res: NextApiResponse) => {
if (req.method === 'POST') {
const resultId = req.query.resultId as string
const log = req.body as
| Omit<Log, 'id' | 'createdAt' | 'resultId'>
| undefined
if (!log) return badRequest(res)
const createdLog = await prisma.log.create({ data: { ...log, resultId } })
return res.send(createdLog)
}
methodNotAllowed(res)
}
export default withSentry(handler)