Skip to content
Merged
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.

4 changes: 2 additions & 2 deletions lib/core/models/branch.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ class Branch {
/// Create from JSON
factory Branch.fromJson(Map<String, dynamic> json) {
return Branch(
id: json['id'],
id: (json['id'] as num?)?.toInt(),
name: json['name'] ?? '',
location: json['location'] ?? '',
managerId: json['manager_id'],
managerId: (json['manager_id'] as num?)?.toInt(),
managerName: json['Manager']?['name'],
createdAt: json['createdAt'] != null
? DateTime.parse(json['createdAt'])
Expand Down
7 changes: 7 additions & 0 deletions lib/core/models/branch_manager.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/// Branch Manager Model (GDM/GPM)
class BranchManager {
final int? id;
final int? profileId;
final String name;
final String email;
final String? phone;
Expand All @@ -15,6 +16,7 @@ class BranchManager {

BranchManager({
this.id,
this.profileId,
required this.name,
required this.email,
this.phone,
Expand All @@ -32,12 +34,16 @@ class BranchManager {
factory BranchManager.fromJson(Map<String, dynamic> json) {
// Extract branchId from nested branchManagerProfile
final branchManagerProfile = json['branchManagerProfile'];
final int? profileId = branchManagerProfile != null
? branchManagerProfile['id'] as int?
: null;
final int? branchId = branchManagerProfile != null
? branchManagerProfile['branchId']
: json['branchId'];

return BranchManager(
id: json['id'],
profileId: profileId,
name: json['name'] ?? '',
email: json['email'] ?? '',
phone: json['phone'],
Expand All @@ -60,6 +66,7 @@ class BranchManager {
Map<String, dynamic> toJson() {
return {
if (id != null) 'id': id,
if (profileId != null) 'branchManagerProfileId': profileId,
'name': name,
'email': email,
if (phone != null) 'phone': phone,
Expand Down
22 changes: 17 additions & 5 deletions lib/core/routing/app_router.dart
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,11 @@ class AppRouter {
RouteNames.reportNewIssue: (context) => const ReportNewIssuePage(),
RouteNames.reportedIssues: (context) => const ReportedIssuesPage(),

// ✅ Added new routes for the tabbed list pages
RouteNames.gdms: (context) => const NetworkTabsPage(initialIndex: 0),
RouteNames.gpms: (context) => const NetworkTabsPage(initialIndex: 1),
RouteNames.outlets: (context) => const NetworkTabsPage(initialIndex: 2),
RouteNames.mes: (context) => const NetworkTabsPage(initialIndex: 3),
// Manage Network tabs — ME only
RouteNames.gdms: (context) => _meOnly(context, const NetworkTabsPage(initialIndex: 0)),
RouteNames.gpms: (context) => _meOnly(context, const NetworkTabsPage(initialIndex: 1)),
RouteNames.outlets: (context) => _meOnly(context, const NetworkTabsPage(initialIndex: 2)),
RouteNames.mes: (context) => _meOnly(context, const NetworkTabsPage(initialIndex: 3)),

// About page removed

Expand All @@ -101,6 +101,18 @@ class AppRouter {
RouteNames.notifications: (context) => const NotificationsPage(),
};

/// Returns [page] only for maintenance_executive; otherwise shows an access-denied screen.
static Widget _meOnly(BuildContext context, Widget page) {
final role = AuthService.instance.currentUser?.role;
if (role == 'maintenance_executive') return page;
return Scaffold(
appBar: AppBar(title: const Text('Access Denied')),
body: const Center(
child: Text('This section is only accessible to Maintenance Executives.'),
),
);
}

/// Handles undefined routes.
static Route<dynamic> onUnknownRoute(RouteSettings settings) {
return MaterialPageRoute(builder: (context) => const UnknownRouteScreen());
Expand Down
14 changes: 8 additions & 6 deletions lib/core/services/auth_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,18 @@ class UserProfile {

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

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?;
maintenanceExecutiveProfileId = (meProfile['id'] as num?)?.toInt();
branchId ??= (meProfile['branchId'] as num?)?.toInt();
}
// Fallback: read flat keys saved by toJson() when restoring from SharedPreferences
maintenanceExecutiveProfileId ??= (json['maintenanceExecutiveProfileId'] as num?)?.toInt();
branchManagerProfileId ??= (json['branchManagerProfileId'] as num?)?.toInt();

return UserProfile(
id: json['id'] as int,
Expand All @@ -53,7 +55,7 @@ class UserProfile {
role: json['role'] as String,
phone: json['phone'] as String?,
profilePicture: json['profilePicture'] as String?,
branchId: branchId ?? json['branchId'] as int?,
branchId: branchId ?? (json['branchId'] as num?)?.toInt(),
branchManagerProfileId: branchManagerProfileId,
maintenanceExecutiveProfileId: maintenanceExecutiveProfileId,
);
Expand Down
7 changes: 6 additions & 1 deletion lib/core/services/branch_manager_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,18 @@ class BranchManagerService {
String? phone,
int? branchId,
String? employeeId,
bool clearBranch = false,
}) async {
try {
final data = <String, dynamic>{};
if (name != null) data['name'] = name;
if (email != null) data['email'] = email;
if (phone != null) data['phone'] = phone;
if (branchId != null) data['branchId'] = branchId;
if (clearBranch) {
data['branchId'] = null;
} else if (branchId != null) {
data['branchId'] = branchId;
}
if (employeeId != null) data['employeeId'] = employeeId;

final response = await _apiService.put('/users/branch-managers/$id', data);
Expand Down
7 changes: 6 additions & 1 deletion lib/core/services/branch_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,17 @@ class BranchService {
String? name,
String? location,
int? managerId,
bool clearManager = false,
}) async {
try {
final data = <String, dynamic>{};
if (name != null) data['name'] = name;
if (location != null) data['location'] = location;
if (managerId != null) data['manager_id'] = managerId;
if (clearManager) {
data['manager_id'] = null;
} else if (managerId != null) {
data['manager_id'] = managerId;
}

final response = await _apiService.put('/branches/$id', data);

Expand Down
Loading