Change in Method to fetch the lcoation side id right after the login from admin credentials #3
|
|
@ -55,10 +55,10 @@ public partial class App : Application
|
||||||
NavigationService navigationService = null!;
|
NavigationService navigationService = null!;
|
||||||
navigationService = new NavigationService(
|
navigationService = new NavigationService(
|
||||||
session,
|
session,
|
||||||
() => new AdminLoginViewModel(authService, session, navigationService, configService, adminAuditService),
|
() => new AdminLoginViewModel(authService, session, navigationService, configService, adminAuditService, employeeLookupService),
|
||||||
() => new ScannerDashboardViewModel(rfidService, navigationService, session, configService, menuLookupService, employeePhotoService),
|
() => new ScannerDashboardViewModel(rfidService, navigationService, session, configService, menuLookupService, employeePhotoService),
|
||||||
() => new MainDashboardViewModel(navigationService, rfidService, configService, session),
|
() => new MainDashboardViewModel(navigationService, rfidService, configService, session),
|
||||||
() => new AdminSettingsAuthViewModel(authService, session, navigationService, configService),
|
() => new AdminSettingsAuthViewModel(authService, session, navigationService, configService, employeeLookupService),
|
||||||
() => new SettingsViewModel(configService, navigationService, adminAuditService, syncService),
|
() => new SettingsViewModel(configService, navigationService, adminAuditService, syncService),
|
||||||
() => new MealSchedulesViewModel(mealScheduleService, navigationService, configService));
|
() => new MealSchedulesViewModel(mealScheduleService, navigationService, configService));
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -6,4 +6,3 @@ namespace UtopiaCanteenSystem.Services;
|
||||||
public readonly record struct AuthResult(
|
public readonly record struct AuthResult(
|
||||||
bool Success,
|
bool Success,
|
||||||
string EmployeeId);
|
string EmployeeId);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -53,4 +53,3 @@ public class AuthService : IAuthService
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -105,6 +105,16 @@ public class ConfigService : IConfigService
|
||||||
SaveConfig();
|
SaveConfig();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void ApplyLocationSiteIdFromAuth(string? locationSiteId)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrWhiteSpace(locationSiteId))
|
||||||
|
return;
|
||||||
|
var digits = new string(locationSiteId.Where(char.IsDigit).ToArray());
|
||||||
|
if (string.IsNullOrEmpty(digits))
|
||||||
|
return;
|
||||||
|
SetSiteId("SITE : " + digits);
|
||||||
|
}
|
||||||
|
|
||||||
public string GetDeviceId()
|
public string GetDeviceId()
|
||||||
{
|
{
|
||||||
//var id = _config.DeviceId ?? string.Empty;
|
//var id = _config.DeviceId ?? string.Empty;
|
||||||
|
|
|
||||||
|
|
@ -72,6 +72,39 @@ public class EmployeeLookupService : IEmployeeLookupService
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public async Task<string?> GetLocationSiteIdByEmployeeSerialAsync(string employeeSerial, CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
var serial = employeeSerial?.Trim() ?? string.Empty;
|
||||||
|
if (string.IsNullOrEmpty(serial))
|
||||||
|
return null;
|
||||||
|
|
||||||
|
var connectionString = _configService.GetHrmsLookupConnectionString();
|
||||||
|
if (string.IsNullOrWhiteSpace(connectionString))
|
||||||
|
return null;
|
||||||
|
|
||||||
|
const string sql = @"
|
||||||
|
SELECT location_site_id
|
||||||
|
FROM employee
|
||||||
|
WHERE serial_number = @serial
|
||||||
|
LIMIT 1";
|
||||||
|
|
||||||
|
await using var conn = new MySqlConnection(connectionString);
|
||||||
|
await conn.OpenAsync(cancellationToken).ConfigureAwait(false);
|
||||||
|
await using var cmd = new MySqlCommand(sql, conn);
|
||||||
|
cmd.Parameters.AddWithValue("@serial", serial);
|
||||||
|
|
||||||
|
await using var reader = await cmd.ExecuteReaderAsync(cancellationToken).ConfigureAwait(false);
|
||||||
|
if (!await reader.ReadAsync(cancellationToken).ConfigureAwait(false))
|
||||||
|
return null;
|
||||||
|
|
||||||
|
if (reader.IsDBNull(0))
|
||||||
|
return null;
|
||||||
|
|
||||||
|
var raw = reader.GetValue(0)?.ToString()?.Trim();
|
||||||
|
return string.IsNullOrEmpty(raw) ? null : raw;
|
||||||
|
}
|
||||||
|
|
||||||
private static string GetString(MySqlDataReader reader, int ordinal)
|
private static string GetString(MySqlDataReader reader, int ordinal)
|
||||||
{
|
{
|
||||||
if (reader.IsDBNull(ordinal)) return string.Empty;
|
if (reader.IsDBNull(ordinal)) return string.Empty;
|
||||||
|
|
|
||||||
|
|
@ -25,6 +25,8 @@ public interface IConfigService
|
||||||
|
|
||||||
string GetSiteId();
|
string GetSiteId();
|
||||||
void SetSiteId(string siteId);
|
void SetSiteId(string siteId);
|
||||||
|
/// <summary>Persist HRMS location_site_id in the same format as the scanner dashboard (digits → "SITE : …").</summary>
|
||||||
|
void ApplyLocationSiteIdFromAuth(string? locationSiteId);
|
||||||
string GetDeviceId();
|
string GetDeviceId();
|
||||||
void SetDeviceId(string deviceId);
|
void SetDeviceId(string deviceId);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -12,4 +12,9 @@ public interface IEmployeeLookupService
|
||||||
/// Finds employee by RFID. Returns null if card is not registered in HRMS.
|
/// Finds employee by RFID. Returns null if card is not registered in HRMS.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
Task<HrmsEmployeeInfo?> GetEmployeeByRfidAsync(string rfid, CancellationToken cancellationToken = default);
|
Task<HrmsEmployeeInfo?> GetEmployeeByRfidAsync(string rfid, CancellationToken cancellationToken = default);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Reads <c>employee.location_site_id</c> where <c>employee.serial_number</c> matches the login employee id. Null if missing or not configured.
|
||||||
|
/// </summary>
|
||||||
|
Task<string?> GetLocationSiteIdByEmployeeSerialAsync(string employeeSerial, CancellationToken cancellationToken = default);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -11,6 +11,7 @@ public partial class AdminLoginViewModel : ObservableObject
|
||||||
private readonly INavigationService _navigation;
|
private readonly INavigationService _navigation;
|
||||||
private readonly IConfigService _config;
|
private readonly IConfigService _config;
|
||||||
private readonly IAdminAuditService _adminAudit;
|
private readonly IAdminAuditService _adminAudit;
|
||||||
|
private readonly IEmployeeLookupService _employeeLookup;
|
||||||
|
|
||||||
[ObservableProperty]
|
[ObservableProperty]
|
||||||
private string _username = string.Empty;
|
private string _username = string.Empty;
|
||||||
|
|
@ -33,13 +34,15 @@ public partial class AdminLoginViewModel : ObservableObject
|
||||||
AppSession session,
|
AppSession session,
|
||||||
INavigationService navigation,
|
INavigationService navigation,
|
||||||
IConfigService config,
|
IConfigService config,
|
||||||
IAdminAuditService adminAudit)
|
IAdminAuditService adminAudit,
|
||||||
|
IEmployeeLookupService employeeLookup)
|
||||||
{
|
{
|
||||||
_authService = authService;
|
_authService = authService;
|
||||||
_session = session;
|
_session = session;
|
||||||
_navigation = navigation;
|
_navigation = navigation;
|
||||||
_config = config;
|
_config = config;
|
||||||
_adminAudit = adminAudit;
|
_adminAudit = adminAudit;
|
||||||
|
_employeeLookup = employeeLookup;
|
||||||
|
|
||||||
RememberCredentials = _config.GetRememberAdminCredentials();
|
RememberCredentials = _config.GetRememberAdminCredentials();
|
||||||
if (RememberCredentials)
|
if (RememberCredentials)
|
||||||
|
|
@ -83,6 +86,16 @@ public partial class AdminLoginViewModel : ObservableObject
|
||||||
|
|
||||||
_session.SetAdminAuthenticated(user, result.EmployeeId);
|
_session.SetAdminAuthenticated(user, result.EmployeeId);
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var siteId = await _employeeLookup.GetLocationSiteIdByEmployeeSerialAsync(result.EmployeeId).ConfigureAwait(true);
|
||||||
|
_config.ApplyLocationSiteIdFromAuth(siteId);
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
// HRMS lookup optional; login still succeeds.
|
||||||
|
}
|
||||||
|
|
||||||
// Persist who logged in (for audit / reporting).
|
// Persist who logged in (for audit / reporting).
|
||||||
await _adminAudit.RecordLoginAsync(user, result.EmployeeId).ConfigureAwait(false);
|
await _adminAudit.RecordLoginAsync(user, result.EmployeeId).ConfigureAwait(false);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -10,6 +10,7 @@ public partial class AdminSettingsAuthViewModel : ObservableObject
|
||||||
private readonly AppSession _session;
|
private readonly AppSession _session;
|
||||||
private readonly INavigationService _navigation;
|
private readonly INavigationService _navigation;
|
||||||
private readonly IConfigService _config;
|
private readonly IConfigService _config;
|
||||||
|
private readonly IEmployeeLookupService _employeeLookup;
|
||||||
|
|
||||||
[ObservableProperty]
|
[ObservableProperty]
|
||||||
private string _username = string.Empty;
|
private string _username = string.Empty;
|
||||||
|
|
@ -27,12 +28,18 @@ public partial class AdminSettingsAuthViewModel : ObservableObject
|
||||||
[ObservableProperty]
|
[ObservableProperty]
|
||||||
private bool _rememberCredentials;
|
private bool _rememberCredentials;
|
||||||
|
|
||||||
public AdminSettingsAuthViewModel(IAuthService authService, AppSession session, INavigationService navigation, IConfigService config)
|
public AdminSettingsAuthViewModel(
|
||||||
|
IAuthService authService,
|
||||||
|
AppSession session,
|
||||||
|
INavigationService navigation,
|
||||||
|
IConfigService config,
|
||||||
|
IEmployeeLookupService employeeLookup)
|
||||||
{
|
{
|
||||||
_authService = authService;
|
_authService = authService;
|
||||||
_session = session;
|
_session = session;
|
||||||
_navigation = navigation;
|
_navigation = navigation;
|
||||||
_config = config;
|
_config = config;
|
||||||
|
_employeeLookup = employeeLookup;
|
||||||
|
|
||||||
RememberCredentials = _config.GetRememberAdminCredentials();
|
RememberCredentials = _config.GetRememberAdminCredentials();
|
||||||
if (RememberCredentials)
|
if (RememberCredentials)
|
||||||
|
|
@ -83,6 +90,16 @@ public partial class AdminSettingsAuthViewModel : ObservableObject
|
||||||
// Refresh session details (who authenticated for Settings).
|
// Refresh session details (who authenticated for Settings).
|
||||||
_session.SetAdminAuthenticated(user, result.EmployeeId);
|
_session.SetAdminAuthenticated(user, result.EmployeeId);
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var siteId = await _employeeLookup.GetLocationSiteIdByEmployeeSerialAsync(result.EmployeeId).ConfigureAwait(true);
|
||||||
|
_config.ApplyLocationSiteIdFromAuth(siteId);
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
// HRMS lookup optional; auth still succeeds.
|
||||||
|
}
|
||||||
|
|
||||||
// Persist credentials only if user opted in.
|
// Persist credentials only if user opted in.
|
||||||
_config.SetRememberAdminCredentials(RememberCredentials);
|
_config.SetRememberAdminCredentials(RememberCredentials);
|
||||||
if (RememberCredentials)
|
if (RememberCredentials)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue