-
Notifications
You must be signed in to change notification settings - Fork 0
/
menu.js
83 lines (70 loc) · 2.4 KB
/
menu.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
const { randomInt } = require('crypto');
const db = require('./database');
class Session {
ussd_proceed(message, _id, nextStep) {
return `CON ${message}`;
}
ussd_end(message) {
return `END ${message}`;
}
}
class Menu {
constructor(session) {
this.session = session;
}
randomOtpGenerator() {
const strLen = 4;
return randomInt(1000, 10000).toString();
}
home(_id) {
const menu = `Main Menu \n1. Get OTP\n2. Validate Access Code\n`;
return this.session.ussd_proceed(menu, _id, '1');
}
generateOtp() {
const otp = this.randomOtpGenerator();
const message = `Generate OTP\nYour generated OTP is: ${otp}\n`;
return this.session.ussd_end(message);
}
validateAccessCode(text, _id, phoneNumber) {
let phaseStr = '';
if (text.split('*').length === 1) {
phaseStr = 'Validate Access Code\nPlease enter your access code.\n';
return this.session.ussd_proceed(phaseStr, _id, '');
}
const inputCode = text.split('*')[1];
const currentDate = new Date().toISOString().split('T')[0];
return new Promise((resolve, reject) => {
db.get(`SELECT * FROM estates WHERE access_code = ? AND valid_until >= ?`, [inputCode, currentDate], (err, row) => {
if (err) {
reject(err);
}
if (row) {
phaseStr = `Access Code Valid\nThe access code ${inputCode} is valid.\n`;
} else {
phaseStr = `Access Code Invalid\nThe access code ${inputCode} is invalid or expired.\n`;
}
resolve(this.session.ussd_end(phaseStr));
});
});
}
checkSecurityNumber(phoneNumber) {
console.log("Checking if security phone number", phoneNumber)
return new Promise((resolve, reject) => {
db.get(`SELECT * FROM security_numbers WHERE phone_number = ?`, [phoneNumber], (err, row) => {
if (err) {
reject(err);
}
if (row) {
resolve(true);
} else {
resolve(false);
}
});
});
}
unavailable() {
return this.session.ussd_end("Service Unavailable");
}
}
module.exports = Menu;
// module.exports = Session;