@@ -239,7 +239,7 @@ def resolve_gym(self, info):
239239 query = Gym .get_query (info ).filter (GymModel .id == self .gym_id ).first ()
240240 return query
241241
242-
242+
243243# MARK: - Capacity Reminder
244244
245245
@@ -261,10 +261,14 @@ class Query(graphene.ObjectType):
261261 get_workouts_by_id = graphene .List (Workout , id = graphene .Int (), description = "Get all of a user's workouts by ID." )
262262 activities = graphene .List (Activity )
263263 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+ )
268272 get_hourly_average_capacities_by_facility_id = graphene .List (
269273 HourlyAverageCapacity , facility_id = graphene .Int (), description = "Get all facility hourly average capacities."
270274 )
@@ -371,7 +375,6 @@ def resolve_get_user_streak(self, info, id):
371375
372376 return {"active_streak" : active_streak , "max_streak" : max_streak }
373377
374-
375378 def resolve_get_hourly_average_capacities_by_facility_id (self , info , facility_id ):
376379 valid_facility_ids = [14492437 , 8500985 , 7169406 , 10055021 , 2323580 , 16099753 , 15446768 , 12572681 ]
377380 if facility_id not in valid_facility_ids :
@@ -452,10 +455,7 @@ def mutate(self, info, name, net_id, email, encoded_image=None):
452455
453456 if encoded_image :
454457 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
459459 headers = {"Content-Type" : "application/json" }
460460 try :
461461 response = requests .post (upload_url , json = payload , headers = headers )
@@ -473,7 +473,8 @@ def mutate(self, info, name, net_id, email, encoded_image=None):
473473 db_session .commit ()
474474
475475 return new_user
476-
476+
477+
477478class EditUser (graphene .Mutation ):
478479 class Arguments :
479480 name = graphene .String (required = False )
@@ -487,7 +488,7 @@ def mutate(self, info, net_id, name=None, email=None, encoded_image=None):
487488 existing_user = db_session .query (UserModel ).filter (UserModel .net_id == net_id ).first ()
488489 if not existing_user :
489490 raise GraphQLError ("User with given net id does not exist." )
490-
491+
491492 if name is not None :
492493 existing_user .name = name
493494 if email is not None :
@@ -499,12 +500,12 @@ def mutate(self, info, net_id, name=None, email=None, encoded_image=None):
499500
500501 payload = {
501502 "bucket" : os .getenv ("BUCKET_NAME" , "DEV_BUCKET" ),
502- "image" : encoded_image # Base64-encoded image string
503+ "image" : encoded_image , # Base64-encoded image string
503504 }
504505 headers = {"Content-Type" : "application/json" }
505-
506+
506507 print (f"Uploading image with payload: { payload } " )
507-
508+
508509 try :
509510 response = requests .post (upload_url , json = payload , headers = headers )
510511 response .raise_for_status ()
@@ -521,6 +522,7 @@ def mutate(self, info, net_id, name=None, email=None, encoded_image=None):
521522 db_session .commit ()
522523 return existing_user
523524
525+
524526class EnterGiveaway (graphene .Mutation ):
525527 class Arguments :
526528 user_net_id = graphene .String (required = True )
@@ -723,39 +725,61 @@ def mutate(self, info, fcm_token, days_of_week, gyms, capacity_percent):
723725 return reminder
724726
725727
726- class ToggleCapacityReminder (graphene .Mutation ):
728+ class EditCapacityReminder (graphene .Mutation ):
727729 class Arguments :
728730 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 )
729734
730735 Output = CapacityReminder
731736
732- def mutate (self , info , reminder_id ):
737+ def mutate (self , info , reminder_id , gyms , days_of_week , capacity_percent ):
733738 reminder = db_session .query (CapacityReminderModel ).filter_by (id = reminder_id ).first ()
734739 if not reminder :
735740 raise GraphQLError ("CapacityReminder not found." )
736741
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
737759 topics = [
738760 f"{ gym } _{ day } _{ reminder .capacity_threshold } " for gym in reminder .gyms for day in reminder .days_of_week
739761 ]
740762
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 } " )
755768
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 ]
758771
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 ()
759783 return reminder
760784
761785
@@ -799,7 +823,7 @@ class Mutation(graphene.ObjectType):
799823 create_report = CreateReport .Field (description = "Creates a new report." )
800824 delete_user = DeleteUserById .Field (description = "Deletes a user by ID." )
801825 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." )
803827 delete_capacity_reminder = DeleteCapacityReminder .Field (description = "Delete a capacity reminder" )
804828
805829
0 commit comments