x
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import 'package:equatable/equatable.dart';
|
||||
import 'package:flux/core/enums_and_consts/consts.dart';
|
||||
import 'package:flux/features/master_data/store/models/store_model.dart';
|
||||
|
||||
// L'Enum magico e blindato per il sistema
|
||||
@@ -27,7 +28,8 @@ class StaffMemberModel extends Equatable {
|
||||
final SystemRole systemRole;
|
||||
final bool isActive;
|
||||
final bool hasJoined;
|
||||
final StoreModel? store;
|
||||
final List<String> assignedStoreIds;
|
||||
final List<StoreModel> assignedStores;
|
||||
|
||||
const StaffMemberModel({
|
||||
this.id,
|
||||
@@ -40,7 +42,8 @@ class StaffMemberModel extends Equatable {
|
||||
this.systemRole = SystemRole.user,
|
||||
this.isActive = true,
|
||||
this.hasJoined = false,
|
||||
this.store,
|
||||
this.assignedStoreIds = const [],
|
||||
this.assignedStores = const [],
|
||||
});
|
||||
|
||||
StaffMemberModel copyWith({
|
||||
@@ -55,7 +58,8 @@ class StaffMemberModel extends Equatable {
|
||||
SystemRole? systemRole,
|
||||
bool? isActive,
|
||||
bool? hasJoined,
|
||||
StoreModel? store,
|
||||
List<String>? assignedStoreIds,
|
||||
List<StoreModel>? assignedStores,
|
||||
}) {
|
||||
return StaffMemberModel(
|
||||
id: id ?? this.id,
|
||||
@@ -68,7 +72,8 @@ class StaffMemberModel extends Equatable {
|
||||
systemRole: systemRole ?? this.systemRole,
|
||||
isActive: isActive ?? this.isActive,
|
||||
hasJoined: hasJoined ?? this.hasJoined,
|
||||
store: store ?? this.store,
|
||||
assignedStoreIds: assignedStoreIds ?? this.assignedStoreIds,
|
||||
assignedStores: assignedStores ?? this.assignedStores,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -84,6 +89,24 @@ class StaffMemberModel extends Equatable {
|
||||
}
|
||||
|
||||
factory StaffMemberModel.fromMap(Map<String, dynamic> map) {
|
||||
// 1. Gestiamo l'array nullo di Supabase trasformandolo in lista vuota
|
||||
final List<String> parsedAssignedStoreIds =
|
||||
map['assigned_store_ids'] != null
|
||||
? List<String>.from(map['assigned_store_ids'])
|
||||
: [];
|
||||
|
||||
// 2. Mappiamo il JOIN degli store, se presente
|
||||
List<StoreModel> storeList = [];
|
||||
|
||||
// Gestione del JSON proveniente dal Join nidificato (es. task_assignments -> staff_members)
|
||||
if (map['store_assignments'] != null) {
|
||||
storeList = (map['store_assignments'] as List)
|
||||
.map((a) => a[Tables.stores])
|
||||
.where((s) => s != null)
|
||||
.map((s) => StoreModel.fromMap(s))
|
||||
.toList();
|
||||
}
|
||||
|
||||
return StaffMemberModel(
|
||||
id: map['id'] as String?,
|
||||
companyId: map['company_id'] ?? '',
|
||||
@@ -95,7 +118,8 @@ class StaffMemberModel extends Equatable {
|
||||
systemRole: SystemRole.fromString(map['system_role']),
|
||||
isActive: map['is_active'] ?? true,
|
||||
hasJoined: map['has_joined'] ?? false,
|
||||
store: map['store'] != null ? StoreModel.fromMap(map['store']) : null,
|
||||
assignedStoreIds: parsedAssignedStoreIds,
|
||||
assignedStores: storeList,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -126,6 +150,7 @@ class StaffMemberModel extends Equatable {
|
||||
systemRole,
|
||||
isActive,
|
||||
hasJoined,
|
||||
store,
|
||||
assignedStoreIds,
|
||||
assignedStores,
|
||||
];
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user