feat(schema): replace manifestarticle article_id FK with sku, rename articlesupplier sku/stock fields
This commit is contained in:
@@ -3,10 +3,8 @@ import {
|
||||
DELETE_BY_ID,
|
||||
DELETE_OPTIONSLIST,
|
||||
DELETE_PICS_BY_ARTICLE,
|
||||
DETACH_MANIFEST_ARTICLES,
|
||||
GET_CHILD_COUNT,
|
||||
GET_LIST_IDS,
|
||||
GET_NAME_FOR_DELETE,
|
||||
} from '../../sql/articleDelete.js';
|
||||
import resolveArticleBySku from './resolveArticleBySku.js';
|
||||
import request from '../request.js';
|
||||
@@ -26,7 +24,7 @@ export default function del(pool) {
|
||||
* await store.article.delete(42, 'SKU-1');
|
||||
*/
|
||||
return async (googleAuthId, sku) => {
|
||||
const { articleId, languageId } = await resolve(googleAuthId, sku);
|
||||
const { articleId } = await resolve(googleAuthId, sku);
|
||||
|
||||
const childCount = await pool
|
||||
.request()
|
||||
@@ -37,15 +35,6 @@ export default function del(pool) {
|
||||
throw new Error('Article has children');
|
||||
}
|
||||
|
||||
const nameResult = await pool
|
||||
.request()
|
||||
.input('articleId', articleId)
|
||||
.input('languageId', languageId)
|
||||
.query(GET_NAME_FOR_DELETE);
|
||||
|
||||
const name = nameResult.recordset[0]?.name ?? sku;
|
||||
const suffix = `article ${name} deleted`;
|
||||
|
||||
const listIds = await pool
|
||||
.request()
|
||||
.input('articleId', articleId)
|
||||
@@ -57,11 +46,6 @@ export default function del(pool) {
|
||||
await tx.begin();
|
||||
|
||||
try {
|
||||
await request(pool, tx)
|
||||
.input('articleId', articleId)
|
||||
.input('suffix', suffix)
|
||||
.query(DETACH_MANIFEST_ARTICLES);
|
||||
|
||||
await request(pool, tx)
|
||||
.input('articleId', articleId)
|
||||
.query(DELETE_PICS_BY_ARTICLE);
|
||||
|
||||
@@ -41,7 +41,7 @@ export default function create(pool) {
|
||||
|
||||
const lines = normalizeLines(body.lines ?? []);
|
||||
|
||||
const { companyId, rootCategoryId } = await access(googleAuthId);
|
||||
const { companyId } = await access(googleAuthId);
|
||||
|
||||
const tx = pool.transaction();
|
||||
await tx.begin();
|
||||
@@ -69,7 +69,6 @@ export default function create(pool) {
|
||||
|
||||
await insertLines(pool, tx, {
|
||||
manifestId,
|
||||
rootCategoryId,
|
||||
lines,
|
||||
ensureTax,
|
||||
});
|
||||
|
||||
@@ -56,7 +56,7 @@ export default function update(pool) {
|
||||
normalizedLines = normalizeLines(patch.lines);
|
||||
}
|
||||
|
||||
const { companyId, rootCategoryId } = await access(googleAuthId);
|
||||
const { companyId } = await access(googleAuthId);
|
||||
|
||||
const tx = pool.transaction();
|
||||
await tx.begin();
|
||||
@@ -125,7 +125,6 @@ export default function update(pool) {
|
||||
|
||||
await insertLines(pool, tx, {
|
||||
manifestId: id,
|
||||
rootCategoryId,
|
||||
lines: normalizedLines,
|
||||
ensureTax,
|
||||
});
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -37,7 +37,7 @@ export default function create(pool) {
|
||||
: null;
|
||||
|
||||
const lines = normalizeLines(body.lines ?? []);
|
||||
const { companyId, rootCategoryId } = await access(googleAuthId);
|
||||
const { companyId } = await access(googleAuthId);
|
||||
|
||||
const tx = pool.transaction();
|
||||
await tx.begin();
|
||||
@@ -63,7 +63,6 @@ export default function create(pool) {
|
||||
|
||||
await insertLines(pool, tx, {
|
||||
manifestId,
|
||||
rootCategoryId,
|
||||
lines,
|
||||
ensureTax,
|
||||
});
|
||||
|
||||
@@ -52,7 +52,7 @@ export default function update(pool) {
|
||||
normalizedLines = normalizeLines(patch.lines);
|
||||
}
|
||||
|
||||
const { companyId, rootCategoryId } = await access(googleAuthId);
|
||||
const { companyId } = await access(googleAuthId);
|
||||
|
||||
const tx = pool.transaction();
|
||||
await tx.begin();
|
||||
@@ -121,7 +121,6 @@ export default function update(pool) {
|
||||
|
||||
await insertLines(pool, tx, {
|
||||
manifestId: id,
|
||||
rootCategoryId,
|
||||
lines: normalizedLines,
|
||||
ensureTax,
|
||||
});
|
||||
|
||||
@@ -37,7 +37,7 @@ export default function create(pool) {
|
||||
: null;
|
||||
|
||||
const lines = normalizeLines(body.lines ?? []);
|
||||
const { companyId, rootCategoryId } = await access(googleAuthId);
|
||||
const { companyId } = await access(googleAuthId);
|
||||
|
||||
const tx = pool.transaction();
|
||||
await tx.begin();
|
||||
@@ -63,7 +63,6 @@ export default function create(pool) {
|
||||
|
||||
await insertLines(pool, tx, {
|
||||
manifestId,
|
||||
rootCategoryId,
|
||||
lines,
|
||||
ensureTax,
|
||||
});
|
||||
|
||||
@@ -52,7 +52,7 @@ export default function update(pool) {
|
||||
normalizedLines = normalizeLines(patch.lines);
|
||||
}
|
||||
|
||||
const { companyId, rootCategoryId } = await access(googleAuthId);
|
||||
const { companyId } = await access(googleAuthId);
|
||||
|
||||
const tx = pool.transaction();
|
||||
await tx.begin();
|
||||
@@ -121,7 +121,6 @@ export default function update(pool) {
|
||||
|
||||
await insertLines(pool, tx, {
|
||||
manifestId: id,
|
||||
rootCategoryId,
|
||||
lines: normalizedLines,
|
||||
ensureTax,
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user