From ee19f9ccaa359a2edd635ce649aac1e318ced665 Mon Sep 17 00:00:00 2001 From: Geekswordsman Date: Fri, 8 Aug 2025 21:47:20 -0400 Subject: [PATCH] Adds a get/set to bNote to allow getting an Attribute by it's Id, or setting an Attribute's value by it's Id --- apps/server/src/becca/entities/bnote.ts | 35 +++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/apps/server/src/becca/entities/bnote.ts b/apps/server/src/becca/entities/bnote.ts index 419c9bdfe..116078e6a 100644 --- a/apps/server/src/becca/entities/bnote.ts +++ b/apps/server/src/becca/entities/bnote.ts @@ -1758,6 +1758,41 @@ class BNote extends AbstractBeccaEntity { 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;