This documentation outlines the design and implementation of a simple wallet system. The wallet system allows users to perform Account crediting
, Account Debit
and make Transfer
- Node.js - es6+
- ExpressJs
- TypeScript
- Mongoose (ODM for MongoDB) - explicit schema definitions.
- Jest
- pino
- Created two models: users and accounts.
- Users model store users information
(name, email and _id)
- Account model store wallet data
(accountId, active, balance)
- accountId points to user(
_id
- ObjectId)
- Users model store users information
- Clone the Repository:
git clone https://github.com/mbanda1/my-wallet.git
cd my-wallet
npm install
- Environment Variables:
I hardcoded .env, you may change configuration details.
PORT=3000
MONGO_URI=mongodb://localhost:27017/wallet_db
-
Start
npm run dev
- this will also seeds initial users (we ned userId to serve as account numbers)
-
Build
npm run build
The wallet system exposes the following API endpoints for wallet operations:
URL:
/api/wallet/credit
Method:
POST
Request Body:
Request
{
"accountId": "65fd89474821cebc7d79a683",
"amount": 2000
}
Response
{
"_id": "65fdb885700e471e27588e62",
"accountId": "65fdaaefa39c90915498209d",
"__v": 0,
"active": true,
"balance": 3,
"updatedAt": "2024-03-22T16:57:41.855Z"
}
- Debit Wallet Account
URL:
/api/wallet/debitMethod:
POSTRequest Body:
Request
{
"accountId": "65fd89474821cebc7d79a683",
"amount": 2
}
Response
{
"_id": "65fdb885700e471e27588e62",
"accountId": "65fdaaefa39c90915498209d",
"__v": 0,
"active": true,
"balance": 1,
"updatedAt": "2024-03-22T16:57:41.855Z"
}
- Make transaction - account transfer
URL:
/api/transaction/sendMethod:
POSTRequest Body:
Request
{
"creditAccountId": "65fdaaefa39c90915498209e",
"debitAccountId": "65fdaaefa39c90915498209d",
"amount": 1
}
Response
{
"debitAccount": {
"_id": "65fdb885700e471e27588e62",
"accountId": "65fdaaefa39c90915498209d",
"__v": 0,
"active": true,
"balance": 0,
"updatedAt": "2024-03-22T16:57:41.855Z"
},
"creditAccount": {
"_id": "65fdbf0b700e471e275892e5",
"accountId": "65fdaaefa39c90915498209e",
"__v": 0,
"active": true,
"balance": 1,
"updatedAt": "2024-03-22T17:25:31.143Z"
}
}
The wallet system implements proper error handling by providing meaningful error messages in the response body along with appropriate HTTP status codes for different error scenarios.
- run
npm run test
Implemented unit test for all services with jest.
Used different log levels (e.g., INFO, WARN, ERROR) to categorize log messages based on their importance. Utilized Pino a fast and low overhead Node.js logger.