challenge_location_service.dart 3.7 KB

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