2024-11-05 22:38:57 -05:00
|
|
|
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";
|
2024-11-05 22:38:57 -05:00
|
|
|
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({
|
2024-11-05 22:38:57 -05:00
|
|
|
orgId: z.string(),
|
2024-10-01 21:53:49 -04:00
|
|
|
});
|
2024-10-01 21:34:07 -04:00
|
|
|
|
2024-11-05 22:38:57 -05: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,
|
2024-11-03 13:57:51 -05:00
|
|
|
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
|
|
|
|
2024-11-05 22:38:57 -05: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
|
|
|
|
2024-11-05 22:38:57 -05: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);
|
2024-11-05 22:38:57 -05:00
|
|
|
return next(
|
|
|
|
createHttpError(
|
|
|
|
HttpCode.INTERNAL_SERVER_ERROR,
|
|
|
|
"An error occurred..."
|
|
|
|
)
|
|
|
|
);
|
2024-10-06 18:05:20 -04:00
|
|
|
}
|
2024-10-02 00:04:40 -04:00
|
|
|
}
|