service (#2)
Reviewed-on: http://catelliub.zapto.org:3000/brontomark/flux/pulls/2 Co-authored-by: Mark M2 Macbook <marco@catelli.it> Co-committed-by: Mark M2 Macbook <marco@catelli.it>
This commit is contained in:
113
lib/features/master_data/providers/blocs/provider_cubit.dart
Normal file
113
lib/features/master_data/providers/blocs/provider_cubit.dart
Normal file
@@ -0,0 +1,113 @@
|
||||
import 'package:equatable/equatable.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:flux/features/master_data/providers/data/provider_repository.dart';
|
||||
import 'package:get_it/get_it.dart';
|
||||
import '../models/provider_model.dart';
|
||||
|
||||
class ProvidersState extends Equatable {
|
||||
final List<ProviderModel> allProviders; // Tutti i provider della company
|
||||
final List<String>
|
||||
associatedIds; // ID dei provider attivi nello store selezionato
|
||||
final bool isLoading;
|
||||
final String? errorMessage;
|
||||
|
||||
const ProvidersState({
|
||||
this.allProviders = const [],
|
||||
this.associatedIds = const [],
|
||||
this.isLoading = false,
|
||||
this.errorMessage,
|
||||
});
|
||||
|
||||
ProvidersState copyWith({
|
||||
List<ProviderModel>? allProviders,
|
||||
List<String>? associatedIds,
|
||||
bool? isLoading,
|
||||
String? errorMessage,
|
||||
}) {
|
||||
return ProvidersState(
|
||||
allProviders: allProviders ?? this.allProviders,
|
||||
associatedIds: associatedIds ?? this.associatedIds,
|
||||
isLoading: isLoading ?? this.isLoading,
|
||||
errorMessage: errorMessage,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
List<Object?> get props => [
|
||||
allProviders,
|
||||
associatedIds,
|
||||
isLoading,
|
||||
errorMessage,
|
||||
];
|
||||
}
|
||||
|
||||
class ProvidersCubit extends Cubit<ProvidersState> {
|
||||
final ProviderRepository _repository = GetIt.I<ProviderRepository>();
|
||||
|
||||
ProvidersCubit() : super(const ProvidersState());
|
||||
|
||||
// Carica i provider della company e quelli associati a uno store specifico
|
||||
Future<void> loadProviders(String companyId, String? storeId) async {
|
||||
emit(state.copyWith(isLoading: true));
|
||||
try {
|
||||
final all = await _repository.fetchAllCompanyProviders(companyId);
|
||||
List<String> associated = [];
|
||||
|
||||
if (storeId != null) {
|
||||
associated = await _repository.fetchAssociatedProviderIds(storeId);
|
||||
}
|
||||
|
||||
emit(
|
||||
state.copyWith(
|
||||
allProviders: all,
|
||||
associatedIds: associated,
|
||||
isLoading: false,
|
||||
),
|
||||
);
|
||||
} catch (e) {
|
||||
emit(state.copyWith(isLoading: false, errorMessage: e.toString()));
|
||||
}
|
||||
}
|
||||
|
||||
// Aggiunge o rimuove l'associazione con lo store
|
||||
Future<void> toggleProviderAssociation({
|
||||
required String providerId,
|
||||
required String storeId,
|
||||
required bool isCurrentlyAssociated,
|
||||
}) async {
|
||||
try {
|
||||
if (isCurrentlyAssociated) {
|
||||
await _repository.disassociateProviderFromStore(
|
||||
providerId: providerId,
|
||||
storeId: storeId,
|
||||
);
|
||||
// Aggiorniamo lo stato locale rimuovendo l'ID
|
||||
final newIds = List<String>.from(state.associatedIds)
|
||||
..remove(providerId);
|
||||
emit(state.copyWith(associatedIds: newIds));
|
||||
} else {
|
||||
await _repository.associateProviderToStore(
|
||||
providerId: providerId,
|
||||
storeId: storeId,
|
||||
);
|
||||
// Aggiorniamo lo stato locale aggiungendo l'ID
|
||||
final newIds = List<String>.from(state.associatedIds)..add(providerId);
|
||||
emit(state.copyWith(associatedIds: newIds));
|
||||
}
|
||||
} catch (e) {
|
||||
emit(state.copyWith(errorMessage: "Errore durante l'aggiornamento: $e"));
|
||||
}
|
||||
}
|
||||
|
||||
// Salvataggio/Update anagrafica (nuovo o modifica)
|
||||
Future<void> saveProvider(ProviderModel provider) async {
|
||||
emit(state.copyWith(isLoading: true));
|
||||
try {
|
||||
await _repository.saveProvider(provider);
|
||||
// Ricarichiamo la lista per vedere le modifiche
|
||||
await loadProviders(provider.companyId, null);
|
||||
} catch (e) {
|
||||
emit(state.copyWith(isLoading: false, errorMessage: e.toString()));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
import 'package:supabase_flutter/supabase_flutter.dart';
|
||||
import '../models/provider_model.dart';
|
||||
|
||||
class ProviderRepository {
|
||||
final _supabase = Supabase.instance.client;
|
||||
|
||||
// --- ASSOCIAZIONE PROVIDER <-> STORE ---
|
||||
|
||||
// Aggiunge un provider a un negozio (Attiva mandato)
|
||||
Future<void> associateProviderToStore({
|
||||
required String providerId,
|
||||
required String storeId,
|
||||
}) async {
|
||||
try {
|
||||
await _supabase.from('providers_in_stores').insert({
|
||||
'provider_id': providerId,
|
||||
'store_id': storeId,
|
||||
});
|
||||
} catch (e) {
|
||||
throw Exception('Errore durante l\'associazione provider: $e');
|
||||
}
|
||||
}
|
||||
|
||||
// Rimuove un provider da un negozio (Disattiva mandato)
|
||||
Future<void> disassociateProviderFromStore({
|
||||
required String providerId,
|
||||
required String storeId,
|
||||
}) async {
|
||||
try {
|
||||
await _supabase
|
||||
.from('providers_in_stores')
|
||||
.delete()
|
||||
.eq('provider_id', providerId)
|
||||
.eq('store_id', storeId);
|
||||
} catch (e) {
|
||||
throw Exception('Errore durante la disassociazione provider: $e');
|
||||
}
|
||||
}
|
||||
|
||||
// Recupera tutti i provider di una company (per la lista generale)
|
||||
Future<List<ProviderModel>> fetchAllCompanyProviders(String companyId) async {
|
||||
try {
|
||||
final response = await _supabase
|
||||
.from('provider')
|
||||
.select()
|
||||
.eq('company_id', companyId);
|
||||
|
||||
return (response as List).map((m) => ProviderModel.fromMap(m)).toList();
|
||||
} catch (e) {
|
||||
throw Exception('Errore fetch provider: $e');
|
||||
}
|
||||
}
|
||||
|
||||
// Recupera gli ID dei provider associati a uno store (utile per le checkbox)
|
||||
Future<List<String>> fetchAssociatedProviderIds(String storeId) async {
|
||||
try {
|
||||
final response = await _supabase
|
||||
.from('providers_in_stores')
|
||||
.select('provider_id')
|
||||
.eq('store_id', storeId);
|
||||
|
||||
return (response as List)
|
||||
.map((item) => item['provider_id'].toString())
|
||||
.toList();
|
||||
} catch (e) {
|
||||
throw Exception('Errore recupero ID associati: $e');
|
||||
}
|
||||
}
|
||||
|
||||
// --- FUNZIONI STANDARD ---
|
||||
|
||||
// Questa la userai nel Form Servizi: carica solo i provider abilitati per lo store
|
||||
Future<List<ProviderModel>> fetchActiveProvidersForStore(
|
||||
String storeId,
|
||||
) async {
|
||||
try {
|
||||
final response = await _supabase
|
||||
.from('provider')
|
||||
.select('*, providers_in_stores!inner(store_id)')
|
||||
.eq('providers_in_stores.store_id', storeId)
|
||||
.eq('is_active', true);
|
||||
|
||||
return (response as List).map((m) => ProviderModel.fromMap(m)).toList();
|
||||
} catch (e) {
|
||||
throw Exception('Errore fetch provider attivi: $e');
|
||||
}
|
||||
}
|
||||
|
||||
// Salva o aggiorna l'anagrafica del Provider
|
||||
Future<void> saveProvider(ProviderModel provider) async {
|
||||
await _supabase.from('provider').upsert(provider.toMap());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
import 'package:equatable/equatable.dart';
|
||||
|
||||
class ProviderModel extends Equatable {
|
||||
final String id;
|
||||
final String nome;
|
||||
final bool telefoniaFissa;
|
||||
final bool telefoniaMobile;
|
||||
final bool energia;
|
||||
final bool assicurazioni;
|
||||
final bool intrattenimento;
|
||||
final bool altro;
|
||||
final bool isActive;
|
||||
final String companyId;
|
||||
|
||||
const ProviderModel({
|
||||
required this.id,
|
||||
required this.nome,
|
||||
required this.telefoniaFissa,
|
||||
required this.telefoniaMobile,
|
||||
required this.energia,
|
||||
required this.assicurazioni,
|
||||
required this.intrattenimento,
|
||||
required this.altro,
|
||||
required this.isActive,
|
||||
required this.companyId,
|
||||
});
|
||||
|
||||
factory ProviderModel.fromMap(Map<String, dynamic> map) {
|
||||
return ProviderModel(
|
||||
id: map['id'],
|
||||
nome: map['nome'],
|
||||
telefoniaFissa: map['telefonia_fissa'] ?? false,
|
||||
telefoniaMobile: map['telefonia_mobile'] ?? false,
|
||||
energia: map['energia'] ?? false,
|
||||
assicurazioni: map['assicurazioni'] ?? false,
|
||||
intrattenimento: map['intrattenimento'] ?? false,
|
||||
altro: map['altro'] ?? false,
|
||||
isActive: map['is_active'] ?? true,
|
||||
companyId: map['company_id'],
|
||||
);
|
||||
}
|
||||
|
||||
Map<String, dynamic> toMap() {
|
||||
return {
|
||||
'nome': nome,
|
||||
'telefonia_fissa': telefoniaFissa,
|
||||
'telefonia_mobile': telefoniaMobile,
|
||||
'energia': energia,
|
||||
'assicurazioni': assicurazioni,
|
||||
'intrattenimento': intrattenimento,
|
||||
'altro': altro,
|
||||
'is_active': isActive,
|
||||
'company_id': companyId,
|
||||
};
|
||||
}
|
||||
|
||||
@override
|
||||
List<Object?> get props => [
|
||||
id,
|
||||
nome,
|
||||
telefoniaFissa,
|
||||
telefoniaMobile,
|
||||
energia,
|
||||
assicurazioni,
|
||||
intrattenimento,
|
||||
altro,
|
||||
isActive,
|
||||
companyId,
|
||||
];
|
||||
|
||||
ProviderModel copyWith({
|
||||
String? id,
|
||||
String? nome,
|
||||
bool? telefoniaFissa,
|
||||
bool? telefoniaMobile,
|
||||
bool? energia,
|
||||
bool? assicurazioni,
|
||||
bool? intrattenimento,
|
||||
bool? altro,
|
||||
bool? isActive,
|
||||
String? companyId,
|
||||
}) {
|
||||
return ProviderModel(
|
||||
id: id ?? this.id,
|
||||
nome: nome ?? this.nome,
|
||||
telefoniaFissa: telefoniaFissa ?? this.telefoniaFissa,
|
||||
telefoniaMobile: telefoniaMobile ?? this.telefoniaMobile,
|
||||
energia: energia ?? this.energia,
|
||||
assicurazioni: assicurazioni ?? this.assicurazioni,
|
||||
intrattenimento: intrattenimento ?? this.intrattenimento,
|
||||
altro: altro ?? this.altro,
|
||||
isActive: isActive ?? this.isActive,
|
||||
companyId: companyId ?? this.companyId,
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user