8.6 KiB
Configuration Fix Summary
Problem
After user login, the application was changing configuration values unexpectedly:
- Connection string changed from production to local/HRMS
- SiteId changed back to "01"
- Config was being overwritten during authentication or navigation
Root Causes Identified
- ProductionMealScheduleService was using
GetHrmsLookupConnectionString()instead of production connection string - AppConfig class had hardcoded defaults that auto-populated when config was recreated
- ConfigService.GetSiteId()/SetSiteId() silently normalized missing values to "01"
- No logging around config load/save operations
- Config overwrites during login-related operations
Fixes Applied
1. ProductionMealScheduleService.cs
File: Services/ProductionMealScheduleService.cs
Changes:
- Changed all occurrences of
GetHrmsLookupConnectionString()toGetMySqlConnectionString() - Updated error messages from "HRMS MySQL connection string not configured" to "Production MySQL connection string not configured"
- Methods affected:
GetActiveSchedulesForSite()GetAllSchedulesAsync()CreateAsync()UpdateAsync()DeleteAsync()
Impact: Meal schedule operations now use the production database connection string instead of the local HRMS lookup connection string.
2. AppConfig Class Defaults Removed
File: Services/ConfigService.cs
Changes:
// BEFORE
public string SiteId { get; set; } = "SITE : 1";
public string HrmsLookupConnectionString { get; set; } = "Server=192.168.90.147;...";
// AFTER
public string SiteId { get; set; } = string.Empty; // No default - must be explicitly configured
public string HrmsLookupConnectionString { get; set; } = string.Empty; // Must be explicitly configured
Impact: Config values will no longer auto-populate with defaults when config file is recreated. Empty strings indicate "not configured" state.
3. ConfigService.GetSiteId() and SetSiteId() Simplified
File: Services/ConfigService.cs
Changes:
// BEFORE - GetSiteId()
if (string.IsNullOrWhiteSpace(raw))
return "01";
// ... normalization logic ...
return Math.Clamp(numeric, 0, 99).ToString("D2");
// AFTER - GetSiteId()
return _config.SiteId ?? string.Empty;
// BEFORE - SetSiteId()
var normalized = siteId;
if (string.IsNullOrWhiteSpace(normalized))
{
normalized = "01";
}
else
{
// ... normalization to 2-digit format ...
}
_config.SiteId = normalized;
// AFTER - SetSiteId()
_config.SiteId = siteId ?? string.Empty;
Impact: SiteId is now stored and retrieved exactly as provided. No automatic normalization to "01". Callers are responsible for formatting.
4. Config Load/Save Logging Added
File: Services/ConfigService.cs
Changes: Added comprehensive logging:
LoadConfig():
- Logs when config file is not found and defaults are being used
- Logs config file path on successful load
- Logs SiteId value (shows "empty=not configured" if empty)
- Logs MySqlConnectionString status ("NOT CONFIGURED" or masked value)
- Logs HrmsLookupConnectionString status ("NOT CONFIGURED" or masked value)
- Logs any exceptions during load
SaveConfig():
- Logs when saving starts
- Logs current SiteId value
- Logs current MySqlConnectionString status (masked)
- Logs current HrmsLookupConnectionString status (masked)
- Logs successful save with file path
- Logs any exceptions during save
MaskConnectionString():
- New helper method to mask sensitive password values in logs
- Shows server and database information but replaces password with "***"
Sample Log Output:
[ConfigService] Config loaded from C:\Users\...\UtopiaCanteenSystem\config.json
[ConfigService] SiteId: '02' (empty=not configured)
[ConfigService] MySqlConnectionString: Server=localhost;Database=hrms;User=root;Password=***
[ConfigService] HrmsLookupConnectionString: NOT CONFIGURED
[ConfigService] Saving config...
[ConfigService] SiteId: '02'
[ConfigService] MySqlConnectionString: Server=localhost;Database=hrms;User=root;Password=***
[ConfigService] Config saved to C:\Users\...\UtopiaCanteenSystem\config.json
Impact: Full visibility into config changes for debugging. Sensitive values are masked.
5. ScannerDashboardViewModel Updated
File: ViewModels/ScannerDashboardViewModel.cs
Changes: Updated site loading logic to handle both legacy "SITE : X" format and new numeric format:
// BEFORE
if (!string.IsNullOrWhiteSpace(siteId) && siteId.StartsWith("SITE : ", ...))
{
var num = siteId.Substring("SITE : ".Length).Trim();
if (num.Length > 0 && num.All(char.IsDigit))
SiteNumber = num;
}
// AFTER
if (!string.IsNullOrWhiteSpace(siteId))
{
// Extract digits from legacy format "SITE : X"or use value directly if already numeric
if (siteId.StartsWith("SITE : ", StringComparison.OrdinalIgnoreCase))
{
var num = siteId.Substring("SITE : ".Length).Trim();
if (num.Length > 0 && num.All(char.IsDigit))
SiteNumber = num;
}
else if (siteId.All(char.IsDigit))
{
SiteNumber = siteId;
}
}
Impact: Backward compatible with legacy config format while supporting new numeric-only format.
Expected Behavior After Fix
✅ Login/Authentication
- Login operations only modify credential-related config values:
RememberAdminCredentialsSavedAdminUsernameSavedAdminPasswordProtected
- Does NOT modify:
SiteIdMySqlConnectionStringHrmsLookupConnectionString- Other unrelated settings
✅ Navigation
- Navigation between views does not trigger config saves
- ViewModel initialization reads config but doesn't modify it
- Meal Schedules page uses production connection string
✅ Config Persistence
- Existing config values are preserved across app restarts
- Defaults only applied when config file truly doesn't exist
- No silent normalization of values
- All config changes are logged
✅ Database Connections
- Production operations (meal schedules, sync) use
MySqlConnectionString - Local HRMS operations (employee lookup, menu lookup, photos) use
HrmsLookupConnectionString - Both connections must be explicitly configured
Testing Recommendations
-
Initial Startup Test:
- Delete config.json
- Run app
- Verify log shows "Config file not found. Creating with defaults."
- Check that SiteId and connection strings are empty (not "01" or hardcoded)
-
Login Test:
- Configure production connection string and SiteId
- Save config
- Perform admin login
- Verify config still has correct values (not reset to defaults)
-
Navigation Test:
- Navigate to Meal Schedules page
- Verify it uses production connection string (check logs)
- Verify meal schedules load from production database
-
Config Change Test:
- Change SiteId in Settings
- Verify log shows old/new values
- Verify change persists after app restart
-
Connection String Masking Test:
- Check debug output for config save operations
- Verify passwords are shown as "***" in logs
Migration Notes
For Existing Users
- Existing config.json files will be preserved
- Legacy "SITE : X" format will be recognized and loaded correctly
- On first save after upgrade, SiteId will be stored without normalization
For New Installations
- Config will be created with empty strings for SiteId and connection strings
- User must explicitly configure these values
- No assumptions about default site or database
Files Modified
Services/ProductionMealScheduleService.cs- Use production connection stringServices/ConfigService.cs- Remove defaults, add logging, simplify SiteId handlingViewModels/ScannerDashboardViewModel.cs- Handle both legacy and new SiteId formats
Breaking Changes
⚠️ None - Changes are backward compatible:
- Legacy "SITE : X" format still supported
- Existing configs preserved
- Only behavior change: no more automatic resets to defaults
Configuration Requirements
Both connection strings should be configured:
appsettings.json or manual config edit:
{
"MySqlConnectionString": "Server=production-server;Database=hrms;User=utopia;Password=***;Port=3306;",
"HrmsLookupConnectionString": "Server=local-hrms-server;Database=hrms;User=utopia;Password=***;Port=3306;"
}
Or configure via Settings UI (for production connection) and manual config edit (for HRMS lookup).
Next Steps
- Test all scenarios listed above
- Monitor debug output for config operations
- Verify production meal schedule operations work correctly
- Ensure login no longer modifies unrelated config values