"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.bypassMaterial = void 0;
var tslib_1 = require("tslib");
var consts_1 = require("../methods/consts");
var _ = require("lodash");
exports.bypassMaterial = {
    rest: {
        method: 'POST',
        fullPath: '/api/pepsico-content/approve/material/bypass/:recordId'
    },
    handler: function (ctx) {
        return tslib_1.__awaiter(this, void 0, void 0, function () {
            var logger, recordId, reason, userId, material, approval, approveField, bypassedField, bypassedByField, bypassReasonField, bypassedAtField, reviewerType, materialUpdate, allMaterials, hasPendingMaterials, allMaterials_1, allMaterials_1_1, mat;
            var _a, e_1, _b, _c;
            return tslib_1.__generator(this, function (_d) {
                switch (_d.label) {
                    case 0:
                        ctx.getObject = this.getObject;
                        return [4, this.getLogger()];
                    case 1:
                        logger = _d.sent();
                        ctx.logger = logger;
                        recordId = ctx.params.recordId;
                        reason = ctx.params.reason;
                        userId = ctx.meta.user.userId;
                        return [4, ctx.getObject('pepsico_material').findOne(recordId)];
                    case 2:
                        material = _d.sent();
                        if (!material) {
                            throw new Error('未找到素材');
                        }
                        if (material.void_material) {
                            throw new Error('已作废的素材无需跳过');
                        }
                        return [4, ctx.getObject('pepsico_material_approval').findOne(material.pepsico_material_approval)];
                    case 3:
                        approval = _d.sent();
                        if (!approval) {
                            throw new Error('未找到关联的审批单');
                        }
                        if (approval.current_step === consts_1.Step.conclusion || approval.current_step === consts_1.Step.draft) {
                            throw new Error('当前步骤不支持跳过操作');
                        }
                        approveField = '';
                        bypassedField = '';
                        bypassedByField = '';
                        bypassReasonField = '';
                        bypassedAtField = '';
                        reviewerType = '';
                        if (approval.current_step === consts_1.Step.departmental_review) {
                            if (_.includes(approval.handler_preca || (approval.pre_ca_reviewer ? [approval.pre_ca_reviewer] : []), userId) && approval.pre_ca_approval_status !== 'pass' && approval.pre_ca_approval_status !== true) {
                                approveField = 'pre_ca_approval_status';
                                reviewerType = 'CA预审';
                            }
                            else if (_.includes(approval.handler_presra || (approval.pre_sra_reviewer ? [approval.pre_sra_reviewer] : []), userId) && approval.pre_sra_approval_status !== 'pass' && approval.pre_sra_approval_status !== true) {
                                approveField = 'pre_sra_approval_status';
                                reviewerType = 'SRA预审';
                            }
                            else if (_.includes(approval.handler_prens || (approval.pre_ns_reviewer ? [approval.pre_ns_reviewer] : []), userId) && approval.pre_ns_approval_status !== 'pass' && approval.pre_ns_approval_status !== true) {
                                approveField = 'pre_ns_approval_status';
                                reviewerType = 'NS预审';
                            }
                            else if (_.includes(approval.handler_prelegal || (approval.pre_legal_reviewer ? [approval.pre_legal_reviewer] : []), userId) && approval.pre_legal_approval_status !== 'pass' && approval.pre_legal_approval_status !== true) {
                                approveField = 'pre_legal_approval_status';
                                reviewerType = 'Legal预审';
                            }
                            else if (_.includes(approval.handler_ca || approval.ca_reviewer, userId) && approval.ca_bypassed !== true && approval.ca_approval_status !== 'pass' && approval.ca_approval_status !== true && approval.ca_approval_status !== false) {
                                approveField = 'ca_approval_status';
                                bypassedField = 'ca_bypassed';
                                bypassedByField = 'ca_bypassed_by';
                                bypassReasonField = 'ca_bypass_reason';
                                bypassedAtField = 'ca_bypassed_at';
                                reviewerType = 'CA';
                            }
                            else if (_.includes(approval.handler_sra || approval.sra_reviewer, userId) && approval.sra_bypassed !== true && approval.sra_approval_status !== 'pass' && approval.sra_approval_status !== true && approval.sra_approval_status !== false) {
                                approveField = 'sra_approval_status';
                                bypassedField = 'sra_bypassed';
                                bypassedByField = 'sra_bypassed_by';
                                bypassReasonField = 'sra_bypass_reason';
                                bypassedAtField = 'sra_bypassed_at';
                                reviewerType = 'SRA';
                            }
                            else if (_.includes(approval.handler_ns || approval.ns_reviewer, userId) && approval.ns_bypassed !== true && approval.ns_approval_status !== 'pass' && approval.ns_approval_status !== true && approval.ns_approval_status !== false) {
                                approveField = 'ns_approval_status';
                                bypassedField = 'ns_bypassed';
                                bypassedByField = 'ns_bypassed_by';
                                bypassReasonField = 'ns_bypass_reason';
                                bypassedAtField = 'ns_bypassed_at';
                                reviewerType = 'NS';
                            }
                            else if (_.includes(approval.handler_legal || approval.legal_reviewer, userId) && approval.legal_bypassed !== true && approval.legal_approval_status !== 'pass' && approval.legal_approval_status !== true && approval.legal_approval_status !== false) {
                                approveField = 'legal_approval_status';
                                bypassedField = 'legal_bypassed';
                                bypassedByField = 'legal_bypassed_by';
                                bypassReasonField = 'legal_bypass_reason';
                                bypassedAtField = 'legal_bypassed_at';
                                reviewerType = 'Legal';
                            }
                        }
                        if (approval.current_step === consts_1.Step.personnel_review) {
                            if (_.includes(approval.handler_preguding || (approval.pre_reviewer ? [approval.pre_reviewer] : []), userId) && approval.pre_approval_status !== 'pass' && approval.pre_approval_status !== true) {
                                approveField = 'pre_approval_status';
                                reviewerType = '固定人员预审';
                            }
                            else if (_.includes(approval.handler_personnel || approval.reviewer, userId)) {
                                approveField = 'approval_status';
                                bypassedField = 'approval_bypassed';
                                bypassedByField = 'approval_bypassed_by';
                                bypassReasonField = 'approval_bypass_reason';
                                bypassedAtField = 'approval_bypassed_at';
                                reviewerType = '固定人员';
                            }
                        }
                        if (approveField === '' && _.includes(approval.handler_kids || approval.kids_reviewer, userId)) {
                            approveField = 'kids_approval_status';
                            bypassedField = 'kids_bypassed';
                            bypassedByField = 'kids_bypassed_by';
                            bypassReasonField = 'kids_bypass_reason';
                            bypassedAtField = 'kids_bypassed_at';
                            reviewerType = 'Kids';
                        }
                        if (!approveField) {
                            throw new Error('您不是当前步骤的审核人，无权限跳过');
                        }
                        if (material[approveField] === true || material[approveField] === 'pass') {
                            throw new Error("\u8BE5\u7D20\u6750\u7684".concat(reviewerType, "\u5BA1\u6838\u5DF2\u5B8C\u6210"));
                        }
                        materialUpdate = (_a = {},
                            _a[approveField] = 'pass',
                            _a);
                        if (bypassedField) {
                            materialUpdate[bypassedField] = true;
                            materialUpdate[bypassedByField] = userId;
                            materialUpdate[bypassReasonField] = reason;
                            materialUpdate[bypassedAtField] = new Date();
                        }
                        return [4, ctx.getObject('pepsico_material').directUpdate(recordId, materialUpdate)];
                    case 4:
                        _d.sent();
                        logger.info("User ".concat(userId, " (").concat(reviewerType, ") bypassed material ").concat(recordId));
                        return [4, ctx.getObject('pepsico_material').find({
                                filters: [
                                    ['pepsico_material_approval', '=', material.pepsico_material_approval],
                                    ['void_material', '!=', true]
                                ]
                            })];
                    case 5:
                        allMaterials = _d.sent();
                        hasPendingMaterials = false;
                        try {
                            for (allMaterials_1 = tslib_1.__values(allMaterials), allMaterials_1_1 = allMaterials_1.next(); !allMaterials_1_1.done; allMaterials_1_1 = allMaterials_1.next()) {
                                mat = allMaterials_1_1.value;
                                if (mat[approveField] !== true && mat[approveField] !== 'pass') {
                                    hasPendingMaterials = true;
                                    break;
                                }
                            }
                        }
                        catch (e_1_1) { e_1 = { error: e_1_1 }; }
                        finally {
                            try {
                                if (allMaterials_1_1 && !allMaterials_1_1.done && (_b = allMaterials_1.return)) _b.call(allMaterials_1);
                            }
                            finally { if (e_1) throw e_1.error; }
                        }
                        if (!(!hasPendingMaterials && bypassedField)) return [3, 7];
                        return [4, ctx.getObject('pepsico_material_approval').directUpdate(approval._id, (_c = {},
                                _c[bypassedField] = true,
                                _c[bypassedByField] = userId,
                                _c[bypassReasonField] = reason,
                                _c[bypassedAtField] = new Date(),
                                _c))];
                    case 6:
                        _d.sent();
                        logger.info("User ".concat(userId, " (").concat(reviewerType, ") also marked approval ").concat(approval._id, " as bypassed (no pending materials)"));
                        _d.label = 7;
                    case 7: return [4, this.submitMaterial.call(this, ctx, recordId, 'pass')];
                    case 8:
                        _d.sent();
                        return [2, {
                                success: true,
                                message: "\u5DF2\u8DF3\u8FC7\u8BE5\u7D20\u6750\u7684".concat(reviewerType, "\u5BA1\u6838")
                            }];
                }
            });
        });
    }
};
//# sourceMappingURL=bypassMaterial.js.map