2
0

Add webhook blocks API public endpoints

This commit is contained in:
Baptiste Arnaud
2022-11-30 13:57:28 +01:00
parent f9ffdbc4c5
commit c799717905
67 changed files with 3030 additions and 429 deletions

View File

@ -1,10 +1,10 @@
import { createContext } from '@/utils/server/context'
import { appRouter } from '@/utils/server/routers/v1/_app'
import { trpcRouter } from '@/utils/server/routers/v1/trpcRouter'
import { captureException } from '@sentry/nextjs'
import { createNextApiHandler } from '@trpc/server/adapters/next'
export default createNextApiHandler({
router: appRouter,
router: trpcRouter,
createContext,
onError({ error }) {
if (error.code === 'INTERNAL_SERVER_ERROR') {

View File

@ -1,10 +1,10 @@
import { createContext } from '@/utils/server/context'
import { appRouter } from '@/utils/server/routers/v1/_app'
import { trpcRouter } from '@/utils/server/routers/v1/trpcRouter'
import { captureException } from '@sentry/nextjs'
import { createOpenApiNextHandler } from 'trpc-openapi'
export default createOpenApiNextHandler({
router: appRouter,
router: trpcRouter,
createContext,
onError({ error }) {
if (error.code === 'INTERNAL_SERVER_ERROR') {

View File

@ -1,36 +0,0 @@
import { withSentry } from '@sentry/nextjs'
import { Plan, Workspace } from 'db'
import prisma from '@/lib/prisma'
import { NextApiRequest, NextApiResponse } from 'next'
import { methodNotAllowed, notAuthenticated } from 'utils/api'
import { getAuthenticatedUser } from '@/features/auth/api'
const handler = async (req: NextApiRequest, res: NextApiResponse) => {
const user = await getAuthenticatedUser(req)
if (!user) return notAuthenticated(res)
if (req.method === 'GET') {
const workspaces = await prisma.workspace.findMany({
where: { members: { some: { userId: user.id } } },
include: { members: true },
orderBy: { createdAt: 'asc' },
})
return res.send({ workspaces })
}
if (req.method === 'POST') {
const data = req.body as Workspace
const workspace = await prisma.workspace.create({
data: {
...data,
members: { create: [{ role: 'ADMIN', userId: user.id }] },
plan:
process.env.ADMIN_EMAIL === user.email ? Plan.LIFETIME : Plan.FREE,
},
})
return res.status(200).json({
workspace,
})
}
methodNotAllowed(res)
}
export default withSentry(handler)

View File

@ -1,41 +0,0 @@
import { withSentry } from '@sentry/nextjs'
import { Prisma, Workspace, WorkspaceRole } from 'db'
import prisma from '@/lib/prisma'
import { NextApiRequest, NextApiResponse } from 'next'
import { getAuthenticatedUser } from '@/features/auth/api'
import { methodNotAllowed, notAuthenticated } from 'utils/api'
const handler = async (req: NextApiRequest, res: NextApiResponse) => {
const user = await getAuthenticatedUser(req)
if (!user) return notAuthenticated(res)
if (req.method === 'PATCH') {
const id = req.query.workspaceId as string
const updates = req.body as Partial<Workspace>
const updatedWorkspace = await prisma.workspace.updateMany({
where: {
id,
members: { some: { userId: user.id, role: WorkspaceRole.ADMIN } },
},
data: updates,
})
return res.status(200).json({
workspace: updatedWorkspace,
})
}
if (req.method === 'DELETE') {
const id = req.query.workspaceId as string
const workspaceFilter: Prisma.WorkspaceWhereInput = {
id,
members: { some: { userId: user.id, role: WorkspaceRole.ADMIN } },
}
await prisma.workspace.deleteMany({
where: workspaceFilter,
})
return res.status(200).json({
message: 'success',
})
}
methodNotAllowed(res)
}
export default withSentry(handler)

View File

@ -11,7 +11,7 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
const workspaceId = req.query.workspaceId as string
const now = new Date()
const firstDayOfMonth = new Date(now.getFullYear(), now.getMonth(), 1)
const lastDayOfMonth = new Date(now.getFullYear(), now.getMonth() + 1, 0)
const lastDayOfMonth = new Date(now.getFullYear(), now.getMonth() + 1, 1)
const totalChatsUsed = await prisma.result.count({
where: {
typebot: {