Files
dashlibstorage/src/store/article/setWeightBySku.js

45 lines
1.2 KiB
JavaScript

import { UPDATE_WEIGHT } from '../../sql/article.js';
import queryBySku from './queryBySku.js';
import resolveArticleBySku from './resolveArticleBySku.js';
/**
* Sets the weight of an article by its SKU.
*
* @param {object} pool - The mssql connection pool.
* @returns {Function} An async function that sets the article weight.
*
* @example
* const fn = setWeightBySku(pool);
* const article = await fn(42, 'SKU-1', 1.5);
* // => { id, sku, name, ... }
*/
export default function setWeightBySku(pool) {
const resolve = resolveArticleBySku(pool);
const load = queryBySku(pool);
return async (googleAuthId, sku, weight) => {
if (!Number.isFinite(weight) || weight < 0) {
throw new Error('Invalid weight');
}
const { articleId, languageId } = await resolve(googleAuthId, sku);
const result = await pool
.request()
.input('articleId', articleId)
.input('weight', weight)
.query(UPDATE_WEIGHT);
if (!result.rowsAffected[0]) {
throw new Error('Article not found');
}
const article = await load(articleId, languageId);
if (!article) {
throw new Error('Article not found');
}
return article;
};
}