fix supabase storage

This commit is contained in:
2026-04-20 16:50:55 +02:00
parent 8dc1c661ed
commit de940cea1f
10 changed files with 172 additions and 100 deletions

View File

@@ -18,7 +18,7 @@ class CustomerRepository {
.upsert(customer.toJson())
.select()
.single();
return CustomerModel.fromJson(response);
return CustomerModel.fromMap(response);
} catch (e) {
throw 'Errore durante il salvataggio del cliente: $e';
}
@@ -32,7 +32,7 @@ class CustomerRepository {
.eq('id', customer.id!)
.select()
.single();
return CustomerModel.fromJson(response);
return CustomerModel.fromMap(response);
} catch (e) {
throw 'Errore durante la modifica del cliente: $e';
}
@@ -43,12 +43,15 @@ class CustomerRepository {
try {
final response = await _supabase
.from('customer')
.select('*, customer_file(count)')
.select('''
*,
customer_file(*)
''')
.eq('company_id', companyId)
.eq('is_active', true)
.order('nome');
return (response as List).map((c) => CustomerModel.fromJson(c)).toList();
return (response as List).map((c) => CustomerModel.fromMap(c)).toList();
} catch (e) {
throw 'Errore nel recupero clienti';
}
@@ -67,7 +70,7 @@ class CustomerRepository {
.or('nome.ilike.%$query%,telefono.ilike.%$query%')
.limit(10);
return (response as List).map((c) => CustomerModel.fromJson(c)).toList();
return (response as List).map((c) => CustomerModel.fromMap(c)).toList();
} catch (e) {
return [];
}
@@ -110,7 +113,7 @@ class CustomerRepository {
customerId: customerId,
name: cleanFileName.fileNameWithoutExtension(),
extension: cleanFileName.fileExtension(),
url: '',
url: storagePath,
fileSize: fileSize,
);
final String mimeType = fileToSave.extension.toLowerCase() == 'pdf'
@@ -133,13 +136,9 @@ class CustomerRepository {
);
}
final String publicUrl = _supabase.storage
.from('documents')
.getPublicUrl(storagePath);
final response = await _supabase
.from('customer_file')
.insert(fileToSave.copyWith(url: publicUrl).toMap())
.insert(fileToSave.toMap())
.select()
.single();

View File

@@ -1,5 +1,6 @@
import 'package:equatable/equatable.dart';
import 'package:flux/core/utils/string_extensions.dart';
import 'package:flux/features/customers/models/customer_file_model.dart';
class CustomerModel extends Equatable {
final String? id; // Bigint in SQL
@@ -12,7 +13,7 @@ class CustomerModel extends Equatable {
final bool nonDisturbare;
final String companyId; // UUID
final bool isActive;
final int fileCount;
final List<CustomerFileModel> files;
const CustomerModel({
this.id,
@@ -25,7 +26,7 @@ class CustomerModel extends Equatable {
this.nonDisturbare = false,
required this.companyId,
this.isActive = true,
this.fileCount = 0,
this.files = const [],
});
@override
@@ -40,7 +41,7 @@ class CustomerModel extends Equatable {
nonDisturbare,
companyId,
isActive,
fileCount,
files,
];
CustomerModel copyWith({
@@ -54,7 +55,7 @@ class CustomerModel extends Equatable {
bool? nonDisturbare,
String? companyId,
bool? isActive,
int? fileCount,
List<CustomerFileModel>? files,
}) {
return CustomerModel(
id: id ?? this.id,
@@ -67,32 +68,31 @@ class CustomerModel extends Equatable {
nonDisturbare: nonDisturbare ?? this.nonDisturbare,
companyId: companyId ?? this.companyId,
isActive: isActive ?? this.isActive,
fileCount: fileCount ?? this.fileCount,
files: files ?? this.files,
);
}
factory CustomerModel.fromJson(Map<String, dynamic> json) {
int count = 0;
if (json['customer_file'] != null &&
(json['customer_file'] as List).isNotEmpty) {
count = json['customer_file'][0]['count'] ?? 0;
}
factory CustomerModel.fromMap(Map<String, dynamic> map) {
return CustomerModel(
id: json['id'] as String,
createdAt: json['created_at'] != null
? DateTime.parse(json['created_at'])
id: map['id'] as String,
createdAt: map['created_at'] != null
? DateTime.parse(map['created_at'])
: null,
nome: (json['nome'] as String).myFormat(),
telefono: json['telefono'],
email: json['email'],
note: json['note'] ?? '',
dataUltimoContatto: json['data_ultimo_contatto'] != null
? DateTime.parse(json['data_ultimo_contatto'])
nome: (map['nome'] as String).myFormat(),
telefono: map['telefono'],
email: map['email'],
note: map['note'] ?? '',
dataUltimoContatto: map['data_ultimo_contatto'] != null
? DateTime.parse(map['data_ultimo_contatto'])
: null,
nonDisturbare: json['non_disturbare'] ?? false,
companyId: json['company_id'] as String,
isActive: json['is_active'] ?? true,
fileCount: count,
nonDisturbare: map['non_disturbare'] ?? false,
companyId: map['company_id'] as String,
isActive: map['is_active'] ?? true,
files:
(map['customer_file'] as List?)
?.map((x) => CustomerFileModel.fromMap(x))
.toList() ??
const [],
);
}

View File

@@ -33,7 +33,7 @@ class _CustomersContentState extends State<CustomersContent> {
void _onSearch(String query) {
final companyId = context.read<SessionBloc>().state.company?.id;
if (companyId != null) {
context.read<CustomerCubit>().searchCustomers( query);
context.read<CustomerCubit>().searchCustomers(query);
}
}
@@ -229,11 +229,11 @@ class _CustomerTile extends StatelessWidget {
style: TextStyle(color: context.secondaryText),
),
],
if (customer.fileCount > 0) ...[
if (customer.files.isNotEmpty) ...[
Text(' - ', style: TextStyle(color: context.secondaryText)),
Icon(Icons.attach_file, size: 14, color: context.accent),
Text(
'${customer.fileCount} doc',
'${customer.files.length} doc',
style: TextStyle(
color: context.accent,
fontWeight: FontWeight.bold,