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

@@ -13,7 +13,7 @@ export default function listByArticleSku(provider) {
* @param {number} googleAuthId - The id of the Google auth user.
* @param {string} sku - The article SKU.
* @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, tx) => {
const runner = makeRunner(provider, tx);
@@ -25,6 +25,7 @@ export default function listByArticleSku(provider) {
articleId: row.articleId,
propertyId: row.propertyId,
name: row.name,
exclusive: Boolean(row.exclusive),
value: row.value,
sort: row.sort,
}));

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,
});

View File

@@ -22,8 +22,9 @@ export default function create(provider) {
* @param {object} input - The property name input.
* @param {string} input.name - The property name.
* @param {number} [input.sort] - Optional sort order.
* @param {boolean} [input.exclusive=false] - Whether values are mutually exclusive on an article.
* @param {object} [tx] - Optional transaction.
* @returns {Promise<{id: number, name: string, sort: number}>}
* @returns {Promise<{id: number, name: string, sort: number, exclusive: boolean}>}
*/
return async (googleAuthId, input = {}, tx) => {
const name = input.name?.trim();
@@ -40,13 +41,16 @@ export default function create(provider) {
sort = next.rows[0].sort;
}
const exclusive = Boolean(input.exclusive) ? 1 : 0;
const result = await runner.query(createSql, {
companyId,
name,
sort,
exclusive,
});
const id = result.rows[0]?.id ?? result.insertId;
return { id, name, sort };
return { id, name, sort, exclusive: Boolean(exclusive) };
};
}

View File

@@ -12,7 +12,7 @@ export default function list(provider) {
*
* @param {number} googleAuthId - The id of the Google auth user.
* @param {object} [tx] - Optional transaction.
* @returns {Promise<Array<{id: number, name: string, sort: number}>>}
* @returns {Promise<Array<{id: number, name: string, sort: number, exclusive: boolean}>>}
*/
return async (googleAuthId, tx) => {
const { companyId } = await access(googleAuthId, tx);
@@ -21,6 +21,7 @@ export default function list(provider) {
id: row.id,
name: row.name,
sort: row.sort,
exclusive: Boolean(row.exclusive),
}));
};
}

View File

@@ -23,8 +23,9 @@ export default function update(provider) {
* @param {object} input - Update patch.
* @param {string} [input.name] - New name.
* @param {number} [input.sort] - New sort.
* @param {boolean} [input.exclusive] - New exclusive flag.
* @param {object} [tx] - Optional transaction.
* @returns {Promise<{id: number, name: string, sort: number}>}
* @returns {Promise<{id: number, name: string, sort: number, exclusive: boolean}>}
*/
return async (googleAuthId, id, input = {}, tx) => {
if (!id || !Number.isInteger(Number(id))) {
@@ -44,14 +45,16 @@ export default function update(provider) {
throw new Error('Name required');
}
const sort = input.sort !== undefined && Number.isInteger(input.sort) ? input.sort : existing.rows[0].sort;
const exclusive = input.exclusive !== undefined ? (Boolean(input.exclusive) ? 1 : 0) : existing.rows[0].exclusive;
await runner.query(updateSql, {
id,
companyId,
name,
sort,
exclusive,
});
return { id, name, sort };
return { id, name, sort, exclusive: Boolean(exclusive) };
};
}