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 OnCallDutyPolicyFeed from "../../Models/DatabaseModels/OnCallDutyPolicyFeed";
import WorkspaceNotificationRuleService from "./WorkspaceNotificationRuleService";
import CaptureSpan from "../Utils/Telemetry/CaptureSpan";
export class Service extends DatabaseService {
    constructor() {
        super(OnCallDutyPolicyFeed);
        if (IsBillingEnabled) {
            this.hardDeleteItemsOlderThanInDays("createdAt", 90);
        }
    }
    async createOnCallDutyPolicyFeedItem(data) {
        var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s;
        try {
            logger.debug("OnCallDutyPolicyFeedService.createOnCallDutyPolicyFeedItem", {
                projectId: (_a = data.projectId) === null || _a === void 0 ? void 0 : _a.toString(),
                onCallDutyPolicyId: (_b = data.onCallDutyPolicyId) === null || _b === void 0 ? void 0 : _b.toString(),
            });
            logger.debug(data, {
                projectId: (_c = data.projectId) === null || _c === void 0 ? void 0 : _c.toString(),
                onCallDutyPolicyId: (_d = data.onCallDutyPolicyId) === null || _d === void 0 ? void 0 : _d.toString(),
            });
            const onCallDutyPolicyFeed = new OnCallDutyPolicyFeed();
            if (!data.onCallDutyPolicyId) {
                throw new BadDataException("OnCallDutyPolicy ID is required");
            }
            if (!data.feedInfoInMarkdown) {
                throw new BadDataException("Log in markdown is required");
            }
            if (!data.onCallDutyPolicyFeedEventType) {
                throw new BadDataException("OnCallDutyPolicy log event is required");
            }
            if (!data.projectId) {
                throw new BadDataException("Project ID is required");
            }
            if (!data.displayColor) {
                data.displayColor = Blue500;
            }
            onCallDutyPolicyFeed.displayColor = data.displayColor;
            onCallDutyPolicyFeed.onCallDutyPolicyId = data.onCallDutyPolicyId;
            onCallDutyPolicyFeed.feedInfoInMarkdown = data.feedInfoInMarkdown;
            onCallDutyPolicyFeed.onCallDutyPolicyFeedEventType =
                data.onCallDutyPolicyFeedEventType;
            onCallDutyPolicyFeed.projectId = data.projectId;
            if (!data.postedAt) {
                onCallDutyPolicyFeed.postedAt = OneUptimeDate.getCurrentDate();
            }
            if (data.userId) {
                onCallDutyPolicyFeed.userId = data.userId;
            }
            if (data.moreInformationInMarkdown) {
                onCallDutyPolicyFeed.moreInformationInMarkdown =
                    data.moreInformationInMarkdown;
            }
            const createdOnCallDutyPolicyFeed = await this.create({
                data: onCallDutyPolicyFeed,
                props: {
                    isRoot: true,
                },
            });
            logger.debug("On Call Duty Policy Feed created", {
                projectId: (_e = data.projectId) === null || _e === void 0 ? void 0 : _e.toString(),
                onCallDutyPolicyId: (_f = data.onCallDutyPolicyId) === null || _f === void 0 ? void 0 : _f.toString(),
            });
            logger.debug(createdOnCallDutyPolicyFeed, {
                projectId: (_g = data.projectId) === null || _g === void 0 ? void 0 : _g.toString(),
                onCallDutyPolicyId: (_h = data.onCallDutyPolicyId) === 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,
                        onCallDutyPolicyId: data.onCallDutyPolicyId,
                        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(),
                    onCallDutyPolicyId: (_l = data.onCallDutyPolicyId) === null || _l === void 0 ? void 0 : _l.toString(),
                });
                logger.error(e, {
                    projectId: (_m = data.projectId) === null || _m === void 0 ? void 0 : _m.toString(),
                    onCallDutyPolicyId: (_o = data.onCallDutyPolicyId) === 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 onCallDutyPolicy feed", {
                projectId: (_p = data.projectId) === null || _p === void 0 ? void 0 : _p.toString(),
                onCallDutyPolicyId: (_q = data.onCallDutyPolicyId) === null || _q === void 0 ? void 0 : _q.toString(),
            });
            logger.error(e, {
                projectId: (_r = data.projectId) === null || _r === void 0 ? void 0 : _r.toString(),
                onCallDutyPolicyId: (_s = data.onCallDutyPolicyId) === 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: {
                onCallDutyPolicyId: data.onCallDutyPolicyId,
            },
            feedInfoInMarkdown: data.feedInfoInMarkdown,
            workspaceNotification: data.workspaceNotification,
        });
    }
}
__decorate([
    CaptureSpan(),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object]),
    __metadata("design:returntype", Promise)
], Service.prototype, "createOnCallDutyPolicyFeedItem", null);
__decorate([
    CaptureSpan(),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object]),
    __metadata("design:returntype", Promise)
], Service.prototype, "sendWorkspaceNotification", null);
export default new Service();
//# sourceMappingURL=OnCallDutyPolicyFeedService.js.map