2024-11-03 13:57:51 -05:00
|
|
|
import { Request, Response, NextFunction } from "express";
|
|
|
|
import { z } from "zod";
|
|
|
|
import { db } from "@server/db";
|
|
|
|
import { userOrgs, users } from "@server/db/schema";
|
|
|
|
import { and, eq } from "drizzle-orm";
|
2024-10-01 21:34:07 -04:00
|
|
|
import response from "@server/utils/response";
|
2024-11-03 13:57:51 -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
|
|
|
|
2024-10-12 22:31:24 -04:00
|
|
|
const removeUserSchema = z.object({
|
|
|
|
userId: z.string().uuid(),
|
2024-11-03 13:57:51 -05:00
|
|
|
orgId: z.string(),
|
2024-10-01 21:53:49 -04:00
|
|
|
});
|
2024-10-01 21:34:07 -04:00
|
|
|
|
2024-11-03 13:57:51 -05:00
|
|
|
export async function removeUserOrg(
|
|
|
|
req: Request,
|
|
|
|
res: Response,
|
|
|
|
next: NextFunction
|
|
|
|
): Promise<any> {
|
2024-10-06 18:05:20 -04:00
|
|
|
try {
|
2024-10-12 22:31:24 -04:00
|
|
|
const parsedParams = removeUserSchema.safeParse(req.params);
|
2024-10-06 18:05:20 -04:00
|
|
|
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-12 22:31:24 -04:00
|
|
|
const { userId, orgId } = parsedParams.data;
|
2024-10-01 21:53:49 -04:00
|
|
|
|
2024-10-06 18:05:20 -04:00
|
|
|
// Check if the user has permission to list sites
|
2024-11-03 13:57:51 -05:00
|
|
|
const hasPermission = await checkUserActionPermission(
|
|
|
|
ActionsEnum.removeUser,
|
|
|
|
req
|
|
|
|
);
|
2024-10-06 18:05:20 -04:00
|
|
|
if (!hasPermission) {
|
2024-11-03 13:57:51 -05:00
|
|
|
return next(
|
|
|
|
createHttpError(
|
|
|
|
HttpCode.FORBIDDEN,
|
|
|
|
"User does not have permission to perform this action"
|
|
|
|
)
|
|
|
|
);
|
2024-10-06 18:05:20 -04:00
|
|
|
}
|
2024-10-06 16:43:59 -04:00
|
|
|
|
2024-10-12 22:31:24 -04:00
|
|
|
// remove the user from the userOrgs table
|
2024-11-03 13:57:51 -05:00
|
|
|
await db
|
|
|
|
.delete(userOrgs)
|
2024-10-12 22:31:24 -04:00
|
|
|
.where(and(eq(userOrgs.userId, userId), eq(userOrgs.orgId, orgId)));
|
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: "User deleted successfully",
|
|
|
|
status: HttpCode.OK,
|
|
|
|
});
|
|
|
|
} catch (error) {
|
|
|
|
logger.error(error);
|
2024-11-03 13:57:51 -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
|
|
|
}
|