"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.bypassApproval = void 0;
var tslib_1 = require("tslib");
var consts_1 = require("../methods/consts");
var _ = require("lodash");
exports.bypassApproval = {
    rest: {
        method: 'POST',
        fullPath: '/api/pepsico-content/approve/bypass-approval/:recordId'
    },
    handler: function (ctx) {
        return tslib_1.__awaiter(this, void 0, void 0, function () {
            var logger, recordId, reason, userId, approval, approveField, bypassedField, bypassedByField, bypassReasonField, bypassedAtField, reviewerType, materials, materials_1, materials_1_1, material, materialUpdate, e_1_1, processFilters, processRecords, processRecords_1, processRecords_1_1, processRecord, e_2_1;
            var _a, e_1, _b, _c, e_2, _d;
            return tslib_1.__generator(this, function (_e) {
                switch (_e.label) {
                    case 0:
                        ctx.getObject = this.getObject;
                        return [4, this.getLogger()];
                    case 1:
                        logger = _e.sent();
                        ctx.logger = logger;
                        recordId = ctx.params.recordId;
                        reason = ctx.params.reason;
                        userId = ctx.meta.user.userId;
                        if (!reason) {
                            throw new Error('请提供跳过原因');
                        }
                        return [4, ctx.getObject('pepsico_material_approval').findOne(recordId)];
                    case 2:
                        approval = _e.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) {
                                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) {
                                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) {
                                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) {
                                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';
                        }
                        console.log('approveField===>', approveField);
                        if (!approveField) {
                            throw new Error('您不是当前步骤的审核人，无权限跳过');
                        }
                        if (!bypassedField) return [3, 4];
                        return [4, ctx.getObject('pepsico_material_approval').directUpdate(recordId, (_a = {},
                                _a[bypassedField] = true,
                                _a[bypassedByField] = userId,
                                _a[bypassReasonField] = reason,
                                _a[bypassedAtField] = new Date(),
                                _a))];
                    case 3:
                        _e.sent();
                        _e.label = 4;
                    case 4: return [4, ctx.getObject('pepsico_material').find({
                            filters: [
                                ['pepsico_material_approval', '=', recordId],
                                ['void_material', '!=', true]
                            ]
                        })];
                    case 5:
                        materials = _e.sent();
                        _e.label = 6;
                    case 6:
                        _e.trys.push([6, 11, 12, 13]);
                        materials_1 = tslib_1.__values(materials), materials_1_1 = materials_1.next();
                        _e.label = 7;
                    case 7:
                        if (!!materials_1_1.done) return [3, 10];
                        material = materials_1_1.value;
                        if (!(material[approveField] !== true && material[approveField] !== 'pass')) return [3, 9];
                        materialUpdate = (_c = {},
                            _c[approveField] = 'pass',
                            _c);
                        if (bypassedField) {
                            materialUpdate[bypassedField] = true;
                            materialUpdate[bypassedByField] = userId;
                            materialUpdate[bypassReasonField] = reason;
                            materialUpdate[bypassedAtField] = new Date();
                        }
                        return [4, ctx.getObject('pepsico_material').directUpdate(material._id, materialUpdate)];
                    case 8:
                        _e.sent();
                        logger.info("User ".concat(userId, " (").concat(reviewerType, ") bypassed material ").concat(material._id, " via approval bypass"));
                        _e.label = 9;
                    case 9:
                        materials_1_1 = materials_1.next();
                        return [3, 7];
                    case 10: return [3, 13];
                    case 11:
                        e_1_1 = _e.sent();
                        e_1 = { error: e_1_1 };
                        return [3, 13];
                    case 12:
                        try {
                            if (materials_1_1 && !materials_1_1.done && (_b = materials_1.return)) _b.call(materials_1);
                        }
                        finally { if (e_1) throw e_1.error; }
                        return [7];
                    case 13:
                        processFilters = [
                            ["owner", "=", userId],
                            ["pepsico_material_approval", "=", recordId],
                            ["current_step", "=", approval.current_step],
                            ["space", "=", approval.space],
                            ["approved", "=", 'progess']
                        ];
                        if (approveField) {
                            if (approveField === 'kids_approval_status') {
                                processFilters.push(["type", "=", 'Kids']);
                            }
                            else if (approveField === 'ca_approval_status') {
                                processFilters.push(["type", "=", 'CA']);
                            }
                            else if (approveField === 'sra_approval_status') {
                                processFilters.push(["type", "=", 'SRA']);
                            }
                            else if (approveField === 'ns_approval_status') {
                                processFilters.push(["type", "=", 'NS']);
                            }
                            else if (approveField === 'legal_approval_status') {
                                processFilters.push(["type", "=", 'Legal']);
                            }
                            else if (approveField === 'approval_status') {
                                processFilters.push(["type", "=", 'guding']);
                            }
                            else if (approveField === 'pre_ns_approval_status') {
                                processFilters.push(["type", "=", 'PreNS']);
                            }
                            else if (approveField === 'pre_legal_approval_status') {
                                processFilters.push(["type", "=", 'PreLegal']);
                            }
                            else if (approveField === 'pre_sra_approval_status') {
                                processFilters.push(["type", "=", 'PreSRA']);
                            }
                            else if (approveField === 'pre_ca_approval_status') {
                                processFilters.push(["type", "=", 'PreCA']);
                            }
                            else if (approveField === 'pre_approval_status') {
                                processFilters.push(["type", "=", 'Preguding']);
                            }
                        }
                        return [4, ctx.getObject("pepsico_approval_process").find({
                                filters: processFilters,
                                sort: "modified asc"
                            })];
                    case 14:
                        processRecords = _e.sent();
                        _e.label = 15;
                    case 15:
                        _e.trys.push([15, 20, 21, 22]);
                        processRecords_1 = tslib_1.__values(processRecords), processRecords_1_1 = processRecords_1.next();
                        _e.label = 16;
                    case 16:
                        if (!!processRecords_1_1.done) return [3, 19];
                        processRecord = processRecords_1_1.value;
                        return [4, ctx.getObject("pepsico_approval_process").update(processRecord._id, {
                                submitted: new Date(),
                                modified: new Date(),
                                modified_by: userId,
                                approved: 'pass',
                                name: reason
                            })];
                    case 17:
                        _e.sent();
                        _e.label = 18;
                    case 18:
                        processRecords_1_1 = processRecords_1.next();
                        return [3, 16];
                    case 19: return [3, 22];
                    case 20:
                        e_2_1 = _e.sent();
                        e_2 = { error: e_2_1 };
                        return [3, 22];
                    case 21:
                        try {
                            if (processRecords_1_1 && !processRecords_1_1.done && (_d = processRecords_1.return)) _d.call(processRecords_1);
                        }
                        finally { if (e_2) throw e_2.error; }
                        return [7];
                    case 22: return [4, this.approvalContent.call(this, ctx, recordId, { approveField: approveField, materialStatus: consts_1.ApprovalStatus.pass })];
                    case 23:
                        _e.sent();
                        return [2, {
                                success: true,
                                message: "\u5DF2\u8DF3\u8FC7\u8BE5\u5BA1\u6279\u5355\u7684".concat(reviewerType, "\u5BA1\u6838")
                            }];
                }
            });
        });
    }
};
//# sourceMappingURL=bypassApproval.js.map