2024-10-19 15:49:16 -04:00
|
|
|
import { Request, Response, NextFunction } from "express";
|
|
|
|
import { z } from "zod";
|
|
|
|
import { db } from "@server/db";
|
|
|
|
import { Org, orgs } from "@server/db/schema";
|
2024-10-02 21:17:38 -04:00
|
|
|
import response from "@server/utils/response";
|
2024-10-19 15:49:16 -04:00
|
|
|
import HttpCode from "@server/types/HttpCode";
|
|
|
|
import createHttpError from "http-errors";
|
|
|
|
import { sql, inArray } from "drizzle-orm";
|
|
|
|
import { ActionsEnum, checkUserActionPermission } from "@server/auth/actions";
|
|
|
|
import logger from "@server/logger";
|
2024-10-02 21:17:38 -04:00
|
|
|
|
|
|
|
const listOrgsSchema = z.object({
|
2024-10-19 15:49:16 -04:00
|
|
|
limit: z
|
|
|
|
.string()
|
|
|
|
.optional()
|
|
|
|
.transform(Number)
|
|
|
|
.pipe(z.number().int().positive().default(10)),
|
|
|
|
offset: z
|
|
|
|
.string()
|
|
|
|
.optional()
|
|
|
|
.transform(Number)
|
|
|
|
.pipe(z.number().int().nonnegative().default(0)),
|
2024-10-02 21:17:38 -04:00
|
|
|
});
|
|
|
|
|
2024-10-19 15:49:16 -04:00
|
|
|
export type ListOrgsResponse = {
|
|
|
|
organizations: Org[];
|
|
|
|
pagination: { total: number; limit: number; offset: number };
|
|
|
|
};
|
|
|
|
|
|
|
|
export async function listOrgs(
|
|
|
|
req: Request,
|
|
|
|
res: Response,
|
|
|
|
next: NextFunction,
|
|
|
|
): Promise<any> {
|
2024-10-06 18:05:20 -04:00
|
|
|
try {
|
|
|
|
const parsedQuery = listOrgsSchema.safeParse(req.query);
|
|
|
|
if (!parsedQuery.success) {
|
|
|
|
return next(
|
|
|
|
createHttpError(
|
|
|
|
HttpCode.BAD_REQUEST,
|
2024-10-19 15:49:16 -04:00
|
|
|
parsedQuery.error.errors.map((e) => e.message).join(", "),
|
|
|
|
),
|
2024-10-06 18:05:20 -04:00
|
|
|
);
|
|
|
|
}
|
2024-10-02 21:17:38 -04:00
|
|
|
|
2024-10-06 18:05:20 -04:00
|
|
|
const { limit, offset } = parsedQuery.data;
|
2024-10-02 21:17:38 -04:00
|
|
|
|
2024-10-06 18:05:20 -04:00
|
|
|
// Check if the user has permission to list sites
|
2024-10-19 15:49:16 -04:00
|
|
|
const hasPermission = await checkUserActionPermission(
|
|
|
|
ActionsEnum.listOrgs,
|
|
|
|
req,
|
|
|
|
);
|
2024-10-06 18:05:20 -04:00
|
|
|
if (!hasPermission) {
|
2024-10-19 15:49:16 -04:00
|
|
|
return next(
|
|
|
|
createHttpError(
|
|
|
|
HttpCode.FORBIDDEN,
|
|
|
|
"User does not have permission to perform this action",
|
|
|
|
),
|
|
|
|
);
|
2024-10-06 18:05:20 -04:00
|
|
|
}
|
2024-10-06 16:43:59 -04:00
|
|
|
|
2024-10-06 18:05:20 -04:00
|
|
|
// Use the userOrgs passed from the middleware
|
|
|
|
const userOrgIds = req.userOrgIds;
|
2024-10-03 22:31:20 -04:00
|
|
|
|
2024-10-06 18:05:20 -04:00
|
|
|
if (!userOrgIds || userOrgIds.length === 0) {
|
2024-10-19 15:49:16 -04:00
|
|
|
return response<ListOrgsResponse>(res, {
|
|
|
|
data: {
|
|
|
|
organizations: [],
|
|
|
|
pagination: {
|
|
|
|
total: 0,
|
|
|
|
limit,
|
|
|
|
offset,
|
2024-10-06 18:05:20 -04:00
|
|
|
},
|
2024-10-19 15:49:16 -04:00
|
|
|
},
|
|
|
|
success: true,
|
|
|
|
error: false,
|
|
|
|
message: "No organizations found for the user",
|
|
|
|
status: HttpCode.OK,
|
|
|
|
});
|
2024-10-06 18:05:20 -04:00
|
|
|
}
|
2024-10-03 22:31:20 -04:00
|
|
|
|
2024-10-19 15:49:16 -04:00
|
|
|
const organizations = await db
|
|
|
|
.select()
|
2024-10-06 18:05:20 -04:00
|
|
|
.from(orgs)
|
|
|
|
.where(inArray(orgs.orgId, userOrgIds))
|
|
|
|
.limit(limit)
|
|
|
|
.offset(offset);
|
2024-10-02 21:17:38 -04:00
|
|
|
|
2024-10-19 15:49:16 -04:00
|
|
|
const totalCountResult = await db
|
|
|
|
.select({ count: sql<number>`cast(count(*) as integer)` })
|
2024-10-06 18:05:20 -04:00
|
|
|
.from(orgs)
|
|
|
|
.where(inArray(orgs.orgId, userOrgIds));
|
|
|
|
const totalCount = totalCountResult[0].count;
|
2024-10-02 21:17:38 -04:00
|
|
|
|
2024-10-19 15:49:16 -04:00
|
|
|
return response<ListOrgsResponse>(res, {
|
2024-10-06 18:05:20 -04:00
|
|
|
data: {
|
|
|
|
organizations,
|
|
|
|
pagination: {
|
|
|
|
total: totalCount,
|
|
|
|
limit,
|
|
|
|
offset,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
success: true,
|
|
|
|
error: false,
|
|
|
|
message: "Organizations retrieved successfully",
|
|
|
|
status: HttpCode.OK,
|
|
|
|
});
|
|
|
|
} catch (error) {
|
|
|
|
logger.error(error);
|
2024-10-19 15:49:16 -04:00
|
|
|
return next(
|
|
|
|
createHttpError(
|
|
|
|
HttpCode.INTERNAL_SERVER_ERROR,
|
|
|
|
"An error occurred...",
|
|
|
|
),
|
|
|
|
);
|
2024-10-06 18:05:20 -04:00
|
|
|
}
|
2024-10-17 00:29:18 -04:00
|
|
|
}
|