Feature/auth wallet money apis - #4
Merged
Merged
Conversation
There was a problem hiding this comment.
Pull request overview
This PR introduces a new Node/Express API surface for payNEXT covering auth, wallets, and money movement (top-up, withdraw, transfer), plus basic DB wiring and containerization.
Changes:
- Added JWT-based auth endpoints (
/auth/*) andrequireAuthmiddleware. - Added wallet endpoints (
/wallets/*) and money movement endpoints (/wallets/:id/top-up,/wallets/:id/withdraw,/transfers). - Added Postgres connection setup, error handling, and Docker packaging.
Reviewed changes
Copilot reviewed 19 out of 21 changed files in this pull request and generated 6 comments.
Show a summary per file
| File | Description |
|---|---|
| api/src/services/moneyService.js | Adds DB transaction helpers (BEGIN/COMMIT/ROLLBACK), wallet row locking, balance updates, and transaction/gateway logging utilities. |
| api/src/server.js | Adds API entrypoint to start the Express app. |
| api/src/routes/walletRoutes.js | Defines authenticated wallet routes plus top-up/withdraw endpoints. |
| api/src/routes/requestRoutes.js | Currently empty but mounted by the master router (this breaks startup). |
| api/src/routes/moneyRoutes.js | Defines authenticated transfer (“sendMoney”) route. |
| api/src/routes/index.js | Adds master router with /health and mounts auth/wallet/transfer/payment-request route groups. |
| api/src/routes/authRoutes.js | Defines register/login/me auth routes. |
| api/src/models/walletModel.js | Adds wallet persistence (create/list/find). |
| api/src/models/userModel.js | Adds user persistence (create/findByEmail/findById). |
| api/src/middleware/errorHandler.js | Adds centralized error handler returning safe 500 responses. |
| api/src/middleware/auth.js | Adds JWT signing and auth enforcement middleware. |
| api/src/controllers/walletController.js | Adds wallet controller endpoints including transactions listing (currently depends on missing model). |
| api/src/controllers/moneyController.js | Implements top-up, withdraw, and transfer logic using DB transactions and row locks. |
| api/src/controllers/authController.js | Implements registration/login/me flows, including initial wallet creation. |
| api/src/config/db.js | Adds Postgres Pool configuration (currently mismatched with .env.example fields). |
| api/src/app.js | Wires Express app, JSON parsing, versioned routes, 404 handler, and error handler. |
| api/package.json | Adds API package metadata and runtime dependencies/scripts. |
| api/package-lock.json | Locks dependency tree for reproducible installs. |
| api/Dockerfile | Adds Docker build/run definition for the API service. |
| api/.env.example | Adds environment variable template (currently has incorrect DB port example). |
| api/.dockerignore | Prevents copying node_modules and .env into Docker build context. |
Files not reviewed (1)
- api/package-lock.json: Generated file
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Comment on lines
+1
to
+2
| const walletModel = require("../models/walletModel"); | ||
| const transactionModel = require("../models/transactionModel"); |
Comment on lines
+21
to
+22
| router.use("/transfers", moneyRoutes); | ||
| router.use("/payment-requests", requestRoutes); |
Comment on lines
+103
to
+121
| const data = await withTransaction(async (client) => { | ||
| const from = await lockWallet(client, fromWalletId); | ||
| if (!from) throw new ApiError("Sender wallet not found", 404); | ||
| if (from.user_id !== req.user.sub) throw new ApiError("Not your wallet", 403); | ||
|
|
||
| const toRes = await client.query( | ||
| "SELECT * FROM wallets WHERE wallet_number = $1", | ||
| [toWalletNumber] | ||
| ); | ||
| const to = toRes.rows[0]; | ||
| if (!to) throw new ApiError("Receiver wallet not found", 404); | ||
| if (to.id === from.id) throw new ApiError("Cannot send to the same wallet", 400); | ||
| if (from.status !== "active" || to.status !== "active") | ||
| throw new ApiError("Wallet is not active", 400); | ||
| if (from.currency !== to.currency) throw new ApiError("Currency mismatch", 400); | ||
| if (Number(from.balance) < amount) throw new ApiError("Insufficient balance", 400); | ||
|
|
||
| const lockedTo = await lockWallet(client, to.id); | ||
|
|
Comment on lines
+1
to
+22
| const jwt = require("jsonwebtoken"); | ||
|
|
||
| function signToken(user) { | ||
| return jwt.sign({ sub: user.id, email: user.email }, process.env.JWT_SECRET, { | ||
| expiresIn: "7d", | ||
| }); | ||
| } | ||
|
|
||
| function requireAuth(req, res, next) { | ||
| const header = req.headers.authorization || ""; | ||
| const token = header.startsWith("Bearer ") ? header.slice(7) : null; | ||
| if (!token) | ||
| return res.status(401).json({ success: false, error: "Missing token" }); | ||
| try { | ||
| req.user = jwt.verify(token, process.env.JWT_SECRET); | ||
| next(); | ||
| } catch { | ||
| return res | ||
| .status(401) | ||
| .json({ success: false, error: "Invalid or expired token" }); | ||
| } | ||
| } |
Comment on lines
+4
to
+10
| const pool = new Pool({ | ||
| host: process.env.DB_HOST || "localhost", | ||
| port: Number(process.env.DB_PORT || 5432), | ||
| user: process.env.DB_USER || "paynext", | ||
| password: process.env.DB_PASSWORD, | ||
| database: process.env.DB_NAME || "paynext_db", | ||
| }); |
Comment on lines
+11
to
+15
| DB_HOST=your-db-host.supabase.com | ||
| DB_PORT=3000 | ||
| DB_USER=postgres | ||
| DB_PASSWORD=change_me | ||
| DB_NAME=postgres |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No description provided.