mirror of
https://github.com/documenso/documenso.git
synced 2025-11-14 08:42:12 +10:00
fix: refactor
This commit is contained in:
@ -5,6 +5,7 @@ import { InfoIcon, Plus } from 'lucide-react';
|
||||
import { useFieldArray, useForm } from 'react-hook-form';
|
||||
import * as z from 'zod';
|
||||
|
||||
import { TEMPLATE_RECIPIENT_PLACEHOLDER_REGEX } from '@documenso/lib/constants/template';
|
||||
import { AppError } from '@documenso/lib/errors/app-error';
|
||||
import type { Recipient } from '@documenso/prisma/client';
|
||||
import { trpc } from '@documenso/trpc/react';
|
||||
@ -96,11 +97,23 @@ export function UseTemplateDialog({
|
||||
resolver: zodResolver(ZAddRecipientsForNewDocumentSchema),
|
||||
defaultValues: {
|
||||
sendDocument: false,
|
||||
recipients: recipients.map((recipient) => ({
|
||||
recipients: recipients.map((recipient) => {
|
||||
const isRecipientPlaceholder = recipient.email.match(TEMPLATE_RECIPIENT_PLACEHOLDER_REGEX);
|
||||
|
||||
if (isRecipientPlaceholder) {
|
||||
return {
|
||||
id: recipient.id,
|
||||
name: '',
|
||||
email: '',
|
||||
})),
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
id: recipient.id,
|
||||
name: recipient.name,
|
||||
email: recipient.email,
|
||||
};
|
||||
}),
|
||||
},
|
||||
});
|
||||
|
||||
@ -253,11 +266,7 @@ export function UseTemplateDialog({
|
||||
</DialogClose>
|
||||
|
||||
<Button type="submit" loading={form.formState.isSubmitting}>
|
||||
{recipients.length === 0
|
||||
? 'Create'
|
||||
: form.getValues('sendDocument')
|
||||
? 'Send'
|
||||
: 'Review'}
|
||||
{form.getValues('sendDocument') ? 'Create and send' : 'Create as draft'}
|
||||
</Button>
|
||||
</DialogFooter>
|
||||
</fieldset>
|
||||
|
||||
@ -196,7 +196,7 @@ test('[TEMPLATES]: use template', async ({ page }) => {
|
||||
await page.getByPlaceholder('Recipient 1').click();
|
||||
await page.getByPlaceholder('Recipient 1').fill('name');
|
||||
|
||||
await page.getByRole('button', { name: 'Review' }).click();
|
||||
await page.getByRole('button', { name: 'Create as draft' }).click();
|
||||
await page.waitForURL(/documents/);
|
||||
await page.getByRole('main').getByRole('link', { name: 'Documents' }).click();
|
||||
await page.waitForURL('/documents');
|
||||
@ -214,7 +214,7 @@ test('[TEMPLATES]: use template', async ({ page }) => {
|
||||
await page.getByPlaceholder('Recipient 1').click();
|
||||
await page.getByPlaceholder('Recipient 1').fill('name');
|
||||
|
||||
await page.getByRole('button', { name: 'Review' }).click();
|
||||
await page.getByRole('button', { name: 'Create as draft' }).click();
|
||||
await page.waitForURL(/\/t\/.+\/documents/);
|
||||
await page.getByRole('main').getByRole('link', { name: 'Documents' }).click();
|
||||
await page.waitForURL(`/t/${team.url}/documents`);
|
||||
|
||||
1
packages/lib/constants/template.ts
Normal file
1
packages/lib/constants/template.ts
Normal file
@ -0,0 +1 @@
|
||||
export const TEMPLATE_RECIPIENT_PLACEHOLDER_REGEX = /recipient\.\d+@documenso\.com/i;
|
||||
@ -1,6 +1,12 @@
|
||||
import { nanoid } from '@documenso/lib/universal/id';
|
||||
import { prisma } from '@documenso/prisma';
|
||||
import type { Recipient } from '@documenso/prisma/client';
|
||||
import type { Field } from '@documenso/prisma/client';
|
||||
import { type Recipient, WebhookTriggerEvents } from '@documenso/prisma/client';
|
||||
|
||||
import { DOCUMENT_AUDIT_LOG_TYPE } from '../../types/document-audit-logs';
|
||||
import type { RequestMetadata } from '../../universal/extract-request-metadata';
|
||||
import { createDocumentAuditLogData } from '../../utils/document-audit-logs';
|
||||
import { triggerWebhook } from '../webhooks/trigger/trigger-webhook';
|
||||
|
||||
type RecipientWithId = {
|
||||
id: number;
|
||||
@ -8,6 +14,11 @@ type RecipientWithId = {
|
||||
email: string;
|
||||
};
|
||||
|
||||
type FinalRecipient = Pick<Recipient, 'name' | 'email' | 'role'> & {
|
||||
templateRecipientId: number;
|
||||
fields: Field[];
|
||||
};
|
||||
|
||||
export type CreateDocumentFromTemplateOptions = {
|
||||
templateId: number;
|
||||
userId: number;
|
||||
@ -18,6 +29,7 @@ export type CreateDocumentFromTemplateOptions = {
|
||||
name?: string;
|
||||
email: string;
|
||||
}[];
|
||||
requestMetadata?: RequestMetadata;
|
||||
};
|
||||
|
||||
export const createDocumentFromTemplate = async ({
|
||||
@ -25,7 +37,14 @@ export const createDocumentFromTemplate = async ({
|
||||
userId,
|
||||
teamId,
|
||||
recipients,
|
||||
requestMetadata,
|
||||
}: CreateDocumentFromTemplateOptions) => {
|
||||
const user = await prisma.user.findFirstOrThrow({
|
||||
where: {
|
||||
id: userId,
|
||||
},
|
||||
});
|
||||
|
||||
const template = await prisma.template.findUnique({
|
||||
where: {
|
||||
id: templateId,
|
||||
@ -46,10 +65,9 @@ export const createDocumentFromTemplate = async ({
|
||||
}),
|
||||
},
|
||||
include: {
|
||||
Recipient: true,
|
||||
Field: {
|
||||
Recipient: {
|
||||
include: {
|
||||
Recipient: true,
|
||||
Field: true,
|
||||
},
|
||||
},
|
||||
templateDocumentData: true,
|
||||
@ -64,7 +82,7 @@ export const createDocumentFromTemplate = async ({
|
||||
throw new Error('Invalid number of recipients.');
|
||||
}
|
||||
|
||||
let finalRecipients: Pick<Recipient, 'name' | 'email' | 'role'>[] = [];
|
||||
let finalRecipients: FinalRecipient[] = [];
|
||||
|
||||
if (recipients.length > 0 && Object.prototype.hasOwnProperty.call(recipients[0], 'id')) {
|
||||
finalRecipients = template.Recipient.map((templateRecipient) => {
|
||||
@ -78,6 +96,8 @@ export const createDocumentFromTemplate = async ({
|
||||
}
|
||||
|
||||
return {
|
||||
templateRecipientId: templateRecipient.id,
|
||||
fields: templateRecipient.Field,
|
||||
name: foundRecipient.name ?? '',
|
||||
email: foundRecipient.email,
|
||||
role: templateRecipient.role,
|
||||
@ -87,6 +107,8 @@ export const createDocumentFromTemplate = async ({
|
||||
// Backwards compatible logic for /v1/ API where we use the index to associate
|
||||
// the provided recipient with the template recipient.
|
||||
finalRecipients = recipients.map((recipient, index) => ({
|
||||
templateRecipientId: template.Recipient[index].id,
|
||||
fields: template.Recipient[index].Field,
|
||||
name: recipient.name ?? '',
|
||||
email: recipient.email,
|
||||
role: template.Recipient[index].role,
|
||||
@ -109,7 +131,8 @@ export const createDocumentFromTemplate = async ({
|
||||
title: template.title,
|
||||
documentDataId: documentData.id,
|
||||
Recipient: {
|
||||
create: finalRecipients.map((recipient) => ({
|
||||
createMany: {
|
||||
data: finalRecipients.map((recipient) => ({
|
||||
email: recipient.email,
|
||||
name: recipient.name,
|
||||
role: recipient.role,
|
||||
@ -117,6 +140,7 @@ export const createDocumentFromTemplate = async ({
|
||||
})),
|
||||
},
|
||||
},
|
||||
},
|
||||
include: {
|
||||
Recipient: {
|
||||
orderBy: {
|
||||
@ -127,31 +151,54 @@ export const createDocumentFromTemplate = async ({
|
||||
},
|
||||
});
|
||||
|
||||
await tx.field.createMany({
|
||||
data: template.Field.map((field) => {
|
||||
const documentRecipient = document.Recipient.find(
|
||||
(recipient) => recipient.email === field.Recipient.email,
|
||||
);
|
||||
let fieldsToCreate: Omit<Field, 'id' | 'secondaryId' | 'templateId'>[] = [];
|
||||
|
||||
if (!documentRecipient) {
|
||||
Object.values(finalRecipients).forEach(({ email, fields }) => {
|
||||
const recipient = document.Recipient.find((recipient) => recipient.email === email);
|
||||
|
||||
if (!recipient) {
|
||||
throw new Error('Recipient not found.');
|
||||
}
|
||||
|
||||
return {
|
||||
fieldsToCreate = fieldsToCreate.concat(
|
||||
fields.map((field) => ({
|
||||
documentId: document.id,
|
||||
recipientId: recipient.id,
|
||||
type: field.type,
|
||||
page: field.page,
|
||||
positionX: field.positionX,
|
||||
positionY: field.positionY,
|
||||
width: field.width,
|
||||
height: field.height,
|
||||
customText: field.customText,
|
||||
inserted: field.inserted,
|
||||
customText: '',
|
||||
inserted: false,
|
||||
})),
|
||||
);
|
||||
});
|
||||
|
||||
await tx.field.createMany({
|
||||
data: fieldsToCreate,
|
||||
});
|
||||
|
||||
await tx.documentAuditLog.create({
|
||||
data: createDocumentAuditLogData({
|
||||
type: DOCUMENT_AUDIT_LOG_TYPE.DOCUMENT_CREATED,
|
||||
documentId: document.id,
|
||||
recipientId: documentRecipient.id,
|
||||
};
|
||||
user,
|
||||
requestMetadata,
|
||||
data: {
|
||||
title: document.title,
|
||||
},
|
||||
}),
|
||||
});
|
||||
|
||||
await triggerWebhook({
|
||||
event: WebhookTriggerEvents.DOCUMENT_CREATED,
|
||||
data: document,
|
||||
userId,
|
||||
teamId,
|
||||
});
|
||||
|
||||
return document;
|
||||
});
|
||||
};
|
||||
|
||||
@ -53,11 +53,14 @@ export const templateRouter = router({
|
||||
throw new Error('You have reached your document limit.');
|
||||
}
|
||||
|
||||
const requestMetadata = extractNextApiRequestMetadata(ctx.req);
|
||||
|
||||
let document: Document = await createDocumentFromTemplate({
|
||||
templateId,
|
||||
teamId,
|
||||
userId: ctx.user.id,
|
||||
recipients: input.recipients,
|
||||
requestMetadata,
|
||||
});
|
||||
|
||||
if (input.sendDocument) {
|
||||
@ -65,7 +68,7 @@ export const templateRouter = router({
|
||||
documentId: document.id,
|
||||
userId: ctx.user.id,
|
||||
teamId,
|
||||
requestMetadata: extractNextApiRequestMetadata(ctx.req),
|
||||
requestMetadata,
|
||||
}).catch((err) => {
|
||||
console.error(err);
|
||||
|
||||
|
||||
@ -103,6 +103,7 @@ export const AddTemplatePlaceholderRecipientsFormPartial = ({
|
||||
appendSigner({
|
||||
formId: nanoid(12),
|
||||
name: `Recipient ${placeholderRecipientCount}`,
|
||||
// Update TEMPLATE_RECIPIENT_PLACEHOLDER_REGEX if this is ever changed.
|
||||
email: `recipient.${placeholderRecipientCount}@documenso.com`,
|
||||
role: RecipientRole.SIGNER,
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user