This commit is contained in:
seb
2026-09-27 21:01:30 +02:00
parent f9c570a036
commit 0cafa11a32
34 changed files with 2010 additions and 1534 deletions

View File

@@ -32,14 +32,18 @@ export default function createUnderParent(provider) {
* @param {number} input.googleAuthId - The id of the Google auth user.
* @param {number} input.parentId - The id of the parent category.
* @param {string} input.name - The article name.
* @param {boolean|number} [input.track_stock=true] - Whether inventory stock is tracked.
* @param {object} [tx] - Optional transaction to run within.
* @returns {Promise<{id: number, parentId: number, name: string, sku: string}>} The created article.
* @returns {Promise<{id: number, parentId: number, name: string, sku: string, track_stock: boolean}>} The created article.
*
* @example
* const article = await store.article.createUnderParent({ googleAuthId: 42, parentId: 5, name: 'Cola' });
* // => { id, parentId, name, sku }
* // => { id, parentId, name, sku, track_stock }
*/
return async ({ googleAuthId, parentId, name }, tx) => {
return async ({ googleAuthId, parentId, name, track_stock, trackStock }, tx) => {
const rawTrack = track_stock ?? trackStock;
const finalTrackStock = rawTrack == null ? 1 : (rawTrack ? 1 : 0);
const runCreate = async (runner) => {
const { companyId, languageId, companyuserId } = await access(
googleAuthId,
@@ -59,6 +63,7 @@ export default function createUnderParent(provider) {
unitId,
unitamount: 1,
weight: 0,
trackStock: finalTrackStock,
});
} catch (err) {
if (isUniqueConstraintError(err)) {
@@ -92,6 +97,7 @@ export default function createUnderParent(provider) {
parentId,
name,
sku,
track_stock: Boolean(finalTrackStock),
};
};

View File

@@ -18,7 +18,7 @@ export default function getBySku(provider) {
* @param {number} googleAuthId - The id of the Google auth user.
* @param {string} sku - The article SKU.
* @param {object} [tx] - Optional transaction.
* @returns {Promise<{article: {id: number, sku: string, name: string, slug: string, unitamount: number|null, weight: number|null, unit: {si: string|null, basesi: string|null, basesifactor: number|null, symbol: string|null, name: string|null}, brand: {id: number|null, name: string, fullHash: string|null, thumbW: number|null, thumbH: number|null, thumbHash: string|null}|null, categoryIds: number[], photos: Array<{sort: number, fullHash: string, fullW: number, fullH: number, fullSize: number, thumbW: number, thumbH: number, thumbHash: string}>, eans: string[], prices: Array<{minmultiplier: number, net: number, taxRate: number, customergroupId: number}>, suppliers: Array<{id: number, articleId: number, entityId: number, supplierName: string|null, sku: string|null, pu: number, stock: number|null, sort: number, priceId: number|null, price: {net: number, taxId: number, taxRate?: number}|null}>, optionsCount: number, articleListCount: number, childCount: number, fatherArticleId: number|null, fatherSku: string|null}}>} The full article detail.
* @returns {Promise<{article: {id: number, sku: string, name: string, slug: string, unitamount: number|null, weight: number|null, track_stock: boolean, unit: {si: string|null, basesi: string|null, basesifactor: number|null, symbol: string|null, name: string|null}, brand: {id: number|null, name: string, fullHash: string|null, thumbW: number|null, thumbH: number|null, thumbHash: string|null}|null, categoryIds: number[], photos: Array<{sort: number, fullHash: string, fullW: number, fullH: number, fullSize: number, thumbW: number, thumbH: number, thumbHash: string}>, eans: string[], prices: Array<{minmultiplier: number, net: number, taxRate: number, customergroupId: number}>, suppliers: Array<{id: number, articleId: number, entityId: number, supplierName: string|null, sku: string|null, pu: number, stock: number|null, sort: number, priceId: number|null, price: {net: number, taxId: number, taxRate?: number}|null}>, optionsCount: number, articleListCount: number, childCount: number, fatherArticleId: number|null, fatherSku: string|null}}>} The full article detail.
* @throws {Error} If the article is not found or the user is not authorized.
*
* @example
@@ -56,6 +56,7 @@ export default function getBySku(provider) {
slug: a.slug,
unitamount: a.unitamount,
weight: a.weight,
track_stock: Boolean(a.track_stock ?? 1),
unit: {
si: a.unitSi,
basesi: a.unitBasesi,
@@ -92,6 +93,7 @@ export default function getBySku(provider) {
warehouses: parseJsonArray(a.warehousesJson).map((w) => ({
id: w.id,
name: w.name,
sort: Number(w.sort ?? 0),
stock: Number(w.stock ?? 0),
})),
optionsCount: a.optionsCount,

View File

@@ -103,6 +103,7 @@ export default function importRows(provider) {
* @param {number|string} [input.rows[].net] - The POS net price (accepts German decimals).
* @param {number|string} [input.rows[].tax] - The tax rate in percent (e.g. `19`).
* @param {Array<string>|string} [input.rows[].eans] - The EANs to set.
* @param {boolean|number} [input.rows[].track_stock=true] - Whether inventory stock is tracked.
* @param {string} [input.rows[].fatherSku] - The father article SKU for a V/K compound link.
* @param {number|string} [input.rows[].deposit] - The deposit (Pfand) value; currently ignored.
* @param {object} [tx] - Optional transaction.
@@ -168,12 +169,16 @@ export default function importRows(provider) {
const unitId = await ensureUnit(runner, companyId, languageId);
const slug = slugify(name);
const rawTrack = row.track_stock ?? row.trackStock;
const trackStock = rawTrack == null ? 1 : (rawTrack ? 1 : 0);
const article = await runner.query(articleQueries.CREATE, {
companyId,
sku,
unitId,
unitamount: 1,
weight: 0,
trackStock,
});
const articleId = article.rows[0]?.id ?? article.insertId;

View File

@@ -94,6 +94,7 @@ export default function listExport(provider) {
unitSi: row.unitSi,
unitSymbol: row.unitSymbol,
unitamount: row.unitamount,
track_stock: Boolean(row.track_stock ?? 1),
brandName: row.brandName,
net: row.net,
taxRate: row.taxRate,

View File

@@ -10,6 +10,7 @@ function shapeArticleDetail(row, categoryIds, photos, eans, prices) {
slug: row.slug,
unitamount: row.unitamount,
weight: row.weight,
track_stock: Boolean(row.track_stock ?? 1),
unit: {
si: row.unitSi,
basesi: row.unitBasesi,

View File

@@ -0,0 +1,53 @@
import { UPDATE_TRACK_STOCK as MSSQL_UPDATE_TRACK_STOCK } from '../../providers/mssql/queries/article.js';
import { UPDATE_TRACK_STOCK as SQLITE_UPDATE_TRACK_STOCK } from '../../providers/sqlite3/queries/article.js';
import queryBySku from './queryBySku.js';
import resolveArticleBySku from './resolveArticleBySku.js';
import makeRunner from '../runner.js';
export default function setTrackStockBySku(provider) {
const resolve = resolveArticleBySku(provider);
const load = queryBySku(provider);
const sql =
provider?.dialect === 'sqlite3'
? SQLITE_UPDATE_TRACK_STOCK
: MSSQL_UPDATE_TRACK_STOCK;
/**
* Sets the track_stock flag of an article by its SKU.
*
* @param {number} googleAuthId - The id of the Google auth user.
* @param {string} sku - The article SKU.
* @param {boolean|number} trackStock - Whether stock tracking is enabled.
* @param {object} [tx] - Optional transaction.
* @returns {Promise<object>} The updated article.
* @throws {Error} If trackStock is invalid or the article is not found.
*
* @example
* const article = await store.article.setTrackStockBySku(42, 'SKU-1', true);
* // => { id, sku, name, track_stock, ... }
*/
return async (googleAuthId, sku, trackStock, tx) => {
if (typeof trackStock !== 'boolean' && trackStock !== 0 && trackStock !== 1) {
throw new Error('Invalid track_stock');
}
const runner = makeRunner(provider, tx);
const { articleId, languageId } = await resolve(googleAuthId, sku, runner);
const result = await runner.query(sql, {
articleId,
trackStock: trackStock ? 1 : 0,
});
if (!result.rowsAffected) {
throw new Error('Article not found');
}
const article = await load(articleId, languageId, runner);
if (!article) {
throw new Error('Article not found');
}
return article;
};
}

View File

@@ -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,