feat(schema): add articlesupplier table and manifestarticle custom sku/name fields

This commit is contained in:
seb
2026-09-14 22:43:03 +02:00
parent af416246b4
commit 2e16a22c99
9 changed files with 1708 additions and 1517 deletions

View File

@@ -5,7 +5,7 @@ import request from './request.js';
* Normalizes and validates an array of manifest line inputs.
*
* @param {Array<object>} lines - The raw line inputs.
* @returns {Array<{sku: string, amount: number, net: number, rebate: number, tax: number, comment: string|null}>} The normalized lines.
* @returns {Array<{sku: string|null, customsku: string|null, customname: string|null, amount: number, net: number, rebate: number, tax: number, comment: string|null}>} The normalized lines.
* @throws {Error} If the lines are invalid.
*/
export function normalizeLines(lines) {
@@ -15,7 +15,16 @@ export function normalizeLines(lines) {
return lines.map((entry) => {
const sku = entry?.sku?.trim?.() ? entry.sku.trim() : null;
if (!sku) {
let customsku = entry?.customsku?.trim?.() ? entry.customsku.trim() : null;
let customname =
entry?.customname?.trim?.() ? entry.customname.trim() : null;
// If an article SKU is present, the custom fields must be null.
if (sku) {
customsku = null;
customname = null;
} else if (!customsku && !customname) {
// Must have either a catalog SKU or at least one custom field.
throw new Error('Invalid lines');
}
@@ -38,7 +47,7 @@ export function normalizeLines(lines) {
throw new Error('Invalid lines');
}
return { sku, amount, net, rebate, tax, comment };
return { sku, customsku, customname, amount, net, rebate, tax, comment };
});
}
@@ -72,6 +81,8 @@ export function shapeLine(row) {
id: row.id,
articleId: row.articleId ?? null,
sku: row.sku ?? null,
customsku: row.customsku ?? null,
customname: row.customname ?? null,
amount: row.amount,
net: row.net,
rebate: row.rebate,
@@ -112,17 +123,16 @@ export async function insertLines(
{ manifestId, rootCategoryId, lines, ensureTax },
) {
for (const line of lines) {
const articleId = await resolveArticleId(
pool,
tx,
rootCategoryId,
line.sku,
);
const articleId = line.sku
? await resolveArticleId(pool, tx, rootCategoryId, line.sku)
: null;
const taxId = await ensureTax(line.tax, tx);
await request(pool, tx)
.input('manifestId', manifestId)
.input('articleId', articleId)
.input('customsku', line.sku ? null : line.customsku ?? null)
.input('customname', line.sku ? null : line.customname ?? null)
.input('amount', line.amount)
.input('net', line.net)
.input('comment', line.comment)