Skip to content

The Open Source DocuSign Alternative.

Merge branch 'main' into feat/public-api

Lucas Smith2a74ce06efb3
diff --cc packages/lib/server-only/document/delete-document.ts
index 176f14bac,5974be3f0..b0b1ad682
--- a/packages/lib/server-only/document/delete-document.ts
+++ b/packages/lib/server-only/document/delete-document.ts
@@@ -14,73 -17,109 +17,97 @@@ import { createDocumentAuditLogData } f
export type DeleteDocumentOptions = {
id: number;
userId: number;
- status: DocumentStatus;
+ teamId?: number;
+ requestMetadata?: RequestMetadata;
};
- export const deleteDocument = async ({ id, userId, teamId }: DeleteDocumentOptions) => {
- // if the document is a draft, hard-delete
+ export const deleteDocument = async ({
+ id,
+ userId,
- status,
++ teamId,
+ requestMetadata,
+ }: DeleteDocumentOptions) => {
- await prisma.document.findFirstOrThrow({
+ const document = await prisma.document.findUnique({
where: {
id,
- OR: [
- {
- userId,
- },
- {
- team: {
- members: {
- some: {
- userId,
+ ...(teamId
+ ? {
+ team: {
+ id: teamId,
+ members: {
+ some: {
+ userId,
+ },
},
},
- },
- },
- ],
+ }
+ : {
+ userId,
+ teamId: null,
+ }),
},
- });
-
- const user = await prisma.user.findFirstOrThrow({
- where: {
- id: userId,
+ include: {
+ Recipient: true,
+ documentMeta: true,
+ User: true,
},
});
+ if (!document) {
+ throw new Error('Document not found');
+ }
+
+ const { status, User: user } = document;
+
+ // if the document is a draft, hard-delete
+ if (status === DocumentStatus.DRAFT) {
+ return await prisma.$transaction(async (tx) => {
+ // Currently redundant since deleting a document will delete the audit logs.
+ // However may be useful if we disassociate audit lgos and documents if required.
+ await tx.documentAuditLog.create({
+ data: createDocumentAuditLogData({
+ documentId: id,
+ type: DOCUMENT_AUDIT_LOG_TYPE.DOCUMENT_DELETED,
+ user,
+ requestMetadata,
+ data: {
+ type: 'HARD',
+ },
+ }),
+ });
+
+ return await tx.document.delete({ where: { id, status: DocumentStatus.DRAFT } });
+ });
+ }
+
// if the document is pending, send cancellation emails to all recipients
-- if (status === DocumentStatus.PENDING) {
- if (document.Recipient.length > 0) {
- await Promise.all(
- document.Recipient.map(async (recipient) => {
- const assetBaseUrl = NEXT_PUBLIC_WEBAPP_URL() || 'http://localhost:3000';
- const document = await prisma.document.findUnique({
- where: {
- id,
- status,
- userId,
- },
- include: {
- Recipient: true,
- documentMeta: true,
- },
- });
++ if (status === DocumentStatus.PENDING && document.Recipient.length > 0) {
++ await Promise.all(
++ document.Recipient.map(async (recipient) => {
++ const assetBaseUrl = NEXT_PUBLIC_WEBAPP_URL() || 'http://localhost:3000';
- const template = createElement(DocumentCancelTemplate, {
- documentName: document.title,
- inviterName: user.name || undefined,
- inviterEmail: user.email,
- assetBaseUrl,
- });
- if (!document) {
- throw new Error('Document not found');
- }
++ const template = createElement(DocumentCancelTemplate, {
++ documentName: document.title,
++ inviterName: user.name || undefined,
++ inviterEmail: user.email,
++ assetBaseUrl,
++ });
- if (document.Recipient.length > 0) {
- await Promise.all(
- document.Recipient.map(async (recipient) => {
- const assetBaseUrl = NEXT_PUBLIC_WEBAPP_URL() || 'http://localhost:3000';
-
- const template = createElement(DocumentCancelTemplate, {
- documentName: document.title,
- inviterName: user.name || undefined,
- inviterEmail: user.email,
- assetBaseUrl,
- });
-
-- await mailer.sendMail({
-- to: {
-- address: recipient.email,
-- name: recipient.name,
-- },
-- from: {
-- name: FROM_NAME,
-- address: FROM_ADDRESS,
-- },
-- subject: 'Document Cancelled',
-- html: render(template),
-- text: render(template, { plainText: true }),
-- });
-- }),
-- );
-- }
++ await mailer.sendMail({
++ to: {
++ address: recipient.email,
++ name: recipient.name,
++ },
++ from: {
++ name: FROM_NAME,
++ address: FROM_ADDRESS,
++ },
++ subject: 'Document Cancelled',
++ html: render(template),
++ text: render(template, { plainText: true }),
++ });
++ }),
++ );
}
// If the document is not a draft, only soft-delete.
diff --cc packages/lib/server-only/document/update-title.ts
index 7c25bba86,f7f7a6b88..eab7f1f28
--- a/packages/lib/server-only/document/update-title.ts
+++ b/packages/lib/server-only/document/update-title.ts
@@@ -26,32 -24,34 +26,36 @@@ export const updateTitle = async (
},
});
- const document = await prisma.document.findFirstOrThrow({
- where: {
- id: documentId,
- OR: [
- {
- userId,
- },
- {
- team: {
- members: {
- some: {
- userId,
+ return await prisma.$transaction(async (tx) => {
+ const document = await tx.document.findFirstOrThrow({
+ where: {
+ id: documentId,
+ ...(teamId
+ ? {
+ team: {
+ id: teamId,
+ members: {
+ some: {
+ userId,
+ },
+ },
},
- },
- },
- },
- ],
- },
- });
+ }
+ : {
+ userId,
+ teamId: null,
+ }),
+ },
+ });
- if (document.title === title) {
- return document;
- }
+ if (document.title === title) {
+ return document;
+ }
+ return await prisma.$transaction(async (tx) => {
+ // Instead of doing everything in a transaction we can use our knowledge
+ // of the current document title to ensure we aren't performing a conflicting
+ // update.
const updatedDocument = await tx.document.update({
where: {
id: documentId,
diff --cc packages/prisma/schema.prisma
index 1ce0cdb83,601f94f97..a40102905
--- a/packages/prisma/schema.prisma
+++ b/packages/prisma/schema.prisma
@@@ -41,12 -41,13 +41,14 @@@ model User
ownedPendingTeams TeamPending[]
teamMembers TeamMember[]
twoFactorSecret String?
- twoFactorEnabled Boolean @default(false)
+ twoFactorEnabled Boolean @default(false)
twoFactorBackupCodes String?
- VerificationToken VerificationToken[]
- ApiToken ApiToken[]
- Template Template[]
- securityAuditLogs UserSecurityAuditLog[]
-
++
+ VerificationToken VerificationToken[]
++ ApiToken ApiToken[]
+ Template Template[]
+ securityAuditLogs UserSecurityAuditLog[]
+ siteSettings SiteSettings[]
@@index([email])
}
diff --cc packages/trpc/server/document-router/router.ts
index 2ab3ee110,eb833684a..9a7556821
--- a/packages/trpc/server/document-router/router.ts
+++ b/packages/trpc/server/document-router/router.ts
@@@ -111,7 -113,12 +113,12 @@@ export const documentRouter = router(
const userId = ctx.user.id;
- return await deleteDocument({ id, userId });
+ return await deleteDocument({
+ id,
+ userId,
- status,
++ // TODO: Get teamId
+ requestMetadata: extractNextApiRequestMetadata(ctx.req),
+ });
} catch (err) {
console.error(err);