Files
sign/packages/trpc/server/webhook-router/router.ts

97 lines
2.8 KiB
TypeScript
Raw Normal View History

2024-02-09 16:07:33 +02:00
import { TRPCError } from '@trpc/server';
import { createWebhook } from '@documenso/lib/server-only/webhooks/create-webhook';
2024-02-09 16:28:18 +02:00
import { deleteWebhookById } from '@documenso/lib/server-only/webhooks/delete-webhook-by-id';
2024-02-14 14:38:58 +02:00
import { editWebhook } from '@documenso/lib/server-only/webhooks/edit-webhook';
import { getWebhookById } from '@documenso/lib/server-only/webhooks/get-webhook-by-id';
2024-02-09 16:07:33 +02:00
import { getWebhooksByUserId } from '@documenso/lib/server-only/webhooks/get-webhooks-by-user-id';
import { authenticatedProcedure, router } from '../trpc';
2024-02-14 14:38:58 +02:00
import {
ZCreateWebhookFormSchema,
ZDeleteWebhookMutationSchema,
ZEditWebhookMutationSchema,
ZGetWebhookByIdQuerySchema,
} from './schema';
2024-02-09 16:07:33 +02:00
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.',
});
}
}),
2024-02-14 14:38:58 +02:00
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.',
});
}
}),
2024-02-09 16:07:33 +02:00
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.',
});
}
}),
2024-02-09 16:28:18 +02:00
deleteWebhook: authenticatedProcedure
2024-02-14 14:38:58 +02:00
.input(ZDeleteWebhookMutationSchema)
2024-02-09 16:28:18 +02:00
.mutation(async ({ input, ctx }) => {
try {
const { id } = input;
return await deleteWebhookById({
id,
userId: ctx.user.id,
});
} catch (err) {
throw new TRPCError({
code: 'BAD_REQUEST',
message: 'We were unable to create this webhook. Please try again later.',
});
}
}),
2024-02-14 14:38:58 +02:00
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.',
});
}
}),
2024-02-09 16:07:33 +02:00
});