u
This commit is contained in:
@@ -41,20 +41,24 @@ export default function updateBySku(provider) {
|
||||
* @param {string} [patch.name] - The new article name.
|
||||
* @param {string} [patch.unitSi] - The new unit SI code.
|
||||
* @param {number} [patch.unitamount] - The new unit amount (must be > 0).
|
||||
* @param {boolean|number} [patch.track_stock] - Whether inventory stock is tracked.
|
||||
* @param {object} [tx] - Optional transaction to run within.
|
||||
* @returns {Promise<object>} The updated article.
|
||||
* @throws {Error} If no valid field is provided or the article is not found.
|
||||
*
|
||||
* @example
|
||||
* const article = await store.article.updateBySku(42, 'SKU-1', { name: 'Cola 1L', unitSi: 'l', unitamount: 1 });
|
||||
* const article = await store.article.updateBySku(42, 'SKU-1', { name: 'Cola 1L', unitSi: 'l', unitamount: 1, track_stock: true });
|
||||
* // => { id, sku, name, ... }
|
||||
*/
|
||||
return async (googleAuthId, sku, patch = {}, tx) => {
|
||||
const hasName = Object.prototype.hasOwnProperty.call(patch, 'name');
|
||||
const hasUnitamount = Object.prototype.hasOwnProperty.call(patch, 'unitamount');
|
||||
const hasUnitSi = Object.prototype.hasOwnProperty.call(patch, 'unitSi');
|
||||
const hasTrackStock =
|
||||
Object.prototype.hasOwnProperty.call(patch, 'track_stock') ||
|
||||
Object.prototype.hasOwnProperty.call(patch, 'trackStock');
|
||||
|
||||
if (!hasName && !hasUnitamount && !hasUnitSi) {
|
||||
if (!hasName && !hasUnitamount && !hasUnitSi && !hasTrackStock) {
|
||||
throw new Error('Name required');
|
||||
}
|
||||
|
||||
@@ -66,6 +70,13 @@ export default function updateBySku(provider) {
|
||||
throw new Error('Invalid unitamount');
|
||||
}
|
||||
|
||||
const rawTrackStock = Object.prototype.hasOwnProperty.call(patch, 'track_stock')
|
||||
? patch.track_stock
|
||||
: patch.trackStock;
|
||||
if (hasTrackStock && typeof rawTrackStock !== 'boolean' && rawTrackStock !== 0 && rawTrackStock !== 1) {
|
||||
throw new Error('Invalid track_stock');
|
||||
}
|
||||
|
||||
const runUpdate = async (runner) => {
|
||||
const { articleId, languageId, companyId, companyuserId } = await resolve(
|
||||
googleAuthId,
|
||||
@@ -122,6 +133,15 @@ export default function updateBySku(provider) {
|
||||
historyLines.push(`Einheit geändert von "${oldLabel}" auf "${newLabel}"`);
|
||||
}
|
||||
}
|
||||
if (hasTrackStock) {
|
||||
const oldTrack = Boolean(current.track_stock ?? 1);
|
||||
const newTrack = Boolean(rawTrackStock);
|
||||
if (oldTrack !== newTrack) {
|
||||
historyLines.push(
|
||||
`Bestandsführung geändert von "${oldTrack ? 'Ja' : 'Nein'}" auf "${newTrack ? 'Ja' : 'Nein'}"`,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if (hasName) {
|
||||
const slug = slugify(patch.name);
|
||||
@@ -155,6 +175,13 @@ export default function updateBySku(provider) {
|
||||
});
|
||||
}
|
||||
|
||||
if (hasTrackStock) {
|
||||
await runner.query(articleQueries.UPDATE_TRACK_STOCK, {
|
||||
articleId,
|
||||
trackStock: Boolean(rawTrackStock) ? 1 : 0,
|
||||
});
|
||||
}
|
||||
|
||||
if (historyLines.length) {
|
||||
await runner.query(insertHistorySql, {
|
||||
articleId,
|
||||
|
||||
Reference in New Issue
Block a user