Files
documenso/packages/trpc/server/admin-router/find-document-jobs.ts
David Nguyen 7f09ba72f4 feat: add envelopes (#2025)
This PR is handles the changes required to support envelopes. The new
envelope editor/signing page will be hidden during release.

The core changes here is to migrate the documents and templates model to
a centralized envelopes model.

Even though Documents and Templates are removed, from the user
perspective they will still exist as we remap envelopes to documents and
templates.
2025-10-14 21:56:36 +11:00

73 lines
1.9 KiB
TypeScript

import { EnvelopeType } from '@prisma/client';
import { AppError, AppErrorCode } from '@documenso/lib/errors/app-error';
import type { FindResultResponse } from '@documenso/lib/types/search-params';
import {
mapSecondaryIdToDocumentId,
unsafeBuildEnvelopeIdQuery,
} from '@documenso/lib/utils/envelope';
import { prisma } from '@documenso/prisma';
import { adminProcedure } from '../trpc';
import {
ZFindDocumentJobsRequestSchema,
ZFindDocumentJobsResponseSchema,
} from './find-document-jobs.types';
export const findDocumentJobsRoute = adminProcedure
.input(ZFindDocumentJobsRequestSchema)
.output(ZFindDocumentJobsResponseSchema)
.query(async ({ input }) => {
const { envelopeId, page = 1, perPage = 5 } = input;
const envelope = await prisma.envelope.findFirst({
where: unsafeBuildEnvelopeIdQuery(
{
type: 'envelopeId',
id: envelopeId,
},
EnvelopeType.DOCUMENT,
),
});
if (!envelope) {
throw new AppError(AppErrorCode.NOT_FOUND, {
message: 'Envelope not found',
});
}
const [data, count] = await Promise.all([
prisma.backgroundJob.findMany({
where: {
jobId: 'internal.seal-document',
payload: {
path: ['documentId'],
equals: mapSecondaryIdToDocumentId(envelope.secondaryId),
},
},
skip: Math.max(page - 1, 0) * perPage,
take: perPage,
orderBy: {
submittedAt: 'desc',
},
}),
prisma.backgroundJob.count({
where: {
jobId: 'internal.seal-document',
payload: {
path: ['documentId'],
equals: mapSecondaryIdToDocumentId(envelope.secondaryId),
},
},
}),
]);
return {
data,
count,
currentPage: Math.max(page, 1),
perPage,
totalPages: Math.ceil(count / perPage),
} satisfies FindResultResponse<typeof data>;
});