2
0

🪥 Consult submissions

This commit is contained in:
Baptiste Arnaud
2021-12-30 10:24:16 +01:00
parent f088f694b9
commit 1093453c07
25 changed files with 575 additions and 138 deletions

View File

@ -9,10 +9,10 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
if (!session?.user)
return res.status(401).json({ message: 'Not authenticated' })
const id = req.query.id.toString()
const typebotId = req.query.typebotId.toString()
if (req.method === 'GET') {
const typebot = await prisma.typebot.findUnique({
where: { id },
where: { id: typebotId },
include: {
publishedTypebot: true,
},
@ -23,14 +23,14 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
}
if (req.method === 'DELETE') {
const typebots = await prisma.typebot.delete({
where: { id },
where: { id: typebotId },
})
return res.send({ typebots })
}
if (req.method === 'PUT') {
const data = JSON.parse(req.body)
const typebots = await prisma.typebot.update({
where: { id },
where: { id: typebotId },
data,
})
return res.send({ typebots })
@ -38,7 +38,7 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
if (req.method === 'PATCH') {
const data = JSON.parse(req.body)
const typebots = await prisma.typebot.update({
where: { id },
where: { id: typebotId },
data,
})
return res.send({ typebots })

View File

@ -0,0 +1,39 @@
import { User } from 'db'
import prisma from 'libs/prisma'
import { NextApiRequest, NextApiResponse } from 'next'
import { getSession } from 'next-auth/react'
import { methodNotAllowed } from 'utils'
const handler = async (req: NextApiRequest, res: NextApiResponse) => {
const session = await getSession({ req })
if (!session?.user)
return res.status(401).send({ message: 'Not authenticated' })
const user = session.user as User
if (req.method === 'GET') {
const typebotId = req.query.typebotId.toString()
const lastResultId = req.query.lastResultId?.toString()
const results = await prisma.result.findMany({
take: 50,
skip: lastResultId ? 1 : 0,
cursor: lastResultId
? {
id: lastResultId,
}
: undefined,
where: {
typebotId,
typebot: { ownerId: user.id },
},
orderBy: {
createdAt: 'desc',
},
include: { answers: true },
})
return res.status(200).send({ results })
}
return methodNotAllowed(res)
}
export default handler

View File

@ -0,0 +1,27 @@
import { User } from 'db'
import prisma from 'libs/prisma'
import { NextApiRequest, NextApiResponse } from 'next'
import { getSession } from 'next-auth/react'
import { methodNotAllowed } from 'utils'
const handler = async (req: NextApiRequest, res: NextApiResponse) => {
const session = await getSession({ req })
if (!session?.user)
return res.status(401).send({ message: 'Not authenticated' })
const user = session.user as User
if (req.method === 'GET') {
const typebotId = req.query.typebotId.toString()
const totalResults = await prisma.result.count({
where: {
typebotId,
typebot: { ownerId: user.id },
},
})
return res.status(200).send({ totalResults })
}
return methodNotAllowed(res)
}
export default handler