"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.myLoadingAssignmentStatus = void 0;
const typeorm_1 = require("typeorm");
const worksheet_base_1 = require("@things-factory/worksheet-base");
const sales_base_1 = require("@things-factory/sales-base");
exports.myLoadingAssignmentStatus = {
    async myLoadingAssignmentStatus(_parentObj, _params, context) {
        const { domain, user } = context.state;
        // Fetch Release Orders where sorter id = user id
        const releaseOrders = await (0, typeorm_1.getRepository)(sales_base_1.ReleaseGood).find({
            where: { domain, sortedBy: user }
        });
        // Fetch Worksheets where type = loading and release good id is in the fetched release orders
        const loadingWorksheets = await (0, typeorm_1.getRepository)(worksheet_base_1.Worksheet).find({
            where: {
                domain,
                type: worksheet_base_1.WORKSHEET_TYPE.LOADING,
                releaseGood: (0, typeorm_1.In)(releaseOrders.map(ro => ro.id)),
                status: (0, typeorm_1.In)([worksheet_base_1.WORKSHEET_STATUS.DEACTIVATED, worksheet_base_1.WORKSHEET_STATUS.EXECUTING])
            }
        });
        return (loadingWorksheets || []).reduce((assignmentStatus, worksheet) => {
            if (worksheet.status === worksheet_base_1.WORKSHEET_STATUS.DEACTIVATED)
                assignmentStatus.pending.push(worksheet);
            if (worksheet.status === worksheet_base_1.WORKSHEET_STATUS.EXECUTING)
                assignmentStatus.loading.push(worksheet);
            return assignmentStatus;
        }, { userId: user.id, pending: [], loading: [] });
    }
};
//# sourceMappingURL=my-loading-assignment-status.js.map