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: