import AlertInternalNote from "../../Models/DatabaseModels/AlertInternalNote";
import NotFoundException from "../../Types/Exception/NotFoundException";
import ObjectID from "../../Types/ObjectID";
import AlertInternalNoteService from "../Services/AlertInternalNoteService";
import Response from "../Utils/Response";
import BaseAPI from "./BaseAPI";
import UserMiddleware from "../Middleware/UserAuthorization";
import CommonAPI from "./CommonAPI";
export default class AlertInternalNoteAPI extends BaseAPI {
    constructor() {
        var _a;
        super(AlertInternalNote, AlertInternalNoteService);
        this.router.get(`${(_a = new this.entityType().getCrudApiPath()) === null || _a === void 0 ? void 0 : _a.toString()}/attachment/:projectId/:noteId/:fileId`, UserMiddleware.getUserMiddleware, async (req, res, next) => {
            try {
                await this.getAttachment(req, res);
            }
            catch (err) {
                next(err);
            }
        });
    }
    async getAttachment(req, res) {
        var _a;
        const noteIdParam = req.params["noteId"];
        const fileIdParam = req.params["fileId"];
        if (!noteIdParam || !fileIdParam) {
            throw new NotFoundException("Attachment not found");
        }
        let noteId;
        let fileId;
        try {
            noteId = new ObjectID(noteIdParam);
            fileId = new ObjectID(fileIdParam);
        }
        catch (_b) {
            throw new NotFoundException("Attachment not found");
        }
        const props = await CommonAPI.getDatabaseCommonInteractionProps(req);
        const note = await this.service.findOneBy({
            query: {
                _id: noteId,
            },
            select: {
                attachments: {
                    _id: true,
                    file: true,
                    fileType: true,
                    name: true,
                },
            },
            props,
        });
        const attachment = (_a = note === null || note === void 0 ? void 0 : note.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=AlertInternalNoteAPI.js.map