"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.activateBulkWorksheetZonePicking = exports.activateWorksheetZonePicking = void 0;
const worksheet_base_1 = require("@things-factory/worksheet-base");
const sales_base_1 = require("@things-factory/sales-base");
const typeorm_1 = require("typeorm");
exports.activateWorksheetZonePicking = {
    async activateWorksheetZonePicking(_, { name }, context) {
        const { user, domain, tx } = context.state;
        const type = worksheet_base_1.WORKSHEET_TYPE.ZONE_PICKING;
        return activateWorksheet({ tx, user, domain, name: [name], type });
    }
};
exports.activateBulkWorksheetZonePicking = {
    async activateBulkWorksheetZonePicking(_, { names }, context) {
        const { user, domain, tx } = context.state;
        const type = worksheet_base_1.WORKSHEET_TYPE.ZONE_PICKING;
        return activateWorksheet({ tx, user, domain, name: names, type });
    }
};
async function activateWorksheet(params) {
    const { tx, user, domain, name, type } = params;
    try {
        //fetch worksheet(s)
        const worksheets = await tx.getRepository(worksheet_base_1.Worksheet).find({
            where: {
                domain,
                status: worksheet_base_1.WORKSHEET_STATUS.DEACTIVATED,
                name: (0, typeorm_1.In)(name),
                type
            },
            relations: [
                'worksheetDetails',
                'worksheetDetails.targetInventory',
                'worksheetDetails.targetInventory.releaseGood'
            ]
        });
        if (!worksheets || worksheets.length === 0) {
            throw new Error('Worksheet(s) not found');
        }
        const updatedWorksheets = await Promise.all(worksheets.map(async (worksheet) => {
            //update worksheet
            await tx.getRepository(worksheet_base_1.Worksheet).update({ id: worksheet.id }, {
                status: worksheet_base_1.WORKSHEET_STATUS.EXECUTING,
                startedAt: new Date(),
                updater: user
            });
            //update worksheet details
            const wsdIds = worksheet.worksheetDetails.map(item => item.id);
            await tx.getRepository(worksheet_base_1.WorksheetDetail).update({ id: (0, typeorm_1.In)(wsdIds) }, {
                status: worksheet_base_1.WORKSHEET_STATUS.EXECUTING,
                updater: user,
                updatedAt: new Date()
            });
            //update oi
            let targetInventoryIds = worksheet.worksheetDetails.map(item => item.targetInventory.id);
            const targetInventory = await tx.getRepository(sales_base_1.OrderInventory).find({
                where: { id: (0, typeorm_1.In)(targetInventoryIds) },
                relations: ['releaseGood']
            });
            const oiIds = targetInventory.map(item => item.id);
            await tx.getRepository(sales_base_1.OrderInventory).update({ id: (0, typeorm_1.In)(oiIds) }, {
                status: sales_base_1.ORDER_STATUS.PROCESSING,
                updater: user,
                updatedAt: new Date()
            });
            //update ro
            const roIds = [...new Set(targetInventory.map(item => item.releaseGood.id))];
            await tx.getRepository(sales_base_1.ReleaseGood).update({ id: (0, typeorm_1.In)(roIds) }, {
                status: sales_base_1.ORDER_STATUS.PROCESSING,
                updater: user,
                updatedAt: new Date()
            });
            //fetch updated worksheet
            let updatedWorksheet = await tx.getRepository(worksheet_base_1.Worksheet).findOne({
                where: { id: worksheet.id },
                relations: [
                    'worksheetDetails',
                    'worksheetDetails.targetInventory',
                    'worksheetDetails.targetInventory.releaseGood'
                ]
            });
            return updatedWorksheet;
        }));
        return updatedWorksheets;
    }
    catch (error) {
        throw error;
    }
}
//# sourceMappingURL=activate-zone-worksheet.js.map