import StatusPageAnnouncement from "../../Models/DatabaseModels/StatusPageAnnouncement";
import NotFoundException from "../../Types/Exception/NotFoundException";
import ObjectID from "../../Types/ObjectID";
import StatusPageAnnouncementService from "../Services/StatusPageAnnouncementService";
import Response from "../Utils/Response";
import BaseAPI from "./BaseAPI";
import UserMiddleware from "../Middleware/UserAuthorization";
import CommonAPI from "./CommonAPI";
export default class StatusPageAnnouncementAPI extends BaseAPI {
    constructor() {
        var _a;
        super(StatusPageAnnouncement, StatusPageAnnouncementService);
        this.router.get(`${(_a = new this.entityType().getCrudApiPath()) === null || _a === void 0 ? void 0 : _a.toString()}/attachment/:projectId/:announcementId/:fileId`, UserMiddleware.getUserMiddleware, async (req, res, next) => {
            try {
                await this.getAttachment(req, res);
            }
            catch (err) {
                next(err);
            }
        });
    }
    async getAttachment(req, res) {
        var _a;
        const announcementIdParam = req.params["announcementId"];
        const fileIdParam = req.params["fileId"];
        if (!announcementIdParam || !fileIdParam) {
            throw new NotFoundException("Attachment not found");
        }
        let announcementId;
        let fileId;
        try {
            announcementId = new ObjectID(announcementIdParam);
            fileId = new ObjectID(fileIdParam);
        }
        catch (_b) {
            throw new NotFoundException("Attachment not found");
        }
        const props = await CommonAPI.getDatabaseCommonInteractionProps(req);
        const announcement = await this.service.findOneBy({
            query: {
                _id: announcementId,
            },
            select: {
                attachments: {
                    _id: true,
                    file: true,
                    fileType: true,
                    name: true,
                },
            },
            props,
        });
        const attachment = (_a = announcement === null || announcement === void 0 ? void 0 : announcement.attachments) === null || _a === void 0 ? void 0 : _a.find((file) => {
            const attachmentId = file._id
                ? file._id.toString()
                : file.id
                    ? file.id.toString()
                    : null;
            return attachmentId === fileId.toString();
        });
        if (!attachment || !attachment.file) {
            throw new NotFoundException("Attachment not found");
        }
        Response.setNoCacheHeaders(res);
        return Response.sendFileResponse(req, res, attachment);
    }
}
//# sourceMappingURL=StatusPageAnnouncementAPI.js.map