mirror of
https://github.com/documenso/documenso.git
synced 2025-11-10 04:22:32 +10:00
chore: revert changes based on feedback
This commit is contained in:
@ -187,7 +187,6 @@ export const DocumentEditForm = ({
|
||||
title: data.title,
|
||||
externalId: data.externalId || null,
|
||||
visibility: data.visibility,
|
||||
attachments: data.attachments ?? [],
|
||||
globalAccessAuth: parsedGlobalAccessAuth.success ? parsedGlobalAccessAuth.data : [],
|
||||
globalActionAuth: data.globalActionAuth ?? [],
|
||||
},
|
||||
|
||||
@ -138,7 +138,6 @@ export const TemplateEditForm = ({
|
||||
title: data.title,
|
||||
externalId: data.externalId || null,
|
||||
visibility: data.visibility,
|
||||
attachments: data.attachments ?? [],
|
||||
globalAccessAuth: parsedGlobalAccessAuth.success ? parsedGlobalAccessAuth.data : [],
|
||||
globalActionAuth: data.globalActionAuth ?? [],
|
||||
},
|
||||
@ -170,14 +169,6 @@ export const TemplateEditForm = ({
|
||||
await Promise.all([
|
||||
updateTemplateSettings({
|
||||
templateId: template.id,
|
||||
data: {
|
||||
attachments: template.attachments?.map((attachment) => ({
|
||||
id: attachment.id,
|
||||
label: attachment.label,
|
||||
url: attachment.url,
|
||||
formId: attachment.id,
|
||||
})),
|
||||
},
|
||||
meta: {
|
||||
signingOrder: data.signingOrder,
|
||||
allowDictateNextSigner: data.allowDictateNextSigner,
|
||||
|
||||
@ -23,7 +23,6 @@ import {
|
||||
} from '@documenso/lib/types/document-auth';
|
||||
import { ZDocumentEmailSettingsSchema } from '@documenso/lib/types/document-email';
|
||||
import { ZFieldMetaPrefillFieldsSchema, ZFieldMetaSchema } from '@documenso/lib/types/field-meta';
|
||||
import { AttachmentSchema } from '@documenso/prisma/generated/zod/modelSchema/AttachmentSchema';
|
||||
|
||||
extendZodWithOpenApi(z);
|
||||
|
||||
@ -193,13 +192,6 @@ export const ZCreateDocumentMutationSchema = z.object({
|
||||
description: 'The globalActionAuth property is only available for Enterprise accounts.',
|
||||
}),
|
||||
formValues: z.record(z.string(), z.union([z.string(), z.boolean(), z.number()])).optional(),
|
||||
attachments: AttachmentSchema.pick({
|
||||
id: true,
|
||||
label: true,
|
||||
url: true,
|
||||
})
|
||||
.array()
|
||||
.optional(),
|
||||
});
|
||||
|
||||
export type TCreateDocumentMutationSchema = z.infer<typeof ZCreateDocumentMutationSchema>;
|
||||
|
||||
22
packages/lib/server-only/attachment/find-attachments.ts
Normal file
22
packages/lib/server-only/attachment/find-attachments.ts
Normal file
@ -0,0 +1,22 @@
|
||||
import { prisma } from '@documenso/prisma';
|
||||
|
||||
import { buildTeamWhereQuery } from '../../utils/teams';
|
||||
|
||||
export type FindAttachmentsOptions = {
|
||||
documentId: number;
|
||||
userId: number;
|
||||
teamId: number;
|
||||
};
|
||||
|
||||
export const findAttachments = async ({ documentId, userId, teamId }: FindAttachmentsOptions) => {
|
||||
const attachments = await prisma.attachment.findMany({
|
||||
where: {
|
||||
document: {
|
||||
id: documentId,
|
||||
team: buildTeamWhereQuery({ teamId, userId }),
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
return attachments;
|
||||
};
|
||||
@ -0,0 +1,84 @@
|
||||
import type { Attachment } from '@prisma/client';
|
||||
|
||||
import { prisma } from '@documenso/prisma';
|
||||
|
||||
import { AppError } from '../../errors/app-error';
|
||||
import { AppErrorCode } from '../../errors/app-error';
|
||||
|
||||
export type CreateAttachmentsOptions = {
|
||||
documentId?: number;
|
||||
templateId?: number;
|
||||
attachments: Pick<Attachment, 'id' | 'label' | 'url' | 'type'>[];
|
||||
};
|
||||
|
||||
export const setDocumentAttachments = async ({
|
||||
documentId,
|
||||
templateId,
|
||||
attachments,
|
||||
}: CreateAttachmentsOptions) => {
|
||||
const document = await prisma.document.findUnique({
|
||||
where: {
|
||||
id: documentId,
|
||||
},
|
||||
});
|
||||
|
||||
if (!document) {
|
||||
throw new AppError(AppErrorCode.NOT_FOUND, {
|
||||
message: 'Document not found',
|
||||
});
|
||||
}
|
||||
|
||||
const existingAttachments = await prisma.attachment.findMany({
|
||||
where: {
|
||||
documentId,
|
||||
},
|
||||
});
|
||||
|
||||
const newIds = attachments.map((a) => a.id).filter(Boolean);
|
||||
const toDelete = existingAttachments.filter((existing) => !newIds.includes(existing.id));
|
||||
|
||||
if (toDelete.length > 0) {
|
||||
await prisma.attachment.deleteMany({
|
||||
where: {
|
||||
id: { in: toDelete.map((a) => a.id) },
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
const upsertedAttachments: Attachment[] = [];
|
||||
|
||||
for (const attachment of attachments) {
|
||||
if (attachment.id) {
|
||||
const updated = await prisma.attachment.upsert({
|
||||
where: { id: attachment.id },
|
||||
update: {
|
||||
label: attachment.label,
|
||||
url: attachment.url,
|
||||
type: attachment.type,
|
||||
templateId,
|
||||
},
|
||||
create: {
|
||||
label: attachment.label,
|
||||
url: attachment.url,
|
||||
type: attachment.type,
|
||||
templateId,
|
||||
documentId,
|
||||
},
|
||||
});
|
||||
upsertedAttachments.push(updated);
|
||||
} else {
|
||||
const created = await prisma.attachment.create({
|
||||
data: {
|
||||
label: attachment.label,
|
||||
url: attachment.url,
|
||||
type: attachment.type,
|
||||
templateId,
|
||||
documentId,
|
||||
},
|
||||
});
|
||||
upsertedAttachments.push(created);
|
||||
}
|
||||
}
|
||||
|
||||
return upsertedAttachments;
|
||||
};
|
||||
@ -7,7 +7,6 @@ import type { ApiRequestMetadata } from '@documenso/lib/universal/extract-reques
|
||||
import type { CreateDocumentAuditLogDataResponse } from '@documenso/lib/utils/document-audit-logs';
|
||||
import { createDocumentAuditLogData } from '@documenso/lib/utils/document-audit-logs';
|
||||
import { prisma } from '@documenso/prisma';
|
||||
import type { Attachment } from '@documenso/prisma/generated/zod/modelSchema/AttachmentSchema';
|
||||
|
||||
import { AppError, AppErrorCode } from '../../errors/app-error';
|
||||
import type { TDocumentAccessAuthTypes, TDocumentActionAuthTypes } from '../../types/document-auth';
|
||||
@ -25,7 +24,6 @@ export type UpdateDocumentOptions = {
|
||||
globalAccessAuth?: TDocumentAccessAuthTypes[];
|
||||
globalActionAuth?: TDocumentActionAuthTypes[];
|
||||
useLegacyFieldInsertion?: boolean;
|
||||
attachments?: Pick<Attachment, 'id' | 'label' | 'url'>[];
|
||||
};
|
||||
requestMetadata: ApiRequestMetadata;
|
||||
};
|
||||
@ -55,7 +53,6 @@ export const updateDocument = async ({
|
||||
},
|
||||
},
|
||||
},
|
||||
attachments: true,
|
||||
},
|
||||
});
|
||||
|
||||
@ -136,8 +133,6 @@ export const updateDocument = async ({
|
||||
documentGlobalActionAuth === undefined || documentGlobalActionAuth === newGlobalActionAuth;
|
||||
const isDocumentVisibilitySame =
|
||||
data.visibility === undefined || data.visibility === document.visibility;
|
||||
const isAttachmentsSame =
|
||||
data.attachments === undefined || data.attachments === document.attachments;
|
||||
|
||||
const auditLogs: CreateDocumentAuditLogDataResponse[] = [];
|
||||
|
||||
@ -217,20 +212,6 @@ export const updateDocument = async ({
|
||||
);
|
||||
}
|
||||
|
||||
if (!isAttachmentsSame) {
|
||||
auditLogs.push(
|
||||
createDocumentAuditLogData({
|
||||
type: DOCUMENT_AUDIT_LOG_TYPE.DOCUMENT_ATTACHMENTS_UPDATED,
|
||||
documentId,
|
||||
metadata: requestMetadata,
|
||||
data: {
|
||||
from: document.attachments,
|
||||
to: data.attachments ?? [],
|
||||
},
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
// Early return if nothing is required.
|
||||
if (auditLogs.length === 0 && data.useLegacyFieldInsertion === undefined) {
|
||||
return document;
|
||||
@ -255,39 +236,6 @@ export const updateDocument = async ({
|
||||
},
|
||||
});
|
||||
|
||||
if (data.attachments) {
|
||||
await tx.attachment.deleteMany({
|
||||
where: {
|
||||
documentId,
|
||||
id: {
|
||||
notIn: data.attachments.map((a) => a.id),
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
await Promise.all(
|
||||
data.attachments.map(
|
||||
async (attachment) =>
|
||||
await tx.attachment.upsert({
|
||||
where: {
|
||||
id: attachment.id,
|
||||
documentId,
|
||||
},
|
||||
update: {
|
||||
label: attachment.label,
|
||||
url: attachment.url,
|
||||
},
|
||||
create: {
|
||||
id: attachment.id,
|
||||
label: attachment.label,
|
||||
url: attachment.url,
|
||||
documentId,
|
||||
},
|
||||
}),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
await tx.documentAuditLog.createMany({
|
||||
data: auditLogs,
|
||||
});
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
import type { Attachment, DocumentVisibility, Template, TemplateMeta } from '@prisma/client';
|
||||
import type { DocumentVisibility, Template, TemplateMeta } from '@prisma/client';
|
||||
|
||||
import { prisma } from '@documenso/prisma';
|
||||
|
||||
@ -21,7 +21,6 @@ export type UpdateTemplateOptions = {
|
||||
publicDescription?: string;
|
||||
type?: Template['type'];
|
||||
useLegacyFieldInsertion?: boolean;
|
||||
attachments?: Pick<Attachment, 'id' | 'label' | 'url'>[];
|
||||
};
|
||||
meta?: Partial<Omit<TemplateMeta, 'id' | 'templateId'>>;
|
||||
};
|
||||
@ -40,7 +39,6 @@ export const updateTemplate = async ({
|
||||
},
|
||||
include: {
|
||||
templateMeta: true,
|
||||
attachments: true,
|
||||
team: {
|
||||
select: {
|
||||
organisation: {
|
||||
@ -100,29 +98,6 @@ export const updateTemplate = async ({
|
||||
publicDescription: data?.publicDescription,
|
||||
publicTitle: data?.publicTitle,
|
||||
useLegacyFieldInsertion: data?.useLegacyFieldInsertion,
|
||||
attachments: {
|
||||
deleteMany: {
|
||||
templateId,
|
||||
id: {
|
||||
notIn: data?.attachments?.map((attachment) => attachment.id),
|
||||
},
|
||||
},
|
||||
upsert: data?.attachments?.map((attachment) => ({
|
||||
where: {
|
||||
id: attachment.id,
|
||||
templateId,
|
||||
},
|
||||
update: {
|
||||
label: attachment.label,
|
||||
url: attachment.url,
|
||||
},
|
||||
create: {
|
||||
id: attachment.id,
|
||||
label: attachment.label,
|
||||
url: attachment.url,
|
||||
},
|
||||
})),
|
||||
},
|
||||
authOptions,
|
||||
templateMeta: {
|
||||
upsert: {
|
||||
|
||||
47
packages/trpc/server/attachment-router/router.ts
Normal file
47
packages/trpc/server/attachment-router/router.ts
Normal file
@ -0,0 +1,47 @@
|
||||
import { findAttachments } from '@documenso/lib/server-only/attachment/find-attachments';
|
||||
import { setDocumentAttachments } from '@documenso/lib/server-only/attachment/set-document-attachments';
|
||||
|
||||
import { authenticatedProcedure, router } from '../trpc';
|
||||
import {
|
||||
ZGetDocumentAttachmentsResponseSchema,
|
||||
ZGetDocumentAttachmentsSchema,
|
||||
ZSetDocumentAttachmentsResponseSchema,
|
||||
ZSetDocumentAttachmentsSchema,
|
||||
} from './schema';
|
||||
|
||||
export const attachmentRouter = router({
|
||||
/**
|
||||
* @private
|
||||
*/
|
||||
getAttachments: authenticatedProcedure
|
||||
.input(ZGetDocumentAttachmentsSchema)
|
||||
.output(ZGetDocumentAttachmentsResponseSchema)
|
||||
.query(async ({ input, ctx }) => {
|
||||
const { documentId } = input;
|
||||
const { user } = ctx;
|
||||
|
||||
const attachments = await findAttachments({
|
||||
documentId,
|
||||
userId: user.id,
|
||||
teamId: ctx.teamId,
|
||||
});
|
||||
|
||||
return attachments;
|
||||
}),
|
||||
/**
|
||||
* @private
|
||||
*/
|
||||
setDocumentAttachments: authenticatedProcedure
|
||||
.input(ZSetDocumentAttachmentsSchema)
|
||||
.output(ZSetDocumentAttachmentsResponseSchema)
|
||||
.mutation(async ({ input, ctx }) => {
|
||||
const { documentId, attachments } = input;
|
||||
|
||||
const updatedAttachments = await setDocumentAttachments({
|
||||
documentId,
|
||||
attachments,
|
||||
});
|
||||
|
||||
return updatedAttachments;
|
||||
}),
|
||||
});
|
||||
@ -13,7 +13,6 @@ import {
|
||||
} from '@documenso/lib/types/document-auth';
|
||||
import { ZDocumentEmailSettingsSchema } from '@documenso/lib/types/document-email';
|
||||
import { isValidRedirectUrl } from '@documenso/lib/utils/is-valid-redirect-url';
|
||||
import { AttachmentSchema } from '@documenso/prisma/generated/zod/modelSchema/AttachmentSchema';
|
||||
import {
|
||||
ZDocumentMetaDateFormatSchema,
|
||||
ZDocumentMetaTimezoneSchema,
|
||||
@ -54,16 +53,6 @@ export const ZAddTemplateSettingsFormSchema = z.object({
|
||||
message: msg`At least one signature type must be enabled`.id,
|
||||
}),
|
||||
}),
|
||||
attachments: AttachmentSchema.pick({
|
||||
id: true,
|
||||
label: true,
|
||||
url: true,
|
||||
})
|
||||
.extend({
|
||||
formId: z.string().min(1),
|
||||
})
|
||||
.array()
|
||||
.optional(),
|
||||
});
|
||||
|
||||
export type TAddTemplateSettingsFormSchema = z.infer<typeof ZAddTemplateSettingsFormSchema>;
|
||||
|
||||
Reference in New Issue
Block a user