Adds a get/set to bNote to allow getting an Attribute by it's Id, or … (#6596)

This commit is contained in:
Elian Doran 2025-08-11 22:43:11 +03:00 committed by GitHub
commit 15705553c7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1758,6 +1758,41 @@ class BNote extends AbstractBeccaEntity<BNote> {
return childBranches;
}
/**
* Return an attribute by it's attributeId. Requires the attribute cache to be available.
* @param attributeId - the id of the attribute owned by this note
* @returns - the BAttribute with the given id or undefined if not found.
*/
getAttributeById(attributeId : string): BAttribute | undefined {
this.__ensureAttributeCacheIsAvailable();
if (!this.__attributeCache) {
throw new Error("Attribute cache not available.");
}
return this.__attributeCache.find((attr) => attr.attributeId === attributeId);
}
/**
* Sets an attribute's value by it's attributeId.
* @param attributeId - the id of the attribute owned by this note
* @param value - the new value to replace
*/
setAttributeValueById(attributeId : string, value? : string) {
const attributes = this.getOwnedAttributes();
const attr = attributes.find((attr) => attr.attributeId === attributeId);
value = value?.toString() || "";
if (attr) {
if (attr.value !== value) {
attr.value = value;
attr.save();
}
} else {
throw new Error(`Attribute with id ${attributeId} not found.`);
}
}
}
export default BNote;