u
This commit is contained in:
@@ -79,6 +79,42 @@ export default function setByArticleSku(provider) {
|
||||
});
|
||||
}
|
||||
|
||||
// Check father / child constraints
|
||||
const artRes = await runner.query(
|
||||
'SELECT id, is_father, fatherarticle_id, fatherattributename_id FROM article WHERE id = @articleId',
|
||||
{ articleId },
|
||||
);
|
||||
const article = artRes.rows[0];
|
||||
|
||||
if (article?.is_father && article.fatherattributename_id) {
|
||||
const hasFatherAttr = validated.some((v) => v.attributeId === article.fatherattributename_id);
|
||||
if (hasFatherAttr) {
|
||||
throw new Error('A father article cannot have attribute values for its variant attribute');
|
||||
}
|
||||
}
|
||||
|
||||
if (article?.fatherarticle_id != null) {
|
||||
let currentFatherId = article.fatherarticle_id;
|
||||
const visited = new Set();
|
||||
while (currentFatherId != null && !visited.has(currentFatherId)) {
|
||||
visited.add(currentFatherId);
|
||||
const fatherRes = await runner.query(
|
||||
'SELECT a.fatherarticle_id, a.fatherattributename_id, an.name AS fatherAttributeName FROM article a LEFT JOIN attributename an ON an.id = a.fatherattributename_id WHERE a.id = @currentFatherId',
|
||||
{ currentFatherId },
|
||||
);
|
||||
const fRow = fatherRes.rows[0];
|
||||
if (!fRow) break;
|
||||
if (fRow.fatherattributename_id != null) {
|
||||
const hasFatherAttr = validated.some((v) => v.attributeId === fRow.fatherattributename_id);
|
||||
if (!hasFatherAttr) {
|
||||
const fatherAttrName = fRow.fatherAttributeName || 'ancestor variant attribute';
|
||||
throw new Error(`Child article must have an attribute value for the father attribute "${fatherAttrName}"`);
|
||||
}
|
||||
}
|
||||
currentFatherId = fRow.fatherarticle_id;
|
||||
}
|
||||
}
|
||||
|
||||
await runner.query(deleteSql, { articleId });
|
||||
|
||||
const inserted = [];
|
||||
|
||||
Reference in New Issue
Block a user