90 lines
		
	
	
		
			2.3 KiB
		
	
	
	
		
			Dart
		
	
	
	
	
	
			
		
		
	
	
			90 lines
		
	
	
		
			2.3 KiB
		
	
	
	
		
			Dart
		
	
	
	
	
	
| import 'package:cloud_firestore/cloud_firestore.dart';
 | |
| 
 | |
| enum AlertType {
 | |
|   lowStock,       // Stock faible
 | |
|   maintenanceDue, // Maintenance à venir
 | |
|   conflict        // Conflit disponibilité
 | |
| }
 | |
| 
 | |
| String alertTypeToString(AlertType type) {
 | |
|   switch (type) {
 | |
|     case AlertType.lowStock:
 | |
|       return 'LOW_STOCK';
 | |
|     case AlertType.maintenanceDue:
 | |
|       return 'MAINTENANCE_DUE';
 | |
|     case AlertType.conflict:
 | |
|       return 'CONFLICT';
 | |
|   }
 | |
| }
 | |
| 
 | |
| AlertType alertTypeFromString(String? type) {
 | |
|   switch (type) {
 | |
|     case 'LOW_STOCK':
 | |
|       return AlertType.lowStock;
 | |
|     case 'MAINTENANCE_DUE':
 | |
|       return AlertType.maintenanceDue;
 | |
|     case 'CONFLICT':
 | |
|       return AlertType.conflict;
 | |
|     default:
 | |
|       return AlertType.conflict;
 | |
|   }
 | |
| }
 | |
| 
 | |
| class AlertModel {
 | |
|   final String id;                  // ID généré automatiquement
 | |
|   final AlertType type;             // Type d'alerte
 | |
|   final String message;             // Message de l'alerte
 | |
|   final String? equipmentId;        // ID de l'équipement concerné (optionnel)
 | |
|   final DateTime createdAt;         // Date de création
 | |
|   final bool isRead;                // Statut lu/non lu
 | |
| 
 | |
|   AlertModel({
 | |
|     required this.id,
 | |
|     required this.type,
 | |
|     required this.message,
 | |
|     this.equipmentId,
 | |
|     required this.createdAt,
 | |
|     this.isRead = false,
 | |
|   });
 | |
| 
 | |
|   factory AlertModel.fromMap(Map<String, dynamic> map, String id) {
 | |
|     return AlertModel(
 | |
|       id: id,
 | |
|       type: alertTypeFromString(map['type']),
 | |
|       message: map['message'] ?? '',
 | |
|       equipmentId: map['equipmentId'],
 | |
|       createdAt: (map['createdAt'] as Timestamp?)?.toDate() ?? DateTime.now(),
 | |
|       isRead: map['isRead'] ?? false,
 | |
|     );
 | |
|   }
 | |
| 
 | |
|   Map<String, dynamic> toMap() {
 | |
|     return {
 | |
|       'type': alertTypeToString(type),
 | |
|       'message': message,
 | |
|       'equipmentId': equipmentId,
 | |
|       'createdAt': Timestamp.fromDate(createdAt),
 | |
|       'isRead': isRead,
 | |
|     };
 | |
|   }
 | |
| 
 | |
|   AlertModel copyWith({
 | |
|     String? id,
 | |
|     AlertType? type,
 | |
|     String? message,
 | |
|     String? equipmentId,
 | |
|     DateTime? createdAt,
 | |
|     bool? isRead,
 | |
|   }) {
 | |
|     return AlertModel(
 | |
|       id: id ?? this.id,
 | |
|       type: type ?? this.type,
 | |
|       message: message ?? this.message,
 | |
|       equipmentId: equipmentId ?? this.equipmentId,
 | |
|       createdAt: createdAt ?? this.createdAt,
 | |
|       isRead: isRead ?? this.isRead,
 | |
|     );
 | |
|   }
 | |
| }
 | |
| 
 | 
