sqlite
This commit is contained in:
@@ -1,10 +1,13 @@
|
||||
import { UPDATE_WEIGHT } from '../../providers/mssql/queries/article.js';
|
||||
import { UPDATE_WEIGHT as MSSQL_UPDATE_WEIGHT } from '../../providers/mssql/queries/article.js';
|
||||
import { UPDATE_WEIGHT as SQLITE_UPDATE_WEIGHT } from '../../providers/sqlite3/queries/article.js';
|
||||
import queryBySku from './queryBySku.js';
|
||||
import resolveArticleBySku from './resolveArticleBySku.js';
|
||||
import makeRunner from '../runner.js';
|
||||
|
||||
export default function setWeightBySku(pool) {
|
||||
const resolve = resolveArticleBySku(pool);
|
||||
const load = queryBySku(pool);
|
||||
export default function setWeightBySku(provider) {
|
||||
const resolve = resolveArticleBySku(provider);
|
||||
const load = queryBySku(provider);
|
||||
const sql = provider?.dialect === 'sqlite3' ? SQLITE_UPDATE_WEIGHT : MSSQL_UPDATE_WEIGHT;
|
||||
|
||||
/**
|
||||
* Sets the weight of an article by its SKU.
|
||||
@@ -12,6 +15,7 @@ export default function setWeightBySku(pool) {
|
||||
* @param {number} googleAuthId - The id of the Google auth user.
|
||||
* @param {string} sku - The article SKU.
|
||||
* @param {number} weight - The weight (must be >= 0).
|
||||
* @param {object} [tx] - Optional transaction.
|
||||
* @returns {Promise<object>} The updated article.
|
||||
* @throws {Error} If the weight is invalid or the article is not found.
|
||||
*
|
||||
@@ -19,24 +23,24 @@ export default function setWeightBySku(pool) {
|
||||
* const article = await store.article.setWeightBySku(42, 'SKU-1', 1.5);
|
||||
* // => { id, sku, name, ... }
|
||||
*/
|
||||
return async (googleAuthId, sku, weight) => {
|
||||
return async (googleAuthId, sku, weight, tx) => {
|
||||
if (!Number.isFinite(weight) || weight < 0) {
|
||||
throw new Error('Invalid weight');
|
||||
}
|
||||
|
||||
const { articleId, languageId } = await resolve(googleAuthId, sku);
|
||||
const runner = makeRunner(provider, tx);
|
||||
const { articleId, languageId } = await resolve(googleAuthId, sku, runner);
|
||||
|
||||
const result = await pool
|
||||
.request()
|
||||
.input('articleId', articleId)
|
||||
.input('weight', weight)
|
||||
.query(UPDATE_WEIGHT);
|
||||
const result = await runner.query(sql, {
|
||||
articleId,
|
||||
weight,
|
||||
});
|
||||
|
||||
if (!result.rowsAffected[0]) {
|
||||
if (!result.rowsAffected) {
|
||||
throw new Error('Article not found');
|
||||
}
|
||||
|
||||
const article = await load(articleId, languageId);
|
||||
const article = await load(articleId, languageId, runner);
|
||||
if (!article) {
|
||||
throw new Error('Article not found');
|
||||
}
|
||||
@@ -44,3 +48,4 @@ export default function setWeightBySku(pool) {
|
||||
return article;
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user