ripristinato codice stabile da branch staff
This commit is contained in:
136
lib/features/master_data/staff/blocs/staff_cubit.dart
Normal file
136
lib/features/master_data/staff/blocs/staff_cubit.dart
Normal file
@@ -0,0 +1,136 @@
|
||||
import 'package:equatable/equatable.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:flux/core/blocs/session/session_bloc.dart';
|
||||
import 'package:flux/features/master_data/staff/data/staff_repository.dart';
|
||||
import 'package:flux/features/master_data/staff/models/staff_member_model.dart';
|
||||
import 'package:flux/features/master_data/store/models/store_model.dart';
|
||||
import 'package:get_it/get_it.dart';
|
||||
|
||||
part 'staff_state.dart';
|
||||
|
||||
class StaffCubit extends Cubit<StaffState> {
|
||||
final StaffRepository _repository = GetIt.I.get<StaffRepository>();
|
||||
final SessionBloc _sessionBloc;
|
||||
|
||||
StaffCubit(this._sessionBloc) : super(const StaffState());
|
||||
|
||||
// Carica tutto lo staff della compagnia
|
||||
Future<void> loadAllStaff() async {
|
||||
emit(state.copyWith(isLoading: true, error: null));
|
||||
try {
|
||||
final staff = await _repository.getStaffMembers(
|
||||
_sessionBloc.state.company!.id,
|
||||
);
|
||||
final Map<String, List<StoreModel>> storesByStaff = {};
|
||||
for (StaffMemberModel member in staff) {
|
||||
storesByStaff[member.id!] = await loadStoresByStaff(member.id!);
|
||||
}
|
||||
emit(
|
||||
state.copyWith(
|
||||
allStaff: staff,
|
||||
isLoading: false,
|
||||
storesByStaff: storesByStaff,
|
||||
),
|
||||
);
|
||||
} catch (e) {
|
||||
emit(state.copyWith(isLoading: false, error: e.toString()));
|
||||
}
|
||||
}
|
||||
|
||||
Future<List<StoreModel>> loadStoresByStaff(String staffId) async {
|
||||
try {
|
||||
return await _repository.getStaffMemberStore(staffId);
|
||||
} catch (e) {
|
||||
emit(state.copyWith(error: e.toString()));
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
// Carica lo staff di uno specifico negozio e aggiorna la mappa
|
||||
Future<void> loadStaffForStore(String storeId) async {
|
||||
try {
|
||||
final staffInStore = await _repository.getStaffMembersInStore(storeId);
|
||||
final newMap = Map<String, List<StaffMemberModel>>.from(
|
||||
state.staffByStore,
|
||||
);
|
||||
newMap[storeId] = staffInStore;
|
||||
emit(state.copyWith(staffByStore: newMap));
|
||||
} catch (e) {
|
||||
// Qui potresti gestire l'errore silenziosamente per non bloccare tutta l'UI
|
||||
}
|
||||
}
|
||||
|
||||
// Salva o aggiorna un membro
|
||||
Future<void> saveStaffMember(StaffMemberModel member) async {
|
||||
emit(state.copyWith(isLoading: true));
|
||||
try {
|
||||
await _repository.saveStaffMember(member);
|
||||
await loadAllStaff(); // Ricarichiamo la lista aggiornata
|
||||
} catch (e) {
|
||||
emit(state.copyWith(isLoading: false, error: e.toString()));
|
||||
}
|
||||
}
|
||||
|
||||
// Associa un dipendente a un negozio
|
||||
Future<void> assignMemberToStore(String staffId, String storeId) async {
|
||||
try {
|
||||
await _repository.assignToStore(staffId, storeId);
|
||||
final stuffStores = await loadStoresByStaff(staffId);
|
||||
final Map<String, List<StoreModel>> storesByStaff = Map.from(
|
||||
state.storesByStaff,
|
||||
);
|
||||
storesByStaff[staffId] = stuffStores;
|
||||
emit(state.copyWith(storesByStaff: storesByStaff));
|
||||
} catch (e) {
|
||||
emit(state.copyWith(error: "Errore nell'assegnazione: $e"));
|
||||
}
|
||||
}
|
||||
|
||||
// Rimuove un dipendente da un negozio
|
||||
Future<void> removeMemberFromStore(String staffId, String storeId) async {
|
||||
try {
|
||||
await _repository.removeFromStore(staffId, storeId);
|
||||
final stuffStores = await loadStoresByStaff(staffId);
|
||||
final Map<String, List<StoreModel>> storesByStaff = Map.from(
|
||||
state.storesByStaff,
|
||||
);
|
||||
storesByStaff[staffId] = stuffStores;
|
||||
emit(state.copyWith(storesByStaff: storesByStaff));
|
||||
} catch (e) {
|
||||
emit(state.copyWith(error: "Errore nella rimozione: $e"));
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> saveStaffWithStores({
|
||||
required StaffMemberModel member,
|
||||
required List<String> selectedStoreIds,
|
||||
}) async {
|
||||
emit(state.copyWith(isLoading: true));
|
||||
try {
|
||||
// 1. Salva o aggiorna l'anagrafica (ci serve l'ID)
|
||||
// Se è un nuovo membro, Supabase ci restituirà l'ID generato
|
||||
final savedMember = await _repository.saveStaffMember(member);
|
||||
final String staffId = savedMember.id!;
|
||||
|
||||
// 2. Sincronizzazione Negozi
|
||||
// Per semplicità e pulizia, rimuoviamo le vecchie assegnazioni e inseriamo le nuove
|
||||
// (Oppure facciamo un confronto tra liste, ma il reset & rewrite è più sicuro qui)
|
||||
await _repository.clearStoreAssignments(staffId);
|
||||
|
||||
if (selectedStoreIds.isNotEmpty) {
|
||||
await Future.wait(
|
||||
selectedStoreIds.map(
|
||||
(storeId) => _repository.assignToStore(staffId, storeId),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
// 3. Rinfresca i dati
|
||||
await loadAllStaff();
|
||||
|
||||
emit(state.copyWith(isLoading: false));
|
||||
} catch (e) {
|
||||
emit(state.copyWith(isLoading: false, error: e.toString()));
|
||||
}
|
||||
}
|
||||
}
|
||||
42
lib/features/master_data/staff/blocs/staff_state.dart
Normal file
42
lib/features/master_data/staff/blocs/staff_state.dart
Normal file
@@ -0,0 +1,42 @@
|
||||
part of 'staff_cubit.dart';
|
||||
|
||||
class StaffState extends Equatable {
|
||||
final List<StaffMemberModel> allStaff;
|
||||
final Map<String, List<StoreModel>> storesByStaff;
|
||||
final Map<String, List<StaffMemberModel>> staffByStore;
|
||||
final bool isLoading;
|
||||
final String? error;
|
||||
|
||||
const StaffState({
|
||||
this.allStaff = const [],
|
||||
this.storesByStaff = const {},
|
||||
this.staffByStore = const {},
|
||||
this.isLoading = false,
|
||||
this.error,
|
||||
});
|
||||
|
||||
StaffState copyWith({
|
||||
List<StaffMemberModel>? allStaff,
|
||||
Map<String, List<StoreModel>>? storesByStaff,
|
||||
Map<String, List<StaffMemberModel>>? staffByStore,
|
||||
bool? isLoading,
|
||||
String? error,
|
||||
}) {
|
||||
return StaffState(
|
||||
allStaff: allStaff ?? this.allStaff,
|
||||
storesByStaff: storesByStaff ?? this.storesByStaff,
|
||||
staffByStore: staffByStore ?? this.staffByStore,
|
||||
isLoading: isLoading ?? this.isLoading,
|
||||
error: error,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
List<Object?> get props => [
|
||||
allStaff,
|
||||
storesByStaff,
|
||||
staffByStore,
|
||||
isLoading,
|
||||
error,
|
||||
];
|
||||
}
|
||||
Reference in New Issue
Block a user