Giter Club home page Giter Club logo

simple-loan-api's Introduction

express

LOAN APPLICATION API

About The Project

Objective: Create a simple RESTful API using Node.js and TypeScript that simulates a basic loan application process for car and personal finance.

Specific Requirements
  1. Setup:
    • Initialize a new Node.js project with TypeScript.
    • Use Express.js or any other Node.js framework for handling HTTP requests.
  2. Database:
    • A simple in-memory data structure (e.g., an array or object) is used to store loan application data. No need to integrate a real database.
  3. API Endpoints:
    • Create the following endpoints:
      • GET /loans : Retrieve a list of all loan applications.
      • GET /loans/:id : Retrieve a single loan application by its ID.
      • POST /loans : Submit a new loan application.
        • The application should include basic information such as the applicant's name, loan amount, loan type (car or personal), income, and interest rate.
        • Calculate the monthly payment based on the loan amount, interest rate, and a fixed loan term (e.g., 5 years for car loans, 3 years for personal loans). Use the provided calculateMonthlyPayment function for this calculation. Include the calculated monthly payment in the response.
      • PUT /loans/:id : Update an existing loan application by its ID.
      • DELETE /loans/:id : Delete an existing loan application by its ID.
    • Ensure that the API responds with appropriate status codes and messages.
  4. Validation:
    • Add validation for the loan application data (e.g., required fields, data types, loan amount limits).
  5. Testing:
    • Write unit tests for your API endpoints using a testing framework of your choice (e.g., Jest, Mocha).
  6. Documentation:
    • Provide a README file with clear instructions on how to set up and run your project, as well as how to run the tests.

(back to top)

Built With

Here the list of the frameworks/libraries used in this project:

(back to top)

Getting Started

To effectively execute this project on your machine, kindly follow the instructions provided below.

Prerequisites

Below are the requirements to run the project:

  • Install NVM for Windows. This is a quick way to easily install different versions of NodeJS depending on your needs.
  • Install NPM
    npm i npm@latest -g
  • Install Nodemon
    npm i nodemon -g
  • Install Docker Desktop for Windows or Docker for Linux. This is optional if you want to use Docker otherwise, you will have to install Redis manually.
  • If Docker can't be installed then, you must install Redis since this project uses this for storage.
  • Install Git to clone the project on your local machine.

Installation

The following steps are essential for running this project on your local machine. Please ensure that you have fulfilled the preceding instructions to ensure the functionality of the project. O

  1. Open up terminal and go to the directory you want to install the project.

  2. Clone the project repository

    git clone [email protected]:kuiamenhmartin/driva-api.git
  3. Install NPM packages. The project uses Node v18.16.1.

    npm install
  4. Create a copy of .env-sample and rename it to .env. Make sure you provide the necessary values for #REDIS section while you can leave the other variable values as is.

    # ENV
    NODE_ENV=development
    PORT=4000
    
    # LOGGER
    LOGGER_SERVICE=loan-api
    LOG_ENABLE_INFO=true
    LOG_ENABLE_WARN=true
    LOG_ENABLE_DEBUG=true
    LOG_ENABLE_VERBOSE=true
    LOG_ENABLE_ERROR=true
    
    # REDIS
    REDIS_HOST='127.0.0.1'
    REDIS_PORT=6709
    REDIS_PASSWORD=
    REDIS_USERNAME=
    REDIS_DB=0
  5. (Optional if step#6 will be followed) If you will be using Docker. Make sure it is already started then, create a copy of docker-compose-example.yml and rename it to docker-compose.yml. Run the command below to run your docker service.

    docker-compose up -d
  6. (Optional if step#5 will be followed) Start Redis on your local machine and make sure you update the #REDIS section in .env file with the needed credentials.

  7. Run command. The default PORT used is 4000 but you can change it in the .env file.

    npm run dev:watch
  8. Once everything is configured, you can now run the command below to fire up the Loan API.

    npm run start
  9. Then access it through http://localhost:4000/

  10. Run integrated test coverage

    npm run coverage

(back to top)

API Documentation

The REST API for this project is listed below.

Create a new loan

Request

POST /api/loans

curl --location 'localhost:4000/api/loans' \
--header 'Content-Type: application/json' \
--data '{
    "firstName": "John",
    "lastName": "Doe",
    "loanAmount": 50000,
    "income": 35000,
    "interestRate": 13.54,
    "loanType":"PERSONAL"
}'

Response

  {
      "status": 200,
      "success": true,
      "message": "New Loan Application successfully created!",
      "data": {
          "id": "5514f659e8b897abdd5101caad96d7b0",
          "firstName": "John",
          "lastName": "Doe",
          "loanAmount": 50000,
          "income": 35000,
          "interestRate": 13.54444,
          "loanType": "PERSONAL",
          "monthlyPayment": 1697.83
      }
  }

(back to top)

Get all loans

GET /api/loans

curl --location 'localhost:4000/api/loans'

Response

  {
    "status": 200,
    "success": true,
    "message": "Loan Applications successfully retrieved!",
    "data": [
      {
        "id": "4ce0b16be027ec66bd0d92f006f6ee6d",
        "firstName": "John",
        "lastName": "Doe",
        "loanAmount": 15000,
        "income": 1000,
        "interestRate": 14.75,
        "loanType": "CAR"
      },
      {
        "id": "538dfc6b119370d7033ce02afbf5d16a",
        "firstName": "Jane",
        "lastName": "Smith",
        "loanAmount": 25000,
        "income": 15000,
        "interestRate": 15.25,
        "loanType": "PERSONAL"
      }
    ]
  }

(back to top)

Get a loan by ID

GET /api/loans/:id

curl --location 'localhost:4000/api/loans/4ce0b16be027ec66bd0d92f006f6ee6d'

Response

  {
    "status": 200,
    "success": true,
    "message": "Loan application successfully retrieved!",
    "data": {
        "id": "4ce0b16be027ec66bd0d92f006f6ee6d",
        "firstName": "John",
        "lastName": "Doe",
        "loanAmount": 15000,
        "income": 1000,
        "interestRate": 14.75,
        "loanType": "CAR"
    }
  }

(back to top)

Update a loan by ID

PUT /api/loans/:id

curl --location --request PUT 'localhost:4000/api/loans/4ce0b16be027ec66bd0d92f006f6ee6d' \
  --header 'Content-Type: application/json' \
  --data '{
      "id": "4ce0b16be027ec66bd0d92f006f6ee6d",
      "firstName": "John",
      "lastName": "Doe",
      "loanAmount": 25000,
      "income": 1700,
      "interestRate": 13,
      "loanType": "PERSONAL"
  }'

Response

  {
    "status": 200,
    "success": true,
    "message": "Loan application '4ce0b16be027ec66bd0d92f006f6ee6d' successfully updated!"
  }

(back to top)

Delete loan by ID

DELETE /api/loans/:id

curl --location --request DELETE 'localhost:4000/api/loans/85d96cf662fcc875e31e50264d2e1d9c'

Response

  {
    "status": 200,
    "success": true,
    "message": "Loan application '85d96cf662fcc875e31e50264d2e1d9c' successfully deleted!"
  }

(back to top)

Error Response

Loan application not found

GET /api/loans/:id

curl --location 'localhost:4000/api/loans/4ce0b16be027ec66bd0d92f006f6ee62'

Response

    {
      "status": 404,
      "success": false,
      "message": "Unknown loan application '4ce0b16be027ec66bd0d92f006f6ee62'",
      "code": "NOT_FOUND"
    }

(back to top)

Create loan application validation

POST /api/loans

curl --location 'localhost:4000/api/loans' \
--header 'Content-Type: application/json' \
--data '{
    "firstName": "John",
    "lastName": "Doe",
    "loanAmount": 50000,
    "income": 35000,
    "interestRate": 13.54,
    "loanType":"MORTGAGE"
}'

Response

  {
    "status": 400,
    "success": false,
    "message": "Please check your inputs",
    "data": [
        {
            "type": "field",
            "value": "MORTGAGE",
            "msg": "loanType must be one of the following CAR | PERSONAL",
            "path": "loanType",
            "location": "body"
        }
    ],
    "code": "BAD_REQUEST"
  }

(back to top)

Invalid loan ID

GET /api/loans/:id

curl --location 'localhost:4000/api/loans/4ce0b16be027ec66bd0d92f006f6ee'

Response

  {
    "status": 400,
    "success": false,
    "message": "Please check your inputs",
    "data": [
        {
            "type": "field",
            "value": "4ce0b16be027ec66bd0d92f006f6ee",
            "msg": "Loan Application Id must be a valid hexadecimal of length 32",
            "path": "id",
            "location": "params"
        }
    ],
    "code": "BAD_REQUEST"
  }

(back to top)

Cheers!

simple-loan-api's People

Contributors

kuiamenhmartin avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.