Skip to content

This is a web API service built using node.js and express.js. The service exposes basic end points to create user and update reward points of a user and retrieve the same.

Notifications You must be signed in to change notification settings

mathad-a/Fetch_Rewards-API

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Fetch Rewards - web service api

This is a web API service built using node.js and express.js. The service exposes basic end points to create user and update reward points of a user and retrieve the same.

Installation and Run

Requirements

  • node.js (version > 12)
  • npm (version > 6.13 usually included with node.js)

Node Packages Used

  • cors
  • md5
  • sqlite3
  • express
  • swagger-ui
  • uuid

RUN

Install depedencies for the service. Run the following command in the project directory.

npm install

Note: Only if you are using MacOS and npm install fails . Then you might probably have to delete and reinstall the (latest) xcode again. For that follow these commands

sudo rm -rf $(xcode-select -print-path)
xcode-select --install

Now install npm modules by
npm install

Start the service by running the following command in the project directory.

node app.js

After running the above command, the service will be available at: http://localhost:3050.

The service is equipped with the swagger overlay to easily test and play around the exposed api endpoints. The swagger playground also explains each and every endpoint in detail. therefore refer the swagger after launching the service for complete API specifications.

User Guide

Once the service is running follow these steps to get started.

  • Create a new user by using register endpoint.
  • Login to the system by using admin/admin123456. (to get the userId of this new user)
  • The successful login will return a JWT token called authJwt. Pass this token in the all_users request to get a list of users. This will return a list of users with user details including userId, username and role.
  • Use the userId returned in the all_users request in further operations like add_points, deduct_points, points_balance endpoints.

Note: add_points endpoint expects transactionDate in unix timestamp format. This is to store the timestamp efficiently across timezones and also a standard of communication across services.

API Specifications

User Endpoints

login The endpoint can be used to login to the system.
Request Url: http://localhost:3050/login
register

The endpoint can be used to create a new user.
Request Url: http://localhost:3050/register

all_users

The endpoint returns all the users present in the system.
Request Url: http://localhost:3050/all_users

Rewards Endpoints

add_points

The endpoint adds reward points for a specific user and specific payer. If the points specified is positive, then the points will be added to the user for that payer. If specified points is negative, it deducts the points for the user from the specific payer from the oldest entry in the transactions table.
Request Url: http://localhost:3050/add_points

deduct_points

The endpoint deducts the specified amount of points from the user in the order of oldest reward transactions.
Request Url: http://localhost:3050/deduct_points

points_balance

The endpoint retrieves the available points balance for the user for every payer the user has ever made transactions with.
Request Url: http://localhost:3050/points_balance

Results:

Login to system as admin

You can see the JWT token (authJWT) in the response body. login

Get all users in the system

You can see the userId of all users. Take the required user's userId. login

Add points

Add points to user account for specific payer and date (in unix timestamp). login

Deduct points

Deduct points from the user account. It returns a list of [payer, points deducted] for each call to spend points. login

Points balance

Returns points balance of the user that will list all positive points per payer. login

About

This is a web API service built using node.js and express.js. The service exposes basic end points to create user and update reward points of a user and retrieve the same.

Topics

Resources

Stars

Watchers

Forks