"use strict";
var __rest = (this && this.__rest) || function (s, e) {
    var t = {};
    for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
        t[p] = s[p];
    if (s != null && typeof Object.getOwnPropertySymbols === "function")
        for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
            if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
                t[p[i]] = s[p[i]];
        }
    return t;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.inventoryExpiryMonitorResolver = void 0;
const biz_base_1 = require("@things-factory/biz-base");
exports.inventoryExpiryMonitorResolver = {
    async inventoryExpiryMonitor(_, params, context) {
        var _a;
        const { domain, user, tx } = context.state;
        const bizplaceId = ((_a = params.filters.find((filter) => filter.name === 'bizplace')) === null || _a === void 0 ? void 0 : _a.value) || 'ALL';
        const v4 = new RegExp(/^[0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i);
        let bizplaceIds;
        if (bizplaceId && (bizplaceId == 'ALL' || bizplaceId.match(v4))) {
            let bizplaces = bizplaceId && bizplaceId != 'ALL' ? [bizplaceId] : await (0, biz_base_1.getPermittedBizplaceIds)(domain, user);
            bizplaceIds = bizplaces.map(bizplace => {
                return "'" + (bizplace === null || bizplace === void 0 ? void 0 : bizplace.trim()) + "'";
            }).join(',');
        }
        else
            throw new Error('invalid bizplaceId');
        const expInventories = await tx.query(`
      SELECT "location", sum("expired") AS "expired", SUM("threeMonths") AS "threeMonths", sum("sixMonths") AS "sixMonths", sum("nineMonths") AS "nineMonths" FROM (
        SELECT 
          CASE WHEN l.TYPE IN ('QUARANTINE','RESERVE') THEN 'QUARANTINED' ELSE 'IN SHELF' END AS "location",
          CASE WHEN i.expiration_date - now()::date <= 0 THEN 1 ELSE 0 END AS "expired",
          CASE WHEN i.expiration_date - now()::date > 0 AND i.expiration_date - now()::date <= 90 THEN 1 ELSE 0 END AS "threeMonths",
          CASE WHEN i.expiration_date - now()::date > 90 AND i.expiration_date - now()::date <= 180 THEN 1 ELSE 0 END AS "sixMonths",
          CASE WHEN i.expiration_date - now()::date > 180 AND i.expiration_date - now()::date <= 270 THEN 1 ELSE 0 END AS "nineMonths"
        FROM inventories i
        INNER JOIN locations l ON i.location_id = l.id
        WHERE i.expiration_date - now()::date <= 270
        AND i.expiration_date NOTNULL
        AND i.status = 'STORED'
        AND i.domain_id = $1
        AND i.bizplace_id in (${bizplaceIds})
      ) AS foo
      GROUP BY "location"
    `, [domain.id]);
        return {
            quarantined: getExpiryCountByStatus(expInventories, 'QUARANTINED'),
            inShelf: getExpiryCountByStatus(expInventories, 'IN SHELF')
        };
    }
};
function getExpiryCountByStatus(expInventories, location) {
    let matchedInv = expInventories.find(inv => inv.location == location);
    if (matchedInv)
        return ((_a) => {
            var { location } = _a, inv = __rest(_a, ["location"]);
            return inv;
        })(matchedInv);
    else
        return { expired: 0, threeMonths: 0, sixMonths: 0, nineMonths: 0 };
}
//# sourceMappingURL=inventory-expiry-monitor.js.map