-
Notifications
You must be signed in to change notification settings - Fork 0
/
awePreSignedUpTrigger.js
122 lines (90 loc) · 4.14 KB
/
awePreSignedUpTrigger.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
const mysql = require('mysql');
exports.handle = function (event, context) {
// Provide MySQL Connection Details from the AWS Lambda env vars
const connection = mysql.createConnection({
host: process.env.DATABASE_HOST,
user: process.env.DATABASE_USERNAME,
password: process.env.DATABASE_PASSWORD,
database: process.env.DATABASE_NAME,
multipleStatements: true
});
// Start Database Connection
connection.connect();
// This use case check whether email, phone number or Username already associated with an another account on the database
var isEmailExist = false;
var isPhoneNumberExist = false;
var isUsernameExist = false;
event.userName = event.userName.trim().toLocaleLowerCase();
event.request.userAttributes.email = event.request.userAttributes.email.trim().toLocaleLowerCase();
getUsernameEmailAndPhoneNumberExistForDriverPromise(event, connection)
.then(function (results) {
if (results[0].result == 1) {
isEmailExist = true;
}
if (results[1].result == 1) {
isPhoneNumberExist = true;
}
if (results[2].result == 1) {
isUsernameExist = true;
}
if (isEmailExist == true && isPhoneNumberExist == true) {
var error = new Error("EMAIL_PHONE_BOTH_EXIST");
context.done(error, event);
} else if (isEmailExist == true) {
var error = new Error("EMAIL_EXIST");
context.done(error, event);
} else if (isPhoneNumberExist == true) {
var error = new Error("PHONE_EXIST");
context.done(error, event);
} else if (isUsernameExist == true) {
var error = new Error("USERNAME_EXIST");
context.done(error, event);
} else {
const driverUsername = event.userName;
const email = event.request.userAttributes.email;
const phoneNumber = event.request.userAttributes.phone_number;
return insertNewDriverInfoToDb(driverUsername, phoneNumber, email, connection);
}
})
.then(function (result) {
connection.end();
context.done(null, event);
})
.catch(function (error) {
console.log(error);
connection.end();
var error = new Error("DATABASE_ERROR");
context.done(error, event);
})
}
// Check Email, Phone Number or Phone Number exists
function getUsernameEmailAndPhoneNumberExistForDriverPromise(event, connection) {
return new Promise(function (resolve, reject) {
const driverUsername = event.userName;
const email = event.request.userAttributes.email;
const phoneNumber = event.request.userAttributes.phone_number;
var getEmailAndPhoneNumberExistForDriverPromiseQuery = "SELECT count(*) AS 'result' FROM Driver WHERE email = ? UNION ALL SELECT count(*) AS 'result' FROM Driver WHERE phone_number = ? UNION ALL SELECT count(*) AS 'result' FROM Driver WHERE driver_username = ?;";
const query = connection.query(getEmailAndPhoneNumberExistForDriverPromiseQuery, [email, phoneNumber, driverUsername], function (error, results, fields) {
console.log(query.sql);
if (error) {
reject(error);
} else {
resolve(results);
}
});
});
}
// Insert New Driver info the table
function insertNewDriverInfoToDb(driver_username, phone_number, email, connection) {
return new Promise(function (resolve, reject) {
var insertNewDriverQuery = `INSERT INTO Driver (driver_id, driver_username, phone_number, email, first_name, last_name, date_of_birth) VALUES (NULL, ?, ?, ?, "", "", "");`;
const query = connection.query(insertNewDriverQuery, [driver_username, phone_number, email], function (error, results, fields) {
console.log(query.sql);
if (error != null) {
reject(error);
} else {
resolve(results);
}
});
});
}