91 lines
2.5 KiB
TypeScript
91 lines
2.5 KiB
TypeScript
import { Request, Response, NextFunction } from "express";
|
|
import { z } from "zod";
|
|
import { db } from "@server/db";
|
|
import response from "@server/lib/response";
|
|
import HttpCode from "@server/types/HttpCode";
|
|
import createHttpError from "http-errors";
|
|
import logger from "@server/logger";
|
|
import { fromError } from "zod-validation-error";
|
|
import { idp, idpOidcConfig, idpOrg } from "@server/db/schemas";
|
|
import { eq, and } from "drizzle-orm";
|
|
import { OpenAPITags, registry } from "@server/openApi";
|
|
|
|
const paramsSchema = z
|
|
.object({
|
|
idpId: z.coerce.number(),
|
|
orgId: z.string()
|
|
})
|
|
.strict();
|
|
|
|
registry.registerPath({
|
|
method: "delete",
|
|
path: "/idp/{idpId}/org/{orgId}",
|
|
description: "Delete an IDP policy for an IDP on an organization.",
|
|
tags: [OpenAPITags.Idp],
|
|
request: {
|
|
params: paramsSchema
|
|
},
|
|
responses: {}
|
|
});
|
|
|
|
export async function deleteIdpOrgPolicy(
|
|
req: Request,
|
|
res: Response,
|
|
next: NextFunction
|
|
): Promise<any> {
|
|
try {
|
|
const parsedParams = paramsSchema.safeParse(req.params);
|
|
if (!parsedParams.success) {
|
|
return next(
|
|
createHttpError(
|
|
HttpCode.BAD_REQUEST,
|
|
fromError(parsedParams.error).toString()
|
|
)
|
|
);
|
|
}
|
|
|
|
const { idpId, orgId } = parsedParams.data;
|
|
|
|
// Check if IDP policy, exists
|
|
const [existing] = await db
|
|
.select()
|
|
.from(idp)
|
|
.leftJoin(
|
|
idpOrg,
|
|
and(eq(idpOrg.orgId, orgId), eq(idpOrg.idpId, idpId))
|
|
)
|
|
.where(eq(idp.idpId, idpId));
|
|
|
|
if (!existing?.idp) {
|
|
return next(
|
|
createHttpError(HttpCode.NOT_FOUND, "Idp does not exist")
|
|
);
|
|
}
|
|
|
|
if (!existing.idpOrg) {
|
|
return next(
|
|
createHttpError(
|
|
HttpCode.NOT_FOUND,
|
|
"Org policy does not exist for this idp"
|
|
)
|
|
);
|
|
}
|
|
|
|
await db
|
|
.delete(idpOrg)
|
|
.where(and(eq(idpOrg.idpId, idpId), eq(idpOrg.orgId, orgId)));
|
|
|
|
return response<null>(res, {
|
|
data: null,
|
|
success: true,
|
|
error: false,
|
|
message: "Idp policy deleted successfully",
|
|
status: HttpCode.OK
|
|
});
|
|
} catch (error) {
|
|
logger.error(error);
|
|
return next(
|
|
createHttpError(HttpCode.INTERNAL_SERVER_ERROR, "An error occurred")
|
|
);
|
|
}
|
|
}
|