Skip to content

Commit

Permalink
VCST-2432: Add global tenant ID option for sequence number generation (
Browse files Browse the repository at this point in the history
…#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)
  • Loading branch information
OlegoO committed Dec 12, 2024
1 parent 585eab1 commit 8f6e993
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,11 @@ public class SequenceNumberGeneratorOptions
/// Defines the delay between retries in seconds.
/// </summary>
public int RetryDelay { get; set; } = 5;

/// <summary>
/// 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.
/// </summary>
public bool UseGlobalTenantId { get; set; } = false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ protected virtual RetryPolicy ConfigureRetryPolicy()
/// <returns></returns>
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);
Expand Down Expand Up @@ -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}";
}

/// <summary>
/// Returns true if counter should be reset.
/// </summary>
Expand All @@ -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:
Expand Down

0 comments on commit 8f6e993

Please sign in to comment.