-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
60 lines (46 loc) · 1.17 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
const express = require('express')
const { fetchDoorFunc, updateDoorAccount } = require('./hygraphService')
const { SendMail } = require('./MailService');
const app = express()
app.use(express.json());
app.use(express.urlencoded({ extended: false }))
let doors = [];
let results;
const OTPGenerator = () => {
let otp = Math.floor(Math.random() * 899999) + 100000;
console.log(otp, 'otp')
return otp;
}
const fetchDoorEmail = async (doorId) => {
results = await fetchDoorFunc();
doors = results;
console.log(doors);
let email;
doors.map(async (door) => {
if (door.doorId.substr(0, 15) == doorId.substr(0, 15)) { email = door.email }
})
return email
}
app.get('/', (req, res) => {
res.send(OTPGenerator())
})
app.get('/api/otp', (req, res) => {
res.json(OTPGenerator())
})
app.post('/api/otp', async (req, res) => {
const email = await fetchDoorEmail(req.body.doorId);
try {
SendMail({ email: email, otp: req.body.otp })
setTimeout(() => {
res.send("success")
}, 2000);
}
catch {
console.log('err')
res.send("error");
}
})
const PORT = 3001
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`)
})