151 lines
4.6 KiB
Dart
151 lines
4.6 KiB
Dart
import 'package:cloud_firestore/cloud_firestore.dart';
|
|
import 'package:latlong2/latlong.dart';
|
|
|
|
enum EventStatus {
|
|
confirmed,
|
|
canceled,
|
|
waitingForApproval,
|
|
}
|
|
|
|
String eventStatusToString(EventStatus status) {
|
|
switch (status) {
|
|
case EventStatus.confirmed:
|
|
return 'CONFIRMED';
|
|
case EventStatus.canceled:
|
|
return 'CANCELED';
|
|
case EventStatus.waitingForApproval:
|
|
default:
|
|
return 'WAITING_FOR_APPROVAL';
|
|
}
|
|
}
|
|
|
|
EventStatus eventStatusFromString(String? status) {
|
|
switch (status) {
|
|
case 'CONFIRMED':
|
|
return EventStatus.confirmed;
|
|
case 'CANCELED':
|
|
return EventStatus.canceled;
|
|
case 'WAITING_FOR_APPROVAL':
|
|
default:
|
|
return EventStatus.waitingForApproval;
|
|
}
|
|
}
|
|
|
|
class EventModel {
|
|
final String id;
|
|
final String name;
|
|
final String description;
|
|
final DateTime startDateTime;
|
|
final DateTime endDateTime;
|
|
final double basePrice;
|
|
final int installationTime;
|
|
final int disassemblyTime;
|
|
final String eventTypeId;
|
|
final String customerId;
|
|
final String address;
|
|
final double latitude;
|
|
final double longitude;
|
|
final List<DocumentReference> workforce;
|
|
final List<Map<String, String>> documents;
|
|
final List<Map<String, dynamic>> options;
|
|
final EventStatus status;
|
|
|
|
EventModel({
|
|
required this.id,
|
|
required this.name,
|
|
required this.description,
|
|
required this.startDateTime,
|
|
required this.endDateTime,
|
|
required this.basePrice,
|
|
required this.installationTime,
|
|
required this.disassemblyTime,
|
|
required this.eventTypeId,
|
|
required this.customerId,
|
|
required this.address,
|
|
required this.latitude,
|
|
required this.longitude,
|
|
required this.workforce,
|
|
required this.documents,
|
|
this.options = const [],
|
|
this.status = EventStatus.waitingForApproval,
|
|
});
|
|
|
|
factory EventModel.fromMap(Map<String, dynamic> map, String id) {
|
|
final List<dynamic> workforceRefs = map['workforce'] ?? [];
|
|
final Timestamp? startTimestamp = map['StartDateTime'] as Timestamp?;
|
|
final Timestamp? endTimestamp = map['EndDateTime'] as Timestamp?;
|
|
|
|
final docsRaw = map['documents'] ?? [];
|
|
final docs = docsRaw is List
|
|
? docsRaw.map<Map<String, String>>((e) {
|
|
if (e is Map) {
|
|
return Map<String, String>.from(e as Map);
|
|
} else if (e is String) {
|
|
final fileName = Uri.decodeComponent(
|
|
e.split('/').last.split('?').first,
|
|
);
|
|
return {'name': fileName, 'url': e};
|
|
} else {
|
|
return {};
|
|
}
|
|
}).toList()
|
|
: <Map<String, String>>[];
|
|
final optionsRaw = map['options'] ?? [];
|
|
final options = optionsRaw is List
|
|
? optionsRaw.map<Map<String, dynamic>>((e) {
|
|
if (e is Map) {
|
|
return Map<String, dynamic>.from(e as Map);
|
|
} else {
|
|
return {};
|
|
}
|
|
}).toList()
|
|
: <Map<String, dynamic>>[];
|
|
return EventModel(
|
|
id: id,
|
|
name: map['Name'] ?? '',
|
|
description: map['Description'] ?? '',
|
|
startDateTime: startTimestamp?.toDate() ?? DateTime.now(),
|
|
endDateTime: endTimestamp?.toDate() ??
|
|
DateTime.now().add(const Duration(hours: 1)),
|
|
basePrice: (map['BasePrice'] ?? map['Price'] ?? 0.0).toDouble(),
|
|
installationTime: map['InstallationTime'] ?? 0,
|
|
disassemblyTime: map['DisassemblyTime'] ?? 0,
|
|
eventTypeId: map['EventType'] is DocumentReference
|
|
? (map['EventType'] as DocumentReference).id
|
|
: '',
|
|
customerId: map['customer'] is DocumentReference
|
|
? (map['customer'] as DocumentReference).id
|
|
: '',
|
|
address: map['Address'] ?? '',
|
|
latitude: (map['Latitude'] ?? 0.0).toDouble(),
|
|
longitude: (map['Longitude'] ?? 0.0).toDouble(),
|
|
workforce: workforceRefs.whereType<DocumentReference>().toList(),
|
|
documents: docs,
|
|
options: options,
|
|
status: eventStatusFromString(map['status'] as String?),
|
|
);
|
|
}
|
|
|
|
Map<String, dynamic> toMap() {
|
|
return {
|
|
'Name': name,
|
|
'Description': description,
|
|
'StartDateTime': Timestamp.fromDate(startDateTime),
|
|
'EndDateTime': Timestamp.fromDate(endDateTime),
|
|
'BasePrice': basePrice,
|
|
'InstallationTime': installationTime,
|
|
'DisassemblyTime': disassemblyTime,
|
|
'EventType': eventTypeId,
|
|
'customer': customerId,
|
|
'Address': address,
|
|
'Position': GeoPoint(latitude, longitude),
|
|
'Latitude': latitude,
|
|
'Longitude': longitude,
|
|
'workforce': workforce,
|
|
'documents': documents,
|
|
'options': options,
|
|
'status': eventStatusToString(status),
|
|
};
|
|
}
|
|
}
|