"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.arrivalNoticesByStatusResolver = void 0;
const typeorm_1 = require("typeorm");
const biz_base_1 = require("@things-factory/biz-base");
const sales_base_1 = require("@things-factory/sales-base");
const shell_1 = require("@things-factory/shell");
exports.arrivalNoticesByStatusResolver = {
    async arrivalNoticesByStatus(_, params, context) {
        var _a;
        const { domain, user } = context.state;
        const bizplaceId = ((_a = params.filters.find((filter) => filter.name === 'bizplace')) === null || _a === void 0 ? void 0 : _a.value) || 'ALL';
        params.filters = params.filters.filter(filter => filter.name !== 'bizplace');
        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 qb = (0, typeorm_1.getRepository)(sales_base_1.ArrivalNotice).createQueryBuilder('an');
        (0, shell_1.buildQuery)(qb, params, context);
        return qb
            .select('an.status', 'status')
            .addSelect('COUNT(*) AS count')
            .andWhere(`an.bizplace_id IN (${bizplaceIds})`)
            .groupBy('an.status')
            .getRawMany();
    }
};
//# sourceMappingURL=arrival-notices-by-status.js.map