token-service-api

A basic token management service using a user account and a transaction for sending and receiving tokens. This API is completely developed in NestJS.

Overview

Application functionality

  • A token service consists of mainly two schemas: account and transaction to manage tokens.
    • account: {userEmail, status: ['active', 'locked'], createdAt, updatedAt}
    • transaction: {userEmail, amount (number of tokens), type: ['send', 'receive'], createdAt}
  • A transaction can only be associated with one account. An account can be associated with many transactions.
  • This API provides functionality to create, update, and delete user accounts and gets a current token balance for the user account. Similarly, it also provides endpoints to create, update and delete transactions.
  • Some validations:
    • No account can have a negative balance
    • No transactions can be made for accounts with the status field set to 'locked'.

Technologies used

  • NestJS - a progressive framework for building efficient, scalable Node.js server-side applications
  • MongoDB - persistent database to store data and fast retrieval

Getting Started

Run application using Docker (Cloud-native) - Recommended

Prerequisites

  • docker & docker-compose
    • Verify using the commands: docker -v & docker-compose -v
      $ docker -v
          Docker version 20.10.21, build baeda1f
      $ docker-compose -v
          Docker Compose version v2.13.0
      

Start application (Steps to Run)

In the project root directory, run:

docker compose up

Stop application

docker compose down

Restart containers with the new code

docker compose up --build

Run application locally in dev mode

Prerequisites

  • node & npm
    • With the installation of the node, npm is by default installed.
    • Verify using the commands: node -v & npm -v
    • (Application tested on)
      $ npm -v
          9.5.1
      $ node -v
          v16.16.0
      
  • MongoDB - run mongoDB documentation.

Start application (Steps to Run)

First, verify .env file in root. Then, in the project root directory, run:

npm ci
npm run start:dev
Explanation
  • npm ci installs npm packages and creates node_modules directory. If npm ci gives any dependency error, use the npm install command instead of it.
  • npm run start:dev command starts the nest application on port 3001.

Unit tests

Using jest, created unit tests for the accounts controller.

In the project root directory, run: npm test

Output:

$ npm test

> token-service-api@0.0.1 test
> jest

 PASS  src/accounts/accounts.controller.spec.ts
  Accounts Controller
    create()
      ✓ should create a new account (18 ms)
    findAll()
      ✓ should return an array of accounts (5 ms)
    filterByEmail()
      ✓ should return an account filtered by email (5 ms)
    update()
      ✓ should return an updated account (10 ms)
    delete()
      ✓ should return a deleted account (3 ms)
    getAccountWithCurrentBalancec()
      ✓ should return an account with a current balance (3 ms)

Test Suites: 1 passed, 1 total
Tests:       6 passed, 6 total
Snapshots:   0 total
Time:        4.261 s, estimated 7 s
Ran all test suites.

Open API (Swagger UI) docs

Once the application starts, Swagger UI will be available at http://localhost:3001/api.

Future Work/Enhancement

  • Add authentication and role-based access
  • For autogenerated OpenApi docs, add error responses.
  • Introduce error handling and logging
  • Implement e2e tests

Resources