From 3d91752f0ede62c6e096291cb535fbcfa6eeef30 Mon Sep 17 00:00:00 2001 From: Eric Brunner Date: Mon, 20 Jan 2025 09:56:08 +0100 Subject: [PATCH 1/4] feat: added MaxNumberOfMessageRecipients --- Applications/ConsumerApi/src/appsettings.json | 3 ++- .../Exceptions/GenericApplicationErrors.cs | 6 ++++++ .../Messages/src/Messages.Application/ApplicationOptions.cs | 3 +++ .../Messages.ConsumerApi/Controllers/MessagesController.cs | 4 ++++ 4 files changed, 15 insertions(+), 1 deletion(-) diff --git a/Applications/ConsumerApi/src/appsettings.json b/Applications/ConsumerApi/src/appsettings.json index eaab20ca3d..27b8727962 100644 --- a/Applications/ConsumerApi/src/appsettings.json +++ b/Applications/ConsumerApi/src/appsettings.json @@ -84,7 +84,8 @@ "Pagination": { "DefaultPageSize": 50, "MaxPageSize": 200 - } + }, + "MaxNumberOfMessageRecipients": 1 }, "Infrastructure": { "SqlDatabase": { diff --git a/BuildingBlocks/src/BuildingBlocks.Application.Abstractions/Exceptions/GenericApplicationErrors.cs b/BuildingBlocks/src/BuildingBlocks.Application.Abstractions/Exceptions/GenericApplicationErrors.cs index 9771f4bd67..a78386b884 100644 --- a/BuildingBlocks/src/BuildingBlocks.Application.Abstractions/Exceptions/GenericApplicationErrors.cs +++ b/BuildingBlocks/src/BuildingBlocks.Application.Abstractions/Exceptions/GenericApplicationErrors.cs @@ -52,5 +52,11 @@ public static ApplicationError InputCannotBeParsed(string reason = "The input ca { return new ApplicationError("error.platform.inputCannotBeParsed", reason); } + + public static ApplicationError InvalidNumberOfRecipients(int maxNumberOfMessageRecipients) + { + return new ApplicationError("error.platform.validation.invalidNumberOfRecipients", + $"The number of recipients exceeds the maximum allowed number of recipients. The maximum number of recipients is {maxNumberOfMessageRecipients}."); + } } } diff --git a/Modules/Messages/src/Messages.Application/ApplicationOptions.cs b/Modules/Messages/src/Messages.Application/ApplicationOptions.cs index 242bec1791..6f12888d49 100644 --- a/Modules/Messages/src/Messages.Application/ApplicationOptions.cs +++ b/Modules/Messages/src/Messages.Application/ApplicationOptions.cs @@ -14,6 +14,9 @@ public class ApplicationOptions [MinLength(3)] [MaxLength(45)] public string DidDomainName { get; set; } = null!; + + [Required] + public int MaxNumberOfMessageRecipients { get; set; } } public class PaginationOptions diff --git a/Modules/Messages/src/Messages.ConsumerApi/Controllers/MessagesController.cs b/Modules/Messages/src/Messages.ConsumerApi/Controllers/MessagesController.cs index ce13403334..02de091207 100644 --- a/Modules/Messages/src/Messages.ConsumerApi/Controllers/MessagesController.cs +++ b/Modules/Messages/src/Messages.ConsumerApi/Controllers/MessagesController.cs @@ -59,6 +59,10 @@ public async Task GetMessage(string id, [FromQuery] bool? noBody, [ProducesError(StatusCodes.Status400BadRequest)] public async Task SendMessage(SendMessageCommand request, CancellationToken cancellationToken) { + if (request.Recipients.Count > _options.MaxNumberOfMessageRecipients) + throw new ApplicationException( + GenericApplicationErrors.Validation.InvalidNumberOfRecipients(_options.MaxNumberOfMessageRecipients)); + var response = await _mediator.Send(request, cancellationToken); return CreatedAtAction(nameof(GetMessage), new { id = response.Id }, response); } From 81bdb9b899fe12b1caf9114be02af887ff9156a1 Mon Sep 17 00:00:00 2001 From: Eric Brunner Date: Mon, 20 Jan 2025 10:30:23 +0100 Subject: [PATCH 2/4] fix: integration-test appsettings --- .ci/appsettings.override.postgres.docker.json | 3 ++- .ci/appsettings.override.postgres.local.json | 3 ++- .ci/appsettings.override.sqlserver.docker.json | 3 ++- .ci/appsettings.override.sqlserver.local.json | 3 ++- .../api.appsettings.local.override.json | 3 ++- 5 files changed, 10 insertions(+), 5 deletions(-) diff --git a/.ci/appsettings.override.postgres.docker.json b/.ci/appsettings.override.postgres.docker.json index 346c97be40..7508b0fc3b 100644 --- a/.ci/appsettings.override.postgres.docker.json +++ b/.ci/appsettings.override.postgres.docker.json @@ -79,7 +79,8 @@ }, "Messages": { "Application": { - "DidDomainName": "localhost" + "DidDomainName": "localhost", + "MaxNumberOfMessageRecipients": 5 }, "Infrastructure": { "SqlDatabase": { diff --git a/.ci/appsettings.override.postgres.local.json b/.ci/appsettings.override.postgres.local.json index 8c297fa75d..4379456b7c 100644 --- a/.ci/appsettings.override.postgres.local.json +++ b/.ci/appsettings.override.postgres.local.json @@ -79,7 +79,8 @@ }, "Messages": { "Application": { - "DidDomainName": "localhost" + "DidDomainName": "localhost", + "MaxNumberOfMessageRecipients": 5 }, "Infrastructure": { "SqlDatabase": { diff --git a/.ci/appsettings.override.sqlserver.docker.json b/.ci/appsettings.override.sqlserver.docker.json index 820f41b081..0756abc8fa 100644 --- a/.ci/appsettings.override.sqlserver.docker.json +++ b/.ci/appsettings.override.sqlserver.docker.json @@ -79,7 +79,8 @@ }, "Messages": { "Application": { - "DidDomainName": "localhost" + "DidDomainName": "localhost", + "MaxNumberOfMessageRecipients": 5 }, "Infrastructure": { "SqlDatabase": { diff --git a/.ci/appsettings.override.sqlserver.local.json b/.ci/appsettings.override.sqlserver.local.json index b73d6fad82..673a5a60a4 100644 --- a/.ci/appsettings.override.sqlserver.local.json +++ b/.ci/appsettings.override.sqlserver.local.json @@ -79,7 +79,8 @@ }, "Messages": { "Application": { - "DidDomainName": "localhost" + "DidDomainName": "localhost", + "MaxNumberOfMessageRecipients": 5 }, "Infrastructure": { "SqlDatabase": { diff --git a/Applications/ConsumerApi/test/ConsumerApi.Tests.Integration/api.appsettings.local.override.json b/Applications/ConsumerApi/test/ConsumerApi.Tests.Integration/api.appsettings.local.override.json index 4cac42fafb..544d85ab97 100644 --- a/Applications/ConsumerApi/test/ConsumerApi.Tests.Integration/api.appsettings.local.override.json +++ b/Applications/ConsumerApi/test/ConsumerApi.Tests.Integration/api.appsettings.local.override.json @@ -50,7 +50,8 @@ }, "Messages": { "Application": { - "DidDomainName": "localhost" + "DidDomainName": "localhost", + "MaxNumberOfMessageRecipients": 5 }, "Infrastructure": { "SqlDatabase": { From afd929e57a85253aac3ae7144fbc2656e6dfcf0b Mon Sep 17 00:00:00 2001 From: Eric Brunner Date: Mon, 20 Jan 2025 11:32:49 +0100 Subject: [PATCH 3/4] fix: updated Validator --- .../Exceptions/GenericApplicationErrors.cs | 6 ------ .../Messages/Commands/SendMessage/Validator.cs | 7 +++++-- .../Messages.ConsumerApi/Controllers/MessagesController.cs | 4 ---- 3 files changed, 5 insertions(+), 12 deletions(-) diff --git a/BuildingBlocks/src/BuildingBlocks.Application.Abstractions/Exceptions/GenericApplicationErrors.cs b/BuildingBlocks/src/BuildingBlocks.Application.Abstractions/Exceptions/GenericApplicationErrors.cs index a78386b884..9771f4bd67 100644 --- a/BuildingBlocks/src/BuildingBlocks.Application.Abstractions/Exceptions/GenericApplicationErrors.cs +++ b/BuildingBlocks/src/BuildingBlocks.Application.Abstractions/Exceptions/GenericApplicationErrors.cs @@ -52,11 +52,5 @@ public static ApplicationError InputCannotBeParsed(string reason = "The input ca { return new ApplicationError("error.platform.inputCannotBeParsed", reason); } - - public static ApplicationError InvalidNumberOfRecipients(int maxNumberOfMessageRecipients) - { - return new ApplicationError("error.platform.validation.invalidNumberOfRecipients", - $"The number of recipients exceeds the maximum allowed number of recipients. The maximum number of recipients is {maxNumberOfMessageRecipients}."); - } } } diff --git a/Modules/Messages/src/Messages.Application/Messages/Commands/SendMessage/Validator.cs b/Modules/Messages/src/Messages.Application/Messages/Commands/SendMessage/Validator.cs index 6cab42e1bf..2a1690e0a1 100644 --- a/Modules/Messages/src/Messages.Application/Messages/Commands/SendMessage/Validator.cs +++ b/Modules/Messages/src/Messages.Application/Messages/Commands/SendMessage/Validator.cs @@ -5,12 +5,13 @@ using Backbone.Modules.Messages.Domain.Ids; using Backbone.Tooling.Extensions; using FluentValidation; +using Microsoft.Extensions.Options; namespace Backbone.Modules.Messages.Application.Messages.Commands.SendMessage; public class Validator : AbstractValidator { - public Validator() + public Validator(IOptions options) { RuleFor(m => m.Recipients) .DetailedNotNull() @@ -21,7 +22,9 @@ public Validator() .SetValidator(new SendMessageCommandRecipientInformationValidator())); RuleFor(m => m.Recipients.Count) - .InclusiveBetween(1, 50).WithErrorCode(GenericApplicationErrors.Validation.InvalidPropertyValue().Code); + .LessThanOrEqualTo(options.Value.MaxNumberOfMessageRecipients) + .WithName("Recipients") + .WithErrorCode(GenericApplicationErrors.Validation.InvalidPropertyValue().Code); RuleFor(m => m.Body).DetailedNotNull().NumberOfBytes(1, 10.Mebibytes()); diff --git a/Modules/Messages/src/Messages.ConsumerApi/Controllers/MessagesController.cs b/Modules/Messages/src/Messages.ConsumerApi/Controllers/MessagesController.cs index 02de091207..ce13403334 100644 --- a/Modules/Messages/src/Messages.ConsumerApi/Controllers/MessagesController.cs +++ b/Modules/Messages/src/Messages.ConsumerApi/Controllers/MessagesController.cs @@ -59,10 +59,6 @@ public async Task GetMessage(string id, [FromQuery] bool? noBody, [ProducesError(StatusCodes.Status400BadRequest)] public async Task SendMessage(SendMessageCommand request, CancellationToken cancellationToken) { - if (request.Recipients.Count > _options.MaxNumberOfMessageRecipients) - throw new ApplicationException( - GenericApplicationErrors.Validation.InvalidNumberOfRecipients(_options.MaxNumberOfMessageRecipients)); - var response = await _mediator.Send(request, cancellationToken); return CreatedAtAction(nameof(GetMessage), new { id = response.Id }, response); } From 200125a0b443c1237279160ed4d6616a77e441d7 Mon Sep 17 00:00:00 2001 From: Eric Brunner Date: Mon, 20 Jan 2025 11:48:01 +0100 Subject: [PATCH 4/4] chore: removed custom validation name --- .../Messages/Commands/SendMessage/Validator.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/Modules/Messages/src/Messages.Application/Messages/Commands/SendMessage/Validator.cs b/Modules/Messages/src/Messages.Application/Messages/Commands/SendMessage/Validator.cs index 2a1690e0a1..19c96f151b 100644 --- a/Modules/Messages/src/Messages.Application/Messages/Commands/SendMessage/Validator.cs +++ b/Modules/Messages/src/Messages.Application/Messages/Commands/SendMessage/Validator.cs @@ -23,7 +23,6 @@ public Validator(IOptions options) RuleFor(m => m.Recipients.Count) .LessThanOrEqualTo(options.Value.MaxNumberOfMessageRecipients) - .WithName("Recipients") .WithErrorCode(GenericApplicationErrors.Validation.InvalidPropertyValue().Code); RuleFor(m => m.Body).DetailedNotNull().NumberOfBytes(1, 10.Mebibytes());