feat: added backend stuff

This commit is contained in:
Catalin Pit
2024-02-09 16:07:33 +02:00
parent b3514bd0c7
commit ddb9dd11d7
6 changed files with 138 additions and 19 deletions

View File

@ -11,6 +11,7 @@ import { teamRouter } from './team-router/router';
import { templateRouter } from './template-router/router';
import { router } from './trpc';
import { twoFactorAuthenticationRouter } from './two-factor-authentication-router/router';
import { webhookRouter } from './webhook-router/router';
export const appRouter = router({
auth: authRouter,
@ -24,6 +25,7 @@ export const appRouter = router({
singleplayer: singleplayerRouter,
team: teamRouter,
template: templateRouter,
webhook: webhookRouter,
twoFactorAuthentication: twoFactorAuthenticationRouter,
});

View File

@ -0,0 +1,35 @@
import { TRPCError } from '@trpc/server';
import { createWebhook } from '@documenso/lib/server-only/webhooks/create-webhook';
import { getWebhooksByUserId } from '@documenso/lib/server-only/webhooks/get-webhooks-by-user-id';
import { authenticatedProcedure, router } from '../trpc';
import { ZCreateWebhookFormSchema } from './schema';
export const webhookRouter = router({
getWebhooks: authenticatedProcedure.query(async ({ ctx }) => {
try {
return await getWebhooksByUserId(ctx.user.id);
} catch (err) {
throw new TRPCError({
code: 'BAD_REQUEST',
message: 'We were unable to fetch your webhooks. Please try again later.',
});
}
}),
createWebhook: authenticatedProcedure
.input(ZCreateWebhookFormSchema)
.mutation(async ({ input, ctx }) => {
try {
return await createWebhook({
...input,
userId: ctx.user.id,
});
} catch (err) {
throw new TRPCError({
code: 'BAD_REQUEST',
message: 'We were unable to create this webhook. Please try again later.',
});
}
}),
});

View File

@ -0,0 +1,14 @@
import { z } from 'zod';
import { WebhookTriggerEvents } from '@documenso/prisma/client';
export const ZCreateWebhookFormSchema = z.object({
webhookUrl: z.string().url(),
eventTriggers: z
.array(z.nativeEnum(WebhookTriggerEvents))
.min(1, { message: 'At least one event trigger is required' }),
secret: z.string().nullable(),
enabled: z.boolean(),
});
export type TCreateWebhookFormSchema = z.infer<typeof ZCreateWebhookFormSchema>;