feat(articlesupplier): add sort column, reorder method, and update queries and schema
This commit is contained in:
@@ -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,
|
||||
})),
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
84
src/store/articlesupplier/order.js
Normal file
84
src/store/articlesupplier/order.js
Normal 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;
|
||||
}
|
||||
};
|
||||
}
|
||||
@@ -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);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user