feat: more webhook functionality

This commit is contained in:
Catalin Pit
2024-02-14 14:38:58 +02:00
parent 0209127136
commit 61958989b4
8 changed files with 287 additions and 14 deletions

View File

@ -2,11 +2,17 @@ import { TRPCError } from '@trpc/server';
import { createWebhook } from '@documenso/lib/server-only/webhooks/create-webhook';
import { deleteWebhookById } from '@documenso/lib/server-only/webhooks/delete-webhook-by-id';
import { editWebhook } from '@documenso/lib/server-only/webhooks/edit-webhook';
import { getWebhookById } from '@documenso/lib/server-only/webhooks/get-webhook-by-id';
import { getWebhooksByUserId } from '@documenso/lib/server-only/webhooks/get-webhooks-by-user-id';
import { authenticatedProcedure, router } from '../trpc';
import { ZCreateWebhookFormSchema } from './schema';
import { ZDeleteWebhookSchema } from './schema';
import {
ZCreateWebhookFormSchema,
ZDeleteWebhookMutationSchema,
ZEditWebhookMutationSchema,
ZGetWebhookByIdQuerySchema,
} from './schema';
export const webhookRouter = router({
getWebhooks: authenticatedProcedure.query(async ({ ctx }) => {
@ -19,6 +25,24 @@ export const webhookRouter = router({
});
}
}),
getWebhookById: authenticatedProcedure
.input(ZGetWebhookByIdQuerySchema)
.query(async ({ input, ctx }) => {
try {
const { id } = input;
return await getWebhookById({
id,
userId: ctx.user.id,
});
} catch (err) {
throw new TRPCError({
code: 'BAD_REQUEST',
message: 'We were unable to fetch your webhook. Please try again later.',
});
}
}),
createWebhook: authenticatedProcedure
.input(ZCreateWebhookFormSchema)
.mutation(async ({ input, ctx }) => {
@ -35,7 +59,7 @@ export const webhookRouter = router({
}
}),
deleteWebhook: authenticatedProcedure
.input(ZDeleteWebhookSchema)
.input(ZDeleteWebhookMutationSchema)
.mutation(async ({ input, ctx }) => {
try {
const { id } = input;
@ -51,4 +75,22 @@ export const webhookRouter = router({
});
}
}),
editWebhook: authenticatedProcedure
.input(ZEditWebhookMutationSchema)
.mutation(async ({ input, ctx }) => {
try {
const { id } = input;
return await editWebhook({
id,
data: 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

@ -11,10 +11,22 @@ export const ZCreateWebhookFormSchema = z.object({
enabled: z.boolean(),
});
export const ZDeleteWebhookSchema = z.object({
export const ZGetWebhookByIdQuerySchema = z.object({
id: z.number(),
});
export const ZEditWebhookMutationSchema = ZCreateWebhookFormSchema.extend({
id: z.number(),
});
export const ZDeleteWebhookMutationSchema = z.object({
id: z.number(),
});
export type TCreateWebhookFormSchema = z.infer<typeof ZCreateWebhookFormSchema>;
export type TDeleteWebhookSchema = z.infer<typeof ZDeleteWebhookSchema>;
export type TGetWebhookByIdQuerySchema = z.infer<typeof ZGetWebhookByIdQuerySchema>;
export type TDeleteWebhookMutationSchema = z.infer<typeof ZDeleteWebhookMutationSchema>;
export type TEditWebhookMutationSchema = z.infer<typeof ZEditWebhookMutationSchema>;