feat(schema): replace manifestarticle article_id FK with sku, rename articlesupplier sku/stock fields
This commit is contained in:
@@ -1,11 +1,11 @@
|
||||
import { CREATE_LINE, RESOLVE_ARTICLE_ID_BY_SKU } from '../sql/manifestDoc.js';
|
||||
import { CREATE_LINE } from '../sql/manifestDoc.js';
|
||||
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|null, customsku: string|null, customname: string|null, amount: number, net: number, rebate: number, tax: number, comment: string|null}>} The normalized lines.
|
||||
* @returns {Array<{sku: 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,16 +15,11 @@ export function normalizeLines(lines) {
|
||||
|
||||
return lines.map((entry) => {
|
||||
const sku = entry?.sku?.trim?.() ? entry.sku.trim() : null;
|
||||
let customsku = entry?.customsku?.trim?.() ? entry.customsku.trim() : null;
|
||||
let customname =
|
||||
const 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.
|
||||
// Must have either an SKU or a custom name.
|
||||
if (!sku && !customname) {
|
||||
throw new Error('Invalid lines');
|
||||
}
|
||||
|
||||
@@ -47,7 +42,7 @@ export function normalizeLines(lines) {
|
||||
throw new Error('Invalid lines');
|
||||
}
|
||||
|
||||
return { sku, customsku, customname, amount, net, rebate, tax, comment };
|
||||
return { sku, customname, amount, net, rebate, tax, comment };
|
||||
});
|
||||
}
|
||||
|
||||
@@ -79,9 +74,7 @@ export function parseDate(value) {
|
||||
export function shapeLine(row) {
|
||||
return {
|
||||
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,
|
||||
@@ -91,28 +84,14 @@ export function shapeLine(row) {
|
||||
};
|
||||
}
|
||||
|
||||
async function resolveArticleId(pool, tx, rootCategoryId, sku) {
|
||||
const result = await request(pool, tx)
|
||||
.input('rootCategoryId', rootCategoryId)
|
||||
.input('sku', sku)
|
||||
.query(RESOLVE_ARTICLE_ID_BY_SKU);
|
||||
|
||||
const articleId = result.recordset[0]?.id;
|
||||
if (!articleId) {
|
||||
throw new Error('Article not found');
|
||||
}
|
||||
return articleId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Inserts manifest lines for a document, resolving each SKU to an article id
|
||||
* Inserts manifest lines for a document, storing each SKU directly
|
||||
* and ensuring the tax rate.
|
||||
*
|
||||
* @param {object} pool - The mssql connection pool.
|
||||
* @param {object} tx - The transaction.
|
||||
* @param {object} params - The insert parameters.
|
||||
* @param {number} params.manifestId - The manifest id.
|
||||
* @param {number} params.rootCategoryId - The company root category id.
|
||||
* @param {Array<object>} params.lines - The normalized lines.
|
||||
* @param {Function} params.ensureTax - The tax ensure function.
|
||||
* @returns {Promise<void>}
|
||||
@@ -120,19 +99,15 @@ async function resolveArticleId(pool, tx, rootCategoryId, sku) {
|
||||
export async function insertLines(
|
||||
pool,
|
||||
tx,
|
||||
{ manifestId, rootCategoryId, lines, ensureTax },
|
||||
{ manifestId, lines, ensureTax },
|
||||
) {
|
||||
for (const line of lines) {
|
||||
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('sku', line.sku ?? null)
|
||||
.input('customname', line.customname ?? null)
|
||||
.input('amount', line.amount)
|
||||
.input('net', line.net)
|
||||
.input('comment', line.comment)
|
||||
@@ -141,3 +116,4 @@ export async function insertLines(
|
||||
.query(CREATE_LINE);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user