mirror of
https://github.com/documenso/documenso.git
synced 2025-11-24 05:32:12 +10:00
Compare commits
3 Commits
feat/get-d
...
feat/audit
| Author | SHA1 | Date | |
|---|---|---|---|
| fc513800ae | |||
| e7affea053 | |||
| 1a577e55a9 |
110
packages/lib/server-only/document/audit-log-query.ts
Normal file
110
packages/lib/server-only/document/audit-log-query.ts
Normal file
@ -0,0 +1,110 @@
|
|||||||
|
import type { DocumentAuditLog, Envelope, Prisma } from '@prisma/client';
|
||||||
|
|
||||||
|
import { prisma } from '@documenso/prisma';
|
||||||
|
|
||||||
|
import { DOCUMENT_AUDIT_LOG_TYPE } from '../../types/document-audit-logs';
|
||||||
|
import type { FindResultResponse } from '../../types/search-params';
|
||||||
|
import { parseDocumentAuditLogData } from '../../utils/document-audit-logs';
|
||||||
|
|
||||||
|
const RECENT_ACTIVITY_EVENT_TYPES = [
|
||||||
|
DOCUMENT_AUDIT_LOG_TYPE.DOCUMENT_COMPLETED,
|
||||||
|
DOCUMENT_AUDIT_LOG_TYPE.DOCUMENT_CREATED,
|
||||||
|
DOCUMENT_AUDIT_LOG_TYPE.DOCUMENT_DELETED,
|
||||||
|
DOCUMENT_AUDIT_LOG_TYPE.DOCUMENT_OPENED,
|
||||||
|
DOCUMENT_AUDIT_LOG_TYPE.DOCUMENT_RECIPIENT_COMPLETED,
|
||||||
|
DOCUMENT_AUDIT_LOG_TYPE.DOCUMENT_RECIPIENT_REJECTED,
|
||||||
|
DOCUMENT_AUDIT_LOG_TYPE.DOCUMENT_SENT,
|
||||||
|
DOCUMENT_AUDIT_LOG_TYPE.DOCUMENT_MOVED_TO_TEAM,
|
||||||
|
];
|
||||||
|
|
||||||
|
export interface AuditLogQueryOptions {
|
||||||
|
envelope: Envelope;
|
||||||
|
page?: number;
|
||||||
|
perPage?: number;
|
||||||
|
orderBy?: {
|
||||||
|
column: keyof DocumentAuditLog;
|
||||||
|
direction: 'asc' | 'desc';
|
||||||
|
};
|
||||||
|
cursor?: string;
|
||||||
|
filterForRecentActivity?: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
|
function buildAuditLogWhereClause(
|
||||||
|
envelope: Envelope,
|
||||||
|
filterForRecentActivity?: boolean,
|
||||||
|
): Prisma.DocumentAuditLogWhereInput {
|
||||||
|
const baseWhereClause: Prisma.DocumentAuditLogWhereInput = {
|
||||||
|
envelopeId: envelope.id,
|
||||||
|
};
|
||||||
|
|
||||||
|
if (!filterForRecentActivity) {
|
||||||
|
return baseWhereClause;
|
||||||
|
}
|
||||||
|
|
||||||
|
const recentActivityConditions: Prisma.DocumentAuditLogWhereInput['OR'] = [
|
||||||
|
{
|
||||||
|
type: {
|
||||||
|
in: RECENT_ACTIVITY_EVENT_TYPES,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
type: DOCUMENT_AUDIT_LOG_TYPE.EMAIL_SENT,
|
||||||
|
data: {
|
||||||
|
path: ['isResending'],
|
||||||
|
equals: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
|
return {
|
||||||
|
...baseWhereClause,
|
||||||
|
OR: recentActivityConditions,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function queryAuditLogs({
|
||||||
|
envelope,
|
||||||
|
page = 1,
|
||||||
|
perPage = 30,
|
||||||
|
orderBy,
|
||||||
|
cursor,
|
||||||
|
filterForRecentActivity,
|
||||||
|
}: AuditLogQueryOptions) {
|
||||||
|
const orderByColumn = orderBy?.column ?? 'createdAt';
|
||||||
|
const orderByDirection = orderBy?.direction ?? 'desc';
|
||||||
|
|
||||||
|
const whereClause = buildAuditLogWhereClause(envelope, filterForRecentActivity);
|
||||||
|
|
||||||
|
const normalizedPage = Math.max(page, 1);
|
||||||
|
const skip = (normalizedPage - 1) * perPage;
|
||||||
|
|
||||||
|
const [data, count] = await Promise.all([
|
||||||
|
prisma.documentAuditLog.findMany({
|
||||||
|
where: whereClause,
|
||||||
|
skip,
|
||||||
|
take: perPage + 1,
|
||||||
|
orderBy: {
|
||||||
|
[orderByColumn]: orderByDirection,
|
||||||
|
},
|
||||||
|
cursor: cursor ? { id: cursor } : undefined,
|
||||||
|
}),
|
||||||
|
prisma.documentAuditLog.count({
|
||||||
|
where: whereClause,
|
||||||
|
}),
|
||||||
|
]);
|
||||||
|
|
||||||
|
const allParsedData = data.map((auditLog) => parseDocumentAuditLogData(auditLog));
|
||||||
|
|
||||||
|
const hasNextPage = allParsedData.length > perPage;
|
||||||
|
const parsedData = hasNextPage ? allParsedData.slice(0, perPage) : allParsedData;
|
||||||
|
const nextCursor = hasNextPage ? allParsedData[perPage].id : undefined;
|
||||||
|
|
||||||
|
return {
|
||||||
|
data: parsedData,
|
||||||
|
count,
|
||||||
|
currentPage: normalizedPage,
|
||||||
|
perPage,
|
||||||
|
totalPages: Math.ceil(count / perPage),
|
||||||
|
nextCursor,
|
||||||
|
} satisfies FindResultResponse<typeof parsedData> & { nextCursor?: string };
|
||||||
|
}
|
||||||
@ -1,17 +1,15 @@
|
|||||||
import { type DocumentAuditLog, EnvelopeType, type Prisma } from '@prisma/client';
|
import type { DocumentAuditLog } from '@prisma/client';
|
||||||
|
import { EnvelopeType } from '@prisma/client';
|
||||||
|
|
||||||
import { prisma } from '@documenso/prisma';
|
import { prisma } from '@documenso/prisma';
|
||||||
|
|
||||||
import { AppError, AppErrorCode } from '../../errors/app-error';
|
import { AppError, AppErrorCode } from '../../errors/app-error';
|
||||||
import { DOCUMENT_AUDIT_LOG_TYPE } from '../../types/document-audit-logs';
|
|
||||||
import type { FindResultResponse } from '../../types/search-params';
|
|
||||||
import { parseDocumentAuditLogData } from '../../utils/document-audit-logs';
|
|
||||||
import { getEnvelopeWhereInput } from '../envelope/get-envelope-by-id';
|
import { getEnvelopeWhereInput } from '../envelope/get-envelope-by-id';
|
||||||
|
import { queryAuditLogs } from './audit-log-query';
|
||||||
|
|
||||||
export interface FindDocumentAuditLogsOptions {
|
interface BaseAuditLogOptions {
|
||||||
userId: number;
|
userId: number;
|
||||||
teamId: number;
|
teamId: number;
|
||||||
documentId: number;
|
|
||||||
page?: number;
|
page?: number;
|
||||||
perPage?: number;
|
perPage?: number;
|
||||||
orderBy?: {
|
orderBy?: {
|
||||||
@ -22,19 +20,24 @@ export interface FindDocumentAuditLogsOptions {
|
|||||||
filterForRecentActivity?: boolean;
|
filterForRecentActivity?: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface FindDocumentAuditLogsOptions extends BaseAuditLogOptions {
|
||||||
|
documentId: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface FindEnvelopeAuditLogsOptions extends BaseAuditLogOptions {
|
||||||
|
envelopeId: string;
|
||||||
|
}
|
||||||
|
|
||||||
export const findDocumentAuditLogs = async ({
|
export const findDocumentAuditLogs = async ({
|
||||||
userId,
|
userId,
|
||||||
teamId,
|
teamId,
|
||||||
documentId,
|
documentId,
|
||||||
page = 1,
|
page,
|
||||||
perPage = 30,
|
perPage,
|
||||||
orderBy,
|
orderBy,
|
||||||
cursor,
|
cursor,
|
||||||
filterForRecentActivity,
|
filterForRecentActivity,
|
||||||
}: FindDocumentAuditLogsOptions) => {
|
}: FindDocumentAuditLogsOptions) => {
|
||||||
const orderByColumn = orderBy?.column ?? 'createdAt';
|
|
||||||
const orderByDirection = orderBy?.direction ?? 'desc';
|
|
||||||
|
|
||||||
const { envelopeWhereInput } = await getEnvelopeWhereInput({
|
const { envelopeWhereInput } = await getEnvelopeWhereInput({
|
||||||
id: {
|
id: {
|
||||||
type: 'documentId',
|
type: 'documentId',
|
||||||
@ -53,67 +56,53 @@ export const findDocumentAuditLogs = async ({
|
|||||||
throw new AppError(AppErrorCode.NOT_FOUND);
|
throw new AppError(AppErrorCode.NOT_FOUND);
|
||||||
}
|
}
|
||||||
|
|
||||||
const whereClause: Prisma.DocumentAuditLogWhereInput = {
|
return queryAuditLogs({
|
||||||
envelopeId: envelope.id,
|
envelope,
|
||||||
};
|
page,
|
||||||
|
|
||||||
// Filter events down to what we consider recent activity.
|
|
||||||
if (filterForRecentActivity) {
|
|
||||||
whereClause.OR = [
|
|
||||||
{
|
|
||||||
type: {
|
|
||||||
in: [
|
|
||||||
DOCUMENT_AUDIT_LOG_TYPE.DOCUMENT_COMPLETED,
|
|
||||||
DOCUMENT_AUDIT_LOG_TYPE.DOCUMENT_CREATED,
|
|
||||||
DOCUMENT_AUDIT_LOG_TYPE.DOCUMENT_DELETED,
|
|
||||||
DOCUMENT_AUDIT_LOG_TYPE.DOCUMENT_OPENED,
|
|
||||||
DOCUMENT_AUDIT_LOG_TYPE.DOCUMENT_RECIPIENT_COMPLETED,
|
|
||||||
DOCUMENT_AUDIT_LOG_TYPE.DOCUMENT_RECIPIENT_REJECTED,
|
|
||||||
DOCUMENT_AUDIT_LOG_TYPE.DOCUMENT_SENT,
|
|
||||||
DOCUMENT_AUDIT_LOG_TYPE.DOCUMENT_MOVED_TO_TEAM,
|
|
||||||
],
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
type: DOCUMENT_AUDIT_LOG_TYPE.EMAIL_SENT,
|
|
||||||
data: {
|
|
||||||
path: ['isResending'],
|
|
||||||
equals: true,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
];
|
|
||||||
}
|
|
||||||
|
|
||||||
const [data, count] = await Promise.all([
|
|
||||||
prisma.documentAuditLog.findMany({
|
|
||||||
where: whereClause,
|
|
||||||
skip: Math.max(page - 1, 0) * perPage,
|
|
||||||
take: perPage + 1,
|
|
||||||
orderBy: {
|
|
||||||
[orderByColumn]: orderByDirection,
|
|
||||||
},
|
|
||||||
cursor: cursor ? { id: cursor } : undefined,
|
|
||||||
}),
|
|
||||||
prisma.documentAuditLog.count({
|
|
||||||
where: whereClause,
|
|
||||||
}),
|
|
||||||
]);
|
|
||||||
|
|
||||||
let nextCursor: string | undefined = undefined;
|
|
||||||
|
|
||||||
const parsedData = data.map((auditLog) => parseDocumentAuditLogData(auditLog));
|
|
||||||
|
|
||||||
if (parsedData.length > perPage) {
|
|
||||||
const nextItem = parsedData.pop();
|
|
||||||
nextCursor = nextItem!.id;
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
|
||||||
data: parsedData,
|
|
||||||
count,
|
|
||||||
currentPage: Math.max(page, 1),
|
|
||||||
perPage,
|
perPage,
|
||||||
totalPages: Math.ceil(count / perPage),
|
orderBy,
|
||||||
nextCursor,
|
cursor,
|
||||||
} satisfies FindResultResponse<typeof parsedData> & { nextCursor?: string };
|
filterForRecentActivity,
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
export const findEnvelopeAuditLogs = async ({
|
||||||
|
userId,
|
||||||
|
teamId,
|
||||||
|
envelopeId,
|
||||||
|
page,
|
||||||
|
perPage,
|
||||||
|
orderBy,
|
||||||
|
cursor,
|
||||||
|
filterForRecentActivity,
|
||||||
|
}: FindEnvelopeAuditLogsOptions) => {
|
||||||
|
const isLegacyDocumentId = /^\d+$/.test(envelopeId);
|
||||||
|
|
||||||
|
const idConfig = isLegacyDocumentId
|
||||||
|
? { type: 'documentId' as const, id: Number(envelopeId) }
|
||||||
|
: { type: 'envelopeId' as const, id: envelopeId };
|
||||||
|
|
||||||
|
const { envelopeWhereInput } = await getEnvelopeWhereInput({
|
||||||
|
id: idConfig,
|
||||||
|
type: isLegacyDocumentId ? EnvelopeType.DOCUMENT : null,
|
||||||
|
userId,
|
||||||
|
teamId,
|
||||||
|
});
|
||||||
|
|
||||||
|
const envelope = await prisma.envelope.findUnique({
|
||||||
|
where: envelopeWhereInput,
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!envelope) {
|
||||||
|
throw new AppError(AppErrorCode.NOT_FOUND);
|
||||||
|
}
|
||||||
|
|
||||||
|
return queryAuditLogs({
|
||||||
|
envelope,
|
||||||
|
page,
|
||||||
|
perPage,
|
||||||
|
orderBy,
|
||||||
|
cursor,
|
||||||
|
filterForRecentActivity,
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|||||||
@ -10,32 +10,18 @@ export const findDocumentAuditLogsRoute = authenticatedProcedure
|
|||||||
.input(ZFindDocumentAuditLogsRequestSchema)
|
.input(ZFindDocumentAuditLogsRequestSchema)
|
||||||
.output(ZFindDocumentAuditLogsResponseSchema)
|
.output(ZFindDocumentAuditLogsResponseSchema)
|
||||||
.query(async ({ input, ctx }) => {
|
.query(async ({ input, ctx }) => {
|
||||||
const { teamId } = ctx;
|
const { orderByColumn, orderByDirection, ...auditLogParams } = input;
|
||||||
|
|
||||||
const {
|
|
||||||
page,
|
|
||||||
perPage,
|
|
||||||
documentId,
|
|
||||||
cursor,
|
|
||||||
filterForRecentActivity,
|
|
||||||
orderByColumn,
|
|
||||||
orderByDirection,
|
|
||||||
} = input;
|
|
||||||
|
|
||||||
ctx.logger.info({
|
ctx.logger.info({
|
||||||
input: {
|
input: {
|
||||||
documentId,
|
documentId: input.documentId,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
return await findDocumentAuditLogs({
|
return await findDocumentAuditLogs({
|
||||||
|
...auditLogParams,
|
||||||
userId: ctx.user.id,
|
userId: ctx.user.id,
|
||||||
teamId,
|
teamId: ctx.teamId,
|
||||||
page,
|
|
||||||
perPage,
|
|
||||||
documentId,
|
|
||||||
cursor,
|
|
||||||
filterForRecentActivity,
|
|
||||||
orderBy: orderByColumn ? { column: orderByColumn, direction: orderByDirection } : undefined,
|
orderBy: orderByColumn ? { column: orderByColumn, direction: orderByDirection } : undefined,
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@ -1,74 +0,0 @@
|
|||||||
import { EnvelopeType } from '@prisma/client';
|
|
||||||
|
|
||||||
import { getEnvelopeWhereInput } from '@documenso/lib/server-only/envelope/get-envelope-by-id';
|
|
||||||
import { mapEnvelopesToDocumentMany } from '@documenso/lib/utils/document';
|
|
||||||
import { mapDocumentIdToSecondaryId } from '@documenso/lib/utils/envelope';
|
|
||||||
import { prisma } from '@documenso/prisma';
|
|
||||||
|
|
||||||
import { authenticatedProcedure } from '../trpc';
|
|
||||||
import {
|
|
||||||
ZGetDocumentsByIdsRequestSchema,
|
|
||||||
ZGetDocumentsByIdsResponseSchema,
|
|
||||||
getDocumentsByIdsMeta,
|
|
||||||
} from './get-documents-by-ids.types';
|
|
||||||
|
|
||||||
export const getDocumentsByIdsRoute = authenticatedProcedure
|
|
||||||
.meta(getDocumentsByIdsMeta)
|
|
||||||
.input(ZGetDocumentsByIdsRequestSchema)
|
|
||||||
.output(ZGetDocumentsByIdsResponseSchema)
|
|
||||||
.mutation(async ({ input, ctx }) => {
|
|
||||||
const { teamId, user } = ctx;
|
|
||||||
const { documentIds } = input;
|
|
||||||
|
|
||||||
ctx.logger.info({
|
|
||||||
input: {
|
|
||||||
documentIds,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
const { envelopeWhereInput } = await getEnvelopeWhereInput({
|
|
||||||
id: {
|
|
||||||
type: 'documentId',
|
|
||||||
id: documentIds[0],
|
|
||||||
},
|
|
||||||
userId: user.id,
|
|
||||||
teamId,
|
|
||||||
type: EnvelopeType.DOCUMENT,
|
|
||||||
});
|
|
||||||
|
|
||||||
const envelopeOrInput = envelopeWhereInput.OR!;
|
|
||||||
|
|
||||||
const secondaryIds = documentIds.map((documentId) => mapDocumentIdToSecondaryId(documentId));
|
|
||||||
|
|
||||||
const envelopes = await prisma.envelope.findMany({
|
|
||||||
where: {
|
|
||||||
type: EnvelopeType.DOCUMENT,
|
|
||||||
secondaryId: {
|
|
||||||
in: secondaryIds,
|
|
||||||
},
|
|
||||||
OR: envelopeOrInput,
|
|
||||||
},
|
|
||||||
include: {
|
|
||||||
user: {
|
|
||||||
select: {
|
|
||||||
id: true,
|
|
||||||
name: true,
|
|
||||||
email: true,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
recipients: {
|
|
||||||
orderBy: {
|
|
||||||
id: 'asc',
|
|
||||||
},
|
|
||||||
},
|
|
||||||
team: {
|
|
||||||
select: {
|
|
||||||
id: true,
|
|
||||||
url: true,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
return envelopes.map((envelope) => mapEnvelopesToDocumentMany(envelope));
|
|
||||||
});
|
|
||||||
@ -1,24 +0,0 @@
|
|||||||
import { z } from 'zod';
|
|
||||||
|
|
||||||
import { ZDocumentManySchema } from '@documenso/lib/types/document';
|
|
||||||
|
|
||||||
import type { TrpcRouteMeta } from '../trpc';
|
|
||||||
|
|
||||||
export const getDocumentsByIdsMeta: TrpcRouteMeta = {
|
|
||||||
openapi: {
|
|
||||||
method: 'POST',
|
|
||||||
path: '/document/get-many',
|
|
||||||
summary: 'Get multiple documents',
|
|
||||||
description: 'Retrieve multiple documents by their IDs',
|
|
||||||
tags: ['Document'],
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
export const ZGetDocumentsByIdsRequestSchema = z.object({
|
|
||||||
documentIds: z.array(z.number()).min(1),
|
|
||||||
});
|
|
||||||
|
|
||||||
export const ZGetDocumentsByIdsResponseSchema = z.array(ZDocumentManySchema);
|
|
||||||
|
|
||||||
export type TGetDocumentsByIdsRequest = z.infer<typeof ZGetDocumentsByIdsRequestSchema>;
|
|
||||||
export type TGetDocumentsByIdsResponse = z.infer<typeof ZGetDocumentsByIdsResponseSchema>;
|
|
||||||
@ -19,7 +19,6 @@ import { findDocumentsInternalRoute } from './find-documents-internal';
|
|||||||
import { findInboxRoute } from './find-inbox';
|
import { findInboxRoute } from './find-inbox';
|
||||||
import { getDocumentRoute } from './get-document';
|
import { getDocumentRoute } from './get-document';
|
||||||
import { getDocumentByTokenRoute } from './get-document-by-token';
|
import { getDocumentByTokenRoute } from './get-document-by-token';
|
||||||
import { getDocumentsByIdsRoute } from './get-documents-by-ids';
|
|
||||||
import { getInboxCountRoute } from './get-inbox-count';
|
import { getInboxCountRoute } from './get-inbox-count';
|
||||||
import { redistributeDocumentRoute } from './redistribute-document';
|
import { redistributeDocumentRoute } from './redistribute-document';
|
||||||
import { searchDocumentRoute } from './search-document';
|
import { searchDocumentRoute } from './search-document';
|
||||||
@ -28,7 +27,6 @@ import { updateDocumentRoute } from './update-document';
|
|||||||
|
|
||||||
export const documentRouter = router({
|
export const documentRouter = router({
|
||||||
get: getDocumentRoute,
|
get: getDocumentRoute,
|
||||||
getMany: getDocumentsByIdsRoute,
|
|
||||||
find: findDocumentsRoute,
|
find: findDocumentsRoute,
|
||||||
create: createDocumentRoute,
|
create: createDocumentRoute,
|
||||||
update: updateDocumentRoute,
|
update: updateDocumentRoute,
|
||||||
|
|||||||
@ -0,0 +1,29 @@
|
|||||||
|
import { findEnvelopeAuditLogs } from '@documenso/lib/server-only/document/find-document-audit-logs';
|
||||||
|
|
||||||
|
import { authenticatedProcedure } from '../trpc';
|
||||||
|
import {
|
||||||
|
ZFindEnvelopeAuditLogsRequestSchema,
|
||||||
|
ZFindEnvelopeAuditLogsResponseSchema,
|
||||||
|
findEnvelopeAuditLogsMeta,
|
||||||
|
} from './find-envelope-audit-logs.types';
|
||||||
|
|
||||||
|
export const findEnvelopeAuditLogsRoute = authenticatedProcedure
|
||||||
|
.meta(findEnvelopeAuditLogsMeta)
|
||||||
|
.input(ZFindEnvelopeAuditLogsRequestSchema)
|
||||||
|
.output(ZFindEnvelopeAuditLogsResponseSchema)
|
||||||
|
.query(async ({ input, ctx }) => {
|
||||||
|
const { orderByColumn, orderByDirection, ...auditLogParams } = input;
|
||||||
|
|
||||||
|
ctx.logger.info({
|
||||||
|
input: {
|
||||||
|
envelopeId: input.envelopeId,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
return await findEnvelopeAuditLogs({
|
||||||
|
...auditLogParams,
|
||||||
|
userId: ctx.user.id,
|
||||||
|
teamId: ctx.teamId,
|
||||||
|
orderBy: orderByColumn ? { column: orderByColumn, direction: orderByDirection } : undefined,
|
||||||
|
});
|
||||||
|
});
|
||||||
@ -0,0 +1,35 @@
|
|||||||
|
import { z } from 'zod';
|
||||||
|
|
||||||
|
import { ZDocumentAuditLogSchema } from '@documenso/lib/types/document-audit-logs';
|
||||||
|
import { ZFindResultResponse, ZFindSearchParamsSchema } from '@documenso/lib/types/search-params';
|
||||||
|
|
||||||
|
import type { TrpcRouteMeta } from '../trpc';
|
||||||
|
|
||||||
|
export const findEnvelopeAuditLogsMeta: TrpcRouteMeta = {
|
||||||
|
openapi: {
|
||||||
|
method: 'GET',
|
||||||
|
path: '/envelope/{envelopeId}/audit-log',
|
||||||
|
summary: 'Get envelope audit logs',
|
||||||
|
description:
|
||||||
|
'Returns paginated audit logs for an envelope given an ID. Accepts both envelope IDs (string) and legacy document IDs (number).',
|
||||||
|
tags: ['Envelope'],
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
export const ZFindEnvelopeAuditLogsRequestSchema = ZFindSearchParamsSchema.extend({
|
||||||
|
envelopeId: z
|
||||||
|
.string()
|
||||||
|
.describe('Envelope ID (e.g., envelope_xxx) or legacy document ID (e.g., 12345)'),
|
||||||
|
cursor: z.string().optional(),
|
||||||
|
filterForRecentActivity: z.boolean().optional(),
|
||||||
|
orderByColumn: z.enum(['createdAt', 'type']).optional(),
|
||||||
|
orderByDirection: z.enum(['asc', 'desc']).default('desc'),
|
||||||
|
});
|
||||||
|
|
||||||
|
export const ZFindEnvelopeAuditLogsResponseSchema = ZFindResultResponse.extend({
|
||||||
|
data: ZDocumentAuditLogSchema.array(),
|
||||||
|
nextCursor: z.string().optional(),
|
||||||
|
});
|
||||||
|
|
||||||
|
export type TFindEnvelopeAuditLogsRequest = z.infer<typeof ZFindEnvelopeAuditLogsRequestSchema>;
|
||||||
|
export type TFindEnvelopeAuditLogsResponse = z.infer<typeof ZFindEnvelopeAuditLogsResponseSchema>;
|
||||||
@ -1,93 +0,0 @@
|
|||||||
import { getEnvelopeWhereInput } from '@documenso/lib/server-only/envelope/get-envelope-by-id';
|
|
||||||
import { prisma } from '@documenso/prisma';
|
|
||||||
|
|
||||||
import { authenticatedProcedure } from '../trpc';
|
|
||||||
import {
|
|
||||||
ZGetEnvelopesByIdsRequestSchema,
|
|
||||||
ZGetEnvelopesByIdsResponseSchema,
|
|
||||||
getEnvelopesByIdsMeta,
|
|
||||||
} from './get-envelopes-by-ids.types';
|
|
||||||
|
|
||||||
export const getEnvelopesByIdsRoute = authenticatedProcedure
|
|
||||||
.meta(getEnvelopesByIdsMeta)
|
|
||||||
.input(ZGetEnvelopesByIdsRequestSchema)
|
|
||||||
.output(ZGetEnvelopesByIdsResponseSchema)
|
|
||||||
.mutation(async ({ input, ctx }) => {
|
|
||||||
const { teamId, user } = ctx;
|
|
||||||
const { envelopeIds } = input;
|
|
||||||
|
|
||||||
ctx.logger.info({
|
|
||||||
input: {
|
|
||||||
envelopeIds,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
const { envelopeWhereInput } = await getEnvelopeWhereInput({
|
|
||||||
id: {
|
|
||||||
type: 'envelopeId',
|
|
||||||
id: envelopeIds[0],
|
|
||||||
},
|
|
||||||
userId: user.id,
|
|
||||||
teamId,
|
|
||||||
type: null,
|
|
||||||
});
|
|
||||||
|
|
||||||
const envelopeOrInput = envelopeWhereInput.OR!;
|
|
||||||
|
|
||||||
const envelopes = await prisma.envelope.findMany({
|
|
||||||
where: {
|
|
||||||
id: {
|
|
||||||
in: envelopeIds,
|
|
||||||
},
|
|
||||||
OR: envelopeOrInput,
|
|
||||||
},
|
|
||||||
include: {
|
|
||||||
envelopeItems: {
|
|
||||||
include: {
|
|
||||||
documentData: true,
|
|
||||||
},
|
|
||||||
orderBy: {
|
|
||||||
order: 'asc',
|
|
||||||
},
|
|
||||||
},
|
|
||||||
folder: true,
|
|
||||||
documentMeta: true,
|
|
||||||
user: {
|
|
||||||
select: {
|
|
||||||
id: true,
|
|
||||||
name: true,
|
|
||||||
email: true,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
recipients: {
|
|
||||||
orderBy: {
|
|
||||||
id: 'asc',
|
|
||||||
},
|
|
||||||
},
|
|
||||||
fields: true,
|
|
||||||
team: {
|
|
||||||
select: {
|
|
||||||
id: true,
|
|
||||||
url: true,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
directLink: {
|
|
||||||
select: {
|
|
||||||
directTemplateRecipientId: true,
|
|
||||||
enabled: true,
|
|
||||||
id: true,
|
|
||||||
token: true,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
return envelopes.map((envelope) => ({
|
|
||||||
...envelope,
|
|
||||||
user: {
|
|
||||||
id: envelope.user.id,
|
|
||||||
name: envelope.user.name || '',
|
|
||||||
email: envelope.user.email,
|
|
||||||
},
|
|
||||||
}));
|
|
||||||
});
|
|
||||||
@ -1,24 +0,0 @@
|
|||||||
import { z } from 'zod';
|
|
||||||
|
|
||||||
import { ZEnvelopeSchema } from '@documenso/lib/types/envelope';
|
|
||||||
|
|
||||||
import type { TrpcRouteMeta } from '../trpc';
|
|
||||||
|
|
||||||
export const getEnvelopesByIdsMeta: TrpcRouteMeta = {
|
|
||||||
openapi: {
|
|
||||||
method: 'POST',
|
|
||||||
path: '/envelope/get-many',
|
|
||||||
summary: 'Get multiple envelopes',
|
|
||||||
description: 'Retrieve multiple envelopes by their IDs',
|
|
||||||
tags: ['Envelope'],
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
export const ZGetEnvelopesByIdsRequestSchema = z.object({
|
|
||||||
envelopeIds: z.array(z.string()).min(1),
|
|
||||||
});
|
|
||||||
|
|
||||||
export const ZGetEnvelopesByIdsResponseSchema = z.array(ZEnvelopeSchema);
|
|
||||||
|
|
||||||
export type TGetEnvelopesByIdsRequest = z.infer<typeof ZGetEnvelopesByIdsRequestSchema>;
|
|
||||||
export type TGetEnvelopesByIdsResponse = z.infer<typeof ZGetEnvelopesByIdsResponseSchema>;
|
|
||||||
@ -18,10 +18,10 @@ import { createEnvelopeRecipientsRoute } from './envelope-recipients/create-enve
|
|||||||
import { deleteEnvelopeRecipientRoute } from './envelope-recipients/delete-envelope-recipient';
|
import { deleteEnvelopeRecipientRoute } from './envelope-recipients/delete-envelope-recipient';
|
||||||
import { getEnvelopeRecipientRoute } from './envelope-recipients/get-envelope-recipient';
|
import { getEnvelopeRecipientRoute } from './envelope-recipients/get-envelope-recipient';
|
||||||
import { updateEnvelopeRecipientsRoute } from './envelope-recipients/update-envelope-recipients';
|
import { updateEnvelopeRecipientsRoute } from './envelope-recipients/update-envelope-recipients';
|
||||||
|
import { findEnvelopeAuditLogsRoute } from './find-envelope-audit-logs';
|
||||||
import { getEnvelopeRoute } from './get-envelope';
|
import { getEnvelopeRoute } from './get-envelope';
|
||||||
import { getEnvelopeItemsRoute } from './get-envelope-items';
|
import { getEnvelopeItemsRoute } from './get-envelope-items';
|
||||||
import { getEnvelopeItemsByTokenRoute } from './get-envelope-items-by-token';
|
import { getEnvelopeItemsByTokenRoute } from './get-envelope-items-by-token';
|
||||||
import { getEnvelopesByIdsRoute } from './get-envelopes-by-ids';
|
|
||||||
import { redistributeEnvelopeRoute } from './redistribute-envelope';
|
import { redistributeEnvelopeRoute } from './redistribute-envelope';
|
||||||
import { setEnvelopeFieldsRoute } from './set-envelope-fields';
|
import { setEnvelopeFieldsRoute } from './set-envelope-fields';
|
||||||
import { setEnvelopeRecipientsRoute } from './set-envelope-recipients';
|
import { setEnvelopeRecipientsRoute } from './set-envelope-recipients';
|
||||||
@ -66,8 +66,10 @@ export const envelopeRouter = router({
|
|||||||
set: setEnvelopeFieldsRoute,
|
set: setEnvelopeFieldsRoute,
|
||||||
sign: signEnvelopeFieldRoute,
|
sign: signEnvelopeFieldRoute,
|
||||||
},
|
},
|
||||||
|
auditLog: {
|
||||||
|
find: findEnvelopeAuditLogsRoute,
|
||||||
|
},
|
||||||
get: getEnvelopeRoute,
|
get: getEnvelopeRoute,
|
||||||
getMany: getEnvelopesByIdsRoute,
|
|
||||||
create: createEnvelopeRoute,
|
create: createEnvelopeRoute,
|
||||||
use: useEnvelopeRoute,
|
use: useEnvelopeRoute,
|
||||||
update: updateEnvelopeRoute,
|
update: updateEnvelopeRoute,
|
||||||
|
|||||||
@ -1,127 +0,0 @@
|
|||||||
import { EnvelopeType } from '@prisma/client';
|
|
||||||
|
|
||||||
import { getEnvelopeWhereInput } from '@documenso/lib/server-only/envelope/get-envelope-by-id';
|
|
||||||
import {
|
|
||||||
mapSecondaryIdToTemplateId,
|
|
||||||
mapTemplateIdToSecondaryId,
|
|
||||||
} from '@documenso/lib/utils/envelope';
|
|
||||||
import { mapFieldToLegacyField } from '@documenso/lib/utils/fields';
|
|
||||||
import { mapRecipientToLegacyRecipient } from '@documenso/lib/utils/recipients';
|
|
||||||
import { prisma } from '@documenso/prisma';
|
|
||||||
|
|
||||||
import { authenticatedProcedure } from '../trpc';
|
|
||||||
import {
|
|
||||||
ZGetTemplatesByIdsRequestSchema,
|
|
||||||
ZGetTemplatesByIdsResponseSchema,
|
|
||||||
getTemplatesByIdsMeta,
|
|
||||||
} from './get-templates-by-ids.types';
|
|
||||||
|
|
||||||
export const getTemplatesByIdsRoute = authenticatedProcedure
|
|
||||||
.meta(getTemplatesByIdsMeta)
|
|
||||||
.input(ZGetTemplatesByIdsRequestSchema)
|
|
||||||
.output(ZGetTemplatesByIdsResponseSchema)
|
|
||||||
.mutation(async ({ input, ctx }) => {
|
|
||||||
const { teamId, user } = ctx;
|
|
||||||
const { templateIds } = input;
|
|
||||||
|
|
||||||
ctx.logger.info({
|
|
||||||
input: {
|
|
||||||
templateIds,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
const { envelopeWhereInput } = await getEnvelopeWhereInput({
|
|
||||||
id: {
|
|
||||||
type: 'templateId',
|
|
||||||
id: templateIds[0],
|
|
||||||
},
|
|
||||||
userId: user.id,
|
|
||||||
teamId,
|
|
||||||
type: EnvelopeType.TEMPLATE,
|
|
||||||
});
|
|
||||||
|
|
||||||
const envelopeOrInput = envelopeWhereInput.OR!;
|
|
||||||
|
|
||||||
const secondaryIds = templateIds.map((templateId) => mapTemplateIdToSecondaryId(templateId));
|
|
||||||
|
|
||||||
const envelopes = await prisma.envelope.findMany({
|
|
||||||
where: {
|
|
||||||
type: EnvelopeType.TEMPLATE,
|
|
||||||
secondaryId: {
|
|
||||||
in: secondaryIds,
|
|
||||||
},
|
|
||||||
OR: envelopeOrInput,
|
|
||||||
},
|
|
||||||
include: {
|
|
||||||
recipients: {
|
|
||||||
orderBy: {
|
|
||||||
id: 'asc',
|
|
||||||
},
|
|
||||||
},
|
|
||||||
fields: true,
|
|
||||||
team: {
|
|
||||||
select: {
|
|
||||||
id: true,
|
|
||||||
url: true,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
documentMeta: {
|
|
||||||
select: {
|
|
||||||
signingOrder: true,
|
|
||||||
distributionMethod: true,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
directLink: {
|
|
||||||
select: {
|
|
||||||
token: true,
|
|
||||||
enabled: true,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
return envelopes.map((envelope) => {
|
|
||||||
const legacyTemplateId = mapSecondaryIdToTemplateId(envelope.secondaryId);
|
|
||||||
|
|
||||||
return {
|
|
||||||
id: legacyTemplateId,
|
|
||||||
envelopeId: envelope.id,
|
|
||||||
type: envelope.templateType,
|
|
||||||
visibility: envelope.visibility,
|
|
||||||
externalId: envelope.externalId,
|
|
||||||
title: envelope.title,
|
|
||||||
userId: envelope.userId,
|
|
||||||
teamId: envelope.teamId,
|
|
||||||
authOptions: envelope.authOptions,
|
|
||||||
createdAt: envelope.createdAt,
|
|
||||||
updatedAt: envelope.updatedAt,
|
|
||||||
publicTitle: envelope.publicTitle,
|
|
||||||
publicDescription: envelope.publicDescription,
|
|
||||||
folderId: envelope.folderId,
|
|
||||||
useLegacyFieldInsertion: envelope.useLegacyFieldInsertion,
|
|
||||||
team: envelope.team
|
|
||||||
? {
|
|
||||||
id: envelope.team.id,
|
|
||||||
url: envelope.team.url,
|
|
||||||
}
|
|
||||||
: null,
|
|
||||||
fields: envelope.fields.map((field) => mapFieldToLegacyField(field, envelope)),
|
|
||||||
recipients: envelope.recipients.map((recipient) =>
|
|
||||||
mapRecipientToLegacyRecipient(recipient, envelope),
|
|
||||||
),
|
|
||||||
templateMeta: envelope.documentMeta
|
|
||||||
? {
|
|
||||||
signingOrder: envelope.documentMeta.signingOrder,
|
|
||||||
distributionMethod: envelope.documentMeta.distributionMethod,
|
|
||||||
}
|
|
||||||
: null,
|
|
||||||
directLink: envelope.directLink
|
|
||||||
? {
|
|
||||||
token: envelope.directLink.token,
|
|
||||||
enabled: envelope.directLink.enabled,
|
|
||||||
}
|
|
||||||
: null,
|
|
||||||
templateDocumentDataId: '', // Backwards compatibility.
|
|
||||||
};
|
|
||||||
});
|
|
||||||
});
|
|
||||||
@ -1,24 +0,0 @@
|
|||||||
import { z } from 'zod';
|
|
||||||
|
|
||||||
import { ZTemplateManySchema } from '@documenso/lib/types/template';
|
|
||||||
|
|
||||||
import type { TrpcRouteMeta } from '../trpc';
|
|
||||||
|
|
||||||
export const getTemplatesByIdsMeta: TrpcRouteMeta = {
|
|
||||||
openapi: {
|
|
||||||
method: 'POST',
|
|
||||||
path: '/template/get-many',
|
|
||||||
summary: 'Get multiple templates',
|
|
||||||
description: 'Retrieve multiple templates by their IDs',
|
|
||||||
tags: ['Template'],
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
export const ZGetTemplatesByIdsRequestSchema = z.object({
|
|
||||||
templateIds: z.array(z.number()).min(1),
|
|
||||||
});
|
|
||||||
|
|
||||||
export const ZGetTemplatesByIdsResponseSchema = z.array(ZTemplateManySchema);
|
|
||||||
|
|
||||||
export type TGetTemplatesByIdsRequest = z.infer<typeof ZGetTemplatesByIdsRequestSchema>;
|
|
||||||
export type TGetTemplatesByIdsResponse = z.infer<typeof ZGetTemplatesByIdsResponseSchema>;
|
|
||||||
@ -30,7 +30,6 @@ import { mapEnvelopeToTemplateLite } from '@documenso/lib/utils/templates';
|
|||||||
|
|
||||||
import { ZGenericSuccessResponse, ZSuccessResponseSchema } from '../schema';
|
import { ZGenericSuccessResponse, ZSuccessResponseSchema } from '../schema';
|
||||||
import { authenticatedProcedure, maybeAuthenticatedProcedure, router } from '../trpc';
|
import { authenticatedProcedure, maybeAuthenticatedProcedure, router } from '../trpc';
|
||||||
import { getTemplatesByIdsRoute } from './get-templates-by-ids';
|
|
||||||
import {
|
import {
|
||||||
ZBulkSendTemplateMutationSchema,
|
ZBulkSendTemplateMutationSchema,
|
||||||
ZCreateDocumentFromDirectTemplateRequestSchema,
|
ZCreateDocumentFromDirectTemplateRequestSchema,
|
||||||
@ -155,11 +154,6 @@ export const templateRouter = router({
|
|||||||
});
|
});
|
||||||
}),
|
}),
|
||||||
|
|
||||||
/**
|
|
||||||
* @public
|
|
||||||
*/
|
|
||||||
getMany: getTemplatesByIdsRoute,
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Wait until RR7 so we can passthrough documents.
|
* Wait until RR7 so we can passthrough documents.
|
||||||
*
|
*
|
||||||
|
|||||||
Reference in New Issue
Block a user