2
0
Files
bot/apps/builder/pages/api/folders/[id].ts

45 lines
1.3 KiB
TypeScript
Raw Normal View History

2022-02-14 10:57:57 +01:00
import { withSentry } from '@sentry/nextjs'
import { DashboardFolder } from 'db'
2021-12-06 15:48:50 +01:00
import prisma from 'libs/prisma'
import { NextApiRequest, NextApiResponse } from 'next'
import { getAuthenticatedUser } from 'services/api/utils'
import { methodNotAllowed, notAuthenticated } from 'utils'
2021-12-06 15:48:50 +01:00
const handler = async (req: NextApiRequest, res: NextApiResponse) => {
const user = await getAuthenticatedUser(req)
if (!user) return notAuthenticated(res)
2021-12-06 15:48:50 +01:00
const id = req.query.id.toString()
if (req.method === 'GET') {
2022-05-13 15:22:44 -07:00
const folder = await prisma.dashboardFolder.findFirst({
where: {
id,
workspace: { members: { some: { userId: user.id } } },
},
2021-12-06 15:48:50 +01:00
})
return res.send({ folder })
}
if (req.method === 'DELETE') {
2022-05-13 15:22:44 -07:00
const folders = await prisma.dashboardFolder.deleteMany({
where: { id, workspace: { members: { some: { userId: user.id } } } },
2021-12-06 15:48:50 +01:00
})
return res.send({ folders })
}
if (req.method === 'PATCH') {
const data = (
typeof req.body === 'string' ? JSON.parse(req.body) : req.body
) as Partial<DashboardFolder>
2022-05-13 15:22:44 -07:00
const folders = await prisma.dashboardFolder.updateMany({
where: {
id,
workspace: { members: { some: { userId: user.id } } },
},
2021-12-06 15:48:50 +01:00
data,
})
return res.send({ typebots: folders })
}
return methodNotAllowed(res)
}
2022-02-14 10:57:57 +01:00
export default withSentry(handler)