x
This commit is contained in:
@@ -12,7 +12,22 @@ class StaffCubit extends Cubit<StaffState> {
|
||||
final StaffRepository _repository = GetIt.I.get<StaffRepository>();
|
||||
final SessionCubit _sessionCubit = GetIt.I<SessionCubit>();
|
||||
|
||||
StaffCubit() : super(const StaffState());
|
||||
StaffCubit() : super(const StaffState()) {
|
||||
init();
|
||||
}
|
||||
|
||||
Future<void> init() async {
|
||||
emit(state.copyWith(status: StaffStatus.loading, error: null));
|
||||
try {
|
||||
final allStaff = await _repository.getStaffMembers(
|
||||
_sessionCubit.state.company!.id!,
|
||||
);
|
||||
|
||||
emit(state.copyWith(status: StaffStatus.success, allStaff: allStaff));
|
||||
} catch (e) {
|
||||
emit(state.copyWith(status: StaffStatus.error, error: e.toString()));
|
||||
}
|
||||
}
|
||||
|
||||
// Carica tutto lo staff della compagnia
|
||||
Future<void> loadAllStaff() async {
|
||||
|
||||
@@ -15,7 +15,9 @@ class StaffRepository {
|
||||
.from(Tables.staffMembers)
|
||||
.select('''
|
||||
*,
|
||||
stores (*)
|
||||
store_assignments:${Tables.staffInStores} (
|
||||
${Tables.stores}(*)
|
||||
)
|
||||
''')
|
||||
.eq('company_id', companyId)
|
||||
.order('name', ascending: true);
|
||||
@@ -27,7 +29,12 @@ class StaffRepository {
|
||||
try {
|
||||
final response = await _supabase
|
||||
.from(Tables.staffMembers)
|
||||
.select()
|
||||
.select('''
|
||||
*,
|
||||
store_assignments:${Tables.staffInStores} (
|
||||
${Tables.stores}(*)
|
||||
)
|
||||
''')
|
||||
.eq('id', staffId)
|
||||
.single();
|
||||
return StaffMemberModel.fromMap(response);
|
||||
|
||||
@@ -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