This commit is contained in:
seb
2026-09-30 20:12:33 +02:00
parent 894f72eba5
commit 69d172d49b
2 changed files with 446 additions and 4 deletions

View File

@@ -62,21 +62,37 @@ export default function adjust(provider) {
const date = input.date ? parseDate(input.date) ?? new Date() : new Date();
const createdAt = new Date();
const isSingle = !Array.isArray(input.lines);
const normalizedLines = rawLines.map((line, idx) => {
const sku = typeof line.sku === 'string' ? line.sku.trim() : '';
const itemLabel = isSingle ? '' : ` for item ${idx + 1}`;
if (line.sku == null) {
throw new Error(`SKU required${itemLabel}`);
}
if (typeof line.sku !== 'string') {
const val = typeof line.sku === 'object' ? JSON.stringify(line.sku) : String(line.sku);
throw new Error(`SKU must be a string${itemLabel} (received ${typeof line.sku}: ${val})`);
}
const sku = line.sku.trim();
if (!sku) {
throw new Error(`SKU required for item ${idx + 1}`);
throw new Error(`SKU cannot be empty${itemLabel}`);
}
const rawWhId = line.warehouseId ?? line.warehouse_id ?? defaultWarehouseId;
if (rawWhId == null) {
throw new Error(`Warehouse required${itemLabel} (${sku})`);
}
const warehouseId = Number(rawWhId);
if (!Number.isInteger(warehouseId) || warehouseId <= 0) {
throw new Error(`Warehouse required for item ${idx + 1} (${sku})`);
throw new Error(`Warehouse must be a positive integer${itemLabel} (${sku})`);
}
if (line.amount == null) {
throw new Error(`Amount required${itemLabel} (${sku})`);
}
const amount = Number(line.amount);
if (!Number.isFinite(amount) || amount === 0) {
throw new Error(`Invalid amount for item ${idx + 1} (${sku}): must be non-zero number`);
throw new Error(`Invalid amount${itemLabel} (${sku}): must be non-zero number`);
}
const text = (line.text ?? line.transactionText ?? line.transaction_text ?? defaultText)?.trim?.() ||