Skip to main content

Pseudocode

I. API Endpoint /assign_badges/

Method : POST

Parameters : user_id, workout_log_id, request_type

Usage request_type = 'workout': - When user clicks Start Workout Button, We need to make a entry in workout_log table with status started (Required Development).

Usage request_type = 'signup': - When new user signup in the app for the first time we send the API request signup.

Usage request_type = 'meal': - When new user personalize thier meal plan.

Usage request_type = 'exercise': - When new user completes certain exercise.

Usage request_type = 'game': - When new user plays certain games.

1. For request_type = 'workout'

In Background it starts to process the below processes,

1.1. Process 1: Workout Validation

  1. Retrieve Workout Time:
    • Query A: Retrieve the workout_time from the exercises table.
    • Query B: Retrieve the user_workout_time from the users_workout_log table.
    • Validation: Compare the results from Query A and Query B to ensure that workout_time (from exercises) matches user_workout_time (from users_workout_log). This ensures that the total workout time recorded matches the user's recorded progress.

1.2. Process 2: Badge Assignment Checks

If Process 1 is validated as true, proceed with the following checks:

1.2.1. Condition Check 1:

  • Requirement: Ensure the user has completed 3 or fewer workouts.

  • Actions:

    • Method get_completed_workout_count() - Retrieves the users_workout_log table to count the number of workout completions for the user. Verify that this count is less than or equal to 3.
    • Method get_today_completed_workouts() - Retrieves the users_workout_log table to count the number of workout completions for the user in today.
    • Method get_today_workout_time() - Retrieves the total workout duration logged by the user for the current day from the users_workout_log table.
    • Method get_yesterday_workout_time() - Retrieves the total workout duration logged by the user for the previous day from the users_workout_log table.
    • Method badge_already_assigned() - Checks wheather the requested badge already assigned for the user.
  • 1.2.1.1. Loop Checks for Badge Assignment:

    • Loop 1: If the count of completed exercises is 1 i.e get_completed_workout_count = 1 and not badge_already_assigned:
      • Check if the C-CMB-01-003 badge is not assigned previously.
      • If not assigned the badge, Reward the badge C-CMB-01-003 and end the loop, the call the function create_badge_assignment().
    • Loop 2: If the count of completed exercises is 2 i.e. get_completed_workout_count = 2:
      • Condition A: Verify that the Today Completed workout = 1 and badge C-CMB-02-004 is not assigned previously i.e today_completed_workouts == 1 and not badge_already_assigned.
      • If not assigned the badge, Reward Badge C-CMB-02-004 and end the loop, the call the function create_badge_assignment().
      • Condition B: Verify that the Today Completed workout = 2 and badge C-CMB-03-005 is not assigned previously i.e today_completed_workouts == 2 and not badge_already_assigned.
      • If not assigned the badge, Reward Badge C-CMB-03-005 and end the loop, the call the function create_badge_assignment().
      • Condition C: Check if the user's workout time today is greater than yesterday's workout time and the badge C-MAB-04-006 not been assigned before i.e today_workout_time > yesterday_workout_time and not badge_already_assigned.
      • If not assigned the badge, Reward the badge C-MAB-04-006 and end the loop, the call the function create_badge_assignment().
    • Loop 3: If the count of completed exercises is 3 i.e. get_completed_workout_count = 3:
      • Condition A: Verify that the Today Completed workout = 3 and badge C-CMB-05-007 is not assigned previously i.e today_completed_workouts == 3 and not badge_already_assigned.
      • If not assigned the badge, Reward the badge C-CMB-05-007 and end the loop, the call the function create_badge_assignment().
      • Condition B: Check if the user's workout time today is greater than yesterday's workout time and the badge C-MAB-04-006 not been assigned before i.e today_workout_time > yesterday_workout_time and not badge_already_assigned.
      • If not assigned the badge, Reward the badge C-MAB-04-006 and end the loop, the call the function create_badge_assignment().

1.2.2. Condition Check 2:

  • Requirement: Ensure the user has completed at least 7 workouts and has completed at least one program.

  • Action:

    • Method get_completed_workout_count() - Retrieves the users_workout_log table to count the number of workout completions for the user. Verify that this count is greater than or equal to 7.
    • Method get_programs_completed_count() - Checks the exercise_workouts table to check if at least one program has a status of "completed" for the respective user.
    • Method calculate_workout_completion_percentage() - Calculates the total completed workout of respective program and return its percentage
    • Method meal_completed() - Checks table user_meal_log and returns that user meal completion status.
    • Method are_workouts_at_same_time - Checks that All workout completed in same time with maximum 30 mins - 1 hr differences
  • 1.2.2.1. Loop Checks for Badge Assignment:

    • Loop 1: If the percentage of completed workout is 80% (Eg. 8/10 completed workouts) i.e percentage_completed == 80 and not badge_already_assigned:
      • Check if the E-MB-01-012 badge is not assigned previously.
      • If not assigned the badge, Reward the badge E-MB-01-012 and end the loop, the call the function create_badge_assignment().
    • Loop 2: If the percentage of completed workout is 90% (i.e 9/10 completed workouts) i.e percentage_completed == 90:
      • Condition A: Verify that the 90% completition of workout and Completition of meal and badge F-MAB-01 is not assigned previously i.e meal_completed and not badge_already_assigned.
      • If not assigned the badge, Reward Badge F-MAB-01 and end the loop, the call the function create_badge_assignment().
      • Condition B: Verify that the 90% completition of workout and All workout completed in same time with maximum 30 mins - 1 hr differences and badge E-MEB-02-013 is not assigned previously i.e same_time_workouts and not badge_already_assigned.
      • If not assigned the badge, Reward Badge E-MEB-02-013 and end the loop, the call the function create_badge_assignment().
      • Condition C: Verify that the 90% completition of workout and badge E-MEB-03-014 is not assigned previously i.e not badge_already_assigned.
      • If not assigned the badge, Reward Badge E-MEB-03-014 and end the loop, the call the function create_badge_assignment().
    • Loop 3: If the percentage of completed workout is 100% (i.e 10/10 completed workouts) i.e percentage_completed == 100:
      • Condition A: Verify that the 100% completition of workout and Completition of meal and badge F-MAB-02 is not assigned previously i.e meal_completed and not badge_already_assigned.
      • If not assigned the badge, Reward Badge F-MAB-02 and end the loop, the call the function create_badge_assignment().
      • Condition B: Verify that the 100% completition of workout and All workout completed in same time with maximum 30 mins - 1 hr differences and badge E-EHB-05-016 is not assigned previously i.e same_time_workouts and not badge_already_assigned.
      • If not assigned the badge, Reward Badge E-EHB-05-016 and end the loop, the call the function create_badge_assignment().
      • Condition C: Verify that the 100% completition of workout and badge E-SB-04-015 is not assigned previously i.e not badge_already_assigned.
      • If not assigned the badge, Reward Badge E-SB-04-015 and end the loop, the call the function create_badge_assignment().

1.2.3. Condition Check 3:

  • Requirement: Ensure the user has completed at least 14 workouts and has completed at least two programs.

  • Action:

    • Method get_completed_workout_count() - Retrieves the users_workout_log table to count the number of workout completions for the user. Verify that this count is greater than or equal to 14.
    • Method get_programs_completed_count() - Checks the exercise_workouts table to check if at least two program has a status of "completed" for the respective user.
    • Method calculate_workout_completion_percentage() - Calculates the total completed workout of respective program and return its percentage.
    • Method are_workouts_at_same_time - Checks that All workout completed in same time with maximum 30 mins - 1 hr differences
  • 1.2.3.1. Loop Checks for Badge Assignment:

    • Loop 1: If the percentage of completed workout is 100% (Eg. 10/10 completed workouts):
      • Condition A: Verify that the 100% completition of workout and All workout completed in same time with maximum 30 mins - 1 hr differences and badge H-EHB-01-021 is not assigned previously i.e percentage_completed == 100 and same_time_workouts and not badge_already_assigned.
      • If not assigned the badge, Reward Badge H-EHB-01-021 and end the loop, the call the function create_badge_assignment().
      • Condition B: Verify that the 100% completition of workout and badge H-EHB-02-022 is not assigned previously i.e not badge_already_assigned.
      • If not assigned the badge, Reward Badge H-EHB-02-022 and end the loop, the call the function create_badge_assignment().

1.2.4. Condition Check 4:

  • Requirement: Ensure the user has completed more than 21 workouts and has completed at least three programs.

  • Action:

    • Method get_completed_workout_count() - Retrieves the users_workout_log table to count the number of workout completions for the user. Verify that this count is greater than or equal to 21.
    • Method get_programs_completed_count() - Checks the exercise_workouts table to check if at least three program has a status of "completed" for the respective user.
    • Method meal_completed() - Checks table user_meal_log and returns that user meal completion status.
    • Method get_consecutive_workout_days() - Checks table users_workout_log and returns total consecutive days.
  • 1.2.4.1. Loop Checks for Badge Assignment:

    • Loop 1: If the workout was completed in consecutive days:
      • Condition A: Verify that the user has 21 Consecutive Days of workout completion and badge I-SB-01-023 is not assigned previously i.e. consecutive_days >= 21 and not badge_already_assigned.
      • If not assigned the badge, Reward Badge I-SB-01-023 and end the loop, the call the function create_badge_assignment().
      • Condition B: Verify that the user has 30 Consecutive Days of workout completion and badge I-SB-02-024 is not assigned previously i.e. consecutive_days >= 30 and not badge_already_assigned.
      • If not assigned the badge, Reward Badge I-SB-02-024 and end the loop, the call the function create_badge_assignment().
      • Condition C: Verify that the user has 90 Consecutive Days of workout completion and badge I-SB-03-025 is not assigned previously i.e. consecutive_days >= 90 and not badge_already_assigned.
      • If not assigned the badge, Reward Badge I-SB-03-025 and end the loop, the call the function create_badge_assignment().
      • Condition D: Verify that the user has 180 Consecutive Days of workout completion and badge I-SB-04-026 is not assigned previously i.e. consecutive_days >= 180 and not badge_already_assigned.
      • If not assigned the badge, Reward Badge I-SB-04-026 and end the loop, the call the function create_badge_assignment().
      • Condition E: Verify that the user has 180 Consecutive Days of workout completion and User completes his meals and badge I-SB-05-027 is not assigned previously i.e. consecutive_days >= 21 and meal_completed_not badge_already_assigned.
      • If not assigned the badge, Reward Badge I-SB-05-027 and end the loop, the call the function create_badge_assignment().

Method create_badge_assignment()

  • Create a queue in RabbitMQ and send the message 'badge_queue_{user_id}_{workoutlog_id}'
  • consumer.py listens the message and once message received, call the reward_badge()
  • reward_badge() - Method create a badge for user in user_badges with status initated

2. For request_type = 'signup'

Conditions of assigning badge

Actions :

  • Method badge_already_assigned() - Checks wheather the requested badge already assigned for the user.
  • Method is_new_user() - Check user has entry in auth_users table, returns value if new user.
  • Method reward_badge() - To create a badge for user in user_badges

Condition :

  • When is_new_user() and not badge_already_assigned()
  • Reward Badge A-CM-01-001 and end the loop and return the badges

3. For request_type = 'meal'

Conditions of assigning badge

Actions :

  • Method badge_already_assigned() - Checks wheather the requested badge already assigned for the user.
  • Method meal_personalized_with_nutrition() - Returns True, When user personalize a meal the we check table user_meal_log that their request contains more nutrition preferences.
  • Method meal_personalized_twice() - Returns True, When user personalize a meal more than twice in a day.

Condition :

  • Loop Checks for Badge Assignment:
    • Loop 1: If the user personalize a meal:
      • Condition A: Verify that the user personalize their meal with high nutrition preferences i.e. meal_personalized_with_nutrition() and not badge_already_assigned()

      • If not assigned the badge, Reward Badge G-NCB-01-019 and end the loop, the call the function reward_badge().

      • Condition B: Verify that the user personalize their meal with more than twice in a day i.e. meal_personalized_twice() and not badge_already_assigned()

      • If not assigned the badge, Reward Badge G-NCB-02-020 and end the loop, the call the function reward_badge().

4. For request_type = 'game'

Conditions of assigning badge

Actions :

  • Method badge_already_assigned() - Checks wheather the requested badge already assigned for the user.

Condition :

  • Condition A: Check If the user completes 2048 game and badge not assigned, Reward Badge D-NCB-03-010 and the call the function reward_badge().
  • Condition B: Check If the user completes Hangman game and badge not assigned, Reward Badge D-NCB-04-011 and the call the function reward_badge().

II. API Endpoint /validate_badge_after_workout_completed/

Method : POST

Parameters : user_id, workout_log_id, user_workout_time

Usage : When user clicks Completed Workout Button, then call the API /validate_badge_after_workout_completed/

Step 1:

Update the workout_log table by setting the user_workout_time and changing the status from initiated to completed.

Step 2:

Invoke the validate_workout_time() method to verify that the user_workout_time matches the exercise_time, confirming the validity of the workout.

Step 3:

If the workout is valid, check the user_badges table for any badges associated with the user_id and workout_log_id. If a badge is found, return it in the API response. If no badge is found, delete the corresponding entry in the user_badges table.