"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.elcclOnhandInventoryCounterResolver = void 0;
const biz_base_1 = require("@things-factory/biz-base");
const typeorm_1 = require("typeorm");
const warehouse_base_1 = require("@things-factory/warehouse-base");
exports.elcclOnhandInventoryCounterResolver = {
    async elcclOnhandInventoryCounter(_, params, context) {
        let bizplaces = await (0, biz_base_1.getPermittedBizplaceIds)(context.state.domain, context.state.user);
        bizplaces = bizplaces.map(bizplace => {
            return "'" + bizplace.trim() + "'";
        }).join(',');
        const result = await (0, typeorm_1.getRepository)(warehouse_base_1.Inventory).query(`
      select sum(qty) as total from (
          select count(pallet_id) as qty from inventories i2
          where status not in ('INTRANSIT', 'TERMINATED', 'DELETED')
          and reusable_pallet_id is null
          and bizplace_id in (${bizplaces})
          and domain_id = '${context.state.domain.id}'
          union all
          select count(distinct ref_order_id) as qty from inventories i2
          where status not in ('INTRANSIT', 'TERMINATED', 'DELETED')
          and reusable_pallet_id notnull
          and ref_order_id notnull
          and bizplace_id in (${bizplaces})
          and domain_id = '${context.state.domain.id}'
          group by reusable_pallet_id
      ) as foo
    `);
        const total = parseInt(result[0].total);
        return total;
    }
};
//# sourceMappingURL=elccl-onhand-inventory-counter.js.map