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