Files
calendar/packages/trpc/server/routers/viewer/admin/listPaginated.handler.ts
T
c818ef3188 feat: implements basic user locking for admins (#12393)
* feat: implements basic user locking for admins

* Update sendPasswordReset.handler.ts

* check fixes

* Update packages/features/ee/users/components/UsersTable.tsx

Co-authored-by: sean-brydon <55134778+sean-brydon@users.noreply.github.com>

---------

Co-authored-by: sean-brydon <55134778+sean-brydon@users.noreply.github.com>
2023-11-17 16:06:29 +00:00

73 lines
1.5 KiB
TypeScript

import { prisma } from "@calcom/prisma";
import type { Prisma } from "@calcom/prisma/client";
import type { TrpcSessionUser } from "../../../trpc";
import type { TListMembersSchema } from "./listPaginated.schema";
type GetOptions = {
ctx: {
user: NonNullable<TrpcSessionUser>;
};
input: TListMembersSchema;
};
const listPaginatedHandler = async ({ input }: GetOptions) => {
const { cursor, limit, searchTerm } = input;
const getTotalUsers = await prisma.user.count();
let searchFilters: Prisma.UserWhereInput = {};
if (searchTerm) {
searchFilters = {
OR: [
{
email: {
contains: searchTerm.toLowerCase(),
},
},
{
username: {
contains: searchTerm.toLocaleLowerCase(),
},
},
],
};
}
const users = await prisma.user.findMany({
cursor: cursor ? { id: cursor } : undefined,
take: limit + 1, // We take +1 as itll be used for the next cursor
where: searchFilters,
orderBy: {
id: "asc",
},
select: {
id: true,
locked: true,
email: true,
username: true,
name: true,
timeZone: true,
role: true,
organizationId: true,
},
});
let nextCursor: typeof cursor | undefined = undefined;
if (users && users.length > limit) {
const nextItem = users.pop();
nextCursor = nextItem?.id;
}
return {
rows: users || [],
nextCursor,
meta: {
totalRowCount: getTotalUsers || 0,
},
};
};
export default listPaginatedHandler;