Files
sign/packages/lib/server-only/user/get-all-users.ts

58 lines
1.1 KiB
TypeScript
Raw Normal View History

2023-09-21 12:43:36 +01:00
import { prisma } from '@documenso/prisma';
2023-10-13 11:48:52 +03:00
import { Prisma } from '@documenso/prisma/client';
2023-09-21 12:43:36 +01:00
2023-10-11 12:32:33 +03:00
type GetAllUsersProps = {
2023-10-06 15:48:05 +03:00
username: string;
email: string;
2023-09-21 12:43:36 +01:00
page: number;
perPage: number;
};
2023-10-06 15:48:05 +03:00
export const findUsers = async ({
username = '',
email = '',
page = 1,
perPage = 10,
2023-10-11 12:32:33 +03:00
}: GetAllUsersProps) => {
2023-10-06 15:48:05 +03:00
const whereClause = Prisma.validator<Prisma.UserWhereInput>()({
OR: [
{
name: {
contains: username,
mode: 'insensitive',
},
},
{
email: {
contains: email,
mode: 'insensitive',
},
},
],
});
2023-09-21 12:43:36 +01:00
const [users, count] = await Promise.all([
await prisma.user.findMany({
2023-10-13 12:16:07 +03:00
include: {
Subscription: true,
2023-09-29 17:12:02 +01:00
Document: {
select: {
id: true,
},
},
2023-09-21 12:43:36 +01:00
},
2023-10-06 15:48:05 +03:00
where: whereClause,
2023-09-21 12:43:36 +01:00
skip: Math.max(page - 1, 0) * perPage,
take: perPage,
}),
2023-10-06 15:48:05 +03:00
await prisma.user.count({
where: whereClause,
}),
2023-09-21 12:43:36 +01:00
]);
return {
users,
totalPages: Math.ceil(count / perPage),
};
};