mirror of
https://github.com/documenso/documenso.git
synced 2025-11-10 04:22:32 +10:00
70 lines
1.7 KiB
TypeScript
70 lines
1.7 KiB
TypeScript
import type { z } from 'zod';
|
|
|
|
import { prisma } from '@documenso/prisma';
|
|
import type { Team } from '@documenso/prisma/client';
|
|
import { Prisma } from '@documenso/prisma/client';
|
|
import { TeamPendingSchema } from '@documenso/prisma/generated/zod';
|
|
|
|
import { type FindResultResponse, ZFindResultResponse } from '../../types/search-params';
|
|
|
|
export interface FindTeamsPendingOptions {
|
|
userId: string;
|
|
query?: string;
|
|
page?: number;
|
|
perPage?: number;
|
|
orderBy?: {
|
|
column: keyof Team;
|
|
direction: 'asc' | 'desc';
|
|
};
|
|
}
|
|
|
|
export const ZFindTeamsPendingResponseSchema = ZFindResultResponse.extend({
|
|
data: TeamPendingSchema.array(),
|
|
});
|
|
|
|
export type TFindTeamsPendingResponse = z.infer<typeof ZFindTeamsPendingResponseSchema>;
|
|
|
|
export const findTeamsPending = async ({
|
|
userId,
|
|
query,
|
|
page = 1,
|
|
perPage = 10,
|
|
orderBy,
|
|
}: FindTeamsPendingOptions): Promise<TFindTeamsPendingResponse> => {
|
|
const orderByColumn = orderBy?.column ?? 'name';
|
|
const orderByDirection = orderBy?.direction ?? 'desc';
|
|
|
|
const whereClause: Prisma.TeamPendingWhereInput = {
|
|
ownerUserId: userId,
|
|
};
|
|
|
|
if (query && query.length > 0) {
|
|
whereClause.name = {
|
|
contains: query,
|
|
mode: Prisma.QueryMode.insensitive,
|
|
};
|
|
}
|
|
|
|
const [data, count] = await Promise.all([
|
|
prisma.teamPending.findMany({
|
|
where: whereClause,
|
|
skip: Math.max(page - 1, 0) * perPage,
|
|
take: perPage,
|
|
orderBy: {
|
|
[orderByColumn]: orderByDirection,
|
|
},
|
|
}),
|
|
prisma.teamPending.count({
|
|
where: whereClause,
|
|
}),
|
|
]);
|
|
|
|
return {
|
|
data,
|
|
count,
|
|
currentPage: Math.max(page, 1),
|
|
perPage,
|
|
totalPages: Math.ceil(count / perPage),
|
|
} satisfies FindResultResponse<typeof data>;
|
|
};
|