import UserMiddleware from "../Middleware/UserAuthorization";
import ProjectSCIMService from "../Services/ProjectSCIMService";
import TeamMemberService from "../Services/TeamMemberService";
import Response from "../Utils/Response";
import BaseAPI from "./BaseAPI";
import BadDataException from "../../Types/Exception/BadDataException";
import NotAuthorizedException from "../../Types/Exception/NotAuthorizedException";
import ObjectID from "../../Types/ObjectID";
import TeamMember from "../../Models/DatabaseModels/TeamMember";
export default class TeamMemberAPI extends BaseAPI {
    constructor() {
        var _a;
        super(TeamMember, TeamMemberService);
        this.router.post(`${(_a = new this.entityType().getCrudApiPath()) === null || _a === void 0 ? void 0 : _a.toString()}/:id/leave`, UserMiddleware.getUserMiddleware, async (req, res, next) => {
            var _a, _b;
            try {
                const oneUptimeRequest = req;
                const idParam = req.params["id"];
                if (!idParam) {
                    return Response.sendErrorResponse(req, res, new BadDataException("Team member id is required"));
                }
                ObjectID.validateUUID(idParam);
                const teamMemberId = new ObjectID(idParam);
                const userId = (_a = oneUptimeRequest.userAuthorization) === null || _a === void 0 ? void 0 : _a.userId;
                if (!userId) {
                    return Response.sendErrorResponse(req, res, new NotAuthorizedException("Not authenticated"));
                }
                const teamMember = await this.service.findOneById({
                    id: teamMemberId,
                    props: { isRoot: true },
                    select: {
                        userId: true,
                        projectId: true,
                    },
                });
                if (!teamMember) {
                    return Response.sendErrorResponse(req, res, new BadDataException("Team member not found"));
                }
                if (((_b = teamMember.userId) === null || _b === void 0 ? void 0 : _b.toString()) !== userId.toString()) {
                    return Response.sendErrorResponse(req, res, new NotAuthorizedException("You can only leave teams you are a member of"));
                }
                if (teamMember.projectId) {
                    const scimCount = (await ProjectSCIMService.countBy({
                        query: {
                            projectId: teamMember.projectId,
                            enablePushGroups: true,
                        },
                        props: { isRoot: true },
                    })).toNumber();
                    if (scimCount > 0) {
                        return Response.sendErrorResponse(req, res, new BadDataException("Team membership is managed by SCIM Push Groups for this project. Please contact your administrator to be removed."));
                    }
                }
                await this.service.deleteOneById({
                    id: teamMemberId,
                    props: { isRoot: true },
                });
                return Response.sendEmptySuccessResponse(req, res);
            }
            catch (err) {
                return next(err);
            }
        });
    }
}
//# sourceMappingURL=TeamMemberAPI.js.map