u
This commit is contained in:
@@ -41,13 +41,27 @@ export default function toggleFather(provider) {
|
||||
throw new Error('attributeNameId is required to become a father article');
|
||||
}
|
||||
|
||||
// Verify article is not a child of another article
|
||||
const currentRes = await runner.query(
|
||||
'SELECT fatherarticle_id FROM article WHERE id = @articleId',
|
||||
{ articleId },
|
||||
);
|
||||
if (currentRes.rows[0]?.fatherarticle_id != null) {
|
||||
throw new Error('A child article cannot become a father');
|
||||
|
||||
// If article is a child, verify attribute is not used by father or any father up the chain
|
||||
let currentFatherId = currentRes.rows[0]?.fatherarticle_id;
|
||||
const visited = new Set();
|
||||
while (currentFatherId != null && !visited.has(currentFatherId)) {
|
||||
visited.add(currentFatherId);
|
||||
const fRes = 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 = fRes.rows[0];
|
||||
if (!fRow) break;
|
||||
if (fRow.fatherattributename_id != null && Number(fRow.fatherattributename_id) === Number(targetAttrId)) {
|
||||
const attrName = fRow.fatherAttributeName || 'ancestor';
|
||||
throw new Error(`Attribute "${attrName}" is already used by an ancestor father article`);
|
||||
}
|
||||
currentFatherId = fRow.fatherarticle_id;
|
||||
}
|
||||
|
||||
// Verify attribute exists and belongs to company
|
||||
@@ -59,6 +73,17 @@ export default function toggleFather(provider) {
|
||||
throw new Error('Attribute name not found');
|
||||
}
|
||||
|
||||
// Verify article does not have an attribute value for this attribute
|
||||
const existingValRes = await runner.query(
|
||||
`SELECT aav.id FROM articleattributevalue aav
|
||||
INNER JOIN attributevalue av ON av.id = aav.attributevalue_id
|
||||
WHERE aav.article_id = @articleId AND av.attribute_id = @attributeId`,
|
||||
{ articleId, attributeId: Number(targetAttrId) },
|
||||
);
|
||||
if (existingValRes.rows.length > 0) {
|
||||
throw new Error('A father article cannot have attribute values for its variant attribute');
|
||||
}
|
||||
|
||||
await runner.query(queries.TOGGLE_FATHER, {
|
||||
articleId,
|
||||
isFather: 1,
|
||||
|
||||
Reference in New Issue
Block a user