feat(articlesupplier): add sort column, reorder method, and update queries and schema

This commit is contained in:
seb
2026-09-17 12:00:41 +02:00
parent 4c196c7209
commit af52ff595f
16 changed files with 264 additions and 78 deletions

View File

@@ -2,6 +2,7 @@ import {
CHECK_EXISTS,
CREATE as CREATE_ARTICLE_SUPPLIER,
GET_BY_ID_AND_ARTICLE_ID,
GET_NEXT_SORT,
} from '../../sql/articlesupplier.js';
import { CREATE as CREATE_PRICE } from '../../sql/price.js';
import request from '../request.js';
@@ -29,10 +30,11 @@ export default function create(pool) {
* @param {string} [input.sku] - The supplier's item number/SKU.
* @param {number} [input.pu] - Packaging unit (defaults to 1).
* @param {number|null} [input.stock] - Current stock at the supplier.
* @param {number} [input.sort] - Display sort order (defaults to next sort).
* @param {number|null} [input.netPrice] - Net purchase price.
* @param {number} [input.taxId] - The tax id for the purchase price.
* @param {number} [input.taxRate] - The tax rate in percent (used if taxId is not provided).
* @returns {Promise<{id: number, articleId: number, entityId: number, supplierName: string|null, sku: string|null, pu: number, stock: number|null, priceId: number|null, price: {net: number, taxId: number, taxRate?: number}|null}>} The created assignment.
* @returns {Promise<{id: number, articleId: number, entityId: number, supplierName: string|null, sku: string|null, pu: number, stock: number|null, sort: number, priceId: number|null, price: {net: number, taxId: number, taxRate?: number}|null}>} The created assignment.
* @throws {Error} If input is invalid, article or supplier is not found, or supplier is already assigned.
*
* @example
@@ -42,7 +44,7 @@ export default function create(pool) {
* pu: 1,
* netPrice: 4.20,
* });
* // => { id, articleId, entityId, supplierName, sku, pu, stock, priceId, price }
* // => { id, articleId, entityId, supplierName, sku, pu, stock, sort, priceId, price }
*/
return async (googleAuthId, sku, input = {}) => {
const entityId = input.entityId;
@@ -63,6 +65,12 @@ export default function create(pool) {
throw new Error('Invalid stock');
}
if (input.sort !== undefined && input.sort !== null) {
if (!Number.isFinite(input.sort)) {
throw new Error('Invalid sort');
}
}
const netPrice =
input.netPrice === undefined || input.netPrice === null
? null
@@ -89,6 +97,14 @@ export default function create(pool) {
await tx.begin();
try {
let sort = input.sort;
if (sort === undefined || sort === null) {
const sortRes = await request(pool, tx)
.input('articleId', articleId)
.query(GET_NEXT_SORT);
sort = sortRes.recordset[0].sort;
}
let priceId = null;
if (netPrice !== null) {
@@ -130,6 +146,7 @@ export default function create(pool) {
.input('sku', supplierSku)
.input('stock', stock)
.input('pu', pu)
.input('sort', sort)
.query(CREATE_ARTICLE_SUPPLIER);
const createdId = createdRes.recordset[0].id;