A robust and secure backend implementation for a banking system, built with Node.js, Express, and MongoDB. The system emphasizes security, transactional integrity, and idempotency for core financial operations.
- Authentication & Authorization: Secure user registration, login, and logout functionalities utilizing JWT (JSON Web Tokens) and secure password hashing with
bcryptjs. - Account Management: Users can create multiple banking accounts, fetch associated accounts, and retrieve balances.
- Transactions & Transfers:
- Secure and reliable inter-account funds transfers.
- Transactions run within MongoDB Sessions/Transactions, meaning that if any part of the process fails (e.g., deducting funds, crediting funds), the entire operation rolls back to prevent inconsistent states.
- Ledger System: Implemented a Double-Entry Bookkeeping Ledger. Every transaction creates corresponding
DEBITandCREDITentries in a separate Ledger collection, bringing higher auditing integrity to account balances. Account balances are dynamically derived from ledger entries rather than just updating a static balance value. - Idempotency Key Support: Money transfer APIs require an
idempotencyKeyparameter. This prevents duplicate transfers (like accidental double-clicks from clients) by guaranteeing that the identical request processed multiple times produces the very same result as being processed only once. - Email Notifications: Integrated
nodemailerto trigger automated email updates upon successful transaction completions.
- JavaScript Runtime: Node.js
- Web Framework: Express.js
- Database: MongoDB (with Mongoose ODM)
- Security:
bcryptjs(Hashing),jsonwebtoken(Auth Tokens),cookie-parser - Mail Service:
nodemailer
POST /register- Register a new user.POST /login- Authenticate a user and set access tokens.POST /logout- Invalidate the current session.
POST /- Create a new bank account for the logged-in user.GET /- Retrieve all accounts linked to the logged-in user.GET /balance/:accountId- Retrieve the current balance for a specified account.
POST /- Create a new funds transfer transaction (requiresfromAccount,toAccount,amount, andidempotencyKey).POST /system/intial-funds- Admin/System endpoint to seed accounts with initial funds.
For every transfer request, the system ensures precision through the following sequence:
- Validates request body and checks Idempotency Key to avoid duplicate processing.
- Verifies that both sender and receiver accounts are ACTIVE.
- Derives the sender's true balance dynamically from past ledger entries.
- Starts a MongoDB Transaction.
- Records the Transaction as
PENDING. - Generates a
DEBITledger entry for the sender. - Generates a
CREDITledger entry for the receiver. - Marks the Transaction as
COMPLETED. - Commits the MongoDB Session.
- Dispatches an email notification to the user.
The API follows a cohesive error handling pattern to deliver standardized status codes and comprehensible feedback signals covering cases such as missing parameters, invalid accounts, insufficient balance, locked accounts, and systemic issues.
ISC