u
This commit is contained in:
@@ -42,42 +42,62 @@ export default function addImage(provider) {
|
||||
|
||||
const sortResult = await runner.query(getSortSql, { articleId });
|
||||
const sort = sortResult.rows[0].sort;
|
||||
const fullHash = newFullHash();
|
||||
const fullHash = newFullHash(processed.full.buffer);
|
||||
|
||||
if (isSqlite) {
|
||||
if (runner.writePicFull) {
|
||||
await runner.writePicFull(fullHash, processed.full.buffer);
|
||||
await runner.writePicThumb(fullHash, processed.thumb.buffer);
|
||||
} else if (provider?.writePicFull) {
|
||||
await provider.writePicFull(fullHash, processed.full.buffer);
|
||||
await provider.writePicThumb(fullHash, processed.thumb.buffer);
|
||||
let picId;
|
||||
const existingPic = await runner.query(
|
||||
'SELECT id FROM pic WHERE hash = @fullHash',
|
||||
{ fullHash },
|
||||
);
|
||||
|
||||
if (existingPic.rows[0]) {
|
||||
picId = existingPic.rows[0].id;
|
||||
} else {
|
||||
if (isSqlite) {
|
||||
if (runner.writePicFull) {
|
||||
await runner.writePicFull(fullHash, processed.full.buffer);
|
||||
await runner.writePicThumb(fullHash, processed.thumb.buffer);
|
||||
} else if (provider?.writePicFull) {
|
||||
await provider.writePicFull(fullHash, processed.full.buffer);
|
||||
await provider.writePicThumb(fullHash, processed.thumb.buffer);
|
||||
}
|
||||
}
|
||||
|
||||
const picResult = await runner.query(createPicSql, {
|
||||
w: processed.full.w,
|
||||
h: processed.full.h,
|
||||
size: processed.full.size,
|
||||
hash: fullHash,
|
||||
thumbw: processed.thumb.w,
|
||||
thumbh: processed.thumb.h,
|
||||
thumbhash: processed.thumb.hash,
|
||||
source: '',
|
||||
fullbinary: processed.full.buffer,
|
||||
thumbbinary: processed.thumb.buffer,
|
||||
});
|
||||
|
||||
picId = picResult.rows[0]?.id ?? picResult.insertId;
|
||||
}
|
||||
|
||||
const picResult = await runner.query(createPicSql, {
|
||||
w: processed.full.w,
|
||||
h: processed.full.h,
|
||||
size: processed.full.size,
|
||||
hash: fullHash,
|
||||
thumbw: processed.thumb.w,
|
||||
thumbh: processed.thumb.h,
|
||||
thumbhash: processed.thumb.hash,
|
||||
source: '',
|
||||
fullbinary: processed.full.buffer,
|
||||
thumbbinary: processed.thumb.buffer,
|
||||
});
|
||||
const existingArticlePic = await runner.query(
|
||||
'SELECT id, sort FROM articlepic WHERE article_id = @articleId AND pic_id = @picId',
|
||||
{ articleId, picId },
|
||||
);
|
||||
|
||||
const picId = picResult.rows[0]?.id ?? picResult.insertId;
|
||||
|
||||
await runner.query(addArticlePicSql, {
|
||||
articleId,
|
||||
picId,
|
||||
sort,
|
||||
});
|
||||
let effectiveSort = sort;
|
||||
if (existingArticlePic.rows[0]) {
|
||||
effectiveSort = existingArticlePic.rows[0].sort;
|
||||
} else {
|
||||
await runner.query(addArticlePicSql, {
|
||||
articleId,
|
||||
picId,
|
||||
sort,
|
||||
});
|
||||
}
|
||||
|
||||
return {
|
||||
picId,
|
||||
sort,
|
||||
sort: effectiveSort,
|
||||
full: {
|
||||
w: processed.full.w,
|
||||
h: processed.full.h,
|
||||
|
||||
@@ -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) => {
|
||||
|
||||
Reference in New Issue
Block a user