98 lines
3.1 KiB
C#
98 lines
3.1 KiB
C#
namespace UtopiaCanteenSystem.Services;
|
|
|
|
/// <summary>
|
|
/// Shared admin login post-processing: persist employee id and site from session/backend.
|
|
/// </summary>
|
|
public static class AdminLoginHelper
|
|
{
|
|
/// <summary>
|
|
/// Admin Access field: persisted <see cref="IConfigService.GetAdminCardId"/> first,
|
|
/// then current session, then SQLite audit, then "ADMIN".
|
|
/// </summary>
|
|
public static string ResolveAdminCardIdForDisplay(
|
|
IConfigService config,
|
|
AppSession? session = null,
|
|
IAdminAuditService? adminAudit = null)
|
|
{
|
|
var fromConfig = (config.GetAdminCardId() ?? string.Empty).Trim();
|
|
if (!string.IsNullOrWhiteSpace(fromConfig))
|
|
return fromConfig;
|
|
|
|
if (session != null && !string.IsNullOrWhiteSpace(session.AdminEmployeeId))
|
|
return session.AdminEmployeeId.Trim();
|
|
|
|
var last = adminAudit?.GetLastLogin();
|
|
if (last != null && !string.IsNullOrWhiteSpace(last.EmployeeId))
|
|
return last.EmployeeId.Trim();
|
|
|
|
return "ADMIN";
|
|
}
|
|
|
|
public static async Task ApplyAdminLoginDefaultsAsync(
|
|
string employeeId,
|
|
IConfigService config,
|
|
IEmployeeLookupService employeeLookup,
|
|
ICanteenBackendApiClient? backendApi = null,
|
|
CancellationToken cancellationToken = default)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(employeeId))
|
|
return;
|
|
|
|
config.SetAdminCardId(employeeId.Trim());
|
|
|
|
string? siteId = null;
|
|
try
|
|
{
|
|
siteId = await employeeLookup.GetLocationSiteIdByEmployeeSerialAsync(employeeId, cancellationToken)
|
|
.ConfigureAwait(false);
|
|
}
|
|
catch
|
|
{
|
|
// Local/HRMS lookup optional on client.
|
|
}
|
|
|
|
if (string.IsNullOrWhiteSpace(siteId) && backendApi != null)
|
|
{
|
|
try
|
|
{
|
|
siteId = await backendApi.GetEmployeeLocationSiteAsync(employeeId, cancellationToken)
|
|
.ConfigureAwait(false);
|
|
}
|
|
catch
|
|
{
|
|
// Backend lookup optional.
|
|
}
|
|
}
|
|
|
|
config.ApplyLocationSiteIdFromAuth(siteId);
|
|
}
|
|
|
|
/// <summary>Format site for UI fields (e.g. "SITE : 02").</summary>
|
|
public static string FormatSiteIdForDisplay(string? siteId)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(siteId))
|
|
return string.Empty;
|
|
|
|
var raw = siteId.Trim();
|
|
if (raw.StartsWith("SITE :", StringComparison.OrdinalIgnoreCase))
|
|
return raw;
|
|
|
|
var digits = new string(raw.Where(char.IsDigit).ToArray());
|
|
return string.IsNullOrEmpty(digits) ? raw : $"SITE : {digits}";
|
|
}
|
|
|
|
/// <summary>Extract digits from site display for scanner header.</summary>
|
|
public static string ExtractSiteDigits(string? siteId)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(siteId))
|
|
return "1";
|
|
|
|
var raw = siteId.Trim();
|
|
if (raw.StartsWith("SITE :", StringComparison.OrdinalIgnoreCase))
|
|
raw = raw.Substring(raw.IndexOf(':') + 1).Trim();
|
|
|
|
var digits = new string(raw.Where(char.IsDigit).ToArray());
|
|
return string.IsNullOrEmpty(digits) ? "1" : digits;
|
|
}
|
|
}
|