Adding points to users

A core feature of R3wards is the ability to add points to users. When a user receives a point, we need to check if they've reached a points threshold for moving up a level, and if they have, give them a badge.

Step 1: Create an "API" flow

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

Step 2: Configure the "Start" component

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

Step 3: Get the user from the database

  1. Add a Select component to the flow.
  2. Rename the component to selectUser.
  3. From the Table dropdown, select users.
  4. Click the + icon alongside the level label. This retrieves the data of the level that's associated with the user.
  5. Enable all of the fields.
  6. In the Filter tab, create a condition that checks if Field: id is equal to Start.queryString.userId.

Step 4: Check if the user exists

  1. Add an If component to the flow.
  2. Create a condition that checks if selectUser.data is not null or empty.

Step 5: If the user doesn't exist, return an error

  1. Add an End component to the flow.
  2. Connect the If component to the End component via the False branch.
  3. Set the Status Code to 404 Not Found.

Step 6: Get the user's level

  1. Add a Call Function component to the flow. This component invokes a Function flow and outputs its result.
  2. Connect the If component to the Call Function component via the True branch.
  3. Rename the component to getUserLevel.
  4. From the Subflow dropdown, select Get level. This is the flow we created in the previous part of this series. Given a certain points value, it retrieves a level from the database.
  5. In the Point field, enter parseInt(selectUser.data[0].points)+1.

Step 7: Update the user's points

  1. Add an Update component to the flow. This component finds a record by its ID and updates one or more of its fields.

  2. Rename the component to updateUserPoints.

  3. From the Table dropdown, select users.

  4. In the Row ID field, enter selectUser.data[0].Id. This expression uses an index to access the first record.

  5. Configure the component with the columns:

    ColumnValue
    pointsparseInt(selectUser.data[0].points)+1
    levelgetUserLevel.returnObject.level.levelId"]

Step 8: Check if the user's level has changed

  1. Add an If component to the flow.
  2. Create a condition that checks if selectUser.data[0].level.Id is equal to getUserLevel.returnObject.level.levelId.

Step 9: If the users level hasn't changed, return an empty object

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

Step 10: If the user's level has changed, get the user's level (again)

  1. Add a Call Function component to the flow.
  2. Connect the If component to the Call Function component via the True branch.
  3. Rename the component to getPreviousLevel.
  4. From the Subflow dropdown, select Get level.
  5. In the Point field, enter parseInt(selectUser.data[0].points).

Step 11: Get badge for the new level

  1. Add a Select component to the flow.
  2. Rename the component to getPreviousBadge.
  3. From the Table dropdown, select badges.
  4. In the Filter tab, create a condition that checks if Field: id is equal to getPreviousUserLevel.returnObject.level.badgeId.

Step 12: Add the badge to the user

  1. Add an Insert component to the flow.

  2. Rename the component to insertNewLevelBadge.

  3. From the Table dropdown, select users_badges.

  4. Configure the component with the following columns:

    ColumnValue
    userIdStart.queryString.userId
    badgeIdgetPreviousUserLevel.returnObject.level.badgeId

Step 13: Create the response body

  1. Add a Build Object component to the flow.

  2. Configure the component with the following properties:

    Property NameValue
    NamegetPreviousBadge.data[0].name
    Image"<badge-image-flow-url>?id=" + getPreviousUserLevel.returnObject.level.badgeId

    But replace <badge-image-flow-url> with the URL of the /badge-image flow. You can find this URL via the project's Swagger page.

Step 14: Create the HTTP response

  1. Add an End component to the flow.
  2. Set the Status Code to 201 Created.
  3. From the Content Type dropdown, select application/json.
  4. In the Body field, enter Build_Object.result.

This is what the complete flow looks like: