feat: Enhance outbound and receiving update functionalities with inventory adjustments

- Updated the `update` function in the outbound controller to include detailed inventory adjustments when modifying outbound records, ensuring accurate stock management.
- Implemented rollback mechanisms for both outbound and receiving updates to maintain data integrity in case of errors.
- Enhanced the `deleteOutbound` function to include inventory recovery and historical logging for deleted outbound records.
- Introduced a new utility function `adjustInventory` to handle inventory changes consistently across different controllers.
- Improved error handling and logging for better traceability during outbound and receiving operations.
This commit is contained in:
kjs
2026-04-20 14:14:24 +09:00
parent 48b9ba3d2a
commit 9737805bf9
48 changed files with 1256 additions and 357 deletions

View File

@@ -384,26 +384,33 @@ export async function getRoutingDetails(req: AuthenticatedRequest, res: Response
const rows = result.rows;
const detailIds = rows.map((r: any) => r.id).filter(Boolean);
let mappingByDetail: Record<string, string[]> = {};
let idsByDetail: Record<string, string[]> = {};
let codesByDetail: Record<string, string[]> = {};
if (detailIds.length > 0) {
const mapRes = await pool.query(
`SELECT routing_detail_id, subcontractor_code
FROM item_routing_subcontractor
WHERE routing_detail_id = ANY($1::uuid[])
ORDER BY seq_order`,
`SELECT irs.routing_detail_id, irs.subcontractor_id, sm.subcontractor_code
FROM item_routing_subcontractor irs
LEFT JOIN subcontractor_mng sm ON irs.subcontractor_id = sm.id
WHERE irs.routing_detail_id = ANY($1::varchar[])
ORDER BY irs.seq_order`,
[detailIds]
);
for (const m of mapRes.rows) {
const key = String(m.routing_detail_id);
if (!mappingByDetail[key]) mappingByDetail[key] = [];
mappingByDetail[key].push(m.subcontractor_code);
(idsByDetail[key] ||= []).push(m.subcontractor_id);
if (m.subcontractor_code) (codesByDetail[key] ||= []).push(m.subcontractor_code);
}
}
const enriched = rows.map((r: any) => {
const list = mappingByDetail[String(r.id)] || [];
// 레거시 폴백: 매핑이 비어있고 legacy 단일 컬럼에 값이 있으면 배열로 포장
if (list.length === 0 && r.outsource_supplier) list.push(r.outsource_supplier);
return { ...r, outsource_supplier_list: list };
const ids = idsByDetail[String(r.id)] || [];
const codes = codesByDetail[String(r.id)] || [];
// 레거시 폴백: 매핑이 비어있고 legacy 단일 컬럼(code)에 값이 있으면 code 배열로 반환
const legacyCodes = ids.length === 0 && r.outsource_supplier ? [r.outsource_supplier] : codes;
return {
...r,
outsource_supplier_ids: ids,
outsource_supplier_list: legacyCodes, // 하위호환 별칭 (code 배열)
};
});
return res.json({ success: true, data: enriched });
@@ -440,24 +447,36 @@ export async function saveRoutingDetails(req: AuthenticatedRequest, res: Respons
);
for (const d of details) {
const suppliers: string[] = Array.isArray(d.outsource_supplier_list)
? d.outsource_supplier_list.filter((s: any) => typeof s === "string" && s.trim() !== "")
: (d.outsource_supplier ? [d.outsource_supplier] : []);
const primaryLegacy = suppliers[0] || d.outsource_supplier || "";
const supplierIds: string[] = Array.isArray(d.outsource_supplier_ids)
? d.outsource_supplier_ids.filter((s: any) => typeof s === "string" && s.trim() !== "")
: [];
// legacy code 해석: 첫 번째 subcontractor_id → subcontractor_code 조회
let legacyCode = "";
if (supplierIds.length > 0) {
const codeRes = await client.query(
`SELECT subcontractor_code FROM subcontractor_mng WHERE id=$1 LIMIT 1`,
[supplierIds[0]]
);
legacyCode = codeRes.rows[0]?.subcontractor_code || "";
} else if (d.outsource_supplier) {
// 프론트가 아직 id 없이 code만 보낸 경우(레거시 호환)
legacyCode = d.outsource_supplier;
}
const insertRes = await client.query(
`INSERT INTO item_routing_detail (id, company_code, routing_version_id, seq_no, process_code, is_required, is_fixed_order, work_type, standard_time, outsource_supplier, writer)
VALUES (gen_random_uuid()::text, $1, $2, $3, $4, $5, $6, $7, $8, $9, $10)
RETURNING id`,
[companyCode, versionId, d.seq_no, d.process_code, d.is_required || "Y", d.is_fixed_order || "Y", d.work_type || "내부", d.standard_time || "0", primaryLegacy, writer]
[companyCode, versionId, d.seq_no, d.process_code, d.is_required || "Y", d.is_fixed_order || "Y", d.work_type || "내부", d.standard_time || "0", legacyCode, writer]
);
const newDetailId = insertRes.rows[0].id;
for (let i = 0; i < suppliers.length; i++) {
for (let i = 0; i < supplierIds.length; i++) {
await client.query(
`INSERT INTO item_routing_subcontractor (id, company_code, routing_detail_id, subcontractor_code, seq_order)
VALUES (gen_random_uuid(), $1, $2, $3, $4)`,
[companyCode, newDetailId, suppliers[i], i]
`INSERT INTO item_routing_subcontractor (id, company_code, routing_detail_id, subcontractor_id, seq_order)
VALUES (gen_random_uuid()::text, $1, $2, $3, $4)`,
[companyCode, newDetailId, supplierIds[i], i]
);
}
}