fosrl.pangolin/server/routers/user/listUsers.ts

141 lines
4 KiB
TypeScript
Raw Normal View History

2024-11-02 16:12:20 -04:00
import { Request, Response, NextFunction } from "express";
import { z } from "zod";
import { db } from "@server/db";
2025-06-04 12:02:07 -04:00
import { idp, roles, userOrgs, users } from "@server/db";
2025-01-01 21:41:31 -05:00
import response from "@server/lib/response";
2024-11-02 16:12:20 -04:00
import HttpCode from "@server/types/HttpCode";
import createHttpError from "http-errors";
import { and, sql } from "drizzle-orm";
2024-11-02 16:12:20 -04:00
import logger from "@server/logger";
import { fromZodError } from "zod-validation-error";
2025-04-06 22:44:14 -04:00
import { OpenAPITags, registry } from "@server/openApi";
2025-04-13 17:57:27 -04:00
import { eq } from "drizzle-orm";
2024-10-02 21:17:38 -04:00
2024-12-21 21:01:12 -05:00
const listUsersParamsSchema = z
.object({
orgId: z.string()
})
.strict();
2024-10-12 22:31:24 -04:00
2024-12-21 21:01:12 -05:00
const listUsersSchema = z
.object({
limit: z
.string()
.optional()
.default("1000")
.transform(Number)
.pipe(z.number().int().nonnegative()),
offset: z
.string()
.optional()
.default("0")
.transform(Number)
.pipe(z.number().int().nonnegative())
})
.strict();
2024-10-02 21:17:38 -04:00
2024-11-02 16:12:20 -04:00
async function queryUsers(orgId: string, limit: number, offset: number) {
return await db
.select({
id: users.userId,
email: users.email,
emailVerified: users.emailVerified,
dateCreated: users.dateCreated,
orgId: userOrgs.orgId,
2025-04-13 17:57:27 -04:00
username: users.username,
name: users.name,
type: users.type,
2024-11-02 16:12:20 -04:00
roleId: userOrgs.roleId,
roleName: roles.name,
2025-04-13 17:57:27 -04:00
isOwner: userOrgs.isOwner,
idpName: idp.name,
idpId: users.idpId
2024-11-02 16:12:20 -04:00
})
.from(users)
2025-04-13 17:57:27 -04:00
.leftJoin(userOrgs, eq(users.userId, userOrgs.userId))
.leftJoin(roles, eq(userOrgs.roleId, roles.roleId))
.leftJoin(idp, eq(users.idpId, idp.idpId))
.where(eq(userOrgs.orgId, orgId))
2024-11-02 16:12:20 -04:00
.limit(limit)
.offset(offset);
}
export type ListUsersResponse = {
users: NonNullable<Awaited<ReturnType<typeof queryUsers>>>;
pagination: { total: number; limit: number; offset: number };
};
2025-04-06 22:44:14 -04:00
registry.registerPath({
method: "get",
path: "/org/{orgId}/users",
description: "List users in an organization.",
tags: [OpenAPITags.Org, OpenAPITags.User],
request: {
params: listUsersParamsSchema,
query: listUsersSchema
},
responses: {}
});
2024-11-02 16:12:20 -04:00
export async function listUsers(
req: Request,
res: Response,
next: NextFunction
): Promise<any> {
2024-10-12 22:31:24 -04:00
try {
const parsedQuery = listUsersSchema.safeParse(req.query);
if (!parsedQuery.success) {
return next(
createHttpError(
HttpCode.BAD_REQUEST,
fromZodError(parsedQuery.error)
2024-10-12 22:31:24 -04:00
)
);
}
const { limit, offset } = parsedQuery.data;
2024-10-06 16:43:59 -04:00
2024-10-12 22:31:24 -04:00
const parsedParams = listUsersParamsSchema.safeParse(req.params);
if (!parsedParams.success) {
return next(
createHttpError(
HttpCode.BAD_REQUEST,
fromZodError(parsedParams.error)
2024-10-12 22:31:24 -04:00
)
);
}
2024-10-02 21:17:38 -04:00
2024-10-12 22:31:24 -04:00
const { orgId } = parsedParams.data;
2024-10-02 21:17:38 -04:00
2024-11-02 16:12:20 -04:00
const usersWithRoles = await queryUsers(
orgId.toString(),
limit,
offset
);
2024-10-12 22:31:24 -04:00
const [{ count }] = await db
.select({ count: sql<number>`count(*)` })
2025-04-18 11:36:34 -04:00
.from(userOrgs)
.where(eq(userOrgs.orgId, orgId));
2024-10-12 22:31:24 -04:00
2024-11-02 16:12:20 -04:00
return response<ListUsersResponse>(res, {
2024-10-12 22:31:24 -04:00
data: {
users: usersWithRoles,
pagination: {
total: count,
limit,
2024-12-21 21:01:12 -05:00
offset
}
2024-10-12 22:31:24 -04:00
},
success: true,
error: false,
message: "Users retrieved successfully",
2024-12-21 21:01:12 -05:00
status: HttpCode.OK
2024-10-12 22:31:24 -04:00
});
} catch (error) {
logger.error(error);
2024-11-02 16:12:20 -04:00
return next(
createHttpError(HttpCode.INTERNAL_SERVER_ERROR, "An error occurred")
);
2024-10-12 22:31:24 -04:00
}
2024-10-13 17:13:47 -04:00
}