Resetting users

The last feature we're going to add is the ability to reset users. Resetting a user will remove their points, send them back to the first level, and remove their badges.

Step 1: Create an "API" flow

  1. In the Explorer pane, click Create new.
  2. Select API.
  3. Type Reset user.
  4. Tap the Return key.

Step 2: Configure the "Start" component

  1. Open the Start component.
  2. From the Method dropdown, select PUT.
  3. In the URL field, enter /reset-user.
  4. Create a query string parameter called userId.

Step 3: Reset the user's points and level

  1. Add an Update component to the flow.
  2. Rename the component to updateUserDetails.
  3. From the Tables dropdown, select users.
  4. In the Row ID field, enter Start.queryString.userId.
  5. Set the points column to a value of 1.
  6. Set the level column to the ID of the first level.

Step 4: Get the user's badges

  1. Add a Select component to the flow.
  2. Rename the component to getUserBadges.
  3. From the Table dropdown, select users_badges.
  4. In the Filter tab, create a condition that checks if Field: userId is equal to Start.queryString.userId.

Step 4: Get the number of badges

  1. Add a Declare Variable component to the flow. This component creates one or more variables that can be referenced later in the flow.
  2. Create a variable named badges-count.
  3. Set the value of the variable to getUserBadges.data.length.

Step 5: Loop through the user's badges

  1. Add a Loop component to the flow. This component loops through the items in an array and, for each item, executes one or more steps of a flow.
  2. In the Items field, enter getUserBadges.data.

Step 6: For each iteration, update the number of badges

  1. Add a Set Variable component to the flow.
  2. From the Variable name dropdown, select badgesCount.
  3. In the Value field, enter badgesCount - 1.

Step 7: Delete the user's badges

  1. Add a Delete component to the flow.
  2. From the Table dropdown, select users_badges.
  3. In the Row IDs field, enter getUserBadges.data[badgesCount].Id. This expression references the ID of the current item that's being iterated through in the loop.

Step 8: Create the HTTP response

  1. Add an End component to the flow.
  2. Set the Status Code to 200 OK.
  3. From the Content Type dropdown, select application/json.
  4. In the Body field, enter {}.

This is what the complete flow looks like: