2024-05-19 17:01:06 +02:00
|
|
|
import 'package:cloud_firestore/cloud_firestore.dart';
|
|
|
|
import 'package:firebase_auth/firebase_auth.dart';
|
|
|
|
import '../constants.dart';
|
|
|
|
import '../enumerations.dart';
|
2024-05-30 01:10:53 +02:00
|
|
|
import '../models/language.dart';
|
|
|
|
import '../models/location.dart';
|
|
|
|
import '../models/user_profile.dart';
|
2024-05-19 17:01:06 +02:00
|
|
|
|
|
|
|
class UserService {
|
2024-05-30 01:10:53 +02:00
|
|
|
UserService._(); // Private constructor to prevent instantiation
|
2024-05-19 17:01:06 +02:00
|
|
|
|
2024-06-02 16:32:19 +02:00
|
|
|
static Future<void> saveUserRegistrationData(UserCredential userCredential,
|
|
|
|
String email, String firstname, String lastname) async {
|
2024-05-19 17:01:06 +02:00
|
|
|
// create full name
|
|
|
|
String fullName = (firstname.isNotEmpty && lastname.isNotEmpty)
|
|
|
|
? '$firstname $lastname'
|
|
|
|
: (firstname.isNotEmpty
|
|
|
|
? firstname
|
|
|
|
: (lastname.isNotEmpty ? lastname : ''));
|
|
|
|
|
|
|
|
// save user info to users document
|
2024-05-30 01:10:53 +02:00
|
|
|
await FirebaseFirestore.instance
|
2024-05-19 17:01:06 +02:00
|
|
|
.collection(Constants.dbCollectionUsers)
|
|
|
|
.doc(userCredential.user!.uid)
|
|
|
|
.set(
|
|
|
|
{
|
2024-05-24 00:30:08 +02:00
|
|
|
Constants.dbFieldUsersID: userCredential.user!.uid,
|
|
|
|
Constants.dbFieldUsersEmail: email,
|
|
|
|
Constants.dbFieldUsersFirstName: firstname,
|
|
|
|
Constants.dbFieldUsersLastName: lastname,
|
|
|
|
Constants.dbFieldUsersName: fullName,
|
2024-05-19 17:01:06 +02:00
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-05-30 01:10:53 +02:00
|
|
|
static Future<List<SkillOption>> getSkillsFromFirebase(
|
2024-05-19 17:01:06 +02:00
|
|
|
bool skillsSought, String userId) async {
|
|
|
|
// Fetch skills from Firestore
|
2024-05-30 01:10:53 +02:00
|
|
|
DocumentSnapshot userDoc = await FirebaseFirestore.instance
|
2024-05-19 17:01:06 +02:00
|
|
|
.collection(Constants.dbCollectionUsers)
|
|
|
|
.doc(userId)
|
|
|
|
.get();
|
|
|
|
|
|
|
|
if (userDoc.exists && userDoc.data() != null) {
|
2024-05-30 16:37:34 +02:00
|
|
|
Map<String, dynamic> userData = userDoc.data()! as Map<String, dynamic>;
|
2024-05-19 17:01:06 +02:00
|
|
|
|
|
|
|
List<dynamic>? skills;
|
|
|
|
if (skillsSought) {
|
|
|
|
skills = userData[Constants.dbFieldUsersSkillsSought];
|
|
|
|
} else {
|
2024-05-30 16:37:34 +02:00
|
|
|
skills = userData[Constants.dbFieldUsersSkills];
|
2024-05-19 17:01:06 +02:00
|
|
|
}
|
|
|
|
|
2024-05-31 18:31:37 +02:00
|
|
|
return convertSkillStringToEnum(skills);
|
2024-05-19 17:01:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2024-05-31 18:31:37 +02:00
|
|
|
static List<SkillOption> convertSkillStringToEnum(List<dynamic>? skills) {
|
|
|
|
if (skills != null && skills.isNotEmpty) {
|
|
|
|
// Convert skills from strings to enum values
|
|
|
|
List<SkillOption> userSkills = skills
|
|
|
|
.map((skill) => SkillOption.values
|
|
|
|
.firstWhere((x) => x.toString() == 'SkillOption.$skill'))
|
|
|
|
.toList();
|
|
|
|
return userSkills;
|
|
|
|
}
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2024-05-31 23:16:15 +02:00
|
|
|
static List<VisionOption> convertVisionStringToEnum(List<dynamic>? visions) {
|
|
|
|
if (visions != null && visions.isNotEmpty) {
|
|
|
|
List<VisionOption> userVisions = visions
|
|
|
|
.map((vision) =>
|
|
|
|
VisionOption.values.firstWhere((x) => x.toString() == vision))
|
|
|
|
.toList();
|
|
|
|
return userVisions;
|
|
|
|
}
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
static List<WorkValueOption> convertWorkValuesStringToEnum(
|
|
|
|
List<dynamic>? values) {
|
|
|
|
if (values != null && values.isNotEmpty) {
|
|
|
|
List<WorkValueOption> userWorkValues = values
|
|
|
|
.map((workValue) => WorkValueOption.values
|
|
|
|
.firstWhere((x) => x.toString() == workValue))
|
|
|
|
.toList();
|
|
|
|
return userWorkValues;
|
|
|
|
}
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2024-05-30 01:10:53 +02:00
|
|
|
static Future<bool> saveSkillsToFirebase(List<SkillOption> selectedOptions,
|
2024-05-19 17:01:06 +02:00
|
|
|
bool skillsSought, String userId) async {
|
|
|
|
try {
|
|
|
|
// Convert enum values to strings, removing leading EnumType with split
|
|
|
|
List<String> skills = selectedOptions
|
|
|
|
.map((option) => option.toString().split('.').last)
|
|
|
|
.toList();
|
|
|
|
|
|
|
|
// Update the corresponding 'skills' field in the user's document
|
|
|
|
String keyToUpdate = skillsSought
|
|
|
|
? Constants.dbFieldUsersSkillsSought
|
|
|
|
: Constants.dbFieldUsersSkills;
|
|
|
|
|
2024-05-30 01:10:53 +02:00
|
|
|
FirebaseFirestore.instance
|
2024-05-19 17:01:06 +02:00
|
|
|
.collection(Constants.dbCollectionUsers)
|
|
|
|
.doc(userId)
|
|
|
|
.update({keyToUpdate: skills});
|
|
|
|
|
|
|
|
return true;
|
|
|
|
} catch (e) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2024-05-30 01:10:53 +02:00
|
|
|
|
|
|
|
/// Get UserProfile for given [userId]
|
|
|
|
static Future<UserProfile> getUserProfileById(String userId) async {
|
|
|
|
FirebaseFirestore firestore = FirebaseFirestore.instance;
|
|
|
|
|
|
|
|
DocumentSnapshot userDoc = await firestore
|
|
|
|
.collection(Constants.dbCollectionUsers)
|
|
|
|
.doc(userId)
|
|
|
|
.get();
|
|
|
|
|
|
|
|
QuerySnapshot languagesSnapshot = await firestore
|
|
|
|
.collection(Constants.dbCollectionUsers)
|
|
|
|
.doc(userId)
|
|
|
|
.collection(Constants.dbCollectionLanguages)
|
|
|
|
.get();
|
|
|
|
List<Language> languages = languagesSnapshot.docs
|
|
|
|
.map((doc) => Language.fromDocument(doc))
|
|
|
|
.toList();
|
|
|
|
|
|
|
|
QuerySnapshot locationsSnapshot = await firestore
|
|
|
|
.collection(Constants.dbCollectionUsers)
|
|
|
|
.doc(userId)
|
|
|
|
.collection(Constants.dbCollectionLocations)
|
|
|
|
.get();
|
|
|
|
Map<String, MyLocation?> locations = {
|
|
|
|
for (var doc in locationsSnapshot.docs)
|
|
|
|
doc.id: MyLocation.fromDocument(doc)
|
|
|
|
};
|
|
|
|
|
|
|
|
// Fill UserProfile including its sub collections
|
|
|
|
UserProfile userProfile = UserProfile.fromDocument(userDoc);
|
|
|
|
userProfile.languages.addAll(languages);
|
|
|
|
userProfile.locations.addAll(locations);
|
|
|
|
|
|
|
|
return userProfile;
|
|
|
|
}
|
2024-06-02 16:32:19 +02:00
|
|
|
|
|
|
|
// get users stream
|
|
|
|
static Stream<List<Map<String, dynamic>>> getUsersStream() {
|
|
|
|
return FirebaseFirestore.instance
|
|
|
|
.collection(Constants.dbCollectionUsers)
|
|
|
|
.snapshots()
|
|
|
|
.map((snapshot) {
|
|
|
|
return snapshot.docs.map((doc) {
|
|
|
|
// iterate each user
|
|
|
|
final user = doc.data();
|
|
|
|
|
|
|
|
//return user
|
|
|
|
return user;
|
|
|
|
}).toList();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get list of matched user ids for a given user
|
|
|
|
static Future<List<String>> getMatchedUserIds(String userId) async {
|
|
|
|
List<String> matchedUserIds = [];
|
|
|
|
|
|
|
|
final snapshot = await FirebaseFirestore.instance
|
|
|
|
.collection(Constants.dbCollectionUsers)
|
|
|
|
.doc(userId)
|
|
|
|
.collection(Constants.dbCollectionMatches)
|
|
|
|
.get();
|
|
|
|
|
|
|
|
for (var doc in snapshot.docs) {
|
|
|
|
final data = doc.data();
|
|
|
|
final otherUserId = data['otherUserId'] as String?;
|
|
|
|
if (otherUserId != null && otherUserId.isNotEmpty) {
|
|
|
|
matchedUserIds.add(otherUserId);
|
|
|
|
}
|
|
|
|
// assuming the match document ID is the matched user's ID
|
|
|
|
// it would just be an one liner:
|
|
|
|
// matchedUserIds.add(doc.id);
|
|
|
|
}
|
|
|
|
|
|
|
|
return matchedUserIds;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get matched users data for a given user as stream
|
|
|
|
static Stream<List<Map<String, dynamic>>> getMatchedUsersStream(
|
|
|
|
String userId) {
|
|
|
|
return FirebaseFirestore.instance
|
|
|
|
.collection(Constants.dbCollectionUsers)
|
|
|
|
.doc(userId)
|
|
|
|
.collection(Constants.dbCollectionMatches)
|
|
|
|
.snapshots()
|
|
|
|
.asyncMap((snapshot) async {
|
|
|
|
final matchedUserIds = snapshot.docs
|
|
|
|
.map((doc) => doc.data()['otherUserId'] as String?)
|
|
|
|
.where((otherUserId) => otherUserId != null && otherUserId.isNotEmpty)
|
|
|
|
.toList();
|
|
|
|
|
|
|
|
if (matchedUserIds.isEmpty) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
final matchedUsersSnapshot = await FirebaseFirestore.instance
|
|
|
|
.collection(Constants.dbCollectionUsers)
|
|
|
|
.where(FieldPath.documentId, whereIn: matchedUserIds)
|
|
|
|
.get();
|
|
|
|
|
|
|
|
return matchedUsersSnapshot.docs.map((doc) => doc.data()).toList();
|
|
|
|
});
|
|
|
|
}
|
2024-05-19 17:01:06 +02:00
|
|
|
}
|