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

@@ -69,6 +69,7 @@ import articlesupplierListByArticleSku from './store/articlesupplier/listByArtic
import articlesupplierCreate from './store/articlesupplier/create.js';
import articlesupplierUpdate from './store/articlesupplier/update.js';
import articlesupplierDelete from './store/articlesupplier/delete.js';
import articlesupplierOrder from './store/articlesupplier/order.js';
import deliveryList from './store/delivery/list.js';
import deliveryGetById from './store/delivery/getById.js';
import deliveryCreate from './store/delivery/create.js';
@@ -245,6 +246,7 @@ export default class LibDashStore {
listByArticleSku: articlesupplierListByArticleSku(pool),
create: articlesupplierCreate(pool),
update: articlesupplierUpdate(pool),
order: articlesupplierOrder(pool),
delete: articlesupplierDelete(pool),
};

View File

@@ -539,10 +539,12 @@ BEGIN
sku NVARCHAR(100) NULL,
stock FLOAT NULL,
pu FLOAT NOT NULL CONSTRAINT DF_articlesupplier_pu DEFAULT 1,
sort INT NOT NULL CONSTRAINT DF_articlesupplier_sort DEFAULT 0,
CONSTRAINT FK_articlesupplier_article FOREIGN KEY (article_id) REFERENCES article(id) ON DELETE CASCADE,
CONSTRAINT FK_articlesupplier_entity FOREIGN KEY (entity_id) REFERENCES entity(id) ON DELETE CASCADE,
CONSTRAINT FK_articlesupplier_price FOREIGN KEY (price_id) REFERENCES price(id) ON DELETE SET NULL,
CONSTRAINT UQ_articlesupplier_article_entity UNIQUE (article_id, entity_id)
CONSTRAINT FK_articlesupplier_price FOREIGN KEY (price_id) REFERENCES price(id) ON DELETE NO ACTION,
CONSTRAINT UQ_articlesupplier_article_entity UNIQUE (article_id, entity_id),
CONSTRAINT UQ_articlesupplier_article_sort UNIQUE (article_id, sort)
);
CREATE INDEX IX_articlesupplier_article_entity

View File

@@ -471,6 +471,7 @@ SELECT
asup.sku,
asup.pu,
asup.stock,
asup.sort,
asup.price_id AS priceId,
p.net AS [price.net],
p.tax_id AS [price.taxId],
@@ -480,7 +481,7 @@ SELECT
LEFT JOIN price p ON p.id = asup.price_id
LEFT JOIN tax t ON t.id = p.tax_id
WHERE asup.article_id = a.id
ORDER BY e.name, asup.id
ORDER BY asup.sort ASC, asup.id ASC
FOR JSON PATH
) AS suppliersJson
FROM article a

View File

@@ -7,6 +7,7 @@ SELECT
asup.sku,
asup.pu,
asup.stock,
asup.sort,
asup.price_id AS priceId,
p.net AS priceNet,
p.tax_id AS priceTaxId,
@@ -18,7 +19,7 @@ LEFT JOIN price p ON p.id = asup.price_id
LEFT JOIN tax t ON t.id = p.tax_id
WHERE asup.article_id = @articleId
AND cg.company_id = @companyId
ORDER BY e.name, asup.id;
ORDER BY asup.sort ASC, asup.id ASC;
`;
export const GET_BY_ID_AND_ARTICLE_ID = `
@@ -30,6 +31,7 @@ SELECT
asup.sku,
asup.pu,
asup.stock,
asup.sort,
asup.price_id AS priceId,
p.net AS priceNet,
p.tax_id AS priceTaxId,
@@ -50,10 +52,16 @@ FROM articlesupplier
WHERE article_id = @articleId AND entity_id = @entityId;
`;
export const GET_NEXT_SORT = `
SELECT ISNULL(MAX(sort), -1) + 1 AS sort
FROM articlesupplier
WHERE article_id = @articleId;
`;
export const CREATE = `
INSERT INTO articlesupplier (article_id, entity_id, price_id, sku, stock, pu)
INSERT INTO articlesupplier (article_id, entity_id, price_id, sku, stock, pu, sort)
OUTPUT INSERTED.id
VALUES (@articleId, @entityId, @priceId, @sku, @stock, @pu);
VALUES (@articleId, @entityId, @priceId, @sku, @stock, @pu, @sort);
`;
export const UPDATE = `
@@ -62,7 +70,8 @@ SET
sku = @sku,
stock = @stock,
pu = @pu,
price_id = @priceId
price_id = @priceId,
sort = @sort
WHERE id = @id AND article_id = @articleId;
`;
@@ -71,6 +80,25 @@ DELETE FROM articlesupplier
WHERE id = @id AND article_id = @articleId;
`;
export const LIST_IDS_BY_ARTICLE = `
SELECT id
FROM articlesupplier
WHERE article_id = @articleId
ORDER BY sort ASC, id ASC;
`;
export const SET_TEMP_SORTS = `
UPDATE articlesupplier
SET sort = -id
WHERE article_id = @articleId;
`;
export const UPDATE_SORT = `
UPDATE articlesupplier
SET sort = @sort
WHERE id = @id AND article_id = @articleId;
`;
export const UPDATE_PRICE = `
UPDATE price
SET

View File

@@ -13,7 +13,7 @@ export default function getBySku(pool) {
*
* @param {number} googleAuthId - The id of the Google auth user.
* @param {string} sku - The article SKU.
* @returns {Promise<{article: {id: number, sku: string, name: string, slug: string, unitamount: number|null, weight: number|null, unit: {si: string|null, basesi: string|null, basesifactor: number|null, symbol: string|null, name: string|null}, brand: {name: string, thumbW: number, thumbH: number, thumbHash: string}|null, categoryIds: number[], photos: Array<{sort: number, fullHash: string, fullW: number, fullH: number, fullSize: number, thumbW: number, thumbH: number, thumbHash: string}>, eans: string[], prices: Array<{minmultiplier: number, net: number, taxRate: number, customergroupId: number}>, suppliers: Array<{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}>, optionsCount: number, articleListCount: number, childCount: number, fatherArticleId: number|null, fatherSku: string|null}}>} The full article detail.
* @returns {Promise<{article: {id: number, sku: string, name: string, slug: string, unitamount: number|null, weight: number|null, unit: {si: string|null, basesi: string|null, basesifactor: number|null, symbol: string|null, name: string|null}, brand: {name: string, thumbW: number, thumbH: number, thumbHash: string}|null, categoryIds: number[], photos: Array<{sort: number, fullHash: string, fullW: number, fullH: number, fullSize: number, thumbW: number, thumbH: number, thumbHash: string}>, eans: string[], prices: Array<{minmultiplier: number, net: number, taxRate: number, customergroupId: number}>, suppliers: Array<{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}>, optionsCount: number, articleListCount: number, childCount: number, fatherArticleId: number|null, fatherSku: string|null}}>} The full article detail.
* @throws {Error} If the article is not found or the user is not authorized.
*
* @example
@@ -80,6 +80,7 @@ export default function getBySku(pool) {
sku: s.sku ?? null,
pu: s.pu,
stock: s.stock ?? null,
sort: s.sort,
priceId: s.priceId ?? null,
price: s.priceId != null && s.price?.net != null ? s.price : null,
})),

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;

View File

@@ -17,6 +17,7 @@ export function shapeArticleSupplier(row) {
sku: row.sku ?? null,
pu: row.pu,
stock: row.stock ?? null,
sort: row.sort,
priceId: row.priceId ?? null,
price:
row.priceId != null && row.priceNet != null

View File

@@ -10,7 +10,7 @@ export default function listByArticleSku(pool) {
*
* @param {number} googleAuthId - The id of the Google auth user.
* @param {string} sku - The article SKU.
* @returns {Promise<Array<{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 supplier assignments.
* @returns {Promise<Array<{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 supplier assignments.
* @throws {Error} If the article is not found or the user is not authorized.
*
* @example

View File

@@ -0,0 +1,84 @@
import {
LIST_IDS_BY_ARTICLE,
SET_TEMP_SORTS,
UPDATE_SORT,
} from '../../sql/articlesupplier.js';
import request from '../request.js';
import resolveArticleBySku from '../article/resolveArticleBySku.js';
export default function order(pool) {
const resolve = resolveArticleBySku(pool);
/**
* Reorders the supplier assignments of an article by its SKU.
*
* @param {number} googleAuthId - The id of the Google auth user.
* @param {string} sku - The article SKU.
* @param {object} input - The reorder input.
* @param {Array<number>} input.ids - The articlesupplier ids in their new order.
* @returns {Promise<{ids: Array<number>}>} The applied order.
* @throws {Error} If the order is invalid.
*
* @example
* const result = await store.articlesupplier.order(42, 'SKU-1', { ids: [12, 10, 15] });
* // => { ids: [12, 10, 15] }
*/
return async (googleAuthId, sku, { ids } = {}) => {
if (!Array.isArray(ids)) {
throw new Error('Invalid supplier order');
}
const { articleId } = await resolve(googleAuthId, sku);
const current = await pool
.request()
.input('articleId', articleId)
.query(LIST_IDS_BY_ARTICLE);
const existing = new Set(current.recordset.map((r) => r.id));
if (ids.length !== existing.size) {
throw new Error('Invalid supplier order');
}
const seen = new Set();
for (const id of ids) {
if (!Number.isFinite(id) || !existing.has(id) || seen.has(id)) {
throw new Error('Invalid supplier order');
}
seen.add(id);
}
const tx = pool.transaction();
await tx.begin();
try {
await request(pool, tx)
.input('articleId', articleId)
.query(SET_TEMP_SORTS);
for (let sort = 0; sort < ids.length; sort++) {
const updateRes = await request(pool, tx)
.input('id', ids[sort])
.input('articleId', articleId)
.input('sort', sort)
.query(UPDATE_SORT);
if (!updateRes.rowsAffected[0]) {
throw new Error('Invalid supplier order');
}
}
await tx.commit();
return { ids };
} catch (err) {
try {
await tx.rollback();
} catch {
// ignore
}
throw err;
}
};
}

View File

@@ -29,10 +29,11 @@ export default function update(pool) {
* @param {string|null} [patch.sku] - The supplier's item number/SKU.
* @param {number} [patch.pu] - Packaging unit (> 0).
* @param {number|null} [patch.stock] - Current stock at the supplier.
* @param {number} [patch.sort] - Display sort order.
* @param {number|null} [patch.netPrice] - Net purchase price.
* @param {number} [patch.taxId] - The tax id for the purchase price.
* @param {number} [patch.taxRate] - The tax rate in percent.
* @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 updated 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 updated assignment.
* @throws {Error} If input is invalid or supplier assignment is not found.
*
* @example
@@ -41,7 +42,7 @@ export default function update(pool) {
* pu: 6,
* netPrice: 22.50,
* });
* // => { id, articleId, entityId, supplierName, sku, pu, stock, priceId, price }
* // => { id, articleId, entityId, supplierName, sku, pu, stock, sort, priceId, price }
*/
return async (googleAuthId, sku, id, patch = {}) => {
if (id == null || !Number.isFinite(id)) {
@@ -60,6 +61,12 @@ export default function update(pool) {
}
}
if (patch.sort !== undefined && patch.sort !== null) {
if (!Number.isFinite(patch.sort)) {
throw new Error('Invalid sort');
}
}
if (patch.netPrice !== undefined && patch.netPrice !== null) {
if (!Number.isFinite(patch.netPrice)) {
throw new Error('Invalid price');
@@ -98,6 +105,7 @@ export default function update(pool) {
.input('sku', existing.sku)
.input('stock', existing.stock)
.input('pu', existing.pu)
.input('sort', existing.sort)
.input('priceId', null)
.query(UPDATE_ARTICLE_SUPPLIER);
@@ -168,6 +176,7 @@ export default function update(pool) {
: patch.stock
: existing.stock;
const nextPu = patch.pu !== undefined ? patch.pu : existing.pu;
const nextSort = patch.sort !== undefined ? patch.sort : existing.sort;
await request(pool, tx)
.input('id', id)
@@ -175,6 +184,7 @@ export default function update(pool) {
.input('sku', nextSku)
.input('stock', nextStock)
.input('pu', nextPu)
.input('sort', nextSort)
.input('priceId', priceId)
.query(UPDATE_ARTICLE_SUPPLIER);