fosrl.pangolin/server/routers/org/updateOrg.ts

81 lines
2.7 KiB
TypeScript
Raw Normal View History

2024-10-01 21:34:07 -04:00
import { Request, Response, NextFunction } from 'express';
2024-10-01 21:53:49 -04:00
import { z } from 'zod';
import { db } from '@server/db';
import { orgs } from '@server/db/schema';
import { eq } from 'drizzle-orm';
2024-10-01 21:34:07 -04:00
import response from "@server/utils/response";
import HttpCode from '@server/types/HttpCode';
2024-10-01 21:53:49 -04:00
import createHttpError from 'http-errors';
2024-10-06 16:43:59 -04:00
import { ActionsEnum, checkUserActionPermission } from '@server/auth/actions';
2024-10-06 18:05:20 -04:00
import logger from '@server/logger';
2024-10-01 21:53:49 -04:00
const updateOrgParamsSchema = z.object({
2024-10-06 18:05:20 -04:00
orgId: z.string().transform(Number).pipe(z.number().int().positive())
2024-10-01 21:53:49 -04:00
});
const updateOrgBodySchema = z.object({
2024-10-06 18:05:20 -04:00
name: z.string().min(1).max(255).optional(),
domain: z.string().min(1).max(255).optional(),
2024-10-01 21:53:49 -04:00
}).refine(data => Object.keys(data).length > 0, {
2024-10-06 18:05:20 -04:00
message: "At least one field must be provided for update"
2024-10-01 21:53:49 -04:00
});
2024-10-01 21:34:07 -04:00
export async function updateOrg(req: Request, res: Response, next: NextFunction): Promise<any> {
2024-10-06 18:05:20 -04:00
try {
const parsedParams = updateOrgParamsSchema.safeParse(req.params);
if (!parsedParams.success) {
return next(
createHttpError(
HttpCode.BAD_REQUEST,
parsedParams.error.errors.map(e => e.message).join(', ')
)
);
}
2024-10-01 21:53:49 -04:00
2024-10-06 18:05:20 -04:00
const parsedBody = updateOrgBodySchema.safeParse(req.body);
if (!parsedBody.success) {
return next(
createHttpError(
HttpCode.BAD_REQUEST,
parsedBody.error.errors.map(e => e.message).join(', ')
)
);
}
2024-10-01 21:53:49 -04:00
2024-10-06 18:05:20 -04:00
const { orgId } = parsedParams.data;
const updateData = parsedBody.data;
2024-10-01 21:53:49 -04:00
2024-10-06 16:43:59 -04:00
2024-10-06 18:05:20 -04:00
// Check if the user has permission to list sites
const hasPermission = await checkUserActionPermission(ActionsEnum.updateOrg, req);
if (!hasPermission) {
return next(createHttpError(HttpCode.FORBIDDEN, 'User does not have permission to list sites'));
}
2024-10-06 16:43:59 -04:00
2024-10-06 18:05:20 -04:00
const updatedOrg = await db.update(orgs)
.set(updateData)
.where(eq(orgs.orgId, orgId))
.returning();
2024-10-01 21:53:49 -04:00
2024-10-06 18:05:20 -04:00
if (updatedOrg.length === 0) {
return next(
createHttpError(
HttpCode.NOT_FOUND,
`Organization with ID ${orgId} not found`
)
);
}
2024-10-01 21:53:49 -04:00
2024-10-06 18:05:20 -04:00
return response(res, {
data: updatedOrg[0],
success: true,
error: false,
message: "Organization updated successfully",
status: HttpCode.OK,
});
} catch (error) {
logger.error(error);
return next(createHttpError(HttpCode.INTERNAL_SERVER_ERROR, "An error occurred..."));
}
}