Skip to content

Commit 8f6e993

Browse files
committed
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)
1 parent 585eab1 commit 8f6e993

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

src/VirtoCommerce.CoreModule.Core/Common/SequenceNumberGeneratorOptions.cs

+6
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,11 @@ public class SequenceNumberGeneratorOptions
1010
/// Defines the delay between retries in seconds.
1111
/// </summary>
1212
public int RetryDelay { get; set; } = 5;
13+
14+
/// <summary>
15+
/// Defines usage of the static tenant id instead of store id for generating unique number.
16+
/// Can be useful when you want to have golobal counter for all stores/tenants.
17+
/// </summary>
18+
public bool UseGlobalTenantId { get; set; } = false;
1319
}
1420
}

src/VirtoCommerce.CoreModule.Data/Services/SequenceNumberGeneratorService.cs

+12-2
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ protected virtual RetryPolicy ConfigureRetryPolicy()
141141
/// <returns></returns>
142142
protected virtual int RequestNextCounter(string tenantId, CounterOptions counterOptions)
143143
{
144-
var sequenceKey = string.IsNullOrEmpty(tenantId) ? counterOptions.NumberTemplate : $"{tenantId}/{counterOptions.NumberTemplate}";
144+
var sequenceKey = GetSequenceKey(tenantId, counterOptions);
145145

146146
using var repository = _repositoryFactory();
147147
var sequence = repository.Sequences.SingleOrDefault(s => s.ObjectType == sequenceKey);
@@ -177,6 +177,16 @@ protected virtual int RequestNextCounter(string tenantId, CounterOptions counter
177177
return sequence.Value;
178178
}
179179

180+
protected virtual string GetSequenceKey(string tenantId, CounterOptions counterOptions)
181+
{
182+
if(_options.UseGlobalTenantId || string.IsNullOrEmpty(tenantId))
183+
{
184+
return counterOptions.NumberTemplate;
185+
}
186+
187+
return $"{tenantId}/{counterOptions.NumberTemplate}";
188+
}
189+
180190
/// <summary>
181191
/// Returns true if counter should be reset.
182192
/// </summary>
@@ -193,7 +203,7 @@ protected virtual bool ShouldResetCounter(DateTime lastResetDate, ResetCounterTy
193203
return currentUtcDate.Date > lastResetDate.Date;
194204
case ResetCounterType.Weekly:
195205
// Reset every Monday
196-
int daysUntilTargetDay = ((int)DayOfWeek.Monday - (int)lastResetDate.DayOfWeek + 7) % 7;
206+
var daysUntilTargetDay = ((int)DayOfWeek.Monday - (int)lastResetDate.DayOfWeek + 7) % 7;
197207
var nextMondayDate = lastResetDate.Date.AddDays(daysUntilTargetDay);
198208
return currentUtcDate >= nextMondayDate;
199209
case ResetCounterType.Monthly:

0 commit comments

Comments
 (0)