This project is a REST API built with Node.js and Express. The API parses and executes financial payment instructions according to a defined syntax and a set of business rules.
The architecture is based on the provided Node.js template, featuring a custom Express server abstraction to ensure consistency and a clear separation of concerns.
The project follows a simplified version of the provided template, focusing only on the necessary components.
-
app.js: The main entry point of the application. It initializes the Express server and dynamically loads all endpoint handlers. -
core/: Contains the foundational modules for the application.core/express/: A custom abstraction over Express.js that standardizes how routes and responses are handled.core/errors/: A simple utility for creating structured application errors.
-
endpoints/: This directory holds all the API endpoint definitions.payment-instructions.js: Defines thePOST /payment-instructionsroute and links it to the payment service.
-
src/services/: Contains the core business logic.PaymentService.js: The main service file that handles the parsing, validation, execution, and response formatting for all payment instructions.
- Instruction Parser: A robust, non-regex parser that deconstructs string-based payment instructions.
- Dual Instruction Formats: Supports both
DEBIT ... FROMandCREDIT ... TOinstruction formats. - Comprehensive Validation: Implements all specified business and syntax rules with corresponding error codes.
- Transaction Execution: Handles both immediate and future-dated (pending) transactions.
- Structured Responses: Provides consistent JSON responses for successful, pending, and failed transactions as per the assessment specification.
The application follows a clear, sequential data flow for each request:
- Client Request: A
POSTrequest with theaccountsandinstructionpayload is sent to the/payment-instructionsendpoint. - Server (
core/express/): The custom Express server receives the request and routes it to the appropriate handler. - Endpoint Handler (
endpoints/payment-instructions.js): The handler extracts theaccountsandinstructionfrom the request body. - Service (
src/services/PaymentService.js): The handler calls theexecuteTransactionfunction, which orchestrates the entire process.- Parsing: The raw instruction string is parsed into a structured object.
- Validation: A series of business and syntax rules are validated. If any rule fails, a structured error response is generated.
- Execution: The transaction is executed (balances are updated) or marked as pending.
- Formatting: A final response object is formatted according to the assessment specifications.
- Response: The endpoint handler receives the final formatted object and sends it back to the client with the appropriate HTTP status code (
200for success/pending,400for failure).
Follow these steps to run the application locally:
npm installOpen the app.js file and add your endpoint folder to the ENDPOINT_CONFIGS array:
const ENDPOINT_CONFIGS = [{ path: './endpoints/{your-endpoint-folder-name}/' }];Replace
{your-endpoint-folder-name}with the actual folder name of your endpoint.
In your terminal, run the following command:
node app.jsThe server should now be running and ready to handle requests at the port 8811 or any port configured in the .env file
Path: POST /payment-instructions
Request Body:
{
"accounts": [
{"id": "a", "balance": 230, "currency": "USD"},
{"id": "b", "balance": 300, "currency": "USD"}
],
"instruction": "DEBIT 30 USD FROM ACCOUNT a FOR CREDIT TO ACCOUNT b"
}The service will process the instruction and return a structured JSON response indicating the outcome of the transaction.