fixed auto tenant selection

This commit is contained in:
Vaibhav Surve 2025-10-08 11:41:38 +05:30
parent 7e75431feb
commit d1d48b1a74
2 changed files with 7 additions and 11 deletions

View File

@ -9,8 +9,6 @@ import 'package:marco/controller/permission_controller.dart';
class TenantSelectionController extends GetxController { class TenantSelectionController extends GetxController {
final TenantService _tenantService = TenantService(); final TenantService _tenantService = TenantService();
TenantSelectionController();
final tenants = <Tenant>[].obs; final tenants = <Tenant>[].obs;
final isLoading = false.obs; final isLoading = false.obs;
final selectedTenantId = RxnString(); final selectedTenantId = RxnString();
@ -34,16 +32,13 @@ class TenantSelectionController extends GetxController {
tenants.value = data.map((e) => Tenant.fromJson(e)).toList(); tenants.value = data.map((e) => Tenant.fromJson(e)).toList();
// Smart auto-selection final recentTenantId = LocalStorage.getRecentTenantId();
final recentTenantId = await LocalStorage.getRecentTenantId();
if (tenants.length == 1) { if (tenants.length == 1) {
await _selectTenant(tenants.first.id); await _selectTenant(tenants.first.id);
} else if (recentTenantId != null) { } else if (recentTenantId != null) {
final recentTenant = final recentTenant = tenants
tenants.where((t) => t.id == recentTenantId).isNotEmpty .firstWhereOrNull((t) => t.id == recentTenantId);
? tenants.firstWhere((t) => t.id == recentTenantId)
: null;
if (recentTenant != null) { if (recentTenant != null) {
await _selectTenant(recentTenant.id); await _selectTenant(recentTenant.id);
@ -99,7 +94,7 @@ class TenantSelectionController extends GetxController {
logSafe("✅ Tenant selection successful: $tenantId"); logSafe("✅ Tenant selection successful: $tenantId");
// 🔹 Load permissions after tenant selection (null-safe) // 🔹 Load permissions after tenant selection (null-safe)
final token = await LocalStorage.getJwtToken(); final token = LocalStorage.getJwtToken();
if (token != null && token.isNotEmpty) { if (token != null && token.isNotEmpty) {
if (!Get.isRegistered<PermissionController>()) { if (!Get.isRegistered<PermissionController>()) {
Get.put(PermissionController()); Get.put(PermissionController());
@ -107,7 +102,8 @@ class TenantSelectionController extends GetxController {
} }
await Get.find<PermissionController>().loadData(token); await Get.find<PermissionController>().loadData(token);
} else { } else {
logSafe("⚠️ JWT token is null. Cannot load permissions.", level: LogLevel.warning); logSafe("⚠️ JWT token is null. Cannot load permissions.",
level: LogLevel.warning);
} }
// Navigate to dashboard // Navigate to dashboard

View File

@ -20,7 +20,7 @@ Future<void> initializeApp() async {
]); ]);
await _setupDeviceInfo(); await _setupDeviceInfo();
await _handleAuthTokens(); // refresh token only, no controller injection await _handleAuthTokens();
await _setupTheme(); await _setupTheme();
await _setupFirebaseMessaging(); await _setupFirebaseMessaging();