This commit is contained in:
2026-04-20 23:52:00 +02:00
parent c5b5b76bd6
commit a19fd1104f
37 changed files with 1546 additions and 428 deletions

View 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"),
);
}
}
}

View File

@@ -0,0 +1,37 @@
import 'package:equatable/equatable.dart';
import 'package:flux/core/blocs/session/session_cubit.dart';
class OnboardingState extends Equatable {
final OnboardingStep step;
final bool isLoading;
final String? error;
final String? companyId; // Salvato dopo lo Step 1
final String? storeId; // Salvato dopo lo Step 2
const OnboardingState({
required this.step,
this.isLoading = false,
this.error,
this.companyId,
this.storeId,
});
OnboardingState copyWith({
OnboardingStep? step,
bool? isLoading,
String? error,
String? companyId,
String? storeId,
}) {
return OnboardingState(
step: step ?? this.step,
isLoading: isLoading ?? this.isLoading,
error: error, // Se non passato, resettiamo l'errore
companyId: companyId ?? this.companyId,
storeId: storeId ?? this.storeId,
);
}
@override
List<Object?> get props => [step, isLoading, error, companyId, storeId];
}