diff options
author | rohan09-raj <rajrohan1914@gmail.com> | 2022-08-16 12:15:55 +0530 |
---|---|---|
committer | rohan09-raj <rajrohan1914@gmail.com> | 2022-08-16 12:15:55 +0530 |
commit | 1c9a3d21cc3479d299181d98fb895614e0aaf649 (patch) | |
tree | d871c9821cab708ed7f12169db6d59a8128c76ce /server | |
parent | 5be7add30a629474c9269355b875ab310a82ba4c (diff) |
random bullshit
Diffstat (limited to 'server')
-rw-r--r-- | server/.env | 9 | ||||
-rw-r--r-- | server/constants/aadhaar.js | 1 | ||||
-rw-r--r-- | server/controllers/users.js | 25 | ||||
-rw-r--r-- | server/routes/users.js | 1 | ||||
-rw-r--r-- | server/utils/aadhaar.js | 117 |
5 files changed, 150 insertions, 3 deletions
diff --git a/server/.env b/server/.env index 02b7c43..586c878 100644 --- a/server/.env +++ b/server/.env @@ -1,2 +1,7 @@ -CONNECTION_URL = <MONGODB_URI> -PORT = 5000
\ No newline at end of file +CONNECTION_URL = mongodb+srv://rohan:8Yf8aLiUHzNsg8bd@cluster0.xyo43.mongodb.net/?retryWrites=true&w=majority +PORT = 5000 + +//TWILIO +TWILIO_ACCOUNT_SID = AC77501e433321b984614102ea5d611580 +TWILIO_AUTH_TOKEN = c6823da64d024efcced488b482c1865f +TWILIO_PHONE_NUMBER = +14235645815
\ No newline at end of file diff --git a/server/constants/aadhaar.js b/server/constants/aadhaar.js new file mode 100644 index 0000000..b4405e8 --- /dev/null +++ b/server/constants/aadhaar.js @@ -0,0 +1 @@ +export const ELEVEN_TIMES_NINE = 99999999999; diff --git a/server/controllers/users.js b/server/controllers/users.js index 3dfed14..18f57b6 100644 --- a/server/controllers/users.js +++ b/server/controllers/users.js @@ -1,5 +1,6 @@ import UserDetails from '../models/users'; import sendOTP from '../services/twilio'; +import generateAadhaar from '../utils/aadhaar'; const createUser = async (req, res) => { const { @@ -54,6 +55,17 @@ const getUser = async (req, res) => { } }; +const getUserByAadhaarNumber = async (req, res) => { + const {aadhaar} = req.params; + const aadhaarNumber = Number(aadhaar); + try { + const user = await UserDetails.findOne({aadhaarNumber}); + res.status(200).json(user); + } catch (error) { + res.status(404).json({message: error}); + } +}; + const getVerifiedUsers = async (req, res) => { try { const verifiedUsers = await UserDetails.find({verified: true}); @@ -81,7 +93,17 @@ const updateUser = async (req, res) => { console.log(id, req.body); try { - await UserDetails.findByIdAndUpdate(id, {$set: req.body}, {new: true}); + const user = await UserDetails.findByIdAndUpdate( + id, + {$set: req.body}, + {new: true} + ); + + if (!user.aadhaarNumber) { + await UserDetails.findByIdAndUpdate(id, { + $set: {aadhaarNumber: generateAadhaar()}, + }); + } res.status(200).json({message: 'User Updated Successfully'}); } catch (error) { @@ -104,6 +126,7 @@ const deleteUser = async (req, res) => { export default { createUser, getUser, + getUserByAadhaarNumber, getVerifiedUsers, getUnverifiedUsers, updateUser, diff --git a/server/routes/users.js b/server/routes/users.js index aba2caf..beb38f1 100644 --- a/server/routes/users.js +++ b/server/routes/users.js @@ -5,6 +5,7 @@ const router = Express.Router(); router.post('/user', users.createUser); router.get('/user/:id', users.getUser); +router.get('/user/aadhaar/:aadhaar', users.getUserByAadhaarNumber); router.get('/verifiedusers', users.getVerifiedUsers); router.get('/unverifiedusers', users.getUnverifiedUsers); diff --git a/server/utils/aadhaar.js b/server/utils/aadhaar.js new file mode 100644 index 0000000..cd81711 --- /dev/null +++ b/server/utils/aadhaar.js @@ -0,0 +1,117 @@ +import {ELEVEN_TIMES_NINE} from '../constants/aadhaar'; + +// const generateCheckSumDigit = (randomNumberArray) => { +// const d = [ +// [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], +// [1, 2, 3, 4, 0, 6, 7, 8, 9, 5], +// [2, 3, 4, 0, 1, 7, 8, 9, 5, 6], +// [3, 4, 0, 1, 2, 8, 9, 5, 6, 7], +// [4, 0, 1, 2, 3, 9, 5, 6, 7, 8], +// [5, 9, 8, 7, 6, 0, 4, 3, 2, 1], +// [6, 5, 9, 8, 7, 1, 0, 4, 3, 2], +// [7, 6, 5, 9, 8, 2, 1, 0, 4, 3], +// [8, 7, 6, 5, 9, 3, 2, 1, 0, 4], +// [9, 8, 7, 6, 5, 4, 3, 2, 1, 0], +// ]; + +// const p = [ +// [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], +// [1, 5, 7, 6, 2, 8, 3, 0, 9, 4], +// [5, 8, 0, 3, 7, 9, 6, 1, 4, 2], +// [8, 9, 1, 6, 0, 4, 3, 5, 2, 7], +// [9, 4, 5, 3, 1, 2, 6, 8, 7, 0], +// [4, 2, 8, 6, 5, 7, 3, 9, 0, 1], +// [2, 7, 9, 3, 8, 0, 6, 4, 1, 5], +// [7, 0, 4, 6, 9, 1, 3, 2, 5, 8], +// ]; + +// const inv = [0, 4, 3, 2, 1, 5, 6, 7, 8, 9]; + +// let c = 0; +// for (let i = 0; i < randomNumberArray.length; i++) { +// c = d[c][p[(i + 1) % 8][randomNumberArray[i]]]; +// } +// return inv[c]; +// }; + +// const generateAadhaar = () => { +// const aadhaarNumberArray = []; +// let randomNumber = Math.floor(Math.random() * ELEVEN_TIMES_NINE + 1); + +// while (randomNumber != 0) { +// const digit = randomNumber % 10; +// aadhaarNumberArray.push(digit); +// randomNumber = Math.floor(randomNumber / 10); +// } + +// const checkSumDigit = generateCheckSumDigit(aadhaarNumberArray); +// aadhaarNumberArray.reverse(); +// aadhaarNumberArray.push(checkSumDigit); +// let aadhaarNumber = 0; +// aadhaarNumberArray.forEach((value, index) => { +// aadhaarNumber = aadhaarNumber + value; +// if (index !== aadhaarNumberArray.length - 1) { +// aadhaarNumber = aadhaarNumber * 10; +// } +// }); + +// return aadhaarNumber; +// }; + +function generate(array) { + const d = [ + [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], + [1, 2, 3, 4, 0, 6, 7, 8, 9, 5], + [2, 3, 4, 0, 1, 7, 8, 9, 5, 6], + [3, 4, 0, 1, 2, 8, 9, 5, 6, 7], + [4, 0, 1, 2, 3, 9, 5, 6, 7, 8], + [5, 9, 8, 7, 6, 0, 4, 3, 2, 1], + [6, 5, 9, 8, 7, 1, 0, 4, 3, 2], + [7, 6, 5, 9, 8, 2, 1, 0, 4, 3], + [8, 7, 6, 5, 9, 3, 2, 1, 0, 4], + [9, 8, 7, 6, 5, 4, 3, 2, 1, 0], + ]; + + const p = [ + [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], + [1, 5, 7, 6, 2, 8, 3, 0, 9, 4], + [5, 8, 0, 3, 7, 9, 6, 1, 4, 2], + [8, 9, 1, 6, 0, 4, 3, 5, 2, 7], + [9, 4, 5, 3, 1, 2, 6, 8, 7, 0], + [4, 2, 8, 6, 5, 7, 3, 9, 0, 1], + [2, 7, 9, 3, 8, 0, 6, 4, 1, 5], + [7, 0, 4, 6, 9, 1, 3, 2, 5, 8], + ]; + const inv = [0, 4, 3, 2, 1, 5, 6, 7, 8, 9]; + + let c = 0; + for (let i = 0; i < array.length; i++) { + c = d[c][p[(i + 1) % 8][array[i]]]; + } + return inv[c]; +} + +function generateAadhaar() { + const aadhaarArray = []; + let x = Math.floor(Math.random() * ELEVEN_TIMES_NINE + 1); + + while (x != 0) { + const digit = x % 10; + aadhaarArray.push(digit); + x = Math.floor(x / 10); + } + const last_digit = generate(aadhaarArray); + aadhaarArray.reverse(); + aadhaarArray.push(last_digit); + let ans = 0; + aadhaarArray.forEach((value, index) => { + ans = ans + value; + if (index !== aadhaarArray.length - 1) { + ans = ans * 10; + } + }); + + return ans; +} + +export default generateAadhaar; |