providers-in-store (#4)
Reviewed-on: http://catelliub.zapto.org:3000/brontomark/flux/pulls/4 Co-authored-by: mark-cachy <marco@catelli.it> Co-committed-by: mark-cachy <marco@catelli.it>
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:equatable/equatable.dart';
|
||||
import 'package:flux/core/blocs/session/session_bloc.dart';
|
||||
import 'package:flux/features/master_data/providers/models/provider_model.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/data/store_repository.dart';
|
||||
@@ -31,7 +32,7 @@ class StoreCubit extends Cubit<StoreState> {
|
||||
Future<void> loadStores() async {
|
||||
emit(state.copyWith(status: StoreStatus.loading));
|
||||
try {
|
||||
final stores = await _repository.getStoresByCompany(
|
||||
final stores = await _repository.fetchAllCompanyStores(
|
||||
_sessionBloc.state.company!.id,
|
||||
);
|
||||
final Map<String, List<StaffMemberModel>> staffByStore = {};
|
||||
@@ -54,18 +55,91 @@ class StoreCubit extends Cubit<StoreState> {
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> saveProvidersForStore(
|
||||
StoreModel store,
|
||||
List<ProviderModel> providers,
|
||||
) async {
|
||||
emit(state.copyWith(status: StoreStatus.loading));
|
||||
|
||||
try {
|
||||
final savedStore = await _repository.saveStore(store);
|
||||
await _repository.syncStoreProviders(savedStore.id!, providers);
|
||||
await loadStores();
|
||||
} catch (e) {
|
||||
emit(
|
||||
state.copyWith(
|
||||
status: StoreStatus.failure,
|
||||
errorMessage: "Errore nel salvataggio dei provider: $e",
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> saveStaffForStore(
|
||||
StoreModel store,
|
||||
List<StaffMemberModel> staffMembers,
|
||||
) async {
|
||||
emit(state.copyWith(status: StoreStatus.loading));
|
||||
|
||||
try {
|
||||
final savedStore = await _repository.saveStore(store);
|
||||
await _repository.syncStoreStaff(savedStore.id!, staffMembers);
|
||||
await loadStores();
|
||||
} catch (e) {
|
||||
emit(
|
||||
state.copyWith(
|
||||
status: StoreStatus.failure,
|
||||
errorMessage: "Errore nel salvataggio dello staff: $e",
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> associateStoreToProvider(
|
||||
String storeId,
|
||||
String providerId,
|
||||
) async {
|
||||
try {
|
||||
await _repository.associateStoreToProvider(
|
||||
providerId: providerId,
|
||||
storeId: storeId,
|
||||
);
|
||||
loadStores();
|
||||
} catch (e) {
|
||||
emit(
|
||||
state.copyWith(
|
||||
status: StoreStatus.failure,
|
||||
errorMessage: "Errore nell'associazione: $e",
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> removeProviderFromStore(
|
||||
String storeId,
|
||||
String providerId,
|
||||
) async {
|
||||
try {
|
||||
await _repository.removeStoreFromProvider(
|
||||
providerId: providerId,
|
||||
storeId: storeId,
|
||||
);
|
||||
loadStores();
|
||||
} catch (e) {
|
||||
emit(
|
||||
state.copyWith(
|
||||
status: StoreStatus.failure,
|
||||
errorMessage: "Errore nella rimozione: $e",
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> assignStaffToStore(String storeId, String staffId) async {
|
||||
try {
|
||||
await _staffRepository.assignToStore(staffId, storeId);
|
||||
// Dopo l'assegnazione, potresti voler ricaricare lo staff per quel negozio
|
||||
final updatedStaff = await _staffRepository.getStaffMembersInStore(
|
||||
storeId,
|
||||
);
|
||||
final newMap = Map<String, List<StaffMemberModel>>.from(
|
||||
state.staffByStore,
|
||||
);
|
||||
newMap[storeId] = updatedStaff;
|
||||
emit(state.copyWith(status: StoreStatus.success, staffByStore: newMap));
|
||||
loadStores();
|
||||
} catch (e) {
|
||||
emit(
|
||||
state.copyWith(status: StoreStatus.failure, errorMessage: e.toString()),
|
||||
@@ -77,14 +151,7 @@ class StoreCubit extends Cubit<StoreState> {
|
||||
Future<void> removeStaffFromStore(String staffId, String storeId) async {
|
||||
try {
|
||||
await _staffRepository.removeFromStore(staffId, storeId);
|
||||
final updatedStaff = await _staffRepository.getStaffMembersInStore(
|
||||
storeId,
|
||||
);
|
||||
final newMap = Map<String, List<StaffMemberModel>>.from(
|
||||
state.staffByStore,
|
||||
);
|
||||
newMap[storeId] = updatedStaff;
|
||||
emit(state.copyWith(staffByStore: newMap));
|
||||
loadStores();
|
||||
} catch (e) {
|
||||
emit(
|
||||
state.copyWith(
|
||||
|
||||
Reference in New Issue
Block a user