diff --git a/packages/consumption/src/consumption/ConsumptionCoreErrors.ts b/packages/consumption/src/consumption/ConsumptionCoreErrors.ts index 4929645aa..87582bba6 100644 --- a/packages/consumption/src/consumption/ConsumptionCoreErrors.ts +++ b/packages/consumption/src/consumption/ConsumptionCoreErrors.ts @@ -310,6 +310,10 @@ class Requests { return new ApplicationError("error.consumption.requests.validation.inheritedFromItem", message); } + public cannotShareRequestWithYourself() { + return new CoreError("error.consumption.requests.cannotShareRequestWithYourself", "You cannot share a Request with yourself."); + } + private static readonly _decideValidation = class { public invalidNumberOfItems(message: string) { return new ApplicationError("error.consumption.requests.decide.validation.invalidNumberOfItems", message); diff --git a/packages/consumption/src/modules/requests/outgoing/OutgoingRequestsController.ts b/packages/consumption/src/modules/requests/outgoing/OutgoingRequestsController.ts index a4757745a..816e8c64c 100644 --- a/packages/consumption/src/modules/requests/outgoing/OutgoingRequestsController.ts +++ b/packages/consumption/src/modules/requests/outgoing/OutgoingRequestsController.ts @@ -40,6 +40,11 @@ export class OutgoingRequestsController extends ConsumptionBaseController { public async canCreate(params: ICanCreateOutgoingRequestParameters): Promise { const parsedParams = CanCreateOutgoingRequestParameters.from(params); + + if (parsedParams.peer?.equals(this.identity.address)) { + return ValidationResult.error(ConsumptionCoreErrors.requests.cannotShareRequestWithYourself()); + } + if (parsedParams.peer) { const relationship = await this.relationshipResolver.getRelationshipToIdentity(parsedParams.peer); diff --git a/packages/consumption/test/modules/requests/OutgoingRequestsController.test.ts b/packages/consumption/test/modules/requests/OutgoingRequestsController.test.ts index 003fbb15f..f3a5f578f 100644 --- a/packages/consumption/test/modules/requests/OutgoingRequestsController.test.ts +++ b/packages/consumption/test/modules/requests/OutgoingRequestsController.test.ts @@ -198,6 +198,32 @@ describe("OutgoingRequestsController", function () { expect(validationResult.items[1].items).toHaveLength(1); expect(validationResult.items[1].items[0].isError()).toBe(true); }); + + test("returns a validation result that contains an error for requests to myself", async function () { + const validationResult = await When.iCallCanCreateForAnOutgoingRequest({ + content: { + items: [ + TestRequestItem.from({ + mustBeAccepted: false + }), + RequestItemGroup.from({ + items: [ + TestRequestItem.from({ + mustBeAccepted: false, + shouldFailAtCanCreateOutgoingRequestItem: true + }) + ] + }) + ] + }, + peer: context.currentIdentity.address + }); + + expect(validationResult).errorValidationResult({ + code: "error.consumption.requests.cannotShareRequestWithYourself", + message: "You cannot share a Request with yourself." + }); + }); }); describe("Create (on active relationship)", function () {