This commit is contained in:
seb
2026-09-29 17:29:13 +02:00
parent ae5d5cc6f8
commit 30820ba8c7
14 changed files with 69 additions and 36 deletions

View File

@@ -24,12 +24,13 @@ export default function setByArticleSku(provider) {
/**
* Replaces all properties for an article by its SKU.
* Enforces exclusive flag on property names.
*
* @param {number} googleAuthId - The id of the Google auth user.
* @param {string} sku - The article SKU.
* @param {Array<{propertyId: number, value: string, sort?: number}>} properties - The properties to set.
* @param {object} [tx] - Optional transaction.
* @returns {Promise<Array<{id: number, articleId: number, propertyId: number, name: string, value: string, sort: number}>>}
* @returns {Promise<Array<{id: number, articleId: number, propertyId: number, name: string, exclusive: boolean, value: string, sort: number}>>}
*/
return async (googleAuthId, sku, properties = [], tx) => {
if (!Array.isArray(properties)) {
@@ -40,7 +41,7 @@ export default function setByArticleSku(provider) {
const { articleId, companyId } = await resolve(googleAuthId, sku, runner);
const validated = [];
const seenProps = new Set();
const seenExclusiveProps = new Set();
for (let i = 0; i < properties.length; i++) {
const item = properties[i];
@@ -48,21 +49,27 @@ export default function setByArticleSku(provider) {
if (!Number.isInteger(propertyId) || propertyId <= 0) {
throw new Error('Invalid propertyId');
}
if (seenProps.has(propertyId)) {
throw new Error('Duplicate propertyId');
}
seenProps.add(propertyId);
const propRes = await runner.query(getPropSql, { id: propertyId, companyId });
if (!propRes.rows[0]) {
const prop = propRes.rows[0];
if (!prop) {
throw new Error(`Property not found: ${propertyId}`);
}
const isExclusive = Boolean(prop.exclusive);
if (isExclusive) {
if (seenExclusiveProps.has(propertyId)) {
throw new Error(`Property "${prop.name}" is exclusive and can only have one value`);
}
seenExclusiveProps.add(propertyId);
}
const value = item.value != null ? String(item.value).trim() : '';
const sort = item.sort != null && Number.isInteger(item.sort) ? item.sort : i;
validated.push({
propertyId,
name: propRes.rows[0].name,
name: prop.name,
exclusive: isExclusive,
value,
sort,
});
@@ -84,6 +91,7 @@ export default function setByArticleSku(provider) {
articleId,
propertyId: item.propertyId,
name: item.name,
exclusive: item.exclusive,
value: item.value,
sort: item.sort,
});