Ledger API

RESTAPI written in node using ExpressJS. This API records a single user's transactions.

Built With

Getting Started

Prerequisites

Make sure you have nvm for easy node management.

  • MacOS - Using HomeBrew
    brew install nvm
  • Linux -
    curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
    Note - Make sure to source nvm before you can use it
     source ~/.nvm/nvm.sh
  • Install Node Version
      nvm install 18.17.1
      nvm use 18.17.1

If you'd like to run the containerized version of the API, you will need to have the Docker CLI

Installation

  1. Clone the repo
    git clone https://github.com/dronavallipranav/FetchBackendAPI.git
  2. Install NPM packages
    npm install
  3. Start the API server on port 8000
    node index.js
    or build the docker image and run the container on port 8000
    docker build -t fetch_api .
    docker run -p 8000:8000 -d fetch_api

Usage

This API provides three endpoints

Endpoint: POST /add

Request Body

{
 payer: <String>
 points: <Integer>
 timestamp: <String>
}

-Creates a user record in the database for the given payer, points, and timestamp.

-response:

  • 200 OK: Points successfully added.
  • 400 Bad Request: Request not formatted correctly.
  • 500 Internal Server Error: Unexpected server error.

Endpoint: POST /spend

Request Body

{
 points: <Integer>
}

-Spends user's points based on order that transactions were received

-response:

  • 200 OK: Ledger table updated,response contains a list of points deducted per payer.
  • 400 Bad Request: Can't spend zero or negative points, or user does not have enough points to spend.
  • 500 Internal Server Error: Unexpected server error.

Endpoint: GET /balance

-Gets user's total points for each payer

-response:

  • 200 OK: Successfully retrieved the list of payers and their point balance.
  • 500 Internal Server Error: Unexpected server error.

(back to top)

Testing

Integration Tests can be run using the command

npm run test

Or to run tests with watch mode

  npm run test:watch

PERSISTENCE NOTE

-TO ALLOW DATA IN DATABASE TO PERSIST BEYOND SERVER RESTARTS - OPEN THE "storage/database.js" FILE AND CHANGE LINE

sequelize
 .sync({ force: true })
 .then(() => console.log("Tables have been synchronized"))
 .catch((error) => console.error("Unable to synchronize the tables:", error));

TO

sequelize
 .sync({ force: false })
 .then(() => console.log("Tables have been synchronized"))
 .catch((error) => console.error("Unable to synchronize the tables:", error));

I chose force sync as a default as I figured it would be easier for the graders to test the functionality

Contact

Pranav Dronavalli - dronavallipranav@gmail.com

(back to top)