From 10e3a3f3bd19882403134373c14e661059b85b39 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julian=20K=C3=B6nig?= <33655937+jkoenig134@users.noreply.github.com> Date: Wed, 2 Oct 2024 11:28:14 +0200 Subject: [PATCH] Decompose relationships at connector startup in AutoDecomposeDeletionProposedRelationshipsModule (#276) --- ...poseDeletionProposedRelationshipsModule.ts | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/modules/autoDecomposeDeletionProposedRelationships/AutoDecomposeDeletionProposedRelationshipsModule.ts b/src/modules/autoDecomposeDeletionProposedRelationships/AutoDecomposeDeletionProposedRelationshipsModule.ts index 40f4a4c6..a3746d86 100644 --- a/src/modules/autoDecomposeDeletionProposedRelationships/AutoDecomposeDeletionProposedRelationshipsModule.ts +++ b/src/modules/autoDecomposeDeletionProposedRelationships/AutoDecomposeDeletionProposedRelationshipsModule.ts @@ -14,15 +14,32 @@ export default class AutoDecomposeDeletionProposedRelationshipsModule extends Co const currentIdentityResult = await this.runtime.getServices().transportServices.account.getIdentityInfo(); this.currentIdentity = currentIdentityResult.value.address; + await this.decomposeDeletionProposedRelationshipsAtStartup(); + this.subscribeToEvent(RelationshipChangedEvent, this.handleRelationshipChanged.bind(this)); } + private async decomposeDeletionProposedRelationshipsAtStartup() { + const services = this.runtime.getServices(); + + const deletionProposedRelationships = await services.transportServices.relationships.getRelationships({ query: { status: "DeletionProposed" } }); + this.logger.info(`Found ${deletionProposedRelationships.value.length} 'DeletionProposed' Relationships.`); + + for (const relationship of deletionProposedRelationships.value) { + await this.decomposeRelationship(relationship.id); + } + } + private async handleRelationshipChanged(event: RelationshipChangedEvent) { if (event.data.status !== RelationshipStatus.DeletionProposed) return; this.logger.info("'DeletionProposed' Relationship detected."); - const result = await this.runtime.getServices().transportServices.relationships.decomposeRelationship({ relationshipId: event.data.id }); + await this.decomposeRelationship(event.data.id); + } + + private async decomposeRelationship(relationshipId: string) { + const result = await this.runtime.getServices().transportServices.relationships.decomposeRelationship({ relationshipId }); if (result.isSuccess) { this.logger.info("'DeletionProposed' Relationship was decomposed successfully.");