var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
import { Blue500 } from "../../Types/BrandColors";
import OneUptimeDate from "../../Types/Date";
import BadDataException from "../../Types/Exception/BadDataException";
import { IsBillingEnabled } from "../EnvironmentConfig";
import logger from "../Utils/Logger";
import DatabaseService from "./DatabaseService";
import IncidentFeed from "../../Models/DatabaseModels/IncidentFeed";
import WorkspaceNotificationRuleService from "./WorkspaceNotificationRuleService";
import { applyIncidentRelatedRecordPrivacyFilter } from "../Utils/Incident/IncidentPrivacyFilter";
import CaptureSpan from "../Utils/Telemetry/CaptureSpan";
export class Service extends DatabaseService {
    constructor() {
        super(IncidentFeed);
        if (IsBillingEnabled) {
            this.hardDeleteItemsOlderThanInDays("createdAt", 3 * 365); // 3 years
        }
    }
    async onBeforeFind(findBy) {
        findBy.query = applyIncidentRelatedRecordPrivacyFilter(findBy.query, findBy.props);
        return { findBy, carryForward: null };
    }
    async countBy(countBy) {
        countBy.query = applyIncidentRelatedRecordPrivacyFilter(countBy.query, countBy.props);
        return super.countBy(countBy);
    }
    async onBeforeUpdate(updateBy) {
        updateBy.query = applyIncidentRelatedRecordPrivacyFilter(updateBy.query, updateBy.props);
        return { updateBy, carryForward: null };
    }
    async onBeforeDelete(deleteBy) {
        deleteBy.query = applyIncidentRelatedRecordPrivacyFilter(deleteBy.query, deleteBy.props);
        return { deleteBy, carryForward: null };
    }
    async createIncidentFeedItem(data) {
        var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s;
        try {
            logger.debug("IncidentFeedService.createIncidentFeedItem", {
                projectId: (_a = data.projectId) === null || _a === void 0 ? void 0 : _a.toString(),
                incidentId: (_b = data.incidentId) === null || _b === void 0 ? void 0 : _b.toString(),
            });
            logger.debug(data, {
                projectId: (_c = data.projectId) === null || _c === void 0 ? void 0 : _c.toString(),
                incidentId: (_d = data.incidentId) === null || _d === void 0 ? void 0 : _d.toString(),
            });
            const incidentFeed = new IncidentFeed();
            if (!data.incidentId) {
                throw new BadDataException("Incident ID is required");
            }
            if (!data.feedInfoInMarkdown) {
                throw new BadDataException("Log in markdown is required");
            }
            if (!data.incidentFeedEventType) {
                throw new BadDataException("Incident log event is required");
            }
            if (!data.projectId) {
                throw new BadDataException("Project ID is required");
            }
            if (!data.displayColor) {
                data.displayColor = Blue500;
            }
            incidentFeed.displayColor = data.displayColor;
            incidentFeed.incidentId = data.incidentId;
            incidentFeed.feedInfoInMarkdown = data.feedInfoInMarkdown;
            incidentFeed.incidentFeedEventType = data.incidentFeedEventType;
            incidentFeed.projectId = data.projectId;
            if (!data.postedAt) {
                incidentFeed.postedAt = OneUptimeDate.getCurrentDate();
            }
            if (data.userId) {
                incidentFeed.userId = data.userId;
            }
            if (data.moreInformationInMarkdown) {
                incidentFeed.moreInformationInMarkdown = data.moreInformationInMarkdown;
            }
            const createdIncidentFeed = await this.create({
                data: incidentFeed,
                props: {
                    isRoot: true,
                },
            });
            logger.debug("Incident Feed created", {
                projectId: (_e = data.projectId) === null || _e === void 0 ? void 0 : _e.toString(),
                incidentId: (_f = data.incidentId) === null || _f === void 0 ? void 0 : _f.toString(),
            });
            logger.debug(createdIncidentFeed, {
                projectId: (_g = data.projectId) === null || _g === void 0 ? void 0 : _g.toString(),
                incidentId: (_h = data.incidentId) === null || _h === void 0 ? void 0 : _h.toString(),
            });
            try {
                // send notification to slack and teams
                if (data.workspaceNotification &&
                    ((_j = data.workspaceNotification) === null || _j === void 0 ? void 0 : _j.sendWorkspaceNotification)) {
                    await this.sendWorkspaceNotification({
                        projectId: data.projectId,
                        incidentId: data.incidentId,
                        feedInfoInMarkdown: data.feedInfoInMarkdown,
                        workspaceNotification: data.workspaceNotification,
                    });
                }
            }
            catch (e) {
                logger.error("Error in sending notification to slack and teams", {
                    projectId: (_k = data.projectId) === null || _k === void 0 ? void 0 : _k.toString(),
                    incidentId: (_l = data.incidentId) === null || _l === void 0 ? void 0 : _l.toString(),
                });
                logger.error(e, {
                    projectId: (_m = data.projectId) === null || _m === void 0 ? void 0 : _m.toString(),
                    incidentId: (_o = data.incidentId) === null || _o === void 0 ? void 0 : _o.toString(),
                });
                // we dont throw this error as it is not a critical error
            }
        }
        catch (e) {
            logger.error("Error in creating incident feed", {
                projectId: (_p = data.projectId) === null || _p === void 0 ? void 0 : _p.toString(),
                incidentId: (_q = data.incidentId) === null || _q === void 0 ? void 0 : _q.toString(),
            });
            logger.error(e, {
                projectId: (_r = data.projectId) === null || _r === void 0 ? void 0 : _r.toString(),
                incidentId: (_s = data.incidentId) === null || _s === void 0 ? void 0 : _s.toString(),
            });
            // we dont throw this error as it is not a critical error
        }
    }
    async sendWorkspaceNotification(data) {
        return await WorkspaceNotificationRuleService.sendWorkspaceMarkdownNotification({
            projectId: data.projectId,
            notificationFor: {
                incidentId: data.incidentId,
            },
            feedInfoInMarkdown: data.feedInfoInMarkdown,
            workspaceNotification: data.workspaceNotification,
        });
    }
}
__decorate([
    CaptureSpan(),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object]),
    __metadata("design:returntype", Promise)
], Service.prototype, "onBeforeFind", null);
__decorate([
    CaptureSpan(),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object]),
    __metadata("design:returntype", Promise)
], Service.prototype, "countBy", null);
__decorate([
    CaptureSpan(),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object]),
    __metadata("design:returntype", Promise)
], Service.prototype, "onBeforeUpdate", null);
__decorate([
    CaptureSpan(),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object]),
    __metadata("design:returntype", Promise)
], Service.prototype, "onBeforeDelete", null);
__decorate([
    CaptureSpan(),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object]),
    __metadata("design:returntype", Promise)
], Service.prototype, "createIncidentFeedItem", null);
__decorate([
    CaptureSpan(),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object]),
    __metadata("design:returntype", Promise)
], Service.prototype, "sendWorkspaceNotification", null);
export default new Service();
//# sourceMappingURL=IncidentFeedService.js.map