"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.calculateSpotCheckRates = void 0;
var tslib_1 = require("tslib");
var _ = require('lodash');
exports.calculateSpotCheckRates = {
    rest: {
        method: 'POST',
        fullPath: '/api/pepsico-content/calculateSpotCheckRates'
    },
    handler: function (ctx) {
        return tslib_1.__awaiter(this, void 0, void 0, function () {
            var objectName, object, userFilters, hasUserFilters, mergeFilters, orgObj, orgs, orgMap, baseFilters, promises, deptNames, deptNames_1, deptNames_1_1, dept, deptBaseFilters, _a, count, ns_count, legal_count, sra_count, ca_count;
            var e_1, _b;
            return tslib_1.__generator(this, function (_c) {
                switch (_c.label) {
                    case 0:
                        objectName = 'pepsico_material';
                        object = this.getObject(objectName);
                        userFilters = [];
                        if (ctx.params.filters) {
                            try {
                                userFilters = typeof ctx.params.filters === 'string'
                                    ? JSON.parse(ctx.params.filters)
                                    : ctx.params.filters;
                                if (!Array.isArray(userFilters))
                                    userFilters = [];
                            }
                            catch (e) {
                                userFilters = [];
                            }
                        }
                        hasUserFilters = userFilters.length > 0;
                        mergeFilters = function (base, extra) {
                            if (extra.length === 0)
                                return base;
                            if (base.length === 0)
                                return extra;
                            return tslib_1.__spreadArray(tslib_1.__spreadArray(tslib_1.__spreadArray([], tslib_1.__read(base), false), ['and'], false), tslib_1.__read(extra), false);
                        };
                        orgObj = this.getObject('organizations');
                        return [4, orgObj.find({
                                filters: [['name', 'in', ['NS', 'Legal', 'SRA', 'CA']]],
                                fields: ['_id', 'name']
                            })];
                    case 1:
                        orgs = _c.sent();
                        orgMap = {};
                        orgs.forEach(function (o) { return orgMap[o.name] = o._id; });
                        baseFilters = [
                            ['approval_status', '=', true],
                            ['kol_classification', '=', 'Mid-tier'],
                            ['void_material', '!=', true],
                        ];
                        promises = [
                            object.count({ filters: hasUserFilters ? userFilters : baseFilters })
                        ];
                        console.log("orgMap", orgMap);
                        deptNames = ['NS', 'Legal', 'SRA', 'CA'];
                        try {
                            for (deptNames_1 = tslib_1.__values(deptNames), deptNames_1_1 = deptNames_1.next(); !deptNames_1_1.done; deptNames_1_1 = deptNames_1.next()) {
                                dept = deptNames_1_1.value;
                                if (orgMap[dept]) {
                                    deptBaseFilters = [['spot_check_departments', '=', orgMap[dept]], ['kol_classification', '=', 'Mid-tier']];
                                    promises.push(object.count({ filters: hasUserFilters ? mergeFilters(userFilters, [['spot_check_departments', '=', orgMap[dept]]]) : deptBaseFilters }));
                                }
                                else {
                                    promises.push(Promise.resolve(0));
                                }
                            }
                        }
                        catch (e_1_1) { e_1 = { error: e_1_1 }; }
                        finally {
                            try {
                                if (deptNames_1_1 && !deptNames_1_1.done && (_b = deptNames_1.return)) _b.call(deptNames_1);
                            }
                            finally { if (e_1) throw e_1.error; }
                        }
                        return [4, Promise.all(promises)];
                    case 2:
                        _a = tslib_1.__read.apply(void 0, [_c.sent(), 5]), count = _a[0], ns_count = _a[1], legal_count = _a[2], sra_count = _a[3], ca_count = _a[4];
                        return [2, {
                                pending_spot_count: count,
                                ns_materials_quantity: ns_count,
                                legal_materials_quantity: legal_count,
                                sra_materials_quantity: sra_count,
                                ca_materials_quantity: ca_count
                            }];
                }
            });
        });
    }
};
//# sourceMappingURL=calculateSpotCheckRates.js.map