mirror of
https://github.com/documenso/documenso.git
synced 2025-11-14 08:42:12 +10:00
feat: add document auth (#1029)
This commit is contained in:
@ -0,0 +1,5 @@
|
||||
-- AlterTable
|
||||
ALTER TABLE "Document" ADD COLUMN "authOptions" JSONB;
|
||||
|
||||
-- AlterTable
|
||||
ALTER TABLE "Recipient" ADD COLUMN "authOptions" JSONB;
|
||||
@ -255,6 +255,7 @@ model Document {
|
||||
id Int @id @default(autoincrement())
|
||||
userId Int
|
||||
User User @relation(fields: [userId], references: [id], onDelete: Cascade)
|
||||
authOptions Json?
|
||||
title String
|
||||
status DocumentStatus @default(DRAFT)
|
||||
Recipient Recipient[]
|
||||
@ -352,6 +353,7 @@ model Recipient {
|
||||
token String
|
||||
expired DateTime?
|
||||
signedAt DateTime?
|
||||
authOptions Json?
|
||||
role RecipientRole @default(SIGNER)
|
||||
readStatus ReadStatus @default(NOT_OPENED)
|
||||
signingStatus SigningStatus @default(NOT_SIGNED)
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
import type { User } from '@prisma/client';
|
||||
import type { Document, User } from '@prisma/client';
|
||||
import { nanoid } from 'nanoid';
|
||||
import fs from 'node:fs';
|
||||
import path from 'node:path';
|
||||
@ -33,19 +33,19 @@ export const seedDocuments = async (documents: DocumentToSeed[]) => {
|
||||
documents.map(async (document, i) =>
|
||||
match(document.type)
|
||||
.with(DocumentStatus.DRAFT, async () =>
|
||||
createDraftDocument(document.sender, document.recipients, {
|
||||
seedDraftDocument(document.sender, document.recipients, {
|
||||
key: i,
|
||||
createDocumentOptions: document.documentOptions,
|
||||
}),
|
||||
)
|
||||
.with(DocumentStatus.PENDING, async () =>
|
||||
createPendingDocument(document.sender, document.recipients, {
|
||||
seedPendingDocument(document.sender, document.recipients, {
|
||||
key: i,
|
||||
createDocumentOptions: document.documentOptions,
|
||||
}),
|
||||
)
|
||||
.with(DocumentStatus.COMPLETED, async () =>
|
||||
createCompletedDocument(document.sender, document.recipients, {
|
||||
seedCompletedDocument(document.sender, document.recipients, {
|
||||
key: i,
|
||||
createDocumentOptions: document.documentOptions,
|
||||
}),
|
||||
@ -55,7 +55,37 @@ export const seedDocuments = async (documents: DocumentToSeed[]) => {
|
||||
);
|
||||
};
|
||||
|
||||
const createDraftDocument = async (
|
||||
export const seedBlankDocument = async (owner: User, options: CreateDocumentOptions = {}) => {
|
||||
const { key, createDocumentOptions = {} } = options;
|
||||
|
||||
const documentData = await prisma.documentData.create({
|
||||
data: {
|
||||
type: DocumentDataType.BYTES_64,
|
||||
data: examplePdf,
|
||||
initialData: examplePdf,
|
||||
},
|
||||
});
|
||||
|
||||
return await prisma.document.create({
|
||||
data: {
|
||||
title: `[TEST] Document ${key} - Draft`,
|
||||
status: DocumentStatus.DRAFT,
|
||||
documentDataId: documentData.id,
|
||||
userId: owner.id,
|
||||
...createDocumentOptions,
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
export const unseedDocument = async (documentId: number) => {
|
||||
await prisma.document.delete({
|
||||
where: {
|
||||
id: documentId,
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
export const seedDraftDocument = async (
|
||||
sender: User,
|
||||
recipients: (User | string)[],
|
||||
options: CreateDocumentOptions = {},
|
||||
@ -114,6 +144,8 @@ const createDraftDocument = async (
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
return document;
|
||||
};
|
||||
|
||||
type CreateDocumentOptions = {
|
||||
@ -121,7 +153,7 @@ type CreateDocumentOptions = {
|
||||
createDocumentOptions?: Partial<Prisma.DocumentUncheckedCreateInput>;
|
||||
};
|
||||
|
||||
const createPendingDocument = async (
|
||||
export const seedPendingDocument = async (
|
||||
sender: User,
|
||||
recipients: (User | string)[],
|
||||
options: CreateDocumentOptions = {},
|
||||
@ -180,9 +212,145 @@ const createPendingDocument = async (
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
return document;
|
||||
};
|
||||
|
||||
const createCompletedDocument = async (
|
||||
export const seedPendingDocumentNoFields = async ({
|
||||
owner,
|
||||
recipients,
|
||||
updateDocumentOptions,
|
||||
}: {
|
||||
owner: User;
|
||||
recipients: (User | string)[];
|
||||
updateDocumentOptions?: Partial<Prisma.DocumentUncheckedUpdateInput>;
|
||||
}) => {
|
||||
const document: Document = await seedBlankDocument(owner);
|
||||
|
||||
for (const recipient of recipients) {
|
||||
const email = typeof recipient === 'string' ? recipient : recipient.email;
|
||||
const name = typeof recipient === 'string' ? recipient : recipient.name ?? '';
|
||||
|
||||
await prisma.recipient.create({
|
||||
data: {
|
||||
email,
|
||||
name,
|
||||
token: nanoid(),
|
||||
readStatus: ReadStatus.OPENED,
|
||||
sendStatus: SendStatus.SENT,
|
||||
signingStatus: SigningStatus.NOT_SIGNED,
|
||||
signedAt: new Date(),
|
||||
Document: {
|
||||
connect: {
|
||||
id: document.id,
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
const createdRecipients = await prisma.recipient.findMany({
|
||||
where: {
|
||||
documentId: document.id,
|
||||
},
|
||||
include: {
|
||||
Field: true,
|
||||
},
|
||||
});
|
||||
|
||||
const latestDocument = updateDocumentOptions
|
||||
? await prisma.document.update({
|
||||
where: {
|
||||
id: document.id,
|
||||
},
|
||||
data: updateDocumentOptions,
|
||||
})
|
||||
: document;
|
||||
|
||||
return {
|
||||
document: latestDocument,
|
||||
recipients: createdRecipients,
|
||||
};
|
||||
};
|
||||
|
||||
export const seedPendingDocumentWithFullFields = async ({
|
||||
owner,
|
||||
recipients,
|
||||
recipientsCreateOptions,
|
||||
updateDocumentOptions,
|
||||
fields = [FieldType.DATE, FieldType.EMAIL, FieldType.NAME, FieldType.SIGNATURE, FieldType.TEXT],
|
||||
}: {
|
||||
owner: User;
|
||||
recipients: (User | string)[];
|
||||
recipientsCreateOptions?: Partial<Prisma.RecipientCreateInput>[];
|
||||
updateDocumentOptions?: Partial<Prisma.DocumentUncheckedUpdateInput>;
|
||||
fields?: FieldType[];
|
||||
}) => {
|
||||
const document: Document = await seedBlankDocument(owner);
|
||||
|
||||
for (const [recipientIndex, recipient] of recipients.entries()) {
|
||||
const email = typeof recipient === 'string' ? recipient : recipient.email;
|
||||
const name = typeof recipient === 'string' ? recipient : recipient.name ?? '';
|
||||
|
||||
await prisma.recipient.create({
|
||||
data: {
|
||||
email,
|
||||
name,
|
||||
token: nanoid(),
|
||||
readStatus: ReadStatus.OPENED,
|
||||
sendStatus: SendStatus.SENT,
|
||||
signingStatus: SigningStatus.NOT_SIGNED,
|
||||
signedAt: new Date(),
|
||||
Document: {
|
||||
connect: {
|
||||
id: document.id,
|
||||
},
|
||||
},
|
||||
Field: {
|
||||
createMany: {
|
||||
data: fields.map((fieldType, fieldIndex) => ({
|
||||
page: 1,
|
||||
type: fieldType,
|
||||
inserted: false,
|
||||
customText: name,
|
||||
positionX: new Prisma.Decimal((recipientIndex + 1) * 5),
|
||||
positionY: new Prisma.Decimal((fieldIndex + 1) * 5),
|
||||
width: new Prisma.Decimal(5),
|
||||
height: new Prisma.Decimal(5),
|
||||
documentId: document.id,
|
||||
})),
|
||||
},
|
||||
},
|
||||
...(recipientsCreateOptions?.[recipientIndex] ?? {}),
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
const createdRecipients = await prisma.recipient.findMany({
|
||||
where: {
|
||||
documentId: document.id,
|
||||
},
|
||||
include: {
|
||||
Field: true,
|
||||
},
|
||||
});
|
||||
|
||||
const latestDocument = updateDocumentOptions
|
||||
? await prisma.document.update({
|
||||
where: {
|
||||
id: document.id,
|
||||
},
|
||||
data: updateDocumentOptions,
|
||||
})
|
||||
: document;
|
||||
|
||||
return {
|
||||
document: latestDocument,
|
||||
recipients: createdRecipients,
|
||||
};
|
||||
};
|
||||
|
||||
export const seedCompletedDocument = async (
|
||||
sender: User,
|
||||
recipients: (User | string)[],
|
||||
options: CreateDocumentOptions = {},
|
||||
@ -241,6 +409,8 @@ const createCompletedDocument = async (
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
return document;
|
||||
};
|
||||
|
||||
/**
|
||||
|
||||
@ -1,29 +0,0 @@
|
||||
import { hashSync } from '@documenso/lib/server-only/auth/hash';
|
||||
|
||||
import { prisma } from '..';
|
||||
|
||||
//
|
||||
// https://github.com/documenso/documenso/pull/713
|
||||
//
|
||||
|
||||
const PULL_REQUEST_NUMBER = 718;
|
||||
|
||||
const EMAIL_DOMAIN = `pr-${PULL_REQUEST_NUMBER}.documenso.com`;
|
||||
|
||||
export const TEST_USER = {
|
||||
name: 'User 1',
|
||||
email: `user1@${EMAIL_DOMAIN}`,
|
||||
password: 'Password123',
|
||||
} as const;
|
||||
|
||||
export const seedDatabase = async () => {
|
||||
await prisma.user.create({
|
||||
data: {
|
||||
name: TEST_USER.name,
|
||||
email: TEST_USER.email,
|
||||
password: hashSync(TEST_USER.password),
|
||||
emailVerified: new Date(),
|
||||
url: TEST_USER.email,
|
||||
},
|
||||
});
|
||||
};
|
||||
19
packages/prisma/seed/subscriptions.ts
Normal file
19
packages/prisma/seed/subscriptions.ts
Normal file
@ -0,0 +1,19 @@
|
||||
import { prisma } from '..';
|
||||
|
||||
export const seedTestEmail = () => `user-${Date.now()}@test.documenso.com`;
|
||||
|
||||
type SeedSubscriptionOptions = {
|
||||
userId: number;
|
||||
priceId: string;
|
||||
};
|
||||
|
||||
export const seedUserSubscription = async ({ userId, priceId }: SeedSubscriptionOptions) => {
|
||||
return await prisma.subscription.create({
|
||||
data: {
|
||||
userId,
|
||||
planId: Date.now().toString(),
|
||||
priceId,
|
||||
status: 'ACTIVE',
|
||||
},
|
||||
});
|
||||
};
|
||||
@ -2,6 +2,8 @@ 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;
|
||||
|
||||
Reference in New Issue
Block a user