methods.js 8.14 KB
Newer Older
Rosanny Sihombing's avatar
Rosanny Sihombing committed
1
2
3
4
5
6
7
const dbconn = require('./dbconn');

var methods = {
    // test method
	currentDate: function() {
		console.log('Current Date is: ' + new Date().toISOString().slice(0, 10));
    },
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
    // ===================== user db =====================
    registerNewUser: function(data, callback) {
        dbconn.user.beginTransaction(function(err) { // START TRANSACTION
            if (err) {
                throw err
            }
            // insert profile
            dbconn.user.query('INSERT INTO user SET ?', data.profile, function (err, results, fields) {
                if (err) {
                  return dbconn.user.rollback(function() {
                    throw err
                  });
                }
                var newUserId = results.insertId
                // set password
                var credentialData = {
                    user_id: newUserId,
                    password: data.password
                }
                dbconn.user.query('INSERT INTO credential SET ?', credentialData, function (err, results, fields) {
                    if (err) {
                        return dbconn.user.rollback(function() {
                            throw err
                        });
                    }
                    // set default user-project-role
                    var projectRoleData = {
                        project_id: 1,  //M4_LAB
                        role_id: 2,     // USER
                        user_id: newUserId
                    }
                    dbconn.user.query('INSERT INTO user_project_role SET ?', projectRoleData, function (err, results, fields) {
                        if (err) {
                            return dbconn.user.rollback(function() {
                                throw err
                            });
                        }
45
46
47
48
49
50
                        // MLAB-129: INSERT verification token
                        let verificationData = {
                            user_id: newUserId,
                            token: data.verificationToken
                        }
                        dbconn.user.query('INSERT INTO verification SET ?', verificationData, function (err, results, fields) {
51
52
53
54
55
                            if (err) {
                                return dbconn.user.rollback(function() {
                                    throw err
                                });
                            }
56
57
58
59
60
61
62
63
64
                            // COMMIT
                            dbconn.user.commit(function(err) {
                                if (err) {
                                    return dbconn.user.rollback(function() {
                                        throw err
                                    })
                                }
                            })
                        })
65
66
67
68
69
70
71
                    })
                });
            });
            callback(err)
        })
    },
    getUserByEmail: function(email, callback) {
72
        dbconn.user.query('SELECT verificationStatus, salutation, title, firstname, lastname, industry, organisation, speciality, m4lab_idp FROM user WHERE email = "' +email+'"', function (err, rows, fields) {
73
74
75
76
77
78
79
80
81
82
            if (err) {
                throw err;
            }
            else {
                if ( rows.length > 0) {
                    user = rows[0];
                }
            }
            callback(user, err);
        });
83
84
85
86
87
88
89
90
91
92
93
94
95
    },
    getUserById: function(userId, callback) {
        dbconn.user.query('SELECT verificationStatus, email, salutation, title, firstname, lastname, industry, organisation, speciality FROM user WHERE id = ' +userId, function (err, rows, fields) {
            if (err) {
                throw err;
            }
            else {
                if ( rows.length > 0) {
                    user = rows[0];
                }
            }
            callback(user, err);
        });
96
    },
Rosanny Sihombing's avatar
Rosanny Sihombing committed
97
	checkUserEmail: function(email, callback) {
98
        let user
99
        dbconn.user.query('SELECT id, email FROM user WHERE email = "' +email+'"', function (err, rows, fields) {
Rosanny Sihombing's avatar
Rosanny Sihombing committed
100
101
102
103
104
105
106
107
108
109
110
            if (err) {
                throw err;
            }
            else {
                if ( rows.length > 0) {
                    user = rows[0];
                }
            }
            callback(err, user);
        });
    },
111
    getUserByToken: function(token, callback) {
112
        let user
113
114
115
116
117
118
119
120
121
122
        dbconn.user.query('SELECT t1.user_id, t2.email FROM userdb.credential AS t1 INNER JOIN userdb.user AS t2 ON t1.user_id = t2.id AND t1.resetPasswordToken = "'
            +token+'" and resetPasswordExpires > '+Date.now(), function (err, rows, fields) {
                if (err) {
                    throw err;
                }
                else {
                    if ( rows.length > 0) {
                        user = rows[0]
                        console.log(user)
                    }
Rosanny Sihombing's avatar
Rosanny Sihombing committed
123
                }
124
                callback(err, user);
Rosanny Sihombing's avatar
Rosanny Sihombing committed
125
            }
126
        );
Rosanny Sihombing's avatar
Rosanny Sihombing committed
127
    },
128
129
130
131
	updateUserById: function(userData, callback) {
		dbconn.user.query('UPDATE user SET ? WHERE id = ' +userData.id, userData, function (err, rows, fields) {
            if (err) throw err
            callback(err)
Rosanny Sihombing's avatar
Rosanny Sihombing committed
132
133
        })
    },
134
135
    updateCredential: function(data, callback) {
        dbconn.user.query('UPDATE credential SET ? WHERE user_id = ' +data.user_id, data, function (err, rows, fields) {
136
137
            if (err) throw err
            callback(err)
Rosanny Sihombing's avatar
Rosanny Sihombing committed
138
139
140
        })
    },
    getUserIdByEmail: function(email, callback) {
141
        let userId
Rosanny Sihombing's avatar
Rosanny Sihombing committed
142
143
        dbconn.user.query('SELECT id FROM user WHERE email = "' +email+'"', function (err, rows, fields) {
            if (err) {
144
                throw err
Rosanny Sihombing's avatar
Rosanny Sihombing committed
145
146
147
            }
            else {
                if ( rows.length > 0) {
148
                    userId = rows[0].id
Rosanny Sihombing's avatar
Rosanny Sihombing committed
149
150
                }
            }
151
            callback(userId, err)
Rosanny Sihombing's avatar
Rosanny Sihombing committed
152
153
154
155
        });
    },
    getUserProjectRole: function(userId, callback) {
        dbconn.user.query('SELECT project_id, role_id FROM user_project_role WHERE user_id = "' +userId+'"', function (err, rows, fields) {
156
157
            if (err) throw err
            callback(rows, err)
Rosanny Sihombing's avatar
Rosanny Sihombing committed
158
159
        });
    },
Wolfgang Knopki's avatar
Wolfgang Knopki committed
160
161
    addUserProjectRole: function(data, callback) {
        dbconn.user.query('INSERT INTO user_project_role SET ?', data, function (err, results, fields){
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
            if (err) throw err
            callback(err)
        })
    },
    getVerificationTokenByUserId: function(userId, callback) {
        let token
        dbconn.user.query('SELECT token FROM verification WHERE user_id = "' +userId+'"', function (err, rows, fields) {
            if (err) {
                throw err
            }
            else {
                if (rows.length > 0) {
                    token = rows[0].token
                }
            }
            callback(token, err)
        })
    },
    getUserIdByVerificationToken: function(token, callback) {
        let userId
        dbconn.user.query('SELECT user_id FROM verification WHERE token = "' +token+'"', function (err, rows, fields) {
            if (err) {
                throw err
            }
            else if(rows[0]) {
                userId = rows[0].user_id
            }
            callback(userId, err)
        })
    },
    verifyUserAccount: function(userData, callback) {
        dbconn.user.beginTransaction(function(err) { // START TRANSACTION
            if (err) { throw err }
            // update user status
            dbconn.user.query('UPDATE user SET ? WHERE id =' +userData.id, userData, function (err, rows, fields) {
                if (err) {
                    return dbconn.user.rollback(function() { throw err })
                }
                // delete verification token
                dbconn.user.query('DELETE FROM verification WHERE user_id = '+userData.id, function (err, rows, fields) {
                    if (err) {
                        return dbconn.user.rollback(function() { throw err })
                    }
                    // COMMIT
                    dbconn.user.commit(function(err) {
                        if (err) {
                            return dbconn.user.rollback(function() { throw err })
                        }
                    })
                })
            })
            callback(err)
Wolfgang Knopki's avatar
Wolfgang Knopki committed
214
        })
Rosanny Sihombing's avatar
Rosanny Sihombing committed
215
216
217
218
    }
};

module.exports = methods;