sqlite
This commit is contained in:
@@ -1,11 +1,15 @@
|
||||
import {
|
||||
CREATE as CREATE_PRICE,
|
||||
DELETE_BY_ARTICLE_AND_CUSTOMERGROUP,
|
||||
CREATE as MSSQL_CREATE_PRICE,
|
||||
DELETE_BY_ARTICLE_AND_CUSTOMERGROUP as MSSQL_DELETE_PRICE,
|
||||
} from '../../providers/mssql/queries/price.js';
|
||||
import request from '../request.js';
|
||||
import {
|
||||
CREATE as SQLITE_CREATE_PRICE,
|
||||
DELETE_BY_ARTICLE_AND_CUSTOMERGROUP as SQLITE_DELETE_PRICE,
|
||||
} from '../../providers/sqlite3/queries/price.js';
|
||||
import taxEnsure from '../tax/ensure.js';
|
||||
import queryBySku from './queryBySku.js';
|
||||
import resolveArticleBySku from './resolveArticleBySku.js';
|
||||
import makeRunner from '../runner.js';
|
||||
|
||||
function normalizePrices(prices) {
|
||||
if (!Array.isArray(prices)) {
|
||||
@@ -42,10 +46,14 @@ function normalizePrices(prices) {
|
||||
return normalized;
|
||||
}
|
||||
|
||||
export default function setPricesBySku(pool) {
|
||||
const resolve = resolveArticleBySku(pool);
|
||||
const ensureTax = taxEnsure(pool);
|
||||
const load = queryBySku(pool);
|
||||
export default function setPricesBySku(provider) {
|
||||
const resolve = resolveArticleBySku(provider);
|
||||
const ensureTax = taxEnsure(provider);
|
||||
const load = queryBySku(provider);
|
||||
const isSqlite = provider?.dialect === 'sqlite3';
|
||||
|
||||
const createPriceSql = isSqlite ? SQLITE_CREATE_PRICE : MSSQL_CREATE_PRICE;
|
||||
const deletePriceSql = isSqlite ? SQLITE_DELETE_PRICE : MSSQL_DELETE_PRICE;
|
||||
|
||||
/**
|
||||
* Replaces the price list of an article by its SKU.
|
||||
@@ -53,6 +61,7 @@ export default function setPricesBySku(pool) {
|
||||
* @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 {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.
|
||||
*
|
||||
@@ -60,58 +69,56 @@ export default function setPricesBySku(pool) {
|
||||
* const article = await store.article.setPricesBySku(42, 'SKU-1', [{ price: 1.5, tax: 19, minmultiplier: 1 }]);
|
||||
* // => { id, sku, name, ... }
|
||||
*/
|
||||
return async (googleAuthId, sku, prices) => {
|
||||
return async (googleAuthId, sku, prices, tx) => {
|
||||
const normalized = normalizePrices(prices);
|
||||
|
||||
const { articleId, languageId, posCustomerGroupId } = await resolve(
|
||||
googleAuthId,
|
||||
sku,
|
||||
);
|
||||
if (!posCustomerGroupId) {
|
||||
throw new Error('POS customer group required');
|
||||
}
|
||||
const runSet = async (runner) => {
|
||||
const { articleId, languageId, posCustomerGroupId } = await resolve(
|
||||
googleAuthId,
|
||||
sku,
|
||||
runner,
|
||||
);
|
||||
if (!posCustomerGroupId) {
|
||||
throw new Error('POS customer group required');
|
||||
}
|
||||
|
||||
const tx = pool.transaction();
|
||||
await tx.begin();
|
||||
|
||||
try {
|
||||
const taxIds = new Map();
|
||||
for (const entry of normalized) {
|
||||
if (!taxIds.has(entry.tax)) {
|
||||
taxIds.set(entry.tax, await ensureTax(entry.tax, tx));
|
||||
taxIds.set(entry.tax, await ensureTax(entry.tax, runner));
|
||||
}
|
||||
}
|
||||
|
||||
await request(pool, tx)
|
||||
.input('articleId', articleId)
|
||||
.input('customerGroupId', posCustomerGroupId)
|
||||
.query(DELETE_BY_ARTICLE_AND_CUSTOMERGROUP);
|
||||
await runner.query(deletePriceSql, {
|
||||
articleId,
|
||||
customerGroupId: posCustomerGroupId,
|
||||
});
|
||||
|
||||
for (const entry of normalized) {
|
||||
await request(pool, tx)
|
||||
.input('taxId', taxIds.get(entry.tax))
|
||||
.input('articleId', articleId)
|
||||
.input('minmultiplier', entry.minmultiplier)
|
||||
.input('customerGroupId', posCustomerGroupId)
|
||||
.input('net', entry.price)
|
||||
.query(CREATE_PRICE);
|
||||
await runner.query(createPriceSql, {
|
||||
taxId: taxIds.get(entry.tax),
|
||||
articleId,
|
||||
minmultiplier: entry.minmultiplier,
|
||||
customerGroupId: posCustomerGroupId,
|
||||
net: entry.price,
|
||||
});
|
||||
}
|
||||
|
||||
await tx.commit();
|
||||
} catch (err) {
|
||||
try {
|
||||
await tx.rollback();
|
||||
} catch {
|
||||
// ignore
|
||||
const article = await load(articleId, languageId, runner);
|
||||
if (!article) {
|
||||
throw new Error('Article not found');
|
||||
}
|
||||
throw err;
|
||||
}
|
||||
|
||||
const article = await load(articleId, languageId);
|
||||
if (!article) {
|
||||
throw new Error('Article not found');
|
||||
}
|
||||
return article;
|
||||
};
|
||||
|
||||
return article;
|
||||
if (tx) {
|
||||
return runSet(makeRunner(provider, tx));
|
||||
}
|
||||
if (typeof provider?.transaction === 'function') {
|
||||
return provider.transaction(runSet);
|
||||
}
|
||||
return runSet(makeRunner(provider));
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user