This commit is contained in:
seb
2026-10-07 21:23:03 +02:00
parent e02bbd65c5
commit 36dd81ee69
7 changed files with 170 additions and 91 deletions

View File

@@ -22,28 +22,37 @@ function normalizePrices(prices) {
const normalized = [];
for (const entry of prices) {
const price = entry?.price;
const tax = entry?.tax;
const rawTax = entry?.taxRate ?? entry?.tax;
const minmultiplier =
entry?.minmultiplier === undefined || entry?.minmultiplier === null
? 1
: entry.minmultiplier;
if (
typeof price !== 'number' || !Number.isFinite(price) || price < 0 ||
typeof tax !== 'number' || !Number.isFinite(tax) || tax < 0 ||
typeof rawTax !== 'number' || !Number.isFinite(rawTax) || rawTax < 0 ||
typeof minmultiplier !== 'number' || !Number.isFinite(minmultiplier) || minmultiplier <= 0
) {
throw new Error('Invalid prices');
}
let netPrice;
if (typeof entry?.net === 'number' && Number.isFinite(entry.net) && entry.net >= 0) {
netPrice = entry.net;
} else if (typeof entry?.price === 'number' && Number.isFinite(entry.price) && entry.price >= 0) {
netPrice = entry.price;
} else if (typeof entry?.gross === 'number' && Number.isFinite(entry.gross) && entry.gross >= 0) {
netPrice = +Number(entry.gross / (1 + rawTax / 100)).toFixed(4);
} else {
throw new Error('Invalid prices');
}
const key = String(minmultiplier);
if (seen.has(key)) {
throw new Error('Invalid prices');
}
seen.add(key);
normalized.push({ price, tax, minmultiplier });
normalized.push({ price: netPrice, tax: rawTax, minmultiplier });
}
return normalized;
@@ -64,13 +73,15 @@ export default function setPricesBySku(provider) {
*
* @param {number} googleAuthId - The id of the Google auth user.
* @param {string} sku - The article SKU.
* @param {Array<{price: number, tax: number, minmultiplier?: number}>} prices - The price entries (minmultiplier defaults to 1).
* @param {Array<{net?: number, gross?: number, price?: number, taxRate?: number, tax?: number, minmultiplier?: number}>} prices - The price entries (net or gross, taxRate or tax percentage, minmultiplier defaults to 1).
* @param {object} [tx] - Optional transaction to run within.
* @returns {Promise<object>} The updated article.
* @throws {Error} If the prices are invalid or the article is not found.
*
* @example
* const article = await store.article.setPricesBySku(42, 'SKU-1', [{ price: 1.5, tax: 19, minmultiplier: 1 }]);
* const article = await store.article.setPricesBySku(42, 'SKU-1', [{ net: 10.0, taxRate: 19, minmultiplier: 1 }]);
* // or with gross:
* const article = await store.article.setPricesBySku(42, 'SKU-1', [{ gross: 11.9, taxRate: 19 }]);
* // => { id, sku, name, ... }
*/
return async (googleAuthId, sku, prices, tx) => {