TKK_Nisa_E32221364/lib/models/user_model.dart

78 lines
1.9 KiB
Dart

import 'package:cloud_firestore/cloud_firestore.dart';
class UserModel {
final String uid;
final String email;
final String displayName;
final String photoURL;
final String role;
final bool isActive;
final DateTime? createdAt;
final DateTime? updatedAt;
UserModel({
required this.uid,
required this.email,
required this.displayName,
required this.photoURL,
required this.role,
required this.isActive,
this.createdAt,
this.updatedAt,
});
factory UserModel.fromFirestore(DocumentSnapshot doc) {
Map<String, dynamic> data = doc.data() as Map<String, dynamic>;
return UserModel(
uid: doc.id,
email: data['email'] ?? '',
displayName: data['displayName'] ?? '',
photoURL: data['photoURL'] ?? '',
role: data['role'] ?? 'user',
isActive: data['isActive'] ?? true,
createdAt: data['createdAt']?.toDate(),
updatedAt: data['updatedAt']?.toDate(),
);
}
Map<String, dynamic> toFirestore() {
return {
'email': email,
'displayName': displayName,
'photoURL': photoURL,
'role': role,
'isActive': isActive,
'createdAt':
createdAt != null
? Timestamp.fromDate(createdAt!)
: FieldValue.serverTimestamp(),
'updatedAt':
updatedAt != null
? Timestamp.fromDate(updatedAt!)
: FieldValue.serverTimestamp(),
};
}
UserModel copyWith({
String? uid,
String? email,
String? displayName,
String? photoURL,
String? role,
bool? isActive,
DateTime? createdAt,
DateTime? updatedAt,
}) {
return UserModel(
uid: uid ?? this.uid,
email: email ?? this.email,
displayName: displayName ?? this.displayName,
photoURL: photoURL ?? this.photoURL,
role: role ?? this.role,
isActive: isActive ?? this.isActive,
createdAt: createdAt ?? this.createdAt,
updatedAt: updatedAt ?? this.updatedAt,
);
}
}