40 lines
1.3 KiB
C#
40 lines
1.3 KiB
C#
using Indotalent.ConfigBackEnd.Interfaces;
|
|
using Indotalent.Infrastructure.AutoNumberGenerator;
|
|
using Indotalent.Infrastructure.Database;
|
|
using Microsoft.EntityFrameworkCore.Infrastructure;
|
|
|
|
namespace Indotalent.ConfigBackEnd.Extensions;
|
|
|
|
public static class AppDbContextExtensions
|
|
{
|
|
public static async Task<string> GenerateAutoNumberAsync(
|
|
this AppDbContext context,
|
|
string entityName,
|
|
string prefixTemplate,
|
|
string? suffixTemplate = null,
|
|
int paddingLength = 4,
|
|
bool useYear = true,
|
|
bool useMonth = false,
|
|
CancellationToken ct = default)
|
|
{
|
|
var currentUserService = context.GetService<ICurrentUserService>();
|
|
var tenantId = currentUserService?.TenantId;
|
|
|
|
if (string.IsNullOrWhiteSpace(tenantId))
|
|
{
|
|
throw new InvalidOperationException("TenantId cannot be null or empty for multi-tenant auto-number sequence generation. Ensure the user session context is valid.");
|
|
}
|
|
|
|
var generator = context.GetService<AutoNumberGeneratorService>();
|
|
|
|
return await generator.GenerateNextNumberAsync(
|
|
tenantId,
|
|
entityName,
|
|
prefixTemplate,
|
|
suffixTemplate,
|
|
paddingLength,
|
|
useYear,
|
|
useMonth,
|
|
ct);
|
|
}
|
|
} |