75 lines
		
	
	
		
			1.9 KiB
		
	
	
	
		
			Dart
		
	
	
	
	
	
			
		
		
	
	
			75 lines
		
	
	
		
			1.9 KiB
		
	
	
	
		
			Dart
		
	
	
	
	
	
| import 'package:cloud_firestore/cloud_firestore.dart';
 | |
| 
 | |
| class UserModel {
 | |
|   final String uid;
 | |
|   final String firstName;
 | |
|   final String lastName;
 | |
|   final String role;
 | |
|   final String profilePhotoUrl;
 | |
|   final String email;
 | |
|   final String phoneNumber;
 | |
| 
 | |
|   UserModel({
 | |
|     required this.uid,
 | |
|     required this.firstName,
 | |
|     required this.lastName,
 | |
|     required this.role,
 | |
|     required this.profilePhotoUrl,
 | |
|     required this.email,
 | |
|     required this.phoneNumber,
 | |
|   });
 | |
| 
 | |
|   // Convertit une Map (Firestore) en UserModel
 | |
|   factory UserModel.fromMap(Map<String, dynamic> data, String uid) {
 | |
|     String roleString;
 | |
|     final roleField = data['role'];
 | |
|     if (roleField is String) {
 | |
|       roleString = roleField;
 | |
|     } else if (roleField is DocumentReference) {
 | |
|       roleString = roleField.id;
 | |
|     } else {
 | |
|       roleString = 'USER';
 | |
|     }
 | |
|     return UserModel(
 | |
|       uid: uid,
 | |
|       firstName: data['firstName'] ?? '',
 | |
|       lastName: data['lastName'] ?? '',
 | |
|       role: roleString,
 | |
|       profilePhotoUrl: data['profilePhotoUrl'] ?? '',
 | |
|       email: data['email'] ?? '',
 | |
|       phoneNumber: data['phoneNumber'] ?? '',
 | |
|     );
 | |
|   }
 | |
| 
 | |
|   // Convertit un UserModel en Map pour Firestore
 | |
|   Map<String, dynamic> toMap() {
 | |
|     return {
 | |
|       'firstName': firstName,
 | |
|       'lastName': lastName,
 | |
|       'role': FirebaseFirestore.instance.collection('roles').doc(role),
 | |
|       'profilePhotoUrl': profilePhotoUrl,
 | |
|       'email': email,
 | |
|       'phoneNumber': phoneNumber,
 | |
|     };
 | |
|   }
 | |
| 
 | |
|   UserModel copyWith({
 | |
|     String? firstName,
 | |
|     String? lastName,
 | |
|     String? role,
 | |
|     String? profilePhotoUrl,
 | |
|     String? email,
 | |
|     String? phoneNumber,
 | |
|   }) {
 | |
|     return UserModel(
 | |
|       uid: uid, // L'UID ne change pas
 | |
|       firstName: firstName ?? this.firstName,
 | |
|       lastName: lastName ?? this.lastName,
 | |
|       role: role ?? this.role,
 | |
|       profilePhotoUrl: profilePhotoUrl ?? this.profilePhotoUrl,
 | |
|       email: email ?? this.email,
 | |
|       phoneNumber: phoneNumber ?? this.phoneNumber,
 | |
|     );
 | |
|   }
 | |
| }
 |