Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple open Requests that lead to a Relationship can exist for the same Identity #309

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions packages/runtime/src/modules/RequestModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,29 @@ export class RequestModule extends RuntimeModule {
return;
}

const otherRequestsThatWouldLeadToARelationship = (
await services.consumptionServices.incomingRequests.getRequests({
query: {
"source.type": "RelationshipTemplate",
status: [LocalRequestStatus.Open, LocalRequestStatus.DecisionRequired, LocalRequestStatus.ManualDecisionRequired, LocalRequestStatus.Decided],
peer: template.createdBy
}
})
).value;
if (otherRequestsThatWouldLeadToARelationship.length !== 0) {
this.logger.info(
`There is already an open Request for a RelationshipTemplate that would lead to a Relationship with the creator of the RelationshipTemplate '${template.id}'. Skipping creation of a new Request.`
);
this.runtime.eventBus.publish(
new RelationshipTemplateProcessedEvent(event.eventTargetAddress, {
template,
result: RelationshipTemplateProcessedResult.NonCompletedRequestExists,
requestId: otherRequestsThatWouldLeadToARelationship[0].id
})
);
return;
}

const requestCreated = await this.createIncomingRequest(services, body.onNewRelationship, template.id);

if (!requestCreated) {
Expand Down
58 changes: 58 additions & 0 deletions packages/runtime/test/modules/RequestModule.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -584,3 +584,61 @@ describe("Handling the rejection and the revocation of a Relationship by the Req
return sRelationship;
}
});

describe("Handle Multiple RelationshipTemplate loadings", () => {
const runtimeServiceProvider = new RuntimeServiceProvider();
let sRuntimeServices: TestRuntimeServices;
let rRuntimeServices: TestRuntimeServices;

beforeAll(async () => {
const runtimeServices = await runtimeServiceProvider.launch(2, { enableRequestModule: true, enableDeciderModule: true });

sRuntimeServices = runtimeServices[0];
rRuntimeServices = runtimeServices[1];
}, 30000);

beforeEach(() => {
sRuntimeServices.eventBus.reset();
rRuntimeServices.eventBus.reset();
});

afterEach(() => {
sRuntimeServices.eventBus.reset();
rRuntimeServices.eventBus.reset();
});

afterAll(async () => await runtimeServiceProvider.stop());

test("no multiple open Requests that lead to a Relationship can exist for the same Identity", async () => {
const relationshipTemplateContent = RelationshipTemplateContent.from({
onNewRelationship: { "@type": "Request", items: [{ "@type": "TestRequestItem", mustBeAccepted: false }] }
}).toJSON();

const firstTemplate = await exchangeTemplate(sRuntimeServices.transport, rRuntimeServices.transport, relationshipTemplateContent);
await rRuntimeServices.eventBus.waitForRunningEventHandlers();
await expect(rRuntimeServices.eventBus).toHavePublished(
RelationshipTemplateProcessedEvent,
(e) => e.data.result === RelationshipTemplateProcessedResult.ManualRequestDecisionRequired
);

const requestForTemplate = (await rRuntimeServices.consumption.incomingRequests.getRequests({ query: { "source.reference": firstTemplate.id } })).value[0];

rRuntimeServices.eventBus.reset();

const secondTemplate = await exchangeTemplate(sRuntimeServices.transport, rRuntimeServices.transport, relationshipTemplateContent);
await rRuntimeServices.eventBus.waitForRunningEventHandlers();

await expect(rRuntimeServices.eventBus).not.toHavePublished(
RelationshipTemplateProcessedEvent,
(e) => e.data.result === RelationshipTemplateProcessedResult.ManualRequestDecisionRequired
);

await expect(rRuntimeServices.eventBus).toHavePublished(
RelationshipTemplateProcessedEvent,
(e) =>
e.data.result === RelationshipTemplateProcessedResult.NonCompletedRequestExists &&
e.data.template.id === secondTemplate.id &&
e.data.requestId === requestForTemplate.id
);
});
});