import User from "../../Models/DatabaseModels/User";
import NotFoundException from "../../Types/Exception/NotFoundException";
import ObjectID from "../../Types/ObjectID";
import UserService from "../Services/UserService";
import logger, { getLogAttributesFromRequest } from "../Utils/Logger";
import Response from "../Utils/Response";
import BaseAPI from "./BaseAPI";
const BLANK_PROFILE_PICTURE_PATH = "/usr/src/Common/UI/Images/users/blank-profile.svg";
export default class UserAPI extends BaseAPI {
    constructor() {
        var _a;
        super(User, UserService);
        this.router.get(`${(_a = new this.entityType().getCrudApiPath()) === null || _a === void 0 ? void 0 : _a.toString()}/profile-picture/:userId`, async (req, res) => {
            const userIdParam = req.params["userId"];
            if (!userIdParam) {
                return this.sendBlankProfile(req, res);
            }
            let userId;
            try {
                userId = new ObjectID(userIdParam);
            }
            catch (_a) {
                return this.sendBlankProfile(req, res);
            }
            try {
                const profilePictureSelect = {
                    profilePictureFile: {
                        _id: true,
                        file: true,
                        fileType: true,
                        name: true,
                    },
                };
                const userById = await UserService.findOneBy({
                    query: {
                        _id: userId,
                    },
                    select: profilePictureSelect,
                    props: {
                        isRoot: true,
                    },
                });
                if (userById && userById.profilePictureFile) {
                    Response.setNoCacheHeaders(res);
                    return Response.sendFileResponse(req, res, userById.profilePictureFile);
                }
                return this.sendBlankProfile(req, res);
            }
            catch (error) {
                logger.error(error, getLogAttributesFromRequest(req));
                return this.sendBlankProfile(req, res);
            }
        });
    }
    sendBlankProfile(req, res) {
        Response.setNoCacheHeaders(res);
        try {
            Response.sendFileByPath(req, res, BLANK_PROFILE_PICTURE_PATH);
        }
        catch (error) {
            logger.error(error, getLogAttributesFromRequest(req));
            Response.sendErrorResponse(req, res, new NotFoundException("User profile picture not found"));
        }
    }
}
//# sourceMappingURL=UserAPI.js.map