medora-provider/lib/data/services/patient_registration_service.dart
DhanshCOSQ 66c3b2fb9c Patient registration complete with authentication flow fixed (#3)
Patient registration complete with authentication flow fixed

Co-authored-by: Benoy Bose <benoybose@gmail.com>
Co-authored-by: Jipson George <152465898+Jipson-cosq@users.noreply.github.com>
Reviewed-on: cosqnet/telemednet#3
Reviewed-by: Benoy Bose <benoybose@cosq.net>
Co-authored-by: DhanshCOSQ <dhanshas@cosq.net>
Co-committed-by: DhanshCOSQ <dhanshas@cosq.net>
2024-10-31 14:20:35 +00:00

114 lines
3.2 KiB
Dart

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter_dotenv/flutter_dotenv.dart';
import 'package:telemednet/data/models/patient.dart';
import '../../controller/patient_controller.dart';
class PatientProfileService {
static final String patientProfileCollectionName =
dotenv.env['PATIENT_PROFILE_COLLECTION_NAME']!;
static final FirebaseFirestore db = FirebaseFirestore.instance;
static Future<bool> savePatientProfile(PatientController controller) async {
try {
final User? user = FirebaseAuth.instance.currentUser;
if (user == null) {
print('No user logged in');
return false;
}
final String uid = user.uid;
final PatientModel patientData = controller.model;
final Map<String, dynamic> patientJson = patientData.toJson();
patientJson['createdAt'] = FieldValue.serverTimestamp();
patientJson['updatedAt'] = FieldValue.serverTimestamp();
patientJson['uid'] = uid;
await db
.collection(patientProfileCollectionName)
.doc(uid)
.set(patientJson);
print('Patient profile saved successfully');
return true;
} catch (e) {
print('Error saving patient profile: $e');
return false;
}
}
static Future<PatientModel?> getPatientProfile() async {
try {
final User? user = FirebaseAuth.instance.currentUser;
if (user == null) {
print('No user logged in');
return null;
}
final String uid = user.uid;
final DocumentSnapshot doc =
await db.collection(patientProfileCollectionName).doc(uid).get();
if (!doc.exists) {
print('No patient profile found for this user');
return null;
}
final data = doc.data() as Map<String, dynamic>;
return PatientModel.fromJson(data);
} catch (e) {
print('Error fetching patient profile: $e');
return null;
}
}
static Future<bool> updatePatientProfile(PatientController controller) async {
try {
final User? user = FirebaseAuth.instance.currentUser;
if (user == null) {
print('No user logged in');
return false;
}
final String uid = user.uid;
final PatientModel patientData = controller.model;
final Map<String, dynamic> patientJson = patientData.toJson();
patientJson['updatedAt'] = FieldValue.serverTimestamp();
await db
.collection(patientProfileCollectionName)
.doc(uid)
.update(patientJson);
print('Patient profile updated successfully');
return true;
} catch (e) {
print('Error updating patient profile: $e');
return false;
}
}
static Future<bool> deletePatientProfile() async {
try {
final User? user = FirebaseAuth.instance.currentUser;
if (user == null) {
print('No user logged in');
return false;
}
final String uid = user.uid;
await db.collection(patientProfileCollectionName).doc(uid).delete();
print('Patient profile deleted successfully');
return true;
} catch (e) {
print('Error deleting patient profile: $e');
return false;
}
}
}