2024-10-02 21:17:38 -04:00
|
|
|
import { Request, Response, NextFunction } from 'express';
|
|
|
|
import { z } from 'zod';
|
|
|
|
import { db } from '@server/db';
|
|
|
|
import { orgs } from '@server/db/schema';
|
|
|
|
import response from "@server/utils/response";
|
|
|
|
import HttpCode from '@server/types/HttpCode';
|
|
|
|
import createHttpError from 'http-errors';
|
2024-10-03 22:31:20 -04:00
|
|
|
import { sql, inArray } from 'drizzle-orm';
|
2024-10-02 21:17:38 -04:00
|
|
|
|
|
|
|
const listOrgsSchema = z.object({
|
|
|
|
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)),
|
|
|
|
});
|
|
|
|
|
|
|
|
export async function listOrgs(req: Request, res: Response, next: NextFunction): Promise<any> {
|
|
|
|
try {
|
|
|
|
const parsedQuery = listOrgsSchema.safeParse(req.query);
|
|
|
|
if (!parsedQuery.success) {
|
|
|
|
return next(
|
|
|
|
createHttpError(
|
|
|
|
HttpCode.BAD_REQUEST,
|
|
|
|
parsedQuery.error.errors.map(e => e.message).join(', ')
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const { limit, offset } = parsedQuery.data;
|
|
|
|
|
2024-10-03 22:31:20 -04:00
|
|
|
// Use the userOrgs passed from the middleware
|
|
|
|
const userOrgIds = req.userOrgs;
|
|
|
|
|
|
|
|
if (!userOrgIds || userOrgIds.length === 0) {
|
|
|
|
return res.status(HttpCode.OK).send(
|
|
|
|
response(res, {
|
|
|
|
data: {
|
|
|
|
organizations: [],
|
|
|
|
pagination: {
|
|
|
|
total: 0,
|
|
|
|
limit,
|
|
|
|
offset,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
success: true,
|
|
|
|
error: false,
|
|
|
|
message: "No organizations found for the user",
|
|
|
|
status: HttpCode.OK,
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-10-02 21:17:38 -04:00
|
|
|
const organizations = await db.select()
|
|
|
|
.from(orgs)
|
2024-10-03 22:31:20 -04:00
|
|
|
.where(inArray(orgs.orgId, userOrgIds))
|
2024-10-02 21:17:38 -04:00
|
|
|
.limit(limit)
|
|
|
|
.offset(offset);
|
|
|
|
|
|
|
|
const totalCountResult = await db.select({ count: sql<number>`cast(count(*) as integer)` })
|
2024-10-03 22:31:20 -04:00
|
|
|
.from(orgs)
|
|
|
|
.where(inArray(orgs.orgId, userOrgIds));
|
2024-10-02 21:17:38 -04:00
|
|
|
const totalCount = totalCountResult[0].count;
|
|
|
|
|
2024-10-03 22:31:20 -04:00
|
|
|
// // Add the user's role for each organization
|
|
|
|
// const organizationsWithRoles = organizations.map(org => ({
|
|
|
|
// ...org,
|
2024-10-06 16:19:04 -04:00
|
|
|
// userRole: req.userOrgRoleIds[org.orgId],
|
2024-10-03 22:31:20 -04:00
|
|
|
// }));
|
|
|
|
|
2024-10-02 21:17:38 -04:00
|
|
|
return res.status(HttpCode.OK).send(
|
|
|
|
response(res, {
|
|
|
|
data: {
|
|
|
|
organizations,
|
|
|
|
pagination: {
|
|
|
|
total: totalCount,
|
|
|
|
limit,
|
|
|
|
offset,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
success: true,
|
|
|
|
error: false,
|
|
|
|
message: "Organizations retrieved successfully",
|
|
|
|
status: HttpCode.OK,
|
|
|
|
})
|
|
|
|
);
|
|
|
|
} catch (error) {
|
|
|
|
next(error);
|
|
|
|
}
|
|
|
|
}
|