From 7a81a0b14e54b95e72c2b687482683f2aea48d60 Mon Sep 17 00:00:00 2001 From: Oleg Zhuk Date: Wed, 11 Dec 2024 16:12:42 +0200 Subject: [PATCH] VCST-2432: Add global tenant ID option for sequence number generation (#234) feat: Introduced a new `UseGlobalTenantId` property in the `SequenceNumberGeneratorOptions` class to support a global counter across all tenants/stores. By default, false. Set VirtoCommerce:SequenceNumberGenerator:UseGlobalTenantId to true, to enable it. (#234) --- .../Common/SequenceNumberGeneratorOptions.cs | 6 ++++++ .../Services/SequenceNumberGeneratorService.cs | 14 ++++++++++++-- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/VirtoCommerce.CoreModule.Core/Common/SequenceNumberGeneratorOptions.cs b/src/VirtoCommerce.CoreModule.Core/Common/SequenceNumberGeneratorOptions.cs index 8b58b037..84356be4 100644 --- a/src/VirtoCommerce.CoreModule.Core/Common/SequenceNumberGeneratorOptions.cs +++ b/src/VirtoCommerce.CoreModule.Core/Common/SequenceNumberGeneratorOptions.cs @@ -10,5 +10,11 @@ public class SequenceNumberGeneratorOptions /// Defines the delay between retries in seconds. /// public int RetryDelay { get; set; } = 5; + + /// + /// Defines usage of the static tenant id instead of store id for generating unique number. + /// Can be useful when you want to have golobal counter for all stores/tenants. + /// + public bool UseGlobalTenantId { get; set; } = false; } } diff --git a/src/VirtoCommerce.CoreModule.Data/Services/SequenceNumberGeneratorService.cs b/src/VirtoCommerce.CoreModule.Data/Services/SequenceNumberGeneratorService.cs index 805a5b0a..9fec7208 100644 --- a/src/VirtoCommerce.CoreModule.Data/Services/SequenceNumberGeneratorService.cs +++ b/src/VirtoCommerce.CoreModule.Data/Services/SequenceNumberGeneratorService.cs @@ -141,7 +141,7 @@ protected virtual RetryPolicy ConfigureRetryPolicy() /// protected virtual int RequestNextCounter(string tenantId, CounterOptions counterOptions) { - var sequenceKey = string.IsNullOrEmpty(tenantId) ? counterOptions.NumberTemplate : $"{tenantId}/{counterOptions.NumberTemplate}"; + var sequenceKey = GetSequenceKey(tenantId, counterOptions); using var repository = _repositoryFactory(); var sequence = repository.Sequences.SingleOrDefault(s => s.ObjectType == sequenceKey); @@ -177,6 +177,16 @@ protected virtual int RequestNextCounter(string tenantId, CounterOptions counter return sequence.Value; } + protected virtual string GetSequenceKey(string tenantId, CounterOptions counterOptions) + { + if(_options.UseGlobalTenantId || string.IsNullOrEmpty(tenantId)) + { + return counterOptions.NumberTemplate; + } + + return $"{tenantId}/{counterOptions.NumberTemplate}"; + } + /// /// Returns true if counter should be reset. /// @@ -193,7 +203,7 @@ protected virtual bool ShouldResetCounter(DateTime lastResetDate, ResetCounterTy return currentUtcDate.Date > lastResetDate.Date; case ResetCounterType.Weekly: // Reset every Monday - int daysUntilTargetDay = ((int)DayOfWeek.Monday - (int)lastResetDate.DayOfWeek + 7) % 7; + var daysUntilTargetDay = ((int)DayOfWeek.Monday - (int)lastResetDate.DayOfWeek + 7) % 7; var nextMondayDate = lastResetDate.Date.AddDays(daysUntilTargetDay); return currentUtcDate >= nextMondayDate; case ResetCounterType.Monthly: