"src/routes/public.ts" did not exist on "ef977518050ea34c757091e952ed5b82b45f5e23"
methods.js 8.69 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
    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+'"')
Rosanny Sihombing's avatar
Rosanny Sihombing committed
71
72
73
74
            if (rows[0][0]) {
                return rows[0][0]
            }
            else { return null }
75
76
77
        } catch (err) {
            console.error(err)
        }
Rosanny Sihombing's avatar
Rosanny Sihombing committed
78
        return null
79
    },
Rosanny Sihombing's avatar
Rosanny Sihombing committed
80
81
82
    getUserEmailById: async function(userId) {
        try {
            let rows = await dbconn.user.promise().query('SELECT email FROM user WHERE id = ' +userId)
Rosanny Sihombing's avatar
Rosanny Sihombing committed
83
            if (rows[0][0]) {
Rosanny Sihombing's avatar
Rosanny Sihombing committed
84
                return rows[0][0].email
85
            }
Rosanny Sihombing's avatar
Rosanny Sihombing committed
86
87
88
89
90
            else { return null }
        } catch (err) {
            console.error(err)
        }
        return null
91
    },
Rosanny Sihombing's avatar
Rosanny Sihombing committed
92
93
94
    checkUserEmail: async function(email) {
        try {
            let rows = await dbconn.user.promise().query('SELECT id, email FROM user WHERE email = "' +email+'"')
Rosanny Sihombing's avatar
Rosanny Sihombing committed
95
            if (rows[0][0]) {
Rosanny Sihombing's avatar
Rosanny Sihombing committed
96
                return rows[0][0]
Rosanny Sihombing's avatar
Rosanny Sihombing committed
97
            }
Rosanny Sihombing's avatar
Rosanny Sihombing committed
98
99
100
101
102
            else { return null }
        } catch (err) {
            console.error(err)
        }
        return null
Rosanny Sihombing's avatar
Rosanny Sihombing committed
103
    },
Rosanny Sihombing's avatar
Rosanny Sihombing committed
104
105
106
107
    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())
Rosanny Sihombing's avatar
Rosanny Sihombing committed
108
            if (rows[0][0]) {
Rosanny Sihombing's avatar
Rosanny Sihombing committed
109
                return rows[0][0]
Rosanny Sihombing's avatar
Rosanny Sihombing committed
110
            }
Rosanny Sihombing's avatar
Rosanny Sihombing committed
111
112
113
114
115
            else { return null }
        } catch (err) {
            console.error(err)
        }
        return null
Rosanny Sihombing's avatar
Rosanny Sihombing committed
116
    },
117
	updateUserById: function(userData, callback) {
118
		dbconn_OBSOLETE.user.query('UPDATE user SET ? WHERE id = ' +userData.id, userData, function (err, rows, fields) {
119
120
            if (err) throw err
            callback(err)
Rosanny Sihombing's avatar
Rosanny Sihombing committed
121
122
        })
    },
123
    updateCredential: function(data, callback) {
124
        dbconn_OBSOLETE.user.query('UPDATE credential SET ? WHERE user_id = ' +data.user_id, data, function (err, rows, fields) {
125
126
            if (err) throw err
            callback(err)
Rosanny Sihombing's avatar
Rosanny Sihombing committed
127
128
        })
    },
Rosanny Sihombing's avatar
Rosanny Sihombing committed
129
    getUserIdByEmail_OBSOLETE: function(email, callback) {
130
        let userId
131
        dbconn_OBSOLETE.user.query('SELECT id FROM user WHERE email = "' +email+'"', function (err, rows, fields) {
Rosanny Sihombing's avatar
Rosanny Sihombing committed
132
            if (err) {
133
                throw err
Rosanny Sihombing's avatar
Rosanny Sihombing committed
134
135
136
            }
            else {
                if ( rows.length > 0) {
137
                    userId = rows[0].id
Rosanny Sihombing's avatar
Rosanny Sihombing committed
138
139
                }
            }
140
            callback(userId, err)
Rosanny Sihombing's avatar
Rosanny Sihombing committed
141
142
        });
    },
Rosanny Sihombing's avatar
Rosanny Sihombing committed
143
    getUserProjectRole_OBSOLETE: function(userId, callback) {
144
        dbconn_OBSOLETE.user.query('SELECT project_id, role_id FROM user_project_role WHERE user_id = "' +userId+'"', function (err, rows, fields) {
145
146
            if (err) throw err
            callback(rows, err)
Rosanny Sihombing's avatar
Rosanny Sihombing committed
147
148
        });
    },
Wolfgang Knopki's avatar
Wolfgang Knopki committed
149
    addUserProjectRole: function(data, callback) {
150
        dbconn_OBSOLETE.user.query('INSERT INTO user_project_role SET ?', data, function (err, results, fields){
151
152
153
154
            if (err) throw err
            callback(err)
        })
    },
Rosanny Sihombing's avatar
Rosanny Sihombing committed
155
156
157
    getVerificationTokenByUserId: async function(userId) {
        try {
            let rows = await dbconn.user.promise().query('SELECT token FROM verification WHERE user_id = "' +userId+'"')
Rosanny Sihombing's avatar
Rosanny Sihombing committed
158
            if (rows[0][0]) {
Rosanny Sihombing's avatar
Rosanny Sihombing committed
159
                return rows[0][0].token
160
            }
Rosanny Sihombing's avatar
Rosanny Sihombing committed
161
162
163
164
165
            else { return null }
        } catch (err) {
            console.error(err)
        }
        return null
166
    },
Rosanny Sihombing's avatar
Rosanny Sihombing committed
167
168
169
    getUserIdByVerificationToken: async function(token) {
        try {
            let rows = await dbconn.user.promise().query('SELECT user_id FROM verification WHERE token = "' +token+'"')
Rosanny Sihombing's avatar
Rosanny Sihombing committed
170
            if (rows[0][0]) {
Rosanny Sihombing's avatar
Rosanny Sihombing committed
171
                return rows[0][0].user_id
172
            }
Rosanny Sihombing's avatar
Rosanny Sihombing committed
173
174
            else {
                return null
175
            }
Rosanny Sihombing's avatar
Rosanny Sihombing committed
176
177
178
179
        } catch (err) {
            console.error(err)
        }
        return null
180
181
    },
    verifyUserAccount: function(userData, callback) {
182
        dbconn_OBSOLETE.user.beginTransaction(function(err) { // START TRANSACTION
183
184
            if (err) { throw err }
            // update user status
185
            dbconn_OBSOLETE.user.query('UPDATE user SET ? WHERE id =' +userData.id, userData, function (err, rows, fields) {
186
                if (err) {
187
                    return dbconn_OBSOLETE.user.rollback(function() { throw err })
188
189
                }
                // delete verification token
190
                dbconn_OBSOLETE.user.query('DELETE FROM verification WHERE user_id = '+userData.id, function (err, rows, fields) {
191
                    if (err) {
192
                        return dbconn_OBSOLETE.user.rollback(function() { throw err })
193
194
                    }
                    // COMMIT
195
                    dbconn_OBSOLETE.user.commit(function(err) {
196
                        if (err) {
197
                            return dbconn_OBSOLETE.user.rollback(function() { throw err })
198
199
200
201
202
                        }
                    })
                })
            })
            callback(err)
Wolfgang Knopki's avatar
Wolfgang Knopki committed
203
        })
204
205
    },
    /* ===== GitLab ===== */
206
207
208
    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
209
210
211
212
213
            if (rows[0][0]) {
                return rows[0][0].gitlab_userId
            } else {
                return null
            }
214
215
216
217
218
219
        }
        catch(err) {
            console.error(err)
            return err
        }
    },
220
    addGitlabUser: function(data, callback){
221
        dbconn_OBSOLETE.user.query('INSERT INTO user_gitlab SET ?', data, function (err) {
222
223
224
            if (err) throw err
            callback(err)
        })
Rosanny Sihombing's avatar
Rosanny Sihombing committed
225
226
227
228
    }
};

module.exports = methods;