mirror of
https://github.com/documenso/documenso.git
synced 2025-11-13 00:03:33 +10:00
feat: added backend stuff
This commit is contained in:
@ -1,16 +1,26 @@
|
|||||||
'use client';
|
'use client';
|
||||||
|
|
||||||
import { useState } from 'react';
|
import { useState } from 'react';
|
||||||
|
|
||||||
import { useRouter } from 'next/navigation';
|
import { useRouter } from 'next/navigation';
|
||||||
|
|
||||||
import { zodResolver } from '@hookform/resolvers/zod';
|
import { zodResolver } from '@hookform/resolvers/zod';
|
||||||
import type * as DialogPrimitive from '@radix-ui/react-dialog';
|
import type * as DialogPrimitive from '@radix-ui/react-dialog';
|
||||||
import { useForm } from 'react-hook-form';
|
import { useForm } from 'react-hook-form';
|
||||||
|
import type { z } from 'zod';
|
||||||
|
|
||||||
|
import { trpc } from '@documenso/trpc/react';
|
||||||
|
import { ZCreateWebhookFormSchema } from '@documenso/trpc/server/webhook-router/schema';
|
||||||
import { Button } from '@documenso/ui/primitives/button';
|
import { Button } from '@documenso/ui/primitives/button';
|
||||||
import { Input } from '@documenso/ui/primitives/input';
|
import {
|
||||||
import { Switch } from '@documenso/ui/primitives/switch';
|
Dialog,
|
||||||
import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle, DialogTrigger, DialogFooter } from '@documenso/ui/primitives/dialog';
|
DialogContent,
|
||||||
|
DialogDescription,
|
||||||
|
DialogFooter,
|
||||||
|
DialogHeader,
|
||||||
|
DialogTitle,
|
||||||
|
DialogTrigger,
|
||||||
|
} from '@documenso/ui/primitives/dialog';
|
||||||
import {
|
import {
|
||||||
Form,
|
Form,
|
||||||
FormControl,
|
FormControl,
|
||||||
@ -19,9 +29,13 @@ import {
|
|||||||
FormLabel,
|
FormLabel,
|
||||||
FormMessage,
|
FormMessage,
|
||||||
} from '@documenso/ui/primitives/form/form';
|
} from '@documenso/ui/primitives/form/form';
|
||||||
|
import { Input } from '@documenso/ui/primitives/input';
|
||||||
|
import { Switch } from '@documenso/ui/primitives/switch';
|
||||||
|
import { useToast } from '@documenso/ui/primitives/use-toast';
|
||||||
|
|
||||||
import { MultiSelectCombobox } from './multiselect-combobox';
|
import { MultiSelectCombobox } from './multiselect-combobox';
|
||||||
|
|
||||||
|
type TCreateWebhookFormSchema = z.infer<typeof ZCreateWebhookFormSchema>;
|
||||||
|
|
||||||
export type CreateWebhookDialogProps = {
|
export type CreateWebhookDialogProps = {
|
||||||
trigger?: React.ReactNode;
|
trigger?: React.ReactNode;
|
||||||
@ -29,10 +43,11 @@ export type CreateWebhookDialogProps = {
|
|||||||
|
|
||||||
export const CreateWebhookDialog = ({ trigger, ...props }: CreateWebhookDialogProps) => {
|
export const CreateWebhookDialog = ({ trigger, ...props }: CreateWebhookDialogProps) => {
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
|
const { toast } = useToast();
|
||||||
const [open, setOpen] = useState(false);
|
const [open, setOpen] = useState(false);
|
||||||
|
|
||||||
const form = useForm<>({
|
const form = useForm<TCreateWebhookFormSchema>({
|
||||||
resolver: zodResolver(),
|
resolver: zodResolver(ZCreateWebhookFormSchema),
|
||||||
values: {
|
values: {
|
||||||
webhookUrl: '',
|
webhookUrl: '',
|
||||||
eventTriggers: [],
|
eventTriggers: [],
|
||||||
@ -41,9 +56,30 @@ export const CreateWebhookDialog = ({ trigger, ...props }: CreateWebhookDialogPr
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
const onSubmit = async () => {
|
const { mutateAsync: createWebhook } = trpc.webhook.createWebhook.useMutation();
|
||||||
console.log('submitted');
|
|
||||||
}
|
const onSubmit = async (values: TCreateWebhookFormSchema) => {
|
||||||
|
try {
|
||||||
|
await createWebhook(values);
|
||||||
|
|
||||||
|
setOpen(false);
|
||||||
|
|
||||||
|
toast({
|
||||||
|
title: 'Webhook created',
|
||||||
|
description: 'The webhook was successfully created.',
|
||||||
|
});
|
||||||
|
|
||||||
|
form.reset();
|
||||||
|
|
||||||
|
router.refresh();
|
||||||
|
} catch (err) {
|
||||||
|
toast({
|
||||||
|
title: 'Error',
|
||||||
|
description: 'An error occurred while creating the webhook. Please try again.',
|
||||||
|
variant: 'destructive',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Dialog
|
<Dialog
|
||||||
@ -92,7 +128,7 @@ export const CreateWebhookDialog = ({ trigger, ...props }: CreateWebhookDialogPr
|
|||||||
listValues={value}
|
listValues={value}
|
||||||
onChange={(values: string[]) => {
|
onChange={(values: string[]) => {
|
||||||
console.log(values);
|
console.log(values);
|
||||||
onChange(values)
|
onChange(values);
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
</FormControl>
|
</FormControl>
|
||||||
@ -101,28 +137,28 @@ export const CreateWebhookDialog = ({ trigger, ...props }: CreateWebhookDialogPr
|
|||||||
)}
|
)}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
<FormField
|
<FormField
|
||||||
control={form.control}
|
control={form.control}
|
||||||
name="secret"
|
name="secret"
|
||||||
render={({ field }) => (
|
render={({ field }) => (
|
||||||
<FormItem>
|
<FormItem>
|
||||||
<FormLabel>Secret</FormLabel>
|
<FormLabel>Secret</FormLabel>
|
||||||
<FormControl>
|
<FormControl>
|
||||||
<Input className="bg-background" {...field} />
|
<Input className="bg-background" {...field} value={field.value ?? ''} />
|
||||||
</FormControl>
|
</FormControl>
|
||||||
<FormMessage />
|
<FormMessage />
|
||||||
</FormItem>
|
</FormItem>
|
||||||
)}
|
)}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
<FormField
|
<FormField
|
||||||
control={form.control}
|
control={form.control}
|
||||||
name="enabled"
|
name="enabled"
|
||||||
render={({ field }) => (
|
render={({ field }) => (
|
||||||
<FormItem className="flex items-center gap-2">
|
<FormItem className="flex items-center gap-2">
|
||||||
<FormLabel className="mt-2">Active</FormLabel>
|
<FormLabel className="mt-2">Active</FormLabel>
|
||||||
<FormControl>
|
<FormControl>
|
||||||
<Switch
|
<Switch
|
||||||
className="bg-background"
|
className="bg-background"
|
||||||
checked={field.value}
|
checked={field.value}
|
||||||
onCheckedChange={field.onChange}
|
onCheckedChange={field.onChange}
|
||||||
@ -135,11 +171,7 @@ export const CreateWebhookDialog = ({ trigger, ...props }: CreateWebhookDialogPr
|
|||||||
|
|
||||||
<DialogFooter>
|
<DialogFooter>
|
||||||
<div className="flex w-full flex-nowrap gap-4">
|
<div className="flex w-full flex-nowrap gap-4">
|
||||||
<Button
|
<Button type="button" variant="secondary" onClick={() => setOpen(false)}>
|
||||||
type="button"
|
|
||||||
variant="secondary"
|
|
||||||
onClick={() => setOpen(false)}
|
|
||||||
>
|
|
||||||
Cancel
|
Cancel
|
||||||
</Button>
|
</Button>
|
||||||
<Button type="submit" loading={form.formState.isSubmitting}>
|
<Button type="submit" loading={form.formState.isSubmitting}>
|
||||||
@ -147,7 +179,6 @@ export const CreateWebhookDialog = ({ trigger, ...props }: CreateWebhookDialogPr
|
|||||||
</Button>
|
</Button>
|
||||||
</div>
|
</div>
|
||||||
</DialogFooter>
|
</DialogFooter>
|
||||||
|
|
||||||
</fieldset>
|
</fieldset>
|
||||||
</form>
|
</form>
|
||||||
</Form>
|
</Form>
|
||||||
|
|||||||
28
packages/lib/server-only/webhooks/create-webhook.ts
Normal file
28
packages/lib/server-only/webhooks/create-webhook.ts
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
import { prisma } from '@documenso/prisma';
|
||||||
|
import type { WebhookTriggerEvents } from '@documenso/prisma/client';
|
||||||
|
|
||||||
|
export interface CreateWebhookOptions {
|
||||||
|
webhookUrl: string;
|
||||||
|
eventTriggers: WebhookTriggerEvents[];
|
||||||
|
secret: string | null;
|
||||||
|
enabled: boolean;
|
||||||
|
userId: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
export const createWebhook = async ({
|
||||||
|
webhookUrl,
|
||||||
|
eventTriggers,
|
||||||
|
secret,
|
||||||
|
enabled,
|
||||||
|
userId,
|
||||||
|
}: CreateWebhookOptions) => {
|
||||||
|
return await prisma.webhook.create({
|
||||||
|
data: {
|
||||||
|
webhookUrl,
|
||||||
|
eventTriggers,
|
||||||
|
secret,
|
||||||
|
enabled,
|
||||||
|
userId,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
};
|
||||||
@ -0,0 +1,9 @@
|
|||||||
|
import { prisma } from '@documenso/prisma';
|
||||||
|
|
||||||
|
export const getWebhooksByUserId = async (userId: number) => {
|
||||||
|
return await prisma.webhook.findMany({
|
||||||
|
where: {
|
||||||
|
userId,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
};
|
||||||
@ -11,6 +11,7 @@ import { teamRouter } from './team-router/router';
|
|||||||
import { templateRouter } from './template-router/router';
|
import { templateRouter } from './template-router/router';
|
||||||
import { router } from './trpc';
|
import { router } from './trpc';
|
||||||
import { twoFactorAuthenticationRouter } from './two-factor-authentication-router/router';
|
import { twoFactorAuthenticationRouter } from './two-factor-authentication-router/router';
|
||||||
|
import { webhookRouter } from './webhook-router/router';
|
||||||
|
|
||||||
export const appRouter = router({
|
export const appRouter = router({
|
||||||
auth: authRouter,
|
auth: authRouter,
|
||||||
@ -24,6 +25,7 @@ export const appRouter = router({
|
|||||||
singleplayer: singleplayerRouter,
|
singleplayer: singleplayerRouter,
|
||||||
team: teamRouter,
|
team: teamRouter,
|
||||||
template: templateRouter,
|
template: templateRouter,
|
||||||
|
webhook: webhookRouter,
|
||||||
twoFactorAuthentication: twoFactorAuthenticationRouter,
|
twoFactorAuthentication: twoFactorAuthenticationRouter,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
35
packages/trpc/server/webhook-router/router.ts
Normal file
35
packages/trpc/server/webhook-router/router.ts
Normal 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.',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}),
|
||||||
|
});
|
||||||
14
packages/trpc/server/webhook-router/schema.ts
Normal file
14
packages/trpc/server/webhook-router/schema.ts
Normal 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>;
|
||||||
Reference in New Issue
Block a user