Skip to content
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

VCST-2432: Add global tenant ID option for sequence number generation #234

Merged
merged 2 commits into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading