Skip to content

Commit bbf812d

Browse files
Add attributeForwardingDetailsQuery to GetOwnAttributesSharedWithPeerRequest (#825)
* feat: add attributeForwardingDetails query to GetOwnAttributesSharedWithPeer * test: query --------- Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
1 parent b4ff797 commit bbf812d

File tree

6 files changed

+133
-7
lines changed

6 files changed

+133
-7
lines changed

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

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1574,15 +1574,27 @@ export class AttributesController extends ConsumptionBaseController {
15741574
return docs.map((doc) => AttributeForwardingDetails.from(doc));
15751575
}
15761576

1577-
public async getAttributesExchangedWithPeer(peer: CoreAddress, query: any, hideTechnical = false, onlyLatestVersions = true): Promise<LocalAttribute[]> {
1578-
const attributesForwardedToPeer = await this.getAttributesForwardedToPeer(peer, query, hideTechnical, onlyLatestVersions);
1577+
public async getAttributesExchangedWithPeer(
1578+
peer: CoreAddress,
1579+
query: any,
1580+
attributeForwardingDetailsQuery?: any,
1581+
hideTechnical = false,
1582+
onlyLatestVersions = true
1583+
): Promise<LocalAttribute[]> {
1584+
const attributesForwardedToPeer = await this.getAttributesForwardedToPeer(peer, query, attributeForwardingDetailsQuery, hideTechnical, onlyLatestVersions);
15791585
const attributesWithPeer = await this.getAttributesWithPeer(peer, query, hideTechnical, onlyLatestVersions);
15801586

15811587
return [...attributesForwardedToPeer, ...attributesWithPeer];
15821588
}
15831589

1584-
public async getAttributesForwardedToPeer(peer: CoreAddress, query: any, hideTechnical = false, onlyLatestVersions = true): Promise<LocalAttribute[]> {
1585-
const forwardingDetailsDocs = await this.forwardingDetails.find({ peer: peer.toString() });
1590+
public async getAttributesForwardedToPeer(
1591+
peer: CoreAddress,
1592+
query: any,
1593+
attributeForwardingDetailsQuery?: any,
1594+
hideTechnical = false,
1595+
onlyLatestVersions = true
1596+
): Promise<LocalAttribute[]> {
1597+
const forwardingDetailsDocs = await this.forwardingDetails.find({ ...attributeForwardingDetailsQuery, peer: peer.toString() });
15861598
const forwardingDetails = forwardingDetailsDocs.map((doc) => AttributeForwardingDetails.from(doc));
15871599

15881600
const attributeIds = [...new Set(forwardingDetails.map((details) => details.attributeId.toString()))];

packages/runtime/src/useCases/common/Schemas.ts

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13815,6 +13815,9 @@ export const GetOwnAttributesSharedWithPeerRequest: any = {
1381513815
"query": {
1381613816
"$ref": "#/definitions/GetOwnAttributesSharedWithPeerRequestQuery"
1381713817
},
13818+
"attributeForwardingDetailsQuery": {
13819+
"$ref": "#/definitions/GetOwnAttributesSharedWithPeerRequestAttributeForwardingDetailsQuery"
13820+
},
1381813821
"hideTechnical": {
1381913822
"type": "boolean"
1382013823
},
@@ -13989,6 +13992,77 @@ export const GetOwnAttributesSharedWithPeerRequest: any = {
1398913992
}
1399013993
},
1399113994
"additionalProperties": false
13995+
},
13996+
"GetOwnAttributesSharedWithPeerRequestAttributeForwardingDetailsQuery": {
13997+
"type": "object",
13998+
"properties": {
13999+
"sourceReference": {
14000+
"anyOf": [
14001+
{
14002+
"type": "string"
14003+
},
14004+
{
14005+
"type": "array",
14006+
"items": {
14007+
"type": "string"
14008+
}
14009+
}
14010+
]
14011+
},
14012+
"sharedAt": {
14013+
"anyOf": [
14014+
{
14015+
"type": "string"
14016+
},
14017+
{
14018+
"type": "array",
14019+
"items": {
14020+
"type": "string"
14021+
}
14022+
}
14023+
]
14024+
},
14025+
"deletionInfo": {
14026+
"anyOf": [
14027+
{
14028+
"type": "string"
14029+
},
14030+
{
14031+
"type": "array",
14032+
"items": {
14033+
"type": "string"
14034+
}
14035+
}
14036+
]
14037+
},
14038+
"deletionInfo.deletionStatus": {
14039+
"anyOf": [
14040+
{
14041+
"type": "string"
14042+
},
14043+
{
14044+
"type": "array",
14045+
"items": {
14046+
"type": "string"
14047+
}
14048+
}
14049+
]
14050+
},
14051+
"deletionInfo.deletionDate": {
14052+
"anyOf": [
14053+
{
14054+
"type": "string"
14055+
},
14056+
{
14057+
"type": "array",
14058+
"items": {
14059+
"type": "string"
14060+
}
14061+
}
14062+
]
14063+
}
14064+
},
14065+
"additionalProperties": false
1399214066
}
1399314067
}
1399414068
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ export class DeleteSharedAttributesForRejectedOrRevokedRelationshipUseCase exten
5151
const forwardedAttributes = (await this.attributesController.getAttributesForwardedToPeer(
5252
relationship.peer.address,
5353
{ "@type": { $in: ["OwnIdentityAttribute", "OwnRelationshipAttribute", "PeerRelationshipAttribute"] } },
54+
{},
5455
undefined,
5556
false
5657
)) as (OwnIdentityAttribute | OwnRelationshipAttribute | PeerRelationshipAttribute)[];

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

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@ import { AddressString, SchemaRepository, SchemaValidator, UseCase } from "../..
77
import { flattenObject } from "../../common/flattenObject";
88
import { AttributeMapper } from "./AttributeMapper";
99
import { GetAttributesRequestQuery, GetAttributesUseCase } from "./GetAttributes";
10+
import { GetForwardingDetailsForAttributeUseCase } from "./GetForwardingDetailsForAttribute";
1011

1112
export interface GetOwnAttributesSharedWithPeerRequest {
1213
peer: AddressString;
1314
query?: GetOwnAttributesSharedWithPeerRequestQuery;
15+
attributeForwardingDetailsQuery?: GetOwnAttributesSharedWithPeerRequestAttributeForwardingDetailsQuery;
1416
hideTechnical?: boolean;
1517
/**
1618
* default: true
@@ -35,6 +37,14 @@ export interface GetOwnAttributesSharedWithPeerRequestQuery {
3537
"deletionInfo.deletionDate"?: string | string[];
3638
}
3739

40+
export interface GetOwnAttributesSharedWithPeerRequestAttributeForwardingDetailsQuery {
41+
sourceReference?: string | string[];
42+
sharedAt?: string | string[];
43+
deletionInfo?: string | string[];
44+
"deletionInfo.deletionStatus"?: string | string[];
45+
"deletionInfo.deletionDate"?: string | string[];
46+
}
47+
3848
class Validator extends SchemaValidator<GetOwnAttributesSharedWithPeerRequest> {
3949
public constructor(@Inject schemaRepository: SchemaRepository) {
4050
super(schemaRepository.getSchema("GetOwnAttributesSharedWithPeerRequest"));
@@ -51,15 +61,18 @@ export class GetOwnAttributesSharedWithPeerUseCase extends UseCase<GetOwnAttribu
5161

5262
protected async executeInternal(request: GetOwnAttributesSharedWithPeerRequest): Promise<Result<LocalAttributeDTO[]>> {
5363
const query: GetAttributesRequestQuery = request.query ?? {};
54-
5564
const flattenedQuery = flattenObject(query);
5665
const dbQuery = GetAttributesUseCase.queryTranslator.parse(flattenedQuery);
66+
dbQuery["@type"] = dbQuery["@type"] ?? { $in: ["OwnIdentityAttribute", "OwnRelationshipAttribute"] };
5767

58-
dbQuery["@type"] = { $in: ["OwnIdentityAttribute", "OwnRelationshipAttribute"] };
68+
const attributeForwardingDetailsQuery = request.attributeForwardingDetailsQuery ?? {};
69+
const flattenedAttributeForwardingDetailsQuery = flattenObject(attributeForwardingDetailsQuery);
70+
const dbAttributeForwardingDetailsQuery = GetForwardingDetailsForAttributeUseCase.queryTranslator.parse(flattenedAttributeForwardingDetailsQuery);
5971

6072
const attributes = await this.attributeController.getAttributesExchangedWithPeer(
6173
CoreAddress.from(request.peer),
6274
dbQuery,
75+
dbAttributeForwardingDetailsQuery,
6376
request.hideTechnical,
6477
request.onlyLatestVersions
6578
);

packages/runtime/src/useCases/transport/relationships/GetAttributesForRelationship.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export class GetAttributesForRelationshipUseCase extends UseCase<GetAttributesFo
3939
return Result.fail(RuntimeErrors.general.recordNotFound(Relationship));
4040
}
4141

42-
const attributes = await this.attributesController.getAttributesExchangedWithPeer(relationship.peer.address, {}, request.hideTechnical, request.onlyLatestVersions);
42+
const attributes = await this.attributesController.getAttributesExchangedWithPeer(relationship.peer.address, {}, {}, request.hideTechnical, request.onlyLatestVersions);
4343

4444
return Result.ok(AttributeMapper.toAttributeDTOList(attributes));
4545
}

packages/runtime/test/consumption/attributes.test.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import {
3131
ForwardedAttributeDeletedByPeerEvent,
3232
GetAttributeUseCase,
3333
GetAttributesUseCase,
34+
GetOwnAttributesSharedWithPeerRequest,
3435
GetOwnAttributesSharedWithPeerUseCase,
3536
GetOwnIdentityAttributesUseCase,
3637
GetPeerAttributesUseCase,
@@ -605,6 +606,31 @@ describe("get OwnIdentityAttributes, own Attributes shared with peer and peer At
605606
const ownAttributesSharedWithPeer = result.value;
606607
expect(ownAttributesSharedWithPeer).toStrictEqual([updatedServices1OwnGivenNameV1, services1OwnRelationshipAttributeV1, services1TechnicalOwnRelationshipAttribute]);
607608
});
609+
610+
test("should return an attribute if it matches the (AttributeForwardingDetails)Query", async function () {
611+
const forwardingDetails = (await services1.consumption.attributes.getForwardingDetailsForAttribute({ attributeId: services1OwnGivenNameV1.id })).value;
612+
const sharedAt = forwardingDetails[0].sharedAt;
613+
614+
const request: GetOwnAttributesSharedWithPeerRequest = {
615+
peer: services2.address,
616+
query: { "@type": "OwnIdentityAttribute" },
617+
attributeForwardingDetailsQuery: { sharedAt }
618+
};
619+
620+
const matchingAttributes = (await services1.consumption.attributes.getOwnAttributesSharedWithPeer(request)).value;
621+
expect(matchingAttributes).toStrictEqual([services1OwnGivenNameV1]);
622+
});
623+
624+
test("should not return an attribute if it doesn't match the (AttributeForwardingDetails)Query", async function () {
625+
const request: GetOwnAttributesSharedWithPeerRequest = {
626+
peer: services2.address,
627+
query: { "@type": "OwnIdentityAttribute" },
628+
attributeForwardingDetailsQuery: { sharedAt: "anotherTime" }
629+
};
630+
631+
const matchingAttributes = (await services1.consumption.attributes.getOwnAttributesSharedWithPeer(request)).value;
632+
expect(matchingAttributes).toHaveLength(0);
633+
});
608634
});
609635

610636
describe(GetPeerAttributesUseCase.name, () => {

0 commit comments

Comments
 (0)