-
Notifications
You must be signed in to change notification settings - Fork 35
VCST-4182: Order Document Count Validation #477
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
824177c
VCST-4182: Order Document Count Validation
OlegoO aa8e26b
fix review comments
OlegoO 2d928ba
Rewrite and expand README with full technical documentation.
OlegoO 88145ac
Fix sonar warnings
OlegoO b7a8dda
Simplify OrderDocumentCountValidator
OlegoO 4a44b0b
Fix code review
OlegoO 02e37f3
Skip document count validation if max is zero or less
OlegoO 86055f5
Merge branch 'dev' into feat/VCST-4182
OlegoO 8a0ff00
Merge branch 'dev' into feat/VCST-4182
OlegoO File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
462 changes: 240 additions & 222 deletions
462
src/VirtoCommerce.OrdersModule.Data/Services/CustomerOrderService.cs
Large diffs are not rendered by default.
Oops, something went wrong.
104 changes: 104 additions & 0 deletions
104
src/VirtoCommerce.OrdersModule.Data/Validators/CustomerOrderValidator.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using FluentValidation; | ||
| using FluentValidation.Results; | ||
| using VirtoCommerce.CoreModule.Core.Common; | ||
| using VirtoCommerce.OrdersModule.Core; | ||
| using VirtoCommerce.OrdersModule.Core.Model; | ||
| using VirtoCommerce.Platform.Core.Settings; | ||
|
|
||
| namespace VirtoCommerce.OrdersModule.Data.Validators; | ||
|
|
||
| public class CustomerOrderValidator : AbstractValidator<CustomerOrder> | ||
| { | ||
| private readonly ISettingsManager _settingsManager; | ||
|
|
||
| public CustomerOrderValidator( | ||
| ISettingsManager settingsManager, | ||
| IEnumerable<IValidator<LineItem>> lineItemValidators, | ||
| IEnumerable<IValidator<Shipment>> shipmentValidators, | ||
| IValidator<PaymentIn> paymentInValidator, | ||
| IEnumerable<IValidator<IOperation>> operationValidators) | ||
| { | ||
| _settingsManager = settingsManager; | ||
|
|
||
| SetDefaultRules(); | ||
|
|
||
| if (lineItemValidators.Any()) | ||
| { | ||
| RuleForEach(order => order.Items).SetValidator(lineItemValidators.Last(), "default"); | ||
| } | ||
|
|
||
| if (shipmentValidators.Any()) | ||
| { | ||
| RuleForEach(order => order.Shipments).SetValidator(shipmentValidators.Last(), "default"); | ||
| } | ||
|
|
||
| RuleForEach(order => order.InPayments).SetValidator(paymentInValidator); | ||
|
|
||
| // Apply all operation-level validators (e.g., document count limits) | ||
| foreach (var operationValidator in operationValidators) | ||
| { | ||
| Include(operationValidator); | ||
| } | ||
| } | ||
|
|
||
| public override ValidationResult Validate(ValidationContext<CustomerOrder> context) | ||
| { | ||
| // Check if validation is enabled (synchronous version) | ||
| var isValidationEnabled = _settingsManager.GetValueAsync<bool>( | ||
| ModuleConstants.Settings.General.CustomerOrderValidation) | ||
| .GetAwaiter() | ||
| .GetResult(); | ||
|
|
||
| if (!isValidationEnabled) | ||
| { | ||
| // Skip validation if disabled | ||
| return new ValidationResult(); | ||
| } | ||
|
|
||
| // Perform validation if enabled | ||
| return base.Validate(context); | ||
| } | ||
|
|
||
| public override async Task<ValidationResult> ValidateAsync(ValidationContext<CustomerOrder> context, CancellationToken cancellation = default) | ||
| { | ||
| // Check if validation is enabled | ||
| var isValidationEnabled = await _settingsManager.GetValueAsync<bool>( | ||
| ModuleConstants.Settings.General.CustomerOrderValidation); | ||
|
|
||
| if (!isValidationEnabled) | ||
| { | ||
| // Skip validation if disabled | ||
| return new ValidationResult(); | ||
| } | ||
|
|
||
| // Perform validation if enabled | ||
| return await base.ValidateAsync(context, cancellation); | ||
| } | ||
|
|
||
| protected void SetDefaultRules() | ||
| { | ||
| #pragma warning disable S109 | ||
| RuleFor(order => order.Number).NotEmpty().MaximumLength(64); | ||
| RuleFor(order => order.CustomerId).NotNull().NotEmpty().MaximumLength(64); | ||
| RuleFor(order => order.CustomerName).NotEmpty().MaximumLength(255); | ||
| RuleFor(order => order.StoreId).NotNull().NotEmpty().MaximumLength(64); | ||
| RuleFor(order => order.StoreName).MaximumLength(255); | ||
| RuleFor(order => order.ChannelId).MaximumLength(64); | ||
| RuleFor(order => order.OrganizationId).MaximumLength(64); | ||
| RuleFor(order => order.OrganizationName).MaximumLength(255); | ||
| RuleFor(order => order.EmployeeId).MaximumLength(64); | ||
| RuleFor(order => order.EmployeeName).MaximumLength(255); | ||
| RuleFor(order => order.SubscriptionId).MaximumLength(64); | ||
| RuleFor(order => order.SubscriptionNumber).MaximumLength(64); | ||
| RuleFor(order => order.LanguageCode).MaximumLength(16) | ||
| .Matches("^[a-z]{2}-[A-Z]{2}$") | ||
| .When(order => !string.IsNullOrEmpty(order.LanguageCode)); | ||
| RuleFor(order => order.ShoppingCartId).MaximumLength(128); | ||
| RuleFor(order => order.PurchaseOrderNumber).MaximumLength(128); | ||
| #pragma warning restore S109 | ||
| } | ||
| } | ||
111 changes: 111 additions & 0 deletions
111
src/VirtoCommerce.OrdersModule.Data/Validators/OrderDocumentCountValidator.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using FluentValidation; | ||
| using VirtoCommerce.CoreModule.Core.Common; | ||
| using VirtoCommerce.OrdersModule.Core; | ||
| using VirtoCommerce.OrdersModule.Core.Model; | ||
| using VirtoCommerce.Platform.Core.Common; | ||
| using VirtoCommerce.Platform.Core.Settings; | ||
|
|
||
| namespace VirtoCommerce.OrdersModule.Data.Validators; | ||
|
|
||
| /// <summary> | ||
| /// Validates that the total number of child documents (operations) per order | ||
| /// does not exceed the configured maximum limit. | ||
| /// Uses the IOperation.ChildrenOperations tree structure for generic traversal. | ||
| /// This ensures system performance, storage optimization, and data consistency. | ||
| /// | ||
| /// This validator works with IOperation interface, making it applicable to any operation type, | ||
| /// though it's primarily designed for root-level operations like CustomerOrder. | ||
| /// </summary> | ||
| public class OrderDocumentCountValidator : AbstractValidator<IOperation> | ||
| { | ||
| private readonly ISettingsManager _settingsManager; | ||
|
|
||
| public OrderDocumentCountValidator(ISettingsManager settingsManager) | ||
| { | ||
| _settingsManager = settingsManager; | ||
|
|
||
| RuleFor(operation => operation) | ||
| .CustomAsync(async (operation, context, cancellationToken) => | ||
| { | ||
| // Only validate root-level operations (like CustomerOrder) that have children | ||
| // Skip validation for child operations to avoid redundant checks | ||
| if (!string.IsNullOrEmpty(operation.ParentOperationId)) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| var maxDocumentCount = await _settingsManager.GetValueAsync<int>( | ||
| ModuleConstants.Settings.General.MaxOrderDocumentCount); | ||
|
|
||
| // Get all operations in the tree (excluding the root operation itself) | ||
| var allOperations = operation.GetFlatObjectsListWithInterface<IOperation>().ToList(); | ||
| var childOperations = allOperations.Where(op => op.Id != operation.Id).ToList(); | ||
cursor[bot] marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| // Total child documents count | ||
| var totalDocumentCount = childOperations.Count; | ||
|
|
||
| if (totalDocumentCount > maxDocumentCount) | ||
| { | ||
| var operationBreakdown = GetOperationBreakdown(childOperations); | ||
|
|
||
| context.AddFailure( | ||
| operation.OperationType, | ||
| $"{operation.OperationType} document count ({totalDocumentCount}) exceeds the maximum allowed limit of {maxDocumentCount}. " + | ||
| $"Documents breakdown: {operationBreakdown}"); | ||
| } | ||
|
|
||
| // Validate each operation that has children | ||
| ValidateOperationChildren(childOperations, maxDocumentCount, context); | ||
| }); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Creates a human-readable breakdown of operations by type | ||
| /// </summary> | ||
| private static string GetOperationBreakdown(IList<IOperation> operations) | ||
| { | ||
| var grouped = operations | ||
| .GroupBy(op => op.OperationType) | ||
| .Select(g => $"{g.Key}={g.Count()}") | ||
| .OrderBy(s => s); | ||
|
|
||
| return string.Join(", ", grouped); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Validates that individual operations do not have too many child operations | ||
| /// </summary> | ||
| private static void ValidateOperationChildren( | ||
| IList<IOperation> allOperations, | ||
| int maxDocumentCount, | ||
| ValidationContext<IOperation> context) | ||
| { | ||
| // Group operations by their parent to count children per operation | ||
| var operationsByParent = allOperations | ||
| .Where(op => !string.IsNullOrEmpty(op.ParentOperationId)) | ||
| .GroupBy(op => op.ParentOperationId) | ||
| .ToDictionary(g => g.Key, g => g.ToList()); | ||
|
|
||
| // Check each operation's child count | ||
| foreach (var operation in allOperations) | ||
| { | ||
| if (operationsByParent.TryGetValue(operation.Id, out var children)) | ||
| { | ||
| var childCount = children.Count; | ||
|
|
||
| if (childCount > maxDocumentCount) | ||
| { | ||
| var childBreakdown = GetOperationBreakdown(children); | ||
|
|
||
| context.AddFailure( | ||
| operation.OperationType, | ||
| $"{operation.OperationType} '{operation.Number}' has {childCount} child documents ({childBreakdown}), " + | ||
| $"which exceeds the maximum allowed limit of {maxDocumentCount}."); | ||
| } | ||
| } | ||
| } | ||
OlegoO marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
|
|
||
23 changes: 23 additions & 0 deletions
23
src/VirtoCommerce.OrdersModule.Data/Validators/PaymentInValidator.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| using FluentValidation; | ||
| using VirtoCommerce.OrdersModule.Core.Model; | ||
|
|
||
| namespace VirtoCommerce.OrdersModule.Data.Validators; | ||
|
|
||
| public class PaymentInValidator : AbstractValidator<PaymentIn> | ||
| { | ||
| public PaymentInValidator() | ||
| { | ||
| SetDefaultRules(); | ||
| } | ||
|
|
||
| protected void SetDefaultRules() | ||
| { | ||
| RuleFor(payment => payment.OrganizationId).MaximumLength(64); | ||
| RuleFor(payment => payment.OrganizationName).MaximumLength(255); | ||
| RuleFor(payment => payment.CustomerId).NotNull().NotEmpty().MaximumLength(64); | ||
| RuleFor(payment => payment.CustomerName).MaximumLength(255); | ||
| RuleFor(payment => payment.Purpose).MaximumLength(1024); | ||
| RuleFor(payment => payment.GatewayCode).MaximumLength(64); | ||
| RuleFor(payment => payment.TaxType).MaximumLength(64); | ||
| } | ||
| } |
6 changes: 5 additions & 1 deletion
6
src/VirtoCommerce.OrdersModule.Web/Extensions/ServiceCollectionExtensions.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.