Files
calendar/packages/trpc/server/routers/viewer/admin/listPaginated.handler.ts
T
Hariom BalharaandGitHub bf45dcf139 fix: More instances of wrong avatar URL fixed (#10519)
* Fix user.avatar

* Fix avtars in admin users listing

* Handle unpublished org
2023-08-03 15:32:38 +00:00

70 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;
};
export const listPaginatedHandler = async ({ ctx, 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,
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,
},
};
};