feat-insert-service (#5)

Reviewed-on: http://catelliub.zapto.org:3000/brontomark/flux/pulls/5
Co-authored-by: mark-cachy <marco@catelli.it>
Co-committed-by: mark-cachy <marco@catelli.it>
This commit is contained in:
2026-04-20 16:52:20 +02:00
committed by brontomark
parent 667bbf6404
commit c3d4f3fac7
63 changed files with 4715 additions and 1371 deletions

View File

@@ -7,6 +7,7 @@ class CustomerFileModel extends Equatable {
final String url;
final String extension;
final DateTime? createdAt;
final int fileSize;
const CustomerFileModel({
this.id,
@@ -15,31 +16,76 @@ class CustomerFileModel extends Equatable {
required this.url,
required this.extension,
this.createdAt,
required this.fileSize,
});
factory CustomerFileModel.fromJson(Map<String, dynamic> json) {
// Trasforma i byte in qualcosa di leggibile (KB, MB, GB)
String get sizeFormatted {
if (fileSize <= 0) return "0 B";
const suffixes = ["B", "KB", "MB", "GB", "TB"];
var i = (fileSize.toString().length - 1) ~/ 3;
if (i >= suffixes.length) i = suffixes.length - 1;
double num = fileSize / (1 << (i * 10));
return "${num.toStringAsFixed(i == 0 ? 0 : 1)} ${suffixes[i]}";
}
bool get isPdf => extension.toLowerCase().replaceAll('.', '') == 'pdf';
CustomerFileModel copyWith({
String? id,
String? customerId,
String? name,
String? url,
String? extension,
DateTime? createdAt,
int? fileSize,
}) {
return CustomerFileModel(
id: json['id'] as String,
customerId: json['customer_id'],
name: json['name'],
url: json['url'],
extension: json['extension'] ?? '',
createdAt: json['created_at'] != null
? DateTime.parse(json['created_at'])
: null,
id: id ?? this.id,
customerId: customerId ?? this.customerId,
name: name ?? this.name,
url: url ?? this.url,
extension: extension ?? this.extension,
createdAt: createdAt ?? this.createdAt,
fileSize: fileSize ?? this.fileSize,
);
}
Map<String, dynamic> toJson() {
factory CustomerFileModel.fromMap(Map<String, dynamic> map) {
return CustomerFileModel(
id: map['id'] as String,
customerId: map['customer_id'],
name: map['name'],
url: map['url'],
extension: map['extension'] ?? '',
createdAt: map['created_at'] != null
? DateTime.parse(map['created_at'])
: null,
fileSize: map['file_size'] is int
? map['file_size']
: int.tryParse(map['file_size']?.toString() ?? '0') ?? 0,
);
}
Map<String, dynamic> toMap() {
return {
if (id != null) 'id': id,
'customer_id': customerId,
'name': name,
'url': url,
'extension': extension,
'file_size': fileSize,
};
}
@override
List<Object?> get props => [id, customerId, name, url, extension, createdAt];
List<Object?> get props => [
id,
customerId,
name,
url,
extension,
createdAt,
fileSize,
];
}

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 [],
);
}