🪥 Consult submissions
This commit is contained in:
@ -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 })
|
39
apps/builder/pages/api/typebots/[typebotId]/results.ts
Normal file
39
apps/builder/pages/api/typebots/[typebotId]/results.ts
Normal 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
|
27
apps/builder/pages/api/typebots/[typebotId]/results/count.ts
Normal file
27
apps/builder/pages/api/typebots/[typebotId]/results/count.ts
Normal 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
|
Reference in New Issue
Block a user