feat: add direct templates links (#1165)

## Description

Direct templates links is a feature that provides template owners the
ability to allow users to create documents based of their templates.

## General outline

This works by allowing the template owner to configure a "direct
recipient" in the template.

When a user opens the direct link to the template, it will create a flow
where they sign the fields configured by the template owner for the
direct recipient. After these fields are signed the following will
occur:

- A document will be created where the owner is the template owner
- The direct recipient fields will be signed
- The document will be sent to any other recipients configured in the
template
- If there are none the document will be immediately completed

## Notes

There's a custom prisma migration to migrate all documents to have
'DOCUMENT' as the source, then sets the column to required.

---------

Co-authored-by: Lucas Smith <me@lucasjamessmith.me>
This commit is contained in:
David Nguyen
2024-06-02 15:49:09 +10:00
committed by GitHub
parent c346a3fd6a
commit d11a68fc4c
71 changed files with 3636 additions and 283 deletions

View File

@ -7,6 +7,7 @@ import { match } from 'ts-pattern';
import { prisma } from '..';
import {
DocumentDataType,
DocumentSource,
DocumentStatus,
FieldType,
Prisma,
@ -68,6 +69,7 @@ export const seedBlankDocument = async (owner: User, options: CreateDocumentOpti
return await prisma.document.create({
data: {
source: DocumentSource.DOCUMENT,
title: `[TEST] Document ${key} - Draft`,
status: DocumentStatus.DRAFT,
documentDataId: documentData.id,
@ -102,6 +104,7 @@ export const seedDraftDocument = async (
const document = await prisma.document.create({
data: {
source: DocumentSource.DOCUMENT,
title: `[TEST] Document ${key} - Draft`,
status: DocumentStatus.DRAFT,
documentDataId: documentData.id,
@ -170,6 +173,7 @@ export const seedPendingDocument = async (
const document = await prisma.document.create({
data: {
source: DocumentSource.DOCUMENT,
title: `[TEST] Document ${key} - Pending`,
status: DocumentStatus.PENDING,
documentDataId: documentData.id,
@ -375,6 +379,7 @@ export const seedCompletedDocument = async (
const document = await prisma.document.create({
data: {
source: DocumentSource.DOCUMENT,
title: `[TEST] Document ${key} - Completed`,
status: DocumentStatus.COMPLETED,
documentDataId: documentData.id,

View File

@ -4,7 +4,7 @@ import path from 'node:path';
import { hashSync } from '@documenso/lib/server-only/auth/hash';
import { prisma } from '..';
import { DocumentDataType, Role } from '../client';
import { DocumentDataType, DocumentSource, Role } from '../client';
export const seedDatabase = async () => {
const examplePdf = fs
@ -54,6 +54,7 @@ export const seedDatabase = async () => {
await prisma.document.create({
data: {
source: DocumentSource.DOCUMENT,
title: 'Example Document',
documentDataId: examplePdfData.id,
userId: exampleUser.id,

View File

@ -1,6 +1,11 @@
import fs from 'node:fs';
import path from 'node:path';
import {
DIRECT_TEMPLATE_RECIPIENT_EMAIL,
DIRECT_TEMPLATE_RECIPIENT_NAME,
} from '@documenso/lib/constants/template';
import { prisma } from '..';
import type { Prisma, User } from '../client';
import { DocumentDataType, ReadStatus, RecipientRole, SendStatus, SigningStatus } from '../client';
@ -13,6 +18,7 @@ type SeedTemplateOptions = {
title?: string;
userId: number;
teamId?: number;
createTemplateOptions?: Partial<Prisma.TemplateCreateInput>;
};
type CreateTemplateOptions = {
@ -88,3 +94,81 @@ export const seedTemplate = async (options: SeedTemplateOptions) => {
},
});
};
export const seedDirectTemplate = async (options: SeedTemplateOptions) => {
const { title = 'Untitled', userId, teamId } = options;
const documentData = await prisma.documentData.create({
data: {
type: DocumentDataType.BYTES_64,
data: examplePdf,
initialData: examplePdf,
},
});
const template = await prisma.template.create({
data: {
title,
templateDocumentData: {
connect: {
id: documentData.id,
},
},
User: {
connect: {
id: userId,
},
},
Recipient: {
create: {
email: DIRECT_TEMPLATE_RECIPIENT_EMAIL,
name: DIRECT_TEMPLATE_RECIPIENT_NAME,
token: Math.random().toString().slice(2, 7),
},
},
...(teamId
? {
team: {
connect: {
id: teamId,
},
},
}
: {}),
...options.createTemplateOptions,
},
include: {
Recipient: true,
User: true,
},
});
const directTemplateRecpient = template.Recipient.find(
(recipient) => recipient.email === DIRECT_TEMPLATE_RECIPIENT_EMAIL,
);
if (!directTemplateRecpient) {
throw new Error('Need to create a direct template recipient');
}
await prisma.templateDirectLink.create({
data: {
templateId: template.id,
enabled: true,
token: Math.random().toString(),
directTemplateRecipientId: directTemplateRecpient.id,
},
});
return await prisma.template.findFirstOrThrow({
where: {
id: template.id,
},
include: {
directLink: true,
Field: true,
Recipient: true,
team: true,
},
});
};

View File

@ -4,8 +4,6 @@ import { hashSync } from '@documenso/lib/server-only/auth/hash';
import { prisma } from '..';
export const seedTestEmail = () => `user-${Date.now()}@test.documenso.com`;
type SeedUserOptions = {
name?: string;
email?: string;
@ -15,6 +13,8 @@ type SeedUserOptions = {
const nanoid = customAlphabet('1234567890abcdef', 10);
export const seedTestEmail = () => `${nanoid()}@test.documenso.com`;
export const seedUser = async ({
name,
email,