8000 :tada: added validation checks · CRYPTOcoderAS/LMS-BE@9639a98 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9639a98

Browse files
🎉 added validation checks
1 parent 04058e5 commit 9639a98

File tree

3 files changed

+126
-6
lines changed

3 files changed

+126
-6
lines changed

controllers/loanController.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
11
const Loan = require('../models/Loan');
2+
const { validateLoanData, validateObjectId } = require('../utils/validations');
23
const { calculateEMISchedule } = require('../utils/loanCalculations');
3-
const { createCsvStringifier } = require('csv-writer');
44

55
exports.createLoan = async (req, res) => {
66
try {
7+
const validationError = validateLoanData(req.body);
8+
if (validationError) {
9+
return res.status(400).json({ error: validationError });
10+
}
11+
12+
if (!validateObjectId(req.body.userId)) {
13+
return res.status(400).json({ error: 'Invalid user ID format' });
14+
}
15+
716
const { userId, disbursementDate, amount, interestRate, tenure } = req.body;
817
const emiSchedule = calculateEMISchedule(amount, interestRate, tenure, disbursementDate);
918

@@ -29,7 +38,6 @@ exports.createLoan = async (req, res) => {
2938
res.status(500).json({ error: 'Server error' });
3039
}
3140
};
32-
3341
exports.getLoanLedger = async (req, res) => {
3442
try {
3543
const loan = await Loan.findById(req.params.id).populate('userId');

controllers/userController.js

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,40 @@
11
const User = require('../models/User');
2+
const { validateUserData, validateObjectId } = require('../utils/validations');
23

34
exports.createUser = async (req, res) => {
45
try {
6+
const validationError = validateUserData(req.body);
7+
if (validationError) {
8+
return res.status(400).json({ error: validationError });
9+
}
10+
511
const user = new User(req.body);
612
await user.save();
713
res.status(201).json(user);
814
} catch (error) {
915
if (error.code === 11000) {
10-
return res.status(400).json({ error: 'Duplicate entry found' });
16+
return res.status(400).json({
17+
error: 'Duplicate entry found. PAN, Aadhaar, GSTIN, or UDYAM already exists.'
18+
});
1119
}
1220
res.status(500).json({ error: 'Server error' });
1321
}
1422
};
1523

1624
exports.getUser = async (req, res) => {
1725
try {
18-
const user = await User.findById(req.params.id);
19-
if (!user) return res.status(404).json({ error: 'User not found' });
26+
const { id } = req.params;
27+
28+
if (!validateObjectId(id)) {
29+
return res.status(400).json({ error: 'Invalid user ID format' });
30+
}
31+
32+
const user = await User.findById(id);
33+
if (!user) {
34+
return res.status(404).json({ error: 'User not found' });
35+
}
2036
res.json(user);
2137
} catch (error) {
2238
res.status(500).json({ error: 'Server error' });
2339
}
24-
};
40+
};

utils/validations.js

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
const validatePAN = (pan) => {
2+
const panRegex = /^[A-Z]{5}[0-9]{4}[A-Z]{1}$/;
3+
return panRegex.test(pan);
4+
};
5+
6+
const validateAadhaar = (aadhaar) => {
7+
const aadhaarRegex = /^\d{12}$/;
8+
return aadhaarRegex.test(aadhaar);
9+
};
10+
11+
const validateGSTIN = (gstin) => {
12+
const gstinRegex = /^[0-9]{2}[A-Z]{5}[0-9]{4}[A-Z]{1}[1-9A-Z]{1}Z[0-9A-Z]{1}$/;
13+
return gstinRegex.test(gstin);
14+
};
15+
16+
const validateUDYAM = (udyam) => {
17+
const udyamRegex = /^UDYAM-[A-Z]{2}-\d{2}-\d{7}$/;
18+
return udyamRegex.test(udyam);
19+
};
20+
< F438 code>21+
const validateAge = (dob) => {
22+
const today = new Date();
23+
const birthDate = new Date(dob);
24+
let age = today.getFullYear() - birthDate.getFullYear();
25+
const monthDiff = today.getMonth() - birthDate.getMonth();
26+
27+
if (monthDiff < 0 || (monthDiff === 0 && today.getDate() < birthDate.getDate())) {
28+
age--;
29+
}
30+
31+
return age >= 18;
32+
};
33+
34+
exports.validateUserData = (userData) => {
35+
const { name, dob, pan, aadhaar, gstin, udyam } = userData;
36+
37+
if (!name || name.trim().length < 2) {
38+
return 'Name must be at least 2 characters long';
39+
}
40+
41+
if (!dob || !validateAge(dob)) {
42+
return 'User must be at least 18 years old';
43+
}
44+
45+
if (!pan || !validatePAN(pan)) {
46+
return 'Invalid PAN format. Should be like ABCDE1234F';
47+
}
48+
49+
if (!aadhaar || !validateAadhaar(aadhaar)) {
50+
return 'Invalid Aadhaar format. Should be 12 digits';
51+
}
52+
53+
if (!gstin || !validateGSTIN(gstin)) {
54+
return 'Invalid GSTIN format. Should be like 27AAPFU0939F1ZV';
55+
}
56+
57+
if (!udyam || !validateUDYAM(udyam)) {
58+
return 'Invalid UDYAM format. Should be like UDYAM-MH-02-0000001';
59+
}
60+
61+
return null;
62+
};
63+
64+
exports.validateLoanData = (loanData) => {
65+
const { amount, interestRate, tenure, disbursementDate } = loanData;
66+
67+
if (!amount || amount <= 0) {
68+
return 'Loan amount must be greater than 0';
69+
}
70+
71+
if (!interestRate || interestRate <= 0 || interestRate > 100) {
72+
return 'Interest rate must be between 0 and 100';
73+
}
74+
75+
if (!tenure || tenure <= 0) {
76+
return 'Tenure must be greater than 0';
77+
}
78+
79+
if (!disbursementDate) {
80+
return 'Disbursement date is required';
81+
}
82+
83+
const disbursementDateTime = new Date(disbursementDate).getTime();
84+
const today = new Date().getTime();
85+
86+
if (isNaN(disbursementDateTime) || disbursementDateTime < today) {
87+
return 'Disbursement date must be today or a future date';
88+
}
89+
90+
return null;
91+
};
92+
93+
exports.validateObjectId = (id) => {
94+
const objectIdRegex = /^[0-9a-fA-F]{24}$/;
95+
return objectIdRegex.test(id);
96+
};

0 commit comments

Comments
 (0)
0