Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .flutter-plugins-dependencies

Large diffs are not rendered by default.

9 changes: 8 additions & 1 deletion lib/core/services/api_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,14 @@ class ApiService {
} else if (response.statusCode == 404) {
throw ApiException('Not found');
} else if (response.statusCode == 500) {
throw ApiException('Server error');
try {
final body = json.decode(response.body);
final err = body['error'];
throw ApiException(err != null ? 'Server error: $err' : 'Server error');
} catch (e) {
if (e is ApiException) rethrow;
throw ApiException('Server error');
}
} else {
throw ApiException('Error: ${response.statusCode}');
}
Expand Down
14 changes: 13 additions & 1 deletion lib/core/services/auth_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class UserProfile {
final String? profilePicture;
final int? branchId;
final int? branchManagerProfileId; // The ID of the BranchManager profile
final int? maintenanceExecutiveProfileId; // The ID of the MaintenanceExecutive profile

UserProfile({
required this.id,
Expand All @@ -23,18 +24,27 @@ class UserProfile {
this.profilePicture,
this.branchId,
this.branchManagerProfileId,
this.maintenanceExecutiveProfileId,
});

factory UserProfile.fromJson(Map<String, dynamic> json) {
// Extract branchId from branchManagerProfile if present
// Extract branchId and branchManagerProfileId from branchManagerProfile if present
int? branchId;
int? branchManagerProfileId;
int? maintenanceExecutiveProfileId;

final branchManagerProfile = json['branchManagerProfile'] as Map<String, dynamic>?;
if (branchManagerProfile != null) {
branchId = branchManagerProfile['branchId'] as int?;
branchManagerProfileId = branchManagerProfile['id'] as int?;
}

final meProfile = json['maintenanceExecutiveProfile'] as Map<String, dynamic>?;
if (meProfile != null) {
maintenanceExecutiveProfileId = meProfile['id'] as int?;
// Also extract branchId from ME profile if not already set
branchId ??= meProfile['branchId'] as int?;
}

return UserProfile(
id: json['id'] as int,
Expand All @@ -45,6 +55,7 @@ class UserProfile {
profilePicture: json['profilePicture'] as String?,
branchId: branchId ?? json['branchId'] as int?,
branchManagerProfileId: branchManagerProfileId,
maintenanceExecutiveProfileId: maintenanceExecutiveProfileId,
);
}

Expand All @@ -58,6 +69,7 @@ class UserProfile {
'profilePicture': profilePicture,
'branchId': branchId,
'branchManagerProfileId': branchManagerProfileId,
'maintenanceExecutiveProfileId': maintenanceExecutiveProfileId,
};
}
}
Expand Down
Loading