implemented a simple backend for mobile banking

This commit is contained in:
2025-06-02 13:24:10 +05:30
commit c350c591f6
26 changed files with 4060 additions and 0 deletions

21
src/app.js Normal file
View File

@@ -0,0 +1,21 @@
const express = require('express');
const cors = require('cors');
const pinoHttp = require('pino-http');
const { logger } = require('./util/logger');
const routes = require('./routes');
const app = express();
app.use(cors());
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
// app.use(pinoHttp({ logger }));
app.use('/api', routes);
app.get('/health', (_, res) => res.send('server is healthy'));
app.use((err, _req, res, _next) => {
logger.error(err, 'uncaught error');
res.status(500).json({ error: 'INTERNAL SERVER ERROR' });
});
module.exports = app;

10
src/config/config.js Normal file
View File

@@ -0,0 +1,10 @@
const dotenv = require('dotenv');
const path = require('path');
dotenv.config({ path: path.resolve(__dirname, '../../.env') });
module.exports = {
port: process.env.PORT || 8080,
dbUrl: process.env.DATABASE_URL,
jwtSecret: process.env.JWT_SECRET,
};

20
src/config/db.js Normal file
View File

@@ -0,0 +1,20 @@
const { Pool } = require('pg');
const { dbUrl } = require('./config');
const { logger } = require('../util/logger');
const pool = new Pool({
connectionString: dbUrl,
});
pool.on('connect', () => {
logger.info('connected to Postgres database');
});
pool.on('error', (err) => {
logger.error(err, 'unexpected database pool error');
});
module.exports = {
query: (text, params) => pool.query(text, params),
pool,
};

View File

@@ -0,0 +1,25 @@
const { validateUser } = require('../services/auth.service');
const { generateToken } = require('../util/jwt');
const { logger } = require('../util/logger');
async function login(req, res) {
const { customerNo, password } = req.body;
if (!customerNo || !password) {
return res
.status(400)
.json({ error: 'customerNo and password are required' });
}
try {
const user = await validateUser(customerNo, password);
if (!user) return res.status(401).json({ error: 'invalid credentials' });
const token = generateToken(user.customer_no);
res.json({ token });
} catch (err) {
logger.error(err, 'login failed');
res.status(500).json({ error: 'something went wrong' });
}
}
module.exports = { login };

View File

@@ -0,0 +1,9 @@
const { logger } = require('../util/logger');
const detailsObject = require('./rajatDetails_wt.json');
function getDetails(user) {
logger.info(user);
return detailsObject;
}
module.exports = { getDetails };

View File

@@ -0,0 +1,116 @@
[
{
"stAccountNo": "50067217630",
"stAccountType": "SA",
"stBookingNumber": "SAVING CHQ FOR NONME",
"stBranchNo": "00002 ",
"stCurrency": "INR",
"stProductType": "SAVING CHQ FOR NONME",
"stApprovedAmount": null,
"stAvailableBalance": "933.84",
"stCurrentBalance": "933.84",
"custdob": "02051982",
"custname": "RAJAT MAHARANA",
"mobileno": "919418573848",
"custaddress": "DATA CENTRE KCCB CIVIL BAZAR D/SHALA DHARAMSHALA KANGRA",
"pincode": "176215",
"id": "AMSPM8798H",
"activeAccounts": 6,
"cifNumber": "30022497139"
},
{
"stAccountNo": "50077736845",
"stAccountType": "TD",
"stBookingNumber": "TD-LTD-NM-IND -1 Yrs",
"stBranchNo": "00002 ",
"stCurrency": "INR",
"stProductType": "TD-LTD-NM-IND -1 Yrs",
"stApprovedAmount": null,
"stAvailableBalance": "10000.00",
"stCurrentBalance": "10000.00",
"custdob": "02051982",
"custname": "RAJAT MAHARANA",
"mobileno": "919418573848",
"custaddress": "DATA CENTRE KCCB CIVIL BAZAR D/SHALA DHARAMSHALA KANGRA",
"pincode": "176215",
"id": "AMSPM8798H",
"activeAccounts": 6,
"cifNumber": "30022497139"
},
{
"stAccountNo": "50077742351",
"stAccountType": "LN",
"stBookingNumber": "DL-AGAINST DEPOSIT",
"stBranchNo": "00002 ",
"stCurrency": "INR",
"stProductType": "DL-AGAINST DEPOSIT",
"stApprovedAmount": "10000.00",
"stAvailableBalance": "10236.00",
"stCurrentBalance": "10236.00",
"custdob": "02051982",
"custname": "RAJAT MAHARANA",
"mobileno": "919418573848",
"custaddress": "DATA CENTRE KCCB CIVIL BAZAR D/SHALA DHARAMSHALA KANGRA",
"pincode": "176215",
"id": "AMSPM8798H",
"activeAccounts": 6,
"cifNumber": "30022497139"
},
{
"stAccountNo": "50077756089",
"stAccountType": "TD",
"stBookingNumber": "TD-LTD-NM-IND -1 Yrs",
"stBranchNo": "00002 ",
"stCurrency": "INR",
"stProductType": "TD-LTD-NM-IND -1 Yrs",
"stApprovedAmount": null,
"stAvailableBalance": "10000.00",
"stCurrentBalance": "10000.00",
"custdob": "02051982",
"custname": "RAJAT MAHARANA",
"mobileno": "919418573848",
"custaddress": "DATA CENTRE KCCB CIVIL BAZAR D/SHALA DHARAMSHALA KANGRA",
"pincode": "176215",
"id": "AMSPM8798H",
"activeAccounts": 6,
"cifNumber": "30022497139"
},
{
"stAccountNo": "50077842356",
"stAccountType": "LN",
"stBookingNumber": "DL-AGAINST DEPOSIT",
"stBranchNo": "00002 ",
"stCurrency": "INR",
"stProductType": "DL-AGAINST DEPOSIT",
"stApprovedAmount": "10000.00",
"stAvailableBalance": "10215.00",
"stCurrentBalance": "10215.00",
"custdob": "02051982",
"custname": "RAJAT MAHARANA",
"mobileno": "919418573848",
"custaddress": "DATA CENTRE KCCB CIVIL BAZAR D/SHALA DHARAMSHALA KANGRA",
"pincode": "176215",
"id": "AMSPM8798H",
"activeAccounts": 6,
"cifNumber": "30022497139"
},
{
"stAccountNo": "50078072406",
"stAccountType": "LN",
"stBookingNumber": "TL-PERSONAL",
"stBranchNo": "00002 ",
"stCurrency": "INR",
"stProductType": "TL-PERSONAL",
"stApprovedAmount": "200000.00",
"stAvailableBalance": "200825.00",
"stCurrentBalance": "200825.00",
"custdob": "02051982",
"custname": "RAJAT MAHARANA",
"mobileno": "919418573848",
"custaddress": "DATA CENTRE KCCB CIVIL BAZAR D/SHALA DHARAMSHALA KANGRA",
"pincode": "176215",
"id": "AMSPM8798H",
"activeAccounts": 6,
"cifNumber": "30022497139"
}
]

View File

@@ -0,0 +1,212 @@
[
{
"stAccountNo": "50067217630",
"stAccountType": "SA",
"stBookingNumber": "SAVING CHQ FOR NONME",
"stBranchNo": "00002 ",
"stCurrency": "INR",
"stProductType": "SAVING CHQ FOR NONME",
"stApprovedAmount": null,
"stAvailableBalance": "933.84",
"stCurrentBalance": "933.84",
"custdob": "02051982",
"custname": "RAJAT MAHARANA",
"mobileno": "919418573848",
"custaddress": "DATA CENTRE KCCB CIVIL BAZAR D/SHALA DHARAMSHALA KANGRA",
"pincode": "176215",
"id": "AMSPM8798H",
"transactions": [
{
"id": "abcd",
"name": "John Doe",
"date": "31-05-2025 09:05:31",
"type": "CR",
"amount": 612
},
{
"id": "efgh",
"name": "John Cena",
"date": "20-05-2025 09:05:20",
"type": "DR",
"amount": 10
}
],
"activeAccounts": 6,
"cifNumber": "30022497139"
},
{
"stAccountNo": "50077736845",
"stAccountType": "TD",
"stBookingNumber": "TD-LTD-NM-IND -1 Yrs",
"stBranchNo": "00002 ",
"stCurrency": "INR",
"stProductType": "TD-LTD-NM-IND -1 Yrs",
"stApprovedAmount": null,
"stAvailableBalance": "10000.00",
"stCurrentBalance": "10000.00",
"custdob": "02051982",
"custname": "RAJAT MAHARANA",
"mobileno": "919418573848",
"custaddress": "DATA CENTRE KCCB CIVIL BAZAR D/SHALA DHARAMSHALA KANGRA",
"pincode": "176215",
"id": "AMSPM8798H",
"transactions": [
{
"id": "abcd",
"name": "John Doe",
"date": "31-05-2025 09:05:31",
"type": "CR",
"amount": 10
},
{
"id": "efgh",
"name": "John Cena",
"date": "20-05-2025 09:05:20",
"type": "DR",
"amount": 662
}
],
"activeAccounts": 6,
"cifNumber": "30022497139"
},
{
"stAccountNo": "50077742351",
"stAccountType": "LN",
"stBookingNumber": "DL-AGAINST DEPOSIT",
"stBranchNo": "00002 ",
"stCurrency": "INR",
"stProductType": "DL-AGAINST DEPOSIT",
"stApprovedAmount": "10000.00",
"stAvailableBalance": "10236.00",
"stCurrentBalance": "10236.00",
"custdob": "02051982",
"custname": "RAJAT MAHARANA",
"mobileno": "919418573848",
"custaddress": "DATA CENTRE KCCB CIVIL BAZAR D/SHALA DHARAMSHALA KANGRA",
"pincode": "176215",
"id": "AMSPM8798H",
"transactions": [
{
"id": "abcd",
"name": "John Doe",
"date": "31-05-2025 09:05:31",
"type": "CR",
"amount": 676
},
{
"id": "efgh",
"name": "John Cena",
"date": "20-05-2025 09:05:20",
"type": "DR",
"amount": 684
}
],
"activeAccounts": 6,
"cifNumber": "30022497139"
},
{
"stAccountNo": "50077756089",
"stAccountType": "TD",
"stBookingNumber": "TD-LTD-NM-IND -1 Yrs",
"stBranchNo": "00002 ",
"stCurrency": "INR",
"stProductType": "TD-LTD-NM-IND -1 Yrs",
"stApprovedAmount": null,
"stAvailableBalance": "10000.00",
"stCurrentBalance": "10000.00",
"custdob": "02051982",
"custname": "RAJAT MAHARANA",
"mobileno": "919418573848",
"custaddress": "DATA CENTRE KCCB CIVIL BAZAR D/SHALA DHARAMSHALA KANGRA",
"pincode": "176215",
"id": "AMSPM8798H",
"transactions": [
{
"id": "abcd",
"name": "John Doe",
"date": "31-05-2025 09:05:31",
"type": "CR",
"amount": 698
},
{
"id": "efgh",
"name": "John Cena",
"date": "20-05-2025 09:05:20",
"type": "DR",
"amount": 706
}
],
"activeAccounts": 6,
"cifNumber": "30022497139"
},
{
"stAccountNo": "50077842356",
"stAccountType": "LN",
"stBookingNumber": "DL-AGAINST DEPOSIT",
"stBranchNo": "00002 ",
"stCurrency": "INR",
"stProductType": "DL-AGAINST DEPOSIT",
"stApprovedAmount": "10000.00",
"stAvailableBalance": "10215.00",
"stCurrentBalance": "10215.00",
"custdob": "02051982",
"custname": "RAJAT MAHARANA",
"mobileno": "919418573848",
"custaddress": "DATA CENTRE KCCB CIVIL BAZAR D/SHALA DHARAMSHALA KANGRA",
"pincode": "176215",
"id": "AMSPM8798H",
"transactions": [
{
"id": "abcd",
"name": "John Doe",
"date": "31-05-2025 09:05:31",
"type": "CR",
"amount": 721
},
{
"id": "efgh",
"name": "John Cena",
"date": "20-05-2025 09:05:20",
"type": "DR",
"amount": 729
}
],
"activeAccounts": 6,
"cifNumber": "30022497139"
},
{
"stAccountNo": "50078072406",
"stAccountType": "LN",
"stBookingNumber": "TL-PERSONAL",
"stBranchNo": "00002 ",
"stCurrency": "INR",
"stProductType": "TL-PERSONAL",
"stApprovedAmount": "200000.00",
"stAvailableBalance": "200825.00",
"stCurrentBalance": "200825.00",
"custdob": "02051982",
"custname": "RAJAT MAHARANA",
"mobileno": "919418573848",
"custaddress": "DATA CENTRE KCCB CIVIL BAZAR D/SHALA DHARAMSHALA KANGRA",
"pincode": "176215",
"id": "AMSPM8798H",
"transactions": [
{
"id": "abcd",
"name": "John Doe",
"date": "31-05-2025 09:05:31",
"type": "CR",
"amount": 10
},
{
"id": "efgh",
"name": "John Cena",
"date": "20-05-2025 09:05:20",
"type": "DR",
"amount": 751
}
],
"activeAccounts": 6,
"cifNumber": "30022497139"
}
]

View File

@@ -0,0 +1,25 @@
const { verifyToken } = require('../util/jwt');
const { logger } = require('../util/logger');
function auth(req, res, next) {
const authHeader = req.headers.authorization;
if (!authHeader || !authHeader.startsWith('Bearer ')) {
return res
.status(401)
.json({ error: 'missing or malformed authorization header' });
}
const token = authHeader.split(' ')[1];
try {
const payload = verifyToken(token);
req.user = payload;
next();
} catch (err) {
logger.error(err, 'error verifying token');
return res.status(401).json({ error: 'invalid or expired token' });
}
}
module.exports = auth;

0
src/models/user.model.js Normal file
View File

8
src/routes/auth.js Normal file
View File

@@ -0,0 +1,8 @@
const express = require('express');
const { login } = require('../controllers/auth.controller');
const router = express.Router();
router.post('/login', login);
module.exports = router;

12
src/routes/details.js Normal file
View File

@@ -0,0 +1,12 @@
const express = require('express');
const customerController = require('../controllers/details');
const auth = require('../middlewares/auth.middleware');
const router = express.Router();
router.get('/details', auth, (req, res) => {
const user = req.user;
const details = customerController.getDetails(user);
return res.json(details);
});
module.exports = router;

12
src/routes/index.js Normal file
View File

@@ -0,0 +1,12 @@
const express = require('express');
const testRoute = require('./test');
const authRoute = require('./auth');
const detailsRoute = require('./details');
const router = express.Router();
router.use('/test', testRoute);
router.use('/auth', authRoute);
router.use('/customer', detailsRoute);
module.exports = router;

16
src/routes/test.js Normal file
View File

@@ -0,0 +1,16 @@
const express = require('express');
const db = require('../config/db');
const { logger } = require('../util/logger');
const router = express.Router();
router.get('/ping-db', async (_req, res) => {
try {
const result = await db.query('SELECT NOW()');
res.json({ time: result.rows[0].now });
} catch (err) {
logger.error(err);
res.status(500).json({ error: err });
}
});
module.exports = router;

22
src/server.js Normal file
View File

@@ -0,0 +1,22 @@
const app = require('./app');
const { port } = require('./config/config');
const { logger } = require('./util/logger');
const db = require('./config/db');
const server = app.listen(port, () => {
logger.info(`server listening on http://localhost:${port}`);
});
const shutdown = () => {
logger.info('shutting down server...');
server.close(() => {
logger.info('http server closed');
db.pool.end(() => {
logger.info('pg pool has ended');
process.exit(0);
});
});
};
process.on('SIGINT', shutdown);
process.on('SIGTERM', shutdown);

View File

@@ -0,0 +1,21 @@
const db = require('../config/db');
const { comparePassword } = require('../util/hash');
const { logger } = require('../util/logger');
async function findUserByCustomerNo(customerNo) {
const result = await db.query('SELECT * FROM users WHERE customer_no = $1', [
customerNo,
]);
return result.rows[0];
}
async function validateUser(customerNo, password) {
const user = await findUserByCustomerNo(customerNo);
if (!user) return null;
logger.info(typeof user);
logger.info(user, 'user from database');
const isMatch = await comparePassword(password, user.password_hash);
return isMatch ? user : null;
}
module.exports = { validateUser };

17
src/util/hash.js Normal file
View File

@@ -0,0 +1,17 @@
const bcrypt = require('bcrypt');
const { logger } = require('./logger');
async function hashPassword(password) {
return await bcrypt.hash(password, 10);
}
async function comparePassword(plain, hash) {
const hashedPassword = await hashPassword(plain);
logger.info(hashedPassword, 'passed password after hashing');
return await bcrypt.compare(plain, hash);
}
module.exports = {
hashPassword,
comparePassword,
};

15
src/util/jwt.js Normal file
View File

@@ -0,0 +1,15 @@
const jwt = require('jsonwebtoken');
const { jwtSecret } = require('../config/config');
function generateToken(payload, expiresIn = '1h') {
return jwt.sign({ payload }, jwtSecret, { expiresIn });
}
function verifyToken(token) {
return jwt.verify(token, jwtSecret);
}
module.exports = {
generateToken,
verifyToken,
};

22
src/util/logger.js Normal file
View File

@@ -0,0 +1,22 @@
const pino = require('pino');
const isDev = process.env.NODE_ENV !== 'production';
const logger = pino({
transport: isDev
? {
target: 'pino-pretty',
options: {
colorize: true,
translateTime: 'SYS:standard',
ignore: 'pid,hostname',
},
}
: undefined,
level: isDev ? 'debug' : 'info',
});
const requestLogger = (req, _res, next) => {
logger.info(`${req.method} ${req.url}`);
next();
};
module.exports = { logger, requestLogger };