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 DatabaseService from "./DatabaseService";
import IncidentService from "./IncidentService";
import OnCallDutyPolicyExecutionLogService from "./OnCallDutyPolicyExecutionLogService";
import OnCallDutyPolicyExecutionLogTimelineService from "./OnCallDutyPolicyExecutionLogTimelineService";
import UserOnCallLogService from "./UserOnCallLogService";
import UserService from "./UserService";
import { LIMIT_PER_PROJECT } from "../../Types/Database/LimitMax";
import BadDataException from "../../Types/Exception/BadDataException";
import CaptureSpan from "../Utils/Telemetry/CaptureSpan";
import OnCallDutyExecutionLogTimelineStatus from "../../Types/OnCallDutyPolicy/OnCalDutyExecutionLogTimelineStatus";
import OnCallDutyPolicyStatus from "../../Types/OnCallDutyPolicy/OnCallDutyPolicyStatus";
import UserNotificationExecutionStatus from "../../Types/UserNotification/UserNotificationExecutionStatus";
import Model from "../../Models/DatabaseModels/UserOnCallLogTimeline";
import AlertService from "./AlertService";
export class Service extends DatabaseService {
    constructor() {
        super(Model);
    }
    async onUpdateSuccess(onUpdate, _updatedItemIds) {
        if (onUpdate.updateBy.data.acknowledgedAt &&
            onUpdate.updateBy.data.isAcknowledged) {
            const items = await this.findBy({
                query: onUpdate.updateBy.query,
                select: {
                    _id: true,
                    projectId: true,
                    userId: true,
                    userNotificationLogId: true,
                    onCallDutyPolicyExecutionLogId: true,
                    triggeredByIncidentId: true,
                    triggeredByAlertId: true,
                    onCallDutyPolicyExecutionLogTimelineId: true,
                },
                skip: 0,
                limit: LIMIT_PER_PROJECT,
                props: {
                    isRoot: true,
                },
            });
            for (const item of items) {
                const isIncident = Boolean(item.triggeredByIncidentId);
                const isAlert = Boolean(item.triggeredByAlertId);
                // this incident is acknowledged.
                // now we need to ack the parent log.
                const user = await UserService.findOneById({
                    id: item.userId,
                    select: {
                        _id: true,
                        name: true,
                        email: true,
                    },
                    props: {
                        isRoot: true,
                    },
                });
                if (!user) {
                    throw new BadDataException("User not found.");
                }
                const statusMessage = `${isIncident ? "Incident" : "Alert"} acknowledged by ${user.name} (${user.email})`;
                await UserOnCallLogService.updateOneById({
                    id: item.userNotificationLogId,
                    data: {
                        acknowledgedAt: onUpdate.updateBy.data.acknowledgedAt,
                        acknowledgedByUserId: item.userId,
                        status: UserNotificationExecutionStatus.Completed,
                        statusMessage: statusMessage,
                    },
                    props: {
                        isRoot: true,
                    },
                });
                //  and then oncall log.
                await OnCallDutyPolicyExecutionLogService.updateOneById({
                    id: item.onCallDutyPolicyExecutionLogId,
                    data: {
                        acknowledgedAt: onUpdate.updateBy.data.acknowledgedAt,
                        acknowledgedByUserId: item.userId,
                        status: OnCallDutyPolicyStatus.Completed,
                        statusMessage: statusMessage,
                    },
                    props: {
                        isRoot: true,
                    },
                });
                // and then oncall log timeline.
                await OnCallDutyPolicyExecutionLogTimelineService.updateOneById({
                    id: item.onCallDutyPolicyExecutionLogTimelineId,
                    data: {
                        acknowledgedAt: onUpdate.updateBy.data.acknowledgedAt,
                        isAcknowledged: true,
                        status: OnCallDutyExecutionLogTimelineStatus.SuccessfullyAcknowledged,
                        statusMessage: statusMessage,
                    },
                    props: {
                        isRoot: true,
                    },
                });
                if (isIncident) {
                    // incident.
                    await IncidentService.acknowledgeIncident(item.triggeredByIncidentId, item.userId);
                }
                if (isAlert) {
                    // alert.
                    await AlertService.acknowledgeAlert(item.triggeredByAlertId, item.userId);
                }
            }
        }
        return onUpdate;
    }
}
__decorate([
    CaptureSpan(),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object, Array]),
    __metadata("design:returntype", Promise)
], Service.prototype, "onUpdateSuccess", null);
export default new Service();
//# sourceMappingURL=UserOnCallLogTimelineService.js.map