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

80 lines
2.3 KiB
TypeScript
Raw Normal View History

import { Request, Response, NextFunction } from "express";
import { z } from "zod";
import { db } from "@server/db";
import { orgs, userActions } 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";
import createHttpError from "http-errors";
import { ActionsEnum, checkUserActionPermission } from "@server/auth/actions";
import logger from "@server/logger";
import { fromError } from "zod-validation-error";
2024-10-01 21:53:49 -04:00
const deleteOrgSchema = z.object({
orgId: z.string(),
2024-10-01 21:53:49 -04:00
});
2024-10-01 21:34:07 -04:00
export async function deleteOrg(
req: Request,
res: Response,
next: NextFunction
): Promise<any> {
2024-10-06 18:05:20 -04:00
try {
const parsedParams = deleteOrgSchema.safeParse(req.params);
if (!parsedParams.success) {
return next(
createHttpError(
HttpCode.BAD_REQUEST,
fromError(parsedParams.error).toString()
2024-10-06 18:05:20 -04:00
)
);
}
2024-10-01 21:53:49 -04:00
2024-10-06 18:05:20 -04:00
const { orgId } = parsedParams.data;
2024-10-01 21:53:49 -04:00
// // Check if the user has permission to list sites
// const hasPermission = await checkUserActionPermission(
// ActionsEnum.deleteOrg,
// req
// );
// if (!hasPermission) {
// return next(
// createHttpError(
// HttpCode.FORBIDDEN,
// "User does not have permission to perform this action"
// )
// );
// }
2024-10-06 16:43:59 -04:00
const deletedOrg = await db
.delete(orgs)
2024-10-06 18:05:20 -04:00
.where(eq(orgs.orgId, orgId))
.returning();
2024-10-01 21:53:49 -04:00
2024-10-06 18:05:20 -04:00
if (deletedOrg.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: null,
success: true,
error: false,
message: "Organization deleted successfully",
status: HttpCode.OK,
});
} catch (error) {
logger.error(error);
return next(
createHttpError(
HttpCode.INTERNAL_SERVER_ERROR,
"An error occurred..."
)
);
2024-10-06 18:05:20 -04:00
}
}