aiuto
This commit is contained in:
90
lib/features/onboarding/blocs/onboarding_cubit.dart
Normal file
90
lib/features/onboarding/blocs/onboarding_cubit.dart
Normal file
@@ -0,0 +1,90 @@
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:flux/core/blocs/session/session_cubit.dart';
|
||||
import 'package:flux/core/data/core_repository.dart';
|
||||
import 'package:flux/features/company/models/company_model.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:flux/features/onboarding/blocs/onboarding_state.dart';
|
||||
|
||||
class OnboardingCubit extends Cubit<OnboardingState> {
|
||||
final CoreRepository _repository;
|
||||
final SessionCubit _sessionCubit;
|
||||
|
||||
OnboardingCubit(this._sessionCubit, this._repository)
|
||||
: super(OnboardingState(step: _sessionCubit.state.onboardingStep));
|
||||
|
||||
// --- STEP 1: REGISTRAZIONE AZIENDA ---
|
||||
Future<void> saveCompany(CompanyModel company) async {
|
||||
emit(state.copyWith(isLoading: true));
|
||||
try {
|
||||
// Il repository restituisce il modello creato con l'ID di Supabase
|
||||
final savedCompany = await _repository.createCompany(company);
|
||||
|
||||
emit(
|
||||
state.copyWith(
|
||||
isLoading: false,
|
||||
step: OnboardingStep.store,
|
||||
companyId: savedCompany.id,
|
||||
),
|
||||
);
|
||||
} catch (e) {
|
||||
emit(
|
||||
state.copyWith(
|
||||
isLoading: false,
|
||||
error: "Errore salvataggio azienda: $e",
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// --- STEP 2: REGISTRAZIONE PRIMO NEGOZIO ---
|
||||
Future<void> saveStore(StoreModel store) async {
|
||||
if (state.companyId == null) return;
|
||||
|
||||
emit(state.copyWith(isLoading: true));
|
||||
try {
|
||||
// Iniettiamo forzatamente il companyId ottenuto dallo step precedente
|
||||
final storeToSave = store.copyWith(companyId: state.companyId);
|
||||
final savedStore = await _repository.createStore(storeToSave);
|
||||
|
||||
emit(
|
||||
state.copyWith(
|
||||
isLoading: false,
|
||||
step: OnboardingStep.staff,
|
||||
storeId: savedStore.id,
|
||||
),
|
||||
);
|
||||
} catch (e) {
|
||||
emit(
|
||||
state.copyWith(isLoading: false, error: "Errore salvataggio store: $e"),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// --- STEP 3: REGISTRAZIONE PROFILO STAFF (PAZIENTE ZERO) ---
|
||||
Future<void> saveStaff(StaffMemberModel staff) async {
|
||||
if (state.companyId == null || state.storeId == null) return;
|
||||
|
||||
emit(state.copyWith(isLoading: true));
|
||||
try {
|
||||
// PARANOIA MODE: Forziamo i legami e il ruolo di sistema 'admin'
|
||||
final staffToSave = staff.copyWith(
|
||||
companyId: state.companyId!,
|
||||
storeId: state.storeId!,
|
||||
userId: _sessionCubit.state.user!.id, // Dall'utente loggato in Supabase
|
||||
systemRole: SystemRole.admin, // Blindato!
|
||||
);
|
||||
|
||||
await _repository.createStaffMember(staffToSave);
|
||||
|
||||
emit(state.copyWith(isLoading: false, step: OnboardingStep.completed));
|
||||
|
||||
// Svegliamo il SessionCubit: lui ricalcolerà tutto e aprirà la Dashboard
|
||||
await _sessionCubit.initializeSession();
|
||||
} catch (e) {
|
||||
emit(
|
||||
state.copyWith(isLoading: false, error: "Errore creazione profilo: $e"),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user