@@ -239,7 +239,7 @@ def resolve_gym(self, info):
239
239
query = Gym .get_query (info ).filter (GymModel .id == self .gym_id ).first ()
240
240
return query
241
241
242
-
242
+
243
243
# MARK: - Capacity Reminder
244
244
245
245
@@ -261,10 +261,14 @@ class Query(graphene.ObjectType):
261
261
get_workouts_by_id = graphene .List (Workout , id = graphene .Int (), description = "Get all of a user's workouts by ID." )
262
262
activities = graphene .List (Activity )
263
263
get_all_reports = graphene .List (Report , description = "Get all reports." )
264
- get_workout_goals = graphene .List (graphene .String , id = graphene .Int (required = True ),
265
- description = "Get the workout goals of a user by ID." )
266
- get_user_streak = graphene .Field (graphene .JSONString , id = graphene .Int (
267
- required = True ), description = "Get the current and max workout streak of a user." )
264
+ get_workout_goals = graphene .List (
265
+ graphene .String , id = graphene .Int (required = True ), description = "Get the workout goals of a user by ID."
266
+ )
267
+ get_user_streak = graphene .Field (
268
+ graphene .JSONString ,
269
+ id = graphene .Int (required = True ),
270
+ description = "Get the current and max workout streak of a user." ,
271
+ )
268
272
get_hourly_average_capacities_by_facility_id = graphene .List (
269
273
HourlyAverageCapacity , facility_id = graphene .Int (), description = "Get all facility hourly average capacities."
270
274
)
@@ -371,7 +375,6 @@ def resolve_get_user_streak(self, info, id):
371
375
372
376
return {"active_streak" : active_streak , "max_streak" : max_streak }
373
377
374
-
375
378
def resolve_get_hourly_average_capacities_by_facility_id (self , info , facility_id ):
376
379
valid_facility_ids = [14492437 , 8500985 , 7169406 , 10055021 , 2323580 , 16099753 , 15446768 , 12572681 ]
377
380
if facility_id not in valid_facility_ids :
@@ -452,10 +455,7 @@ def mutate(self, info, name, net_id, email, encoded_image=None):
452
455
453
456
if encoded_image :
454
457
upload_url = os .getenv ("DIGITAL_OCEAN_URL" )
455
- payload = {
456
- "bucket" : os .getenv ("BUCKET_NAME" ),
457
- "image" : encoded_image # Base64-encoded image string
458
- }
458
+ payload = {"bucket" : os .getenv ("BUCKET_NAME" ), "image" : encoded_image } # Base64-encoded image string
459
459
headers = {"Content-Type" : "application/json" }
460
460
try :
461
461
response = requests .post (upload_url , json = payload , headers = headers )
@@ -473,7 +473,8 @@ def mutate(self, info, name, net_id, email, encoded_image=None):
473
473
db_session .commit ()
474
474
475
475
return new_user
476
-
476
+
477
+
477
478
class EditUser (graphene .Mutation ):
478
479
class Arguments :
479
480
name = graphene .String (required = False )
@@ -487,7 +488,7 @@ def mutate(self, info, net_id, name=None, email=None, encoded_image=None):
487
488
existing_user = db_session .query (UserModel ).filter (UserModel .net_id == net_id ).first ()
488
489
if not existing_user :
489
490
raise GraphQLError ("User with given net id does not exist." )
490
-
491
+
491
492
if name is not None :
492
493
existing_user .name = name
493
494
if email is not None :
@@ -499,12 +500,12 @@ def mutate(self, info, net_id, name=None, email=None, encoded_image=None):
499
500
500
501
payload = {
501
502
"bucket" : os .getenv ("BUCKET_NAME" , "DEV_BUCKET" ),
502
- "image" : encoded_image # Base64-encoded image string
503
+ "image" : encoded_image , # Base64-encoded image string
503
504
}
504
505
headers = {"Content-Type" : "application/json" }
505
-
506
+
506
507
print (f"Uploading image with payload: { payload } " )
507
-
508
+
508
509
try :
509
510
response = requests .post (upload_url , json = payload , headers = headers )
510
511
response .raise_for_status ()
@@ -521,6 +522,7 @@ def mutate(self, info, net_id, name=None, email=None, encoded_image=None):
521
522
db_session .commit ()
522
523
return existing_user
523
524
525
+
524
526
class EnterGiveaway (graphene .Mutation ):
525
527
class Arguments :
526
528
user_net_id = graphene .String (required = True )
@@ -723,39 +725,61 @@ def mutate(self, info, fcm_token, days_of_week, gyms, capacity_percent):
723
725
return reminder
724
726
725
727
726
- class ToggleCapacityReminder (graphene .Mutation ):
728
+ class EditCapacityReminder (graphene .Mutation ):
727
729
class Arguments :
728
730
reminder_id = graphene .Int (required = True )
731
+ gyms = graphene .List (graphene .String , required = True )
732
+ days_of_week = graphene .List (graphene .String , required = True )
733
+ capacity_percent = graphene .Int (required = True )
729
734
730
735
Output = CapacityReminder
731
736
732
- def mutate (self , info , reminder_id ):
737
+ def mutate (self , info , reminder_id , gyms , days_of_week , capacity_percent ):
733
738
reminder = db_session .query (CapacityReminderModel ).filter_by (id = reminder_id ).first ()
734
739
if not reminder :
735
740
raise GraphQLError ("CapacityReminder not found." )
736
741
742
+ # Validate days of the week
743
+ validated_workout_days = []
744
+ for day in days_of_week :
745
+ try :
746
+ validated_workout_days .append (DayOfWeekGraphQLEnum [day .upper ()].value )
747
+ except KeyError :
748
+ raise GraphQLError (f"Invalid day of the week: { day } " )
749
+
750
+ # Validate gyms
751
+ valid_gyms = []
752
+ for gym in gyms :
753
+ try :
754
+ valid_gyms .append (CapacityReminderGymGraphQLEnum [gym ].value )
755
+ except KeyError :
756
+ raise GraphQLError (f"Invalid gym: { gym } " )
757
+
758
+ # Unsubscribe from old reminders
737
759
topics = [
738
760
f"{ gym } _{ day } _{ reminder .capacity_threshold } " for gym in reminder .gyms for day in reminder .days_of_week
739
761
]
740
762
741
- if reminder .is_active :
742
- # Toggle to inactive and unsubscribe
743
- for topic in topics :
744
- try :
745
- messaging .unsubscribe_from_topic (reminder .fcm_token , topic )
746
- except Exception as error :
747
- raise GraphQLError (f"Error unsubscribing from topic: { error } " )
748
- else :
749
- # Toggle to active and resubscribe
750
- for topic in topics :
751
- try :
752
- messaging .subscribe_to_topic (reminder .fcm_token , topic )
753
- except Exception as error :
754
- raise GraphQLError (f"Error subscribing to topic: { error } " )
763
+ for topic in topics :
764
+ try :
765
+ messaging .unsubscribe_from_topic (reminder .fcm_token , topic )
766
+ except Exception as error :
767
+ raise GraphQLError (f"Error subscribing to topic: { error } " )
755
768
756
- reminder . is_active = not reminder . is_active
757
- db_session . commit ()
769
+ # Subscribe to new reminders
770
+ topics = [ f" { gym } _ { day } _ { reminder . capacity_threshold } " for gym in valid_gyms for day in validated_workout_days ]
758
771
772
+ for topic in topics :
773
+ try :
774
+ messaging .subscribe_to_topic (reminder .fcm_token , topic )
775
+ except Exception as error :
776
+ raise GraphQLError (f"Error subscribing to topic: { error } " )
777
+
778
+ reminder .gyms = valid_gyms
779
+ reminder .days_of_week = validated_workout_days
780
+ reminder .capacity_threshold = capacity_percent
781
+
782
+ db_session .commit ()
759
783
return reminder
760
784
761
785
@@ -799,7 +823,7 @@ class Mutation(graphene.ObjectType):
799
823
create_report = CreateReport .Field (description = "Creates a new report." )
800
824
delete_user = DeleteUserById .Field (description = "Deletes a user by ID." )
801
825
create_capacity_reminder = CreateCapacityReminder .Field (description = "Create a new capacity reminder." )
802
- toggle_capacity_reminder = ToggleCapacityReminder .Field (description = "Toggle a capacity reminder on or off ." )
826
+ edit_capacity_reminder = EditCapacityReminder .Field (description = "Edit capacity reminder." )
803
827
delete_capacity_reminder = DeleteCapacityReminder .Field (description = "Delete a capacity reminder" )
804
828
805
829
0 commit comments