mirror of
https://github.com/documenso/documenso.git
synced 2025-11-15 17:21:41 +10:00
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:
@ -0,0 +1,45 @@
|
||||
/*
|
||||
Warnings:
|
||||
|
||||
- Added the required column `source` to the `Document` table without a default value. This is not possible if the table is not empty.
|
||||
|
||||
*/
|
||||
-- CreateEnum
|
||||
CREATE TYPE "DocumentSource" AS ENUM ('DOCUMENT', 'TEMPLATE', 'TEMPLATE_DIRECT_LINK');
|
||||
|
||||
-- AlterTable
|
||||
ALTER TABLE "Document" ADD COLUMN "source" "DocumentSource",
|
||||
ADD COLUMN "templateId" INTEGER;
|
||||
|
||||
-- Custom: UpdateTable
|
||||
UPDATE "Document" SET "source" = 'DOCUMENT' WHERE "source" IS NULL;
|
||||
|
||||
-- Custom: AlterColumn
|
||||
ALTER TABLE "Document" ALTER COLUMN "source" SET NOT NULL;
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE "TemplateDirectLink" (
|
||||
"id" TEXT NOT NULL,
|
||||
"templateId" INTEGER NOT NULL,
|
||||
"token" TEXT NOT NULL,
|
||||
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
"enabled" BOOLEAN NOT NULL,
|
||||
"directTemplateRecipientId" INTEGER NOT NULL,
|
||||
|
||||
CONSTRAINT "TemplateDirectLink_pkey" PRIMARY KEY ("id")
|
||||
);
|
||||
|
||||
-- CreateIndex
|
||||
CREATE UNIQUE INDEX "TemplateDirectLink_id_key" ON "TemplateDirectLink"("id");
|
||||
|
||||
-- CreateIndex
|
||||
CREATE UNIQUE INDEX "TemplateDirectLink_templateId_key" ON "TemplateDirectLink"("templateId");
|
||||
|
||||
-- CreateIndex
|
||||
CREATE UNIQUE INDEX "TemplateDirectLink_token_key" ON "TemplateDirectLink"("token");
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE "Document" ADD CONSTRAINT "Document_templateId_fkey" FOREIGN KEY ("templateId") REFERENCES "Template"("id") ON DELETE SET NULL ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE "TemplateDirectLink" ADD CONSTRAINT "TemplateDirectLink_templateId_fkey" FOREIGN KEY ("templateId") REFERENCES "Template"("id") ON DELETE CASCADE ON UPDATE CASCADE;
|
||||
@ -261,6 +261,12 @@ enum DocumentStatus {
|
||||
COMPLETED
|
||||
}
|
||||
|
||||
enum DocumentSource {
|
||||
DOCUMENT
|
||||
TEMPLATE
|
||||
TEMPLATE_DIRECT_LINK
|
||||
}
|
||||
|
||||
model Document {
|
||||
id Int @id @default(autoincrement())
|
||||
userId Int
|
||||
@ -281,6 +287,9 @@ model Document {
|
||||
deletedAt DateTime?
|
||||
teamId Int?
|
||||
team Team? @relation(fields: [teamId], references: [id])
|
||||
templateId Int?
|
||||
template Template? @relation(fields: [templateId], references: [id], onDelete: SetNull)
|
||||
source DocumentSource
|
||||
|
||||
auditLogs DocumentAuditLog[]
|
||||
|
||||
@ -572,15 +581,29 @@ model Template {
|
||||
createdAt DateTime @default(now())
|
||||
updatedAt DateTime @default(now()) @updatedAt
|
||||
|
||||
team Team? @relation(fields: [teamId], references: [id], onDelete: Cascade)
|
||||
templateDocumentData DocumentData @relation(fields: [templateDocumentDataId], references: [id], onDelete: Cascade)
|
||||
User User @relation(fields: [userId], references: [id], onDelete: Cascade)
|
||||
team Team? @relation(fields: [teamId], references: [id], onDelete: Cascade)
|
||||
templateDocumentData DocumentData @relation(fields: [templateDocumentDataId], references: [id], onDelete: Cascade)
|
||||
User User @relation(fields: [userId], references: [id], onDelete: Cascade)
|
||||
Recipient Recipient[]
|
||||
Field Field[]
|
||||
directLink TemplateDirectLink?
|
||||
documents Document[]
|
||||
|
||||
@@unique([templateDocumentDataId])
|
||||
}
|
||||
|
||||
model TemplateDirectLink {
|
||||
id String @id @unique @default(cuid())
|
||||
templateId Int @unique
|
||||
token String @unique
|
||||
createdAt DateTime @default(now())
|
||||
enabled Boolean
|
||||
|
||||
directTemplateRecipientId Int
|
||||
|
||||
template Template @relation(fields: [templateId], references: [id], onDelete: Cascade)
|
||||
}
|
||||
|
||||
model SiteSettings {
|
||||
id String @id
|
||||
enabled Boolean @default(false)
|
||||
|
||||
@ -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,
|
||||
|
||||
@ -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,
|
||||
|
||||
@ -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,
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
@ -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,
|
||||
|
||||
@ -3,6 +3,7 @@ import type {
|
||||
Field,
|
||||
Recipient,
|
||||
Template,
|
||||
TemplateDirectLink,
|
||||
TemplateMeta,
|
||||
} from '@documenso/prisma/client';
|
||||
|
||||
@ -12,6 +13,7 @@ export type TemplateWithData = Template & {
|
||||
};
|
||||
|
||||
export type TemplateWithDetails = Template & {
|
||||
directLink: TemplateDirectLink | null;
|
||||
templateDocumentData: DocumentData;
|
||||
templateMeta: TemplateMeta | null;
|
||||
Recipient: Recipient[];
|
||||
|
||||
Reference in New Issue
Block a user