"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.warehouseInventoryAdjustment = void 0;
const integration_base_1 = require("@things-factory/integration-base");
const sales_base_1 = require("@things-factory/sales-base");
const warehouse_base_1 = require("@things-factory/warehouse-base");
const warehouse_base_2 = require("@things-factory/warehouse-base");
const worksheet_base_1 = require("@things-factory/worksheet-base");
const typeorm_1 = require("typeorm");
exports.warehouseInventoryAdjustment = {
    async warehouseInventoryAdjustment(_, { patches }, context) {
        try {
            const item = await (0, warehouse_base_2.approveInventoryChanges)(patches, context);
            // Retrieve order inventory
            const { tx } = context.state;
            const _inventoryChanges = await tx.getRepository(warehouse_base_2.InventoryChange).find({
                where: { id: (0, typeorm_1.In)(patches.map(item => item.id)) },
                relations: [
                    'inventory',
                    'inventory.bizplace',
                    'inventory.bizplace.domain',
                    'inventory.product',
                    'inventory.location',
                    'inventory.warehouse',
                    'bizplace',
                    'bizplace.domain',
                    'bizplace.company',
                    'bizplace.company.domain',
                    'product',
                    'product.productDetails',
                    'product.productDetails.childProductDetail',
                    'location'
                ],
                order: {
                    createdAt: 'ASC'
                }
            });
            if (_inventoryChanges.length > 0) {
                if (_inventoryChanges.some(ic => ic.transactionType == warehouse_base_1.INVENTORY_TRANSACTION_TYPE.MISSING)) {
                    //handle missing inventory
                    const invId = _inventoryChanges
                        .filter((ic) => ic.transactionType == warehouse_base_1.INVENTORY_TRANSACTION_TYPE.MISSING)
                        .map((ic) => ic.inventory.id);
                    const orderInventory = await tx.getRepository(sales_base_1.OrderInventory).find({
                        where: { inventory: (0, typeorm_1.In)(invId), status: (0, typeorm_1.In)([sales_base_1.ORDER_INVENTORY_STATUS.PICKING, sales_base_1.ORDER_INVENTORY_STATUS.PROCESSING]) }
                    });
                    await tx.getRepository(sales_base_1.OrderInventory).update({
                        inventory: (0, typeorm_1.In)(invId),
                        status: (0, typeorm_1.In)([sales_base_1.ORDER_INVENTORY_STATUS.PICKING, sales_base_1.ORDER_INVENTORY_STATUS.PROCESSING])
                    }, { status: sales_base_1.ORDER_INVENTORY_STATUS.MISSING });
                    const orderInvId = orderInventory.map((oi) => oi.id);
                    const releaseGoodId = orderInventory.map((roId) => roId.releaseGoodId);
                    await tx.getRepository(worksheet_base_1.WorksheetDetail).update({
                        targetInventory: (0, typeorm_1.In)(orderInvId),
                        status: (0, typeorm_1.In)([worksheet_base_1.WORKSHEET_STATUS.EXECUTING, worksheet_base_1.WORKSHEET_STATUS.DEACTIVATED])
                    }, { status: worksheet_base_1.WORKSHEET_STATUS.MISSING });
                    await tx.getRepository(worksheet_base_1.Worksheet).update({
                        releaseGood: (0, typeorm_1.In)(releaseGoodId),
                        status: worksheet_base_1.WORKSHEET_STATUS.EXECUTING,
                        type: (0, typeorm_1.Not)((0, typeorm_1.In)([worksheet_base_1.WORKSHEET_TYPE.ZONE_PICKING, worksheet_base_1.WORKSHEET_TYPE.LOADING]))
                    }, { status: worksheet_base_1.WORKSHEET_STATUS.DEACTIVATED });
                }
                if (_inventoryChanges.some(ic => ic.transactionType == warehouse_base_1.INVENTORY_TRANSACTION_TYPE.EXPIRED)) {
                    //handle expired inventory
                    const invId = _inventoryChanges
                        .filter((ic) => ic.transactionType == warehouse_base_1.INVENTORY_TRANSACTION_TYPE.EXPIRED)
                        .map((ic) => ic.inventory.id);
                    const orderInventory = await tx.getRepository(sales_base_1.OrderInventory).find({
                        where: { inventory: (0, typeorm_1.In)(invId), status: (0, typeorm_1.In)([sales_base_1.ORDER_INVENTORY_STATUS.PICKING, sales_base_1.ORDER_INVENTORY_STATUS.PROCESSING]) }
                    });
                    const inventoryId = orderInventory.map((oi) => oi.inventoryId);
                    const releaseGoodId = orderInventory.map((roId) => roId.releaseGoodId);
                    await tx.getRepository(sales_base_1.ReleaseGood).update({
                        id: (0, typeorm_1.In)(releaseGoodId),
                        status: (0, typeorm_1.In)([sales_base_1.ORDER_STATUS.PROCESSING, sales_base_1.ORDER_STATUS.PARTIAL_PROCESSING])
                    }, { status: sales_base_1.ORDER_STATUS.OBSOLETE });
                    await tx.getRepository(warehouse_base_2.Inventory).update({
                        id: (0, typeorm_1.In)(inventoryId),
                        status: warehouse_base_2.INVENTORY_STATUS.STORED
                    }, { obsolete: true });
                    await tx.getRepository(warehouse_base_2.InventoryChange).update({
                        inventory: (0, typeorm_1.In)(inventoryId),
                        status: 'PENDING'
                    }, { status: 'APPROVED' });
                }
                for (const invChange of _inventoryChanges) {
                    (0, integration_base_1.webhookHandler)(invChange, invChange.inventory.bizplace, integration_base_1.WebhookEventsEnum.InventoryAdjusted);
                }
            }
            return item;
        }
        catch (error) {
            throw error;
        }
    }
};
//# sourceMappingURL=warehouse-intentory-adjustment.js.map