Skip to content

Commit c9de61b

Browse files
author
Ravi Maurya
authored
ethvjti apis (#222)
1 parent 040ddaf commit c9de61b

File tree

3 files changed

+118
-0
lines changed

3 files changed

+118
-0
lines changed
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
const EthUser = require("../models/EthVJTIWallet");
2+
const sendEmail = require("../utility/sendEmail");
3+
4+
const generateOTP = () => {
5+
var digits = "0123456789";
6+
let OTP = "";
7+
for (let i = 0; i < 4; i++) {
8+
OTP += digits[Math.floor(Math.random() * 10)];
9+
}
10+
return OTP;
11+
};
12+
13+
const sendMailToSingerUser = async (otp, email) => {
14+
const mailSubject = "EthVJTI Launch NFT One Time Password";
15+
const mailData = `Your OTP for EthVJTI Launch NFT is: ${otp}`;
16+
await sendEmail(email, mailSubject, mailData);
17+
};
18+
19+
const sendOTP = async (req, res) => {
20+
try {
21+
const { email, walletAddress } = req.body;
22+
const newOTP = generateOTP();
23+
24+
sendMailToSingerUser(newOTP, email);
25+
26+
const userDetails = {
27+
email,
28+
walletAddress,
29+
isMinted: false,
30+
emailVerificationOTP: newOTP,
31+
};
32+
33+
const userCheck = await EthUser.findOne({
34+
email: email,
35+
})
36+
.lean();
37+
38+
let newUser;
39+
if(userCheck){
40+
if(userCheck.isMinted){
41+
return res.status(400).json({ error: "NFT is already minted for this user" });
42+
}
43+
newUser = await EthUser.findByIdAndUpdate(
44+
userCheck._id,
45+
userDetails,
46+
{new: true}
47+
);
48+
}else{
49+
res.status(404).json({error: "Email ID was not registered"});
50+
}
51+
52+
return res
53+
.status(200)
54+
.json({
55+
message: `OTP Sent to ${email}`,
56+
user: newUser,
57+
});
58+
} catch (error) {
59+
return res.status(400).json({ error: error.message });
60+
}
61+
};
62+
63+
const verifyOTP = async (req, res) => {
64+
try {
65+
const { email, otp } = req.body;
66+
67+
const userCheck = await EthUser.findOne({
68+
email: email,
69+
})
70+
.lean();
71+
if(userCheck && userCheck.emailVerificationOTP == otp){
72+
await EthUser.findByIdAndUpdate(userCheck._id, {isMinted: true, emailVerificationOTP: ""});
73+
return res.status(200).json({ message: "OTP Correct", email: email });
74+
}
75+
return res.status(400).json({ error: "OTP not valid" });
76+
} catch (error) {
77+
return res.status(400).json({ error: error.message });
78+
}
79+
};
80+
81+
module.exports = {
82+
sendOTP,
83+
verifyOTP
84+
}

server/src/models/EthVJTIWallet.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
const mongoose = require('mongoose')
2+
3+
const ethuser = new mongoose.Schema({
4+
email: {
5+
type: String,
6+
required: true
7+
},
8+
walletAddress: {
9+
type: String,
10+
required: false
11+
},
12+
isMinted: {
13+
type: Boolean,
14+
required: false,
15+
default: false,
16+
},
17+
emailVerificationOTP: {
18+
type: String,
19+
required: false
20+
}
21+
})
22+
23+
ethuser.index({ email: 1 })
24+
25+
const EthUser = mongoose.model('ethuser', ethuser)
26+
27+
module.exports = EthUser;

server/src/routes.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const AchievementsController = require("./controllers/AchievementsController");
1111
const MagazineController = require("./controllers/MagazineController");
1212
const CompanyController = require("./controllers/CompanyController");
1313
const InterviewController = require("./controllers/InterviewController");
14+
const EthVJTIController = require("./controllers/EthVJTIController");
1415
const upload = require("./middleware/upload");
1516
const auth = require("./middleware/auth");
1617
const blog = require("./middleware/blog");
@@ -319,4 +320,10 @@ module.exports = (app) => {
319320
app.post('/api/interview/draft', auth.verifyToken, InterviewController.saveDraftInterview, cache.deleteCache);
320321
app.post('/api/interview/verify', auth.verifyToken, user.isMember, InterviewController.verifyInterview, cache.deleteCache);
321322
app.post('/api/interviewImageUpload', auth.verifyToken, upload.single('expImage'), InterviewController.uploadImage, cache.deleteCache);
323+
324+
// EthVJTI
325+
app.post('/api/ethvjti/sendotp', EthVJTIController.sendOTP);
326+
app.get('/api/ethvjti/verifyotp', EthVJTIController.verifyOTP);
327+
322328
}
329+

0 commit comments

Comments
 (0)