methods.js 8.59 KB
Newer Older
Rosanny Sihombing's avatar
Rosanny Sihombing committed
1
2
const dbconn_OBSOLETE = require('../config/dbconn') // DO NOT USE THIS FOR NEW FUNCTIONS
const dbconn = require('../config/dbconn2')
Rosanny Sihombing's avatar
Rosanny Sihombing committed
3
4

var methods = {
5
6
    // ===================== user db =====================
    registerNewUser: function(data, callback) {
7
        dbconn_OBSOLETE.user.beginTransaction(function(err) { // START TRANSACTION
8
9
10
11
            if (err) {
                throw err
            }
            // insert profile
12
            dbconn_OBSOLETE.user.query('INSERT INTO user SET ?', data.profile, function (err, results, fields) {
13
                if (err) {
14
                  return dbconn_OBSOLETE.user.rollback(function() {
15
16
17
18
19
20
21
22
23
                    throw err
                  });
                }
                var newUserId = results.insertId
                // set password
                var credentialData = {
                    user_id: newUserId,
                    password: data.password
                }
24
                dbconn_OBSOLETE.user.query('INSERT INTO credential SET ?', credentialData, function (err, results, fields) {
25
                    if (err) {
26
                        return dbconn_OBSOLETE.user.rollback(function() {
27
28
29
30
31
32
33
34
35
                            throw err
                        });
                    }
                    // set default user-project-role
                    var projectRoleData = {
                        project_id: 1,  //M4_LAB
                        role_id: 2,     // USER
                        user_id: newUserId
                    }
36
                    dbconn_OBSOLETE.user.query('INSERT INTO user_project_role SET ?', projectRoleData, function (err, results, fields) {
37
                        if (err) {
38
                            return dbconn_OBSOLETE.user.rollback(function() {
39
40
41
                                throw err
                            });
                        }
42
43
44
45
46
                        // MLAB-129: INSERT verification token
                        let verificationData = {
                            user_id: newUserId,
                            token: data.verificationToken
                        }
47
                        dbconn_OBSOLETE.user.query('INSERT INTO verification SET ?', verificationData, function (err, results, fields) {
48
                            if (err) {
49
                                return dbconn_OBSOLETE.user.rollback(function() {
50
51
52
                                    throw err
                                });
                            }
53
                            // COMMIT
54
                            dbconn_OBSOLETE.user.commit(function(err) {
55
                                if (err) {
56
                                    return dbconn_OBSOLETE.user.rollback(function() {
57
58
59
60
61
                                        throw err
                                    })
                                }
                            })
                        })
62
63
64
65
66
67
                    })
                });
            });
            callback(err)
        })
    },
68
69
70
71
72
73
74
    getUserByEmail: async function(email) {
        try {
            let rows = await dbconn.user.promise().query('SELECT id, verificationStatus, salutation, title, firstname, lastname, industry, organisation, speciality, m4lab_idp FROM user WHERE email = "' +email+'"')
            return rows[0][0]
        } catch (err) {
            console.error(err)
        }
Rosanny Sihombing's avatar
Rosanny Sihombing committed
75
        return null
76
    },
Rosanny Sihombing's avatar
Rosanny Sihombing committed
77
78
79
80
81
    getUserEmailById: async function(userId) {
        try {
            let rows = await dbconn.user.promise().query('SELECT email FROM user WHERE id = ' +userId)
            if (rows[0]) {
                return rows[0][0].email
82
            }
Rosanny Sihombing's avatar
Rosanny Sihombing committed
83
84
85
86
87
            else { return null }
        } catch (err) {
            console.error(err)
        }
        return null
88
    },
Rosanny Sihombing's avatar
Rosanny Sihombing committed
89
90
91
92
93
    checkUserEmail: async function(email) {
        try {
            let rows = await dbconn.user.promise().query('SELECT id, email FROM user WHERE email = "' +email+'"')
            if (rows[0]) {
                return rows[0][0]
Rosanny Sihombing's avatar
Rosanny Sihombing committed
94
            }
Rosanny Sihombing's avatar
Rosanny Sihombing committed
95
96
97
98
99
            else { return null }
        } catch (err) {
            console.error(err)
        }
        return null
Rosanny Sihombing's avatar
Rosanny Sihombing committed
100
    },
Rosanny Sihombing's avatar
Rosanny Sihombing committed
101
102
103
104
105
106
    getUserByToken: async function(token) {
        try {
            let rows = await dbconn.user.promise().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())
            if (rows[0]) {
                return rows[0][0]
Rosanny Sihombing's avatar
Rosanny Sihombing committed
107
            }
Rosanny Sihombing's avatar
Rosanny Sihombing committed
108
109
110
111
112
            else { return null }
        } catch (err) {
            console.error(err)
        }
        return null
Rosanny Sihombing's avatar
Rosanny Sihombing committed
113
    },
114
	updateUserById: function(userData, callback) {
115
		dbconn_OBSOLETE.user.query('UPDATE user SET ? WHERE id = ' +userData.id, userData, function (err, rows, fields) {
116
117
            if (err) throw err
            callback(err)
Rosanny Sihombing's avatar
Rosanny Sihombing committed
118
119
        })
    },
120
    updateCredential: function(data, callback) {
121
        dbconn_OBSOLETE.user.query('UPDATE credential SET ? WHERE user_id = ' +data.user_id, data, function (err, rows, fields) {
122
123
            if (err) throw err
            callback(err)
Rosanny Sihombing's avatar
Rosanny Sihombing committed
124
125
        })
    },
Rosanny Sihombing's avatar
Rosanny Sihombing committed
126
    getUserIdByEmail_OBSOLETE: function(email, callback) {
127
        let userId
128
        dbconn_OBSOLETE.user.query('SELECT id FROM user WHERE email = "' +email+'"', function (err, rows, fields) {
Rosanny Sihombing's avatar
Rosanny Sihombing committed
129
            if (err) {
130
                throw err
Rosanny Sihombing's avatar
Rosanny Sihombing committed
131
132
133
            }
            else {
                if ( rows.length > 0) {
134
                    userId = rows[0].id
Rosanny Sihombing's avatar
Rosanny Sihombing committed
135
136
                }
            }
137
            callback(userId, err)
Rosanny Sihombing's avatar
Rosanny Sihombing committed
138
139
        });
    },
Rosanny Sihombing's avatar
Rosanny Sihombing committed
140
    getUserProjectRole_OBSOLETE: function(userId, callback) {
141
        dbconn_OBSOLETE.user.query('SELECT project_id, role_id FROM user_project_role WHERE user_id = "' +userId+'"', function (err, rows, fields) {
142
143
            if (err) throw err
            callback(rows, err)
Rosanny Sihombing's avatar
Rosanny Sihombing committed
144
145
        });
    },
Wolfgang Knopki's avatar
Wolfgang Knopki committed
146
    addUserProjectRole: function(data, callback) {
147
        dbconn_OBSOLETE.user.query('INSERT INTO user_project_role SET ?', data, function (err, results, fields){
148
149
150
151
            if (err) throw err
            callback(err)
        })
    },
Rosanny Sihombing's avatar
Rosanny Sihombing committed
152
153
154
155
156
    getVerificationTokenByUserId: async function(userId) {
        try {
            let rows = await dbconn.user.promise().query('SELECT token FROM verification WHERE user_id = "' +userId+'"')
            if (rows[0]) {
                return rows[0][0].token
157
            }
Rosanny Sihombing's avatar
Rosanny Sihombing committed
158
159
160
161
162
            else { return null }
        } catch (err) {
            console.error(err)
        }
        return null
163
    },
Rosanny Sihombing's avatar
Rosanny Sihombing committed
164
165
166
167
168
    getUserIdByVerificationToken: async function(token) {
        try {
            let rows = await dbconn.user.promise().query('SELECT user_id FROM verification WHERE token = "' +token+'"')
            if (rows[0]) {
                return rows[0][0].user_id
169
            }
Rosanny Sihombing's avatar
Rosanny Sihombing committed
170
171
            else {
                return null
172
            }
Rosanny Sihombing's avatar
Rosanny Sihombing committed
173
174
175
176
        } catch (err) {
            console.error(err)
        }
        return null
177
178
    },
    verifyUserAccount: function(userData, callback) {
179
        dbconn_OBSOLETE.user.beginTransaction(function(err) { // START TRANSACTION
180
181
            if (err) { throw err }
            // update user status
182
            dbconn_OBSOLETE.user.query('UPDATE user SET ? WHERE id =' +userData.id, userData, function (err, rows, fields) {
183
                if (err) {
184
                    return dbconn_OBSOLETE.user.rollback(function() { throw err })
185
186
                }
                // delete verification token
187
                dbconn_OBSOLETE.user.query('DELETE FROM verification WHERE user_id = '+userData.id, function (err, rows, fields) {
188
                    if (err) {
189
                        return dbconn_OBSOLETE.user.rollback(function() { throw err })
190
191
                    }
                    // COMMIT
192
                    dbconn_OBSOLETE.user.commit(function(err) {
193
                        if (err) {
194
                            return dbconn_OBSOLETE.user.rollback(function() { throw err })
195
196
197
198
199
                        }
                    })
                })
            })
            callback(err)
Wolfgang Knopki's avatar
Wolfgang Knopki committed
200
        })
201
202
    },
    /* ===== GitLab ===== */
203
204
205
    getGitlabId: async function(userId) {
        try {
            let rows = await dbconn.user.promise().query('SELECT gu.gitlab_userId FROM user_gitlab gu, user u WHERE u.id = "' +userId+'" and gu.user_id = u.id')
Rosanny Sihombing's avatar
Rosanny Sihombing committed
206
207
208
209
210
            if (rows[0][0]) {
                return rows[0][0].gitlab_userId
            } else {
                return null
            }
211
212
213
214
215
216
        }
        catch(err) {
            console.error(err)
            return err
        }
    },
217
    addGitlabUser: function(data, callback){
218
        dbconn_OBSOLETE.user.query('INSERT INTO user_gitlab SET ?', data, function (err) {
219
220
221
            if (err) throw err
            callback(err)
        })
Rosanny Sihombing's avatar
Rosanny Sihombing committed
222
223
224
225
    }
};

module.exports = methods;