challenge_location_service.dart 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import 'package:cloud_firestore/cloud_firestore.dart';
  2. import 'package:physigo/friends/models/challenge_location_request.dart';
  3. const userId = "tlmysIvwTBaoZKWqBofx";
  4. class ChallengeLocationService {
  5. static final _db = FirebaseFirestore.instance;
  6. static final _users = _db.collection("Users");
  7. static final _shareLocationRequests = _db.collection("ShareLocationRequests");
  8. static final _friends = _db.collection("Friends");
  9. static Future<List<ChallengeLocationRequest>> getShareChallengeLocationRequests() async {
  10. final requests = await _shareLocationRequests.where("user_id", isEqualTo: userId).get();
  11. if (requests.docs.isEmpty) {
  12. return [];
  13. }
  14. final requesterIdLocationId = {
  15. for (final request in requests.docs)
  16. request.data()['requester_id']: {"requestId": request.id, "locationRef": request.data()['location']}
  17. };
  18. final requesters = await _users.where(FieldPath.documentId, whereIn: requesterIdLocationId.keys.toList()).get();
  19. final shareChallengeLocationRequests = await Future.wait(requesters.docs.map((requester) async {
  20. final data = requester.data();
  21. final place = (await requesterIdLocationId[requester.id]!["locationRef"].get()).data();
  22. return ChallengeLocationRequest(
  23. id: requesterIdLocationId[requester.id]!["requestId"]!,
  24. name: data['name'],
  25. surname: data['surname'],
  26. locationName: place['name'],
  27. locationRef: requesterIdLocationId[requester.id]!["locationRef"]!);
  28. }));
  29. return shareChallengeLocationRequests;
  30. }
  31. static Future<void> shareChallengeLocation(String friendId) async {
  32. await Future.wait([_checkExistingFriendRelation(friendId), _checkExistingChallengeLocationRequest(friendId)]);
  33. final user = await _users.doc(userId).get();
  34. final shareLocationRequest = {"user_id": friendId, "requester_id": userId, "location": user['weekly_place']};
  35. await _shareLocationRequests.add(shareLocationRequest);
  36. }
  37. static Future<void> acceptChallengeLocationRequest(ChallengeLocationRequest request) async {
  38. final newLocation = {"weekly_place": request.locationRef};
  39. await _users.doc(userId).update(newLocation);
  40. await _shareLocationRequests.doc(request.id).delete();
  41. }
  42. static Future<void> refuseChallengeLocationRequest(ChallengeLocationRequest request) async {
  43. await _shareLocationRequests.doc(request.id).delete();
  44. }
  45. static Future<void> _checkExistingFriendRelation(String friendId) async {
  46. if (friendId == userId) {
  47. return Future.error("You cannot share a location with yourself");
  48. }
  49. var friendRelationByUserId = _friends
  50. .where("user_id", isEqualTo: userId)
  51. .where("friend_id", isEqualTo: friendId)
  52. .where("request_status", isEqualTo: "accepted")
  53. .get();
  54. var friendRelationByFriendId = _friends
  55. .where("friend_id", isEqualTo: userId)
  56. .where("user_id", isEqualTo: friendId)
  57. .where("request_status", isEqualTo: "accepted")
  58. .get();
  59. final relations = await Future.wait([friendRelationByUserId, friendRelationByFriendId]);
  60. if (relations.expand((relation) => relation.docs).isEmpty) {
  61. return Future.error("You must be friend to share a location");
  62. }
  63. }
  64. static Future<void> _checkExistingChallengeLocationRequest(String friendId) async {
  65. final requests = await _shareLocationRequests
  66. .where("requester_id", isEqualTo: userId)
  67. .where("user_id", isEqualTo: friendId)
  68. .get();
  69. if (requests.docs.isNotEmpty) {
  70. return Future.error("You already shared your location with this person");
  71. }
  72. }
  73. }