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:
2026-04-17 15:37:14 +02:00
committed by brontomark
parent c1b6c9e7ac
commit 667bbf6404
14 changed files with 688 additions and 335 deletions

View File

@@ -1,4 +1,5 @@
import 'package:equatable/equatable.dart';
import 'package:flux/features/master_data/store/models/store_model.dart';
class ProviderModel extends Equatable {
final String? id;
@@ -11,7 +12,7 @@ class ProviderModel extends Equatable {
final bool altro;
final bool isActive;
final String companyId;
final int storesCount;
final List<StoreModel> associatedStores;
const ProviderModel({
this.id,
@@ -24,10 +25,21 @@ class ProviderModel extends Equatable {
required this.altro,
required this.isActive,
required this.companyId,
this.storesCount = 0, // Numero di store associati, default a 0
this.associatedStores = const [],
});
factory ProviderModel.fromMap(Map<String, dynamic> map) {
// Estraiamo la lista dalla pivot e poi prendiamo l'oggetto 'store' annidato
final pivotList = map['associated_stores'] as List?;
List<StoreModel> stores = [];
if (pivotList != null) {
stores = pivotList
.where((item) => item['store'] != null) // Sicurezza
.map(
(item) => StoreModel.fromMap(item['store'] as Map<String, dynamic>),
)
.toList();
}
return ProviderModel(
id: map['id'],
nome: map['nome'],
@@ -39,11 +51,7 @@ class ProviderModel extends Equatable {
altro: map['altro'] ?? false,
isActive: map['is_active'] ?? true,
companyId: map['company_id'],
storesCount:
map['providers_in_stores'] != null &&
map['providers_in_stores'].isNotEmpty
? map['providers_in_stores'][0]['count'] as int
: 0, // Assumiamo che l'API possa restituire questo campo
associatedStores: stores,
);
}
@@ -79,7 +87,7 @@ class ProviderModel extends Equatable {
altro,
isActive,
companyId,
storesCount,
associatedStores,
];
ProviderModel copyWith({
@@ -93,7 +101,7 @@ class ProviderModel extends Equatable {
bool? altro,
bool? isActive,
String? companyId,
int? storesCount,
List<StoreModel>? associatedStores,
}) {
return ProviderModel(
id: id ?? this.id,
@@ -106,7 +114,7 @@ class ProviderModel extends Equatable {
altro: altro ?? this.altro,
isActive: isActive ?? this.isActive,
companyId: companyId ?? this.companyId,
storesCount: storesCount ?? this.storesCount,
associatedStores: associatedStores ?? this.associatedStores,
);
}
}