"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.sendDelayNotifications = exports.sendNotifications = void 0;
var tslib_1 = require("tslib");
var moment = require('moment');
var sendNotifications = function (ctx, materialApproval, nextApprovers, actionLabel) { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
    var nextApprovers_1, nextApprovers_1_1, to, e_1_1, spaceUsers, spaceUsers_1, spaceUsers_1_1, spaceUser, error_1, e_2_1, error_2;
    var e_1, _a, e_2, _b;
    return tslib_1.__generator(this, function (_c) {
        switch (_c.label) {
            case 0:
                console.log("sendNotifications", materialApproval._id, materialApproval.name, nextApprovers, actionLabel);
                _c.label = 1;
            case 1:
                _c.trys.push([1, 21, , 22]);
                _c.label = 2;
            case 2:
                _c.trys.push([2, 7, 8, 9]);
                nextApprovers_1 = tslib_1.__values(nextApprovers), nextApprovers_1_1 = nextApprovers_1.next();
                _c.label = 3;
            case 3:
                if (!!nextApprovers_1_1.done) return [3, 6];
                to = nextApprovers_1_1.value;
                if (!to) return [3, 5];
                return [4, ctx.broker.call('notifications.add', {
                        message: {
                            name: "".concat(actionLabel, ": ").concat(materialApproval.name),
                            body: "",
                            related_to: {
                                o: "pepsico_material_approval",
                                ids: [materialApproval._id]
                            },
                            related_name: materialApproval.name,
                            from: materialApproval.owner,
                            space: materialApproval.space
                        },
                        from: materialApproval.owner,
                        to: to
                    })];
            case 4:
                _c.sent();
                _c.label = 5;
            case 5:
                nextApprovers_1_1 = nextApprovers_1.next();
                return [3, 3];
            case 6: return [3, 9];
            case 7:
                e_1_1 = _c.sent();
                e_1 = { error: e_1_1 };
                return [3, 9];
            case 8:
                try {
                    if (nextApprovers_1_1 && !nextApprovers_1_1.done && (_a = nextApprovers_1.return)) _a.call(nextApprovers_1);
                }
                finally { if (e_1) throw e_1.error; }
                return [7];
            case 9: return [4, ctx.getObject('space_users').find({ filters: [['user', '=', nextApprovers], ['space', '=', materialApproval.space]] })];
            case 10:
                spaceUsers = _c.sent();
                _c.label = 11;
            case 11:
                _c.trys.push([11, 18, 19, 20]);
                spaceUsers_1 = tslib_1.__values(spaceUsers), spaceUsers_1_1 = spaceUsers_1.next();
                _c.label = 12;
            case 12:
                if (!!spaceUsers_1_1.done) return [3, 17];
                spaceUser = spaceUsers_1_1.value;
                if (!(spaceUser.email && spaceUser.email_notification)) return [3, 16];
                _c.label = 13;
            case 13:
                _c.trys.push([13, 15, , 16]);
                return [4, ctx.broker.call('@builder6/email.send', {
                        to: spaceUser.email,
                        subject: "".concat(actionLabel, ": ").concat(materialApproval.name),
                        html: "\n                            <p>\u60A8\u6709\u4E00\u4EFD\u6587\u4EF6\u9700\u8981\u67E5\u770B\u548C\u5904\u7406\u3002\u8BF7\u70B9\u51FB\u4E0B\u9762\u7684\u94FE\u63A5\u6765\u8BBF\u95EE\u8BE5\u6587\u4EF6\uFF1A</p>\n                            <a href=\"".concat(process.env.ROOT_URL, "/app/pepsico_content_review/pepsico_material_approval/view/").concat(materialApproval._id, "?side_object=pepsico_material_approval&side_listview_id=pending\">").concat(materialApproval.name, "</a>\n                        ")
                    })];
            case 14:
                _c.sent();
                return [3, 16];
            case 15:
                error_1 = _c.sent();
                console.log(error_1);
                return [3, 16];
            case 16:
                spaceUsers_1_1 = spaceUsers_1.next();
                return [3, 12];
            case 17: return [3, 20];
            case 18:
                e_2_1 = _c.sent();
                e_2 = { error: e_2_1 };
                return [3, 20];
            case 19:
                try {
                    if (spaceUsers_1_1 && !spaceUsers_1_1.done && (_b = spaceUsers_1.return)) _b.call(spaceUsers_1);
                }
                finally { if (e_2) throw e_2.error; }
                return [7];
            case 20:
                console.log('sendNotifications-email: ' + nextApprovers.length);
                return [3, 22];
            case 21:
                error_2 = _c.sent();
                console.log(error_2);
                return [3, 22];
            case 22: return [2];
        }
    });
}); };
exports.sendNotifications = sendNotifications;
var sendDelayNotifications = function (ctx, intervalMinutes) { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
    var triggerTime, records, records_1, records_1_1, record, e_3_1;
    var e_3, _a;
    return tslib_1.__generator(this, function (_b) {
        switch (_b.label) {
            case 0:
                console.time("sendDelayNotifications");
                triggerTime = moment();
                return [4, ctx.getObject('pepsico_material_approval').find({
                        filters: [
                            ['launch', '=', 'postpone'],
                            ['postpone_launch_date', '>=', triggerTime.toDate()],
                            ['postpone_launch_date', '<=', moment(triggerTime).add(intervalMinutes, 'minute').toDate()]
                        ]
                    })];
            case 1:
                records = _b.sent();
                console.log("sendDelayNotifications count:", records.length, {
                    filters: [
                        ['launch', '=', 'postpone'],
                        ['postpone_launch_date', '>=', triggerTime.toDate()],
                        ['postpone_launch_date', '<=', moment(triggerTime).add(intervalMinutes, 'minute').toDate()]
                    ]
                });
                _b.label = 2;
            case 2:
                _b.trys.push([2, 7, 8, 9]);
                records_1 = tslib_1.__values(records), records_1_1 = records_1.next();
                _b.label = 3;
            case 3:
                if (!!records_1_1.done) return [3, 6];
                record = records_1_1.value;
                if (!record.material_owner_id) return [3, 5];
                return [4, (0, exports.sendNotifications)(ctx, record, [record.material_owner_id], '请确认上线')];
            case 4:
                _b.sent();
                _b.label = 5;
            case 5:
                records_1_1 = records_1.next();
                return [3, 3];
            case 6: return [3, 9];
            case 7:
                e_3_1 = _b.sent();
                e_3 = { error: e_3_1 };
                return [3, 9];
            case 8:
                try {
                    if (records_1_1 && !records_1_1.done && (_a = records_1.return)) _a.call(records_1);
                }
                finally { if (e_3) throw e_3.error; }
                return [7];
            case 9:
                console.timeEnd("sendDelayNotifications");
                return [2];
        }
    });
}); };
exports.sendDelayNotifications = sendDelayNotifications;
//# sourceMappingURL=notification.js.map