Files
documenso/packages/lib/server-only/admin/admin-find-documents.ts
T
ephraimduncan 138d663c25 chore: merge main, resolve biome formatting conflicts
Merge origin/main into feat/external-2fa-codes. Resolve formatting
conflicts caused by biome rollout; preserve both feature streams:
PR's external 2FA token + signing-session 2FA proof additions plus
main's RateLimit/RecipientExpired/signingReminders/date-auto-insert.

In complete-document-with-token.ts, drop the duplicate early
field-fetching block introduced when main moved that logic later
with date auto-insert support; keep the EXTERNAL_TWO_FACTOR_AUTH
check using derivedRecipientActionAuth.
2026-05-12 11:46:11 +00:00

102 lines
2.0 KiB
TypeScript

import { prisma } from '@documenso/prisma';
import { EnvelopeType, type Prisma } from '@prisma/client';
import type { FindResultResponse } from '../../types/search-params';
export interface AdminFindDocumentsOptions {
query?: string;
page?: number;
perPage?: number;
}
export const adminFindDocuments = async ({ query, page = 1, perPage = 10 }: AdminFindDocumentsOptions) => {
let termFilters: Prisma.EnvelopeWhereInput | undefined = !query
? undefined
: {
title: {
contains: query,
mode: 'insensitive',
},
};
if (query && query.startsWith('envelope_')) {
termFilters = {
id: {
equals: query,
},
};
}
if (query && query.startsWith('document_')) {
termFilters = {
secondaryId: {
equals: query,
},
};
}
if (query) {
const isQueryAnInteger = !isNaN(parseInt(query));
if (isQueryAnInteger) {
termFilters = {
secondaryId: {
equals: `document_${query}`,
},
};
}
}
const [data, count] = await Promise.all([
prisma.envelope.findMany({
where: {
type: EnvelopeType.DOCUMENT,
...termFilters,
},
skip: Math.max(page - 1, 0) * perPage,
take: perPage,
orderBy: {
createdAt: 'desc',
},
include: {
user: {
select: {
id: true,
name: true,
email: true,
},
},
recipients: true,
team: {
select: {
id: true,
url: true,
},
},
envelopeItems: {
select: {
id: true,
envelopeId: true,
title: true,
order: true,
},
},
},
}),
prisma.envelope.count({
where: {
type: EnvelopeType.DOCUMENT,
...termFilters,
},
}),
]);
return {
data,
count,
currentPage: Math.max(page, 1),
perPage,
totalPages: Math.ceil(count / perPage),
} satisfies FindResultResponse<typeof data>;
};