Skip to content

Commit 537a09e

Browse files
AttributeForwardingDetails are not deleted if associated Attribute is deleted (#822)
* feat: delete ForwardingDetails of associated attribute is deleted * test: rename ForwardingDetails to AttributeForwardingDetails * refactor: rename _deleteForwardingDetails --------- Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
1 parent d59176f commit 537a09e

3 files changed

Lines changed: 56 additions & 28 deletions

File tree

packages/consumption/src/modules/attributes/AttributesController.ts

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -809,6 +809,8 @@ export class AttributesController extends ConsumptionBaseController {
809809
if (!attribute) throw TransportCoreErrors.general.recordNotFound(LocalAttribute, attributeId.toString());
810810

811811
await this.deleteAttributeUnsafe(attributeId);
812+
await this._deleteForwardingDetails({ attributeId: attribute.id.toString() });
813+
812814
this.eventBus.publish(new AttributeDeletedEvent(this.identity.address.toString(), attribute));
813815
}
814816

@@ -826,25 +828,28 @@ export class AttributesController extends ConsumptionBaseController {
826828
"@type": { $in: ["OwnIdentityAttribute", "OwnRelationshipAttribute", "PeerRelationshipAttribute"] }
827829
})) as (OwnIdentityAttribute | OwnRelationshipAttribute | PeerRelationshipAttribute)[];
828830
for (const attribute of forwardedAttributes) {
829-
await this.removeForwardingDetailsFromAttribute(attribute, peer);
831+
await this.deleteForwardingDetailsForAttribute(attribute, peer);
830832
}
831833
}
832834

833-
public async removeForwardingDetailsFromAttribute<T extends OwnIdentityAttribute | OwnRelationshipAttribute | PeerRelationshipAttribute>(
834-
attribute: T,
835-
peer: CoreAddress
836-
): Promise<T> {
837-
const existingForwardingDetailsDocs = await this.forwardingDetails.find({ attributeId: attribute.id.toString(), peer: peer.toString() });
838-
const existingForwardingDetails = existingForwardingDetailsDocs.map((obj) => AttributeForwardingDetails.from(obj));
835+
public async deleteForwardingDetailsForAttribute(attribute: OwnIdentityAttribute | OwnRelationshipAttribute | PeerRelationshipAttribute, peer?: CoreAddress): Promise<void> {
836+
const query: any = { attributeId: attribute.id.toString() };
837+
if (peer) query["peer"] = peer.toString();
839838

840-
for (const entry of existingForwardingDetails) {
841-
await this.forwardingDetails.delete(entry);
842-
}
839+
await this._deleteForwardingDetails(query);
843840

844841
await this.updateNumberOfForwards(attribute);
845842

846843
this.eventBus.publish(new AttributeForwardingDetailsChangedEvent(this.identity.address.toString(), attribute));
847-
return attribute;
844+
}
845+
846+
private async _deleteForwardingDetails(query: any): Promise<void> {
847+
const existingForwardingDetailsDocs = await this.forwardingDetails.find(query);
848+
const existingForwardingDetails = existingForwardingDetailsDocs.map((obj) => AttributeForwardingDetails.from(obj));
849+
850+
for (const entry of existingForwardingDetails) {
851+
await this.forwardingDetails.delete(entry);
852+
}
848853
}
849854

850855
public async executeFullAttributeDeletionProcess(attribute: LocalAttribute): Promise<void> {
@@ -1566,7 +1571,6 @@ export class AttributesController extends ConsumptionBaseController {
15661571

15671572
public async getForwardingDetailsForAttribute(attribute: LocalAttribute, query: any = {}): Promise<AttributeForwardingDetails[]> {
15681573
const docs = await this.forwardingDetails.find({ ...query, attributeId: attribute.id.toString() });
1569-
15701574
return docs.map((doc) => AttributeForwardingDetails.from(doc));
15711575
}
15721576

packages/consumption/test/modules/attributes/AttributesController.test.ts

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -327,8 +327,8 @@ describe("AttributesController", function () {
327327
});
328328
});
329329

330-
describe("change ForwardingDetails of Attributes", function () {
331-
test("should allow to add ForwardingDetails to an OwnIdentityAttribute", async function () {
330+
describe("change AttributeForwardingDetails of Attributes", function () {
331+
test("should allow to add AttributeForwardingDetails to an OwnIdentityAttribute", async function () {
332332
const attributeParams = {
333333
content: IdentityAttribute.from({
334334
value: {
@@ -350,7 +350,7 @@ describe("AttributesController", function () {
350350
expect(isForwarded).toBe(true);
351351
});
352352

353-
test("should allow to add ForwardingDetails to an OwnRelationshipAttribute", async function () {
353+
test("should allow to add AttributeForwardingDetails to an OwnRelationshipAttribute", async function () {
354354
const thirdPartyAddress = CoreAddress.from("thirdPartyAdress");
355355
const peerAddress = CoreAddress.from("peerAddress");
356356

@@ -381,7 +381,7 @@ describe("AttributesController", function () {
381381
expect(isForwarded).toBe(true);
382382
});
383383

384-
test("should allow to add ForwardingDetails to a PeerRelationshipAttribute", async function () {
384+
test("should allow to add AttributeForwardingDetails to a PeerRelationshipAttribute", async function () {
385385
const thirdPartyAddress = CoreAddress.from("thirdPartyAdress");
386386
const peerAddress = CoreAddress.from("peerAddress");
387387

@@ -412,7 +412,7 @@ describe("AttributesController", function () {
412412
expect(isForwarded).toBe(true);
413413
});
414414

415-
test("should publish an event adding ForwardingDetails to an Attribute", async function () {
415+
test("should publish an event adding AttributeForwardingDetails to an Attribute", async function () {
416416
const attributeParams = {
417417
content: IdentityAttribute.from({
418418
value: {
@@ -431,7 +431,7 @@ describe("AttributesController", function () {
431431
mockEventBus.expectLastPublishedEvent(AttributeForwardingDetailsChangedEvent, forwardedAttribute);
432432
});
433433

434-
test("should throw trying to add ForwardingDetails to an Attribute if it is already forwarded", async function () {
434+
test("should throw trying to add AttributeForwardingDetails to an Attribute if it is already forwarded", async function () {
435435
const attributeParams = {
436436
content: IdentityAttribute.from({
437437
value: {
@@ -449,7 +449,7 @@ describe("AttributesController", function () {
449449
);
450450
});
451451

452-
test("should allow to add ForwardingDetails to an Attribute if it is already forwarded but DeletedByRecipient", async function () {
452+
test("should allow to add AttributeForwardingDetails to an Attribute if it is already forwarded but DeletedByRecipient", async function () {
453453
const attributeParams = {
454454
content: IdentityAttribute.from({
455455
value: {
@@ -472,7 +472,7 @@ describe("AttributesController", function () {
472472
expect(updatedAttribute.numberOfForwards).toBe(2);
473473
});
474474

475-
test("should updated ForwardingDetails of an Attribute if it is already forwarded but ToBeDeletedByRecipient", async function () {
475+
test("should updated AttributeForwardingDetails of an Attribute if it is already forwarded but ToBeDeletedByRecipient", async function () {
476476
const attributeParams = {
477477
content: IdentityAttribute.from({
478478
value: {
@@ -498,7 +498,7 @@ describe("AttributesController", function () {
498498
expect(forwardingDetails[0].deletionInfo).toBeUndefined();
499499
});
500500

501-
test("should remove ForwardingDetails from an Attribute", async function () {
501+
test("should delete AttributeForwardingDetails for an Attribute", async function () {
502502
const attributeParams = {
503503
content: IdentityAttribute.from({
504504
value: {
@@ -516,12 +516,13 @@ describe("AttributesController", function () {
516516
const isForwarded = await consumptionController.attributes.isAttributeForwardedToPeer(forwardedAttribute, peer);
517517
expect(isForwarded).toBe(true);
518518

519-
const updatedAttribute = await consumptionController.attributes.removeForwardingDetailsFromAttribute(forwardedAttribute, peer);
520-
const updatedIsForwarded = await consumptionController.attributes.isAttributeForwardedToPeer(updatedAttribute, peer);
519+
await consumptionController.attributes.deleteForwardingDetailsForAttribute(forwardedAttribute, peer);
520+
const updatedAttribute = await consumptionController.attributes.getLocalAttribute(forwardedAttribute.id);
521+
const updatedIsForwarded = await consumptionController.attributes.isAttributeForwardedToPeer(updatedAttribute!, peer);
521522
expect(updatedIsForwarded).toBe(false);
522523
});
523524

524-
test("should publish an event when removing ForwardingDetails from an Attribute", async function () {
525+
test("should publish an event when removing AttributeForwardingDetails from an Attribute", async function () {
525526
const attributeParams = {
526527
content: IdentityAttribute.from({
527528
value: {
@@ -537,11 +538,12 @@ describe("AttributesController", function () {
537538
const forwardedAttribute = await consumptionController.attributes.addForwardingDetailsToAttribute(attribute, peer, CoreId.from("aSourceReferenceId"));
538539
mockEventBus.clearPublishedEvents();
539540

540-
const updatedAttribute = await consumptionController.attributes.removeForwardingDetailsFromAttribute(forwardedAttribute, peer);
541+
await consumptionController.attributes.deleteForwardingDetailsForAttribute(forwardedAttribute, peer);
542+
const updatedAttribute = (await consumptionController.attributes.getLocalAttribute(forwardedAttribute.id)) as OwnIdentityAttribute;
541543
mockEventBus.expectLastPublishedEvent(AttributeForwardingDetailsChangedEvent, updatedAttribute);
542544
});
543545

544-
test("should not change Attribute trying to remove ForwardingDetails from an Attribute without ForwardingDetails", async function () {
546+
test("should not change Attribute trying to remove AttributeForwardingDetails from an Attribute without AttributeForwardingDetails", async function () {
545547
const attributeParams = {
546548
content: IdentityAttribute.from({
547549
value: {
@@ -554,7 +556,9 @@ describe("AttributesController", function () {
554556
const attributeWithoutForwardingDetails = await consumptionController.attributes.createOwnIdentityAttribute(attributeParams);
555557

556558
const peer = CoreAddress.from("address");
557-
const unchangedAttribute = await consumptionController.attributes.removeForwardingDetailsFromAttribute(attributeWithoutForwardingDetails, peer);
559+
await consumptionController.attributes.deleteForwardingDetailsForAttribute(attributeWithoutForwardingDetails, peer);
560+
561+
const unchangedAttribute = await consumptionController.attributes.getLocalAttribute(attributeWithoutForwardingDetails.id);
558562
expect(unchangedAttribute).toStrictEqual(attributeWithoutForwardingDetails);
559563
});
560564
});
@@ -1044,6 +1048,26 @@ describe("AttributesController", function () {
10441048
expect(peerAttribute).toBeUndefined();
10451049
});
10461050

1051+
test("should delete AttributeForwardingDetails when deleting an Attribute", async function () {
1052+
const attribute = await consumptionController.attributes.createOwnIdentityAttribute({
1053+
content: IdentityAttribute.from({
1054+
value: EMailAddress.from({
1055+
value: "my@email.address"
1056+
}),
1057+
owner: consumptionController.accountController.identity.address
1058+
})
1059+
});
1060+
await consumptionController.attributes.addForwardingDetailsToAttribute(attribute, CoreAddress.from("aPeer"), CoreId.from("aSourceReference"));
1061+
1062+
const forwardingDetailsBeforeAttributeDeletion = await consumptionController.attributes.getForwardingDetailsForAttribute(attribute);
1063+
expect(forwardingDetailsBeforeAttributeDeletion).toHaveLength(1);
1064+
1065+
await consumptionController.attributes.deleteAttribute(attribute.id);
1066+
1067+
const forwardingDetailsAfterAttributeDeletion = await consumptionController.attributes.getForwardingDetailsForAttribute(attribute);
1068+
expect(forwardingDetailsAfterAttributeDeletion).toHaveLength(0);
1069+
});
1070+
10471071
describe("should validate and execute full Attribute deletion process", function () {
10481072
let predecessorOwnIdentityAttribute: OwnIdentityAttribute;
10491073
let successorOwnIdentityAttribute: OwnIdentityAttribute;

packages/runtime/src/useCases/consumption/attributes/DeleteSharedAttributesForRejectedOrRevokedRelationship.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ export class DeleteSharedAttributesForRejectedOrRevokedRelationshipUseCase exten
5656
)) as (OwnIdentityAttribute | OwnRelationshipAttribute | PeerRelationshipAttribute)[];
5757

5858
for (const attribute of forwardedAttributes) {
59-
await this.attributesController.removeForwardingDetailsFromAttribute(attribute, relationship.peer.address);
59+
await this.attributesController.deleteForwardingDetailsForAttribute(attribute, relationship.peer.address);
6060
}
6161

6262
await this.accountController.syncDatawallet();

0 commit comments

Comments
 (0)