A basic token management service using a user account and a transaction for sending and receiving tokens. This API is completely developed in NestJS.
- 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'.
- NestJS - a progressive framework for building efficient, scalable Node.js server-side applications
- MongoDB - persistent database to store data and fast retrieval
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
- Verify using the commands:
In the project root directory, run:
docker compose up
docker compose down
docker compose up --build
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.
First, verify .env
file in root
. Then, in the project root directory, run:
npm ci
npm run start:dev
npm ci
installs npm packages and createsnode_modules
directory. Ifnpm ci
gives any dependency error, use thenpm install
command instead of it.npm run start:dev
command starts the nest application on port3001
.
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.
Once the application starts, Swagger UI will be available at http://localhost:3001/api.
- Add authentication and role-based access
- For autogenerated OpenApi docs, add error responses.
- Introduce error handling and logging
- Implement e2e tests