mirror of
https://github.com/documenso/documenso.git
synced 2026-07-13 22:37:24 +10:00
17b261df1f
This PR fixes a bug in the `/api/v2/template` endpoint where the pagination parameter `perPage` was being ignored. Previously, the endpoint would return all matching templates regardless of the requested limit, which could lead to performance issues and incorrect API behavior.
104 lines
2.1 KiB
TypeScript
104 lines
2.1 KiB
TypeScript
import type { TemplateType } from '@prisma/client';
|
|
import { EnvelopeType, type Prisma } from '@prisma/client';
|
|
|
|
import { prisma } from '@documenso/prisma';
|
|
|
|
import { TEAM_DOCUMENT_VISIBILITY_MAP } from '../../constants/teams';
|
|
import { type FindResultResponse } from '../../types/search-params';
|
|
import { getMemberRoles } from '../team/get-member-roles';
|
|
|
|
export type FindTemplatesOptions = {
|
|
userId: number;
|
|
teamId: number;
|
|
type?: TemplateType;
|
|
page?: number;
|
|
perPage?: number;
|
|
folderId?: string;
|
|
};
|
|
|
|
export const findTemplates = async ({
|
|
userId,
|
|
teamId,
|
|
type,
|
|
page = 1,
|
|
perPage = 10,
|
|
folderId,
|
|
}: FindTemplatesOptions) => {
|
|
const whereFilter: Prisma.EnvelopeWhereInput[] = [];
|
|
|
|
const { teamRole } = await getMemberRoles({
|
|
teamId,
|
|
reference: {
|
|
type: 'User',
|
|
id: userId,
|
|
},
|
|
});
|
|
|
|
whereFilter.push(
|
|
{ teamId },
|
|
{
|
|
OR: [
|
|
{
|
|
visibility: {
|
|
in: TEAM_DOCUMENT_VISIBILITY_MAP[teamRole],
|
|
},
|
|
},
|
|
{ userId, teamId },
|
|
],
|
|
},
|
|
);
|
|
|
|
if (folderId) {
|
|
whereFilter.push({ folderId });
|
|
} else {
|
|
whereFilter.push({ folderId: null });
|
|
}
|
|
|
|
const [data, count] = await Promise.all([
|
|
prisma.envelope.findMany({
|
|
where: {
|
|
type: EnvelopeType.TEMPLATE,
|
|
templateType: type,
|
|
AND: whereFilter,
|
|
},
|
|
include: {
|
|
team: {
|
|
select: {
|
|
id: true,
|
|
url: true,
|
|
},
|
|
},
|
|
fields: true,
|
|
recipients: true,
|
|
documentMeta: true,
|
|
directLink: {
|
|
select: {
|
|
token: true,
|
|
enabled: true,
|
|
},
|
|
},
|
|
},
|
|
skip: Math.max(page - 1, 0) * perPage,
|
|
take: perPage,
|
|
orderBy: {
|
|
createdAt: 'desc',
|
|
},
|
|
}),
|
|
prisma.envelope.count({
|
|
where: {
|
|
type: EnvelopeType.TEMPLATE,
|
|
templateType: type,
|
|
AND: whereFilter,
|
|
},
|
|
}),
|
|
]);
|
|
|
|
return {
|
|
data,
|
|
count,
|
|
currentPage: Math.max(page, 1),
|
|
perPage,
|
|
totalPages: Math.ceil(count / perPage),
|
|
} satisfies FindResultResponse<typeof data>;
|
|
};
|