u
This commit is contained in:
@@ -27,7 +27,8 @@ export default function setByArticleSku(provider) {
|
||||
|
||||
/**
|
||||
* Replaces all properties for an article by its SKU.
|
||||
* Enforces exclusive flag on property names.
|
||||
* Enforces exclusive flag on property names. An exclusive property that already has a value
|
||||
* is never overwritten: a different value throws `Exclusive property cannot be overwritten: <name>`.
|
||||
*
|
||||
* @param {number} googleAuthId - The id of the Google auth user.
|
||||
* @param {string} sku - The article SKU.
|
||||
@@ -79,6 +80,12 @@ export default function setByArticleSku(provider) {
|
||||
}
|
||||
|
||||
const value = item.value != null ? String(item.value).trim() : '';
|
||||
if (isExclusive) {
|
||||
const existing = oldProps.find((p) => p.propertyId === propertyId);
|
||||
if (existing && existing.value !== '' && existing.value !== value) {
|
||||
throw new Error(`Exclusive property cannot be overwritten: ${prop.name}`);
|
||||
}
|
||||
}
|
||||
const sort = item.sort != null && Number.isInteger(item.sort) ? item.sort : i;
|
||||
validated.push({
|
||||
propertyId,
|
||||
|
||||
@@ -47,6 +47,16 @@ export default function update(provider) {
|
||||
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;
|
||||
|
||||
if (exclusive && !existing.rows[0].exclusive) {
|
||||
const multi = await runner.query(
|
||||
`SELECT article_id FROM articleproperty WHERE property_id = @id GROUP BY article_id HAVING COUNT(*) > 1`,
|
||||
{ id },
|
||||
);
|
||||
if (multi.rows.length > 0) {
|
||||
throw new Error(`Cannot set property exclusive: ${multi.rows.length} article(s) have multiple values`);
|
||||
}
|
||||
}
|
||||
|
||||
await runner.query(updateSql, {
|
||||
id,
|
||||
companyId,
|
||||
|
||||
Reference in New Issue
Block a user