88 lines
2.5 KiB
Dart
88 lines
2.5 KiB
Dart
/// Shared attachment metadata for GRN / Asset / Purchase Order files.
|
|
class EntityAttachmentModel {
|
|
const EntityAttachmentModel({
|
|
required this.id,
|
|
this.parentId,
|
|
this.fileName,
|
|
this.fileType,
|
|
this.fileSize,
|
|
this.uploadedByName,
|
|
this.attachmentType,
|
|
this.createdAt,
|
|
});
|
|
|
|
final String id;
|
|
final String? parentId;
|
|
final String? fileName;
|
|
final String? fileType;
|
|
final int? fileSize;
|
|
final String? uploadedByName;
|
|
final String? attachmentType;
|
|
final DateTime? createdAt;
|
|
|
|
factory EntityAttachmentModel.fromJson(Map<String, dynamic> json) {
|
|
return EntityAttachmentModel(
|
|
id: _idFromJson(json['id']),
|
|
parentId: _idFromJsonNullable(
|
|
json['asset_id'] ?? json['po_id'] ?? json['grn_id'],
|
|
),
|
|
fileName: json['file_name'] as String?,
|
|
fileType: json['file_type'] as String?,
|
|
fileSize: _intFromJsonNullable(json['file_size']),
|
|
uploadedByName: _readUploadedByName(json),
|
|
attachmentType: json['attachment_type'] as String?,
|
|
createdAt: _dateFromJsonNullable(json['created_at']),
|
|
);
|
|
}
|
|
|
|
bool get isPdf =>
|
|
(fileType ?? '').toLowerCase().contains('pdf') ||
|
|
(fileName ?? '').toLowerCase().endsWith('.pdf');
|
|
|
|
bool get isImage {
|
|
final type = (fileType ?? '').toLowerCase();
|
|
final name = (fileName ?? '').toLowerCase();
|
|
return type.startsWith('image/') ||
|
|
name.endsWith('.jpg') ||
|
|
name.endsWith('.jpeg') ||
|
|
name.endsWith('.png') ||
|
|
name.endsWith('.webp');
|
|
}
|
|
}
|
|
|
|
String _idFromJson(Object? value) {
|
|
if (value == null) return '';
|
|
return value.toString();
|
|
}
|
|
|
|
String? _idFromJsonNullable(Object? value) {
|
|
if (value == null) return null;
|
|
final s = value.toString().trim();
|
|
return s.isEmpty ? null : s;
|
|
}
|
|
|
|
int? _intFromJsonNullable(Object? value) {
|
|
if (value == null) return null;
|
|
if (value is int) return value;
|
|
return int.tryParse(value.toString());
|
|
}
|
|
|
|
DateTime? _dateFromJsonNullable(Object? value) {
|
|
if (value == null) return null;
|
|
if (value is DateTime) return value;
|
|
return DateTime.tryParse(value.toString());
|
|
}
|
|
|
|
String? _readUploadedByName(Map<String, dynamic> json) {
|
|
final flat = json['uploaded_by_name'];
|
|
if (flat is String && flat.trim().isNotEmpty) return flat;
|
|
final nested = json['uploaded_by_user'] ?? json['uploaded_by'];
|
|
if (nested is Map) {
|
|
final name = nested['full_name'] ?? nested['name'];
|
|
if (name != null && name.toString().trim().isNotEmpty) {
|
|
return name.toString().trim();
|
|
}
|
|
}
|
|
return null;
|
|
}
|