methods.ts 8.37 KB
Newer Older
Rosanny Sihombing's avatar
Rosanny Sihombing committed
1
import dbconn = require('../config/dbconn')
Rosanny Sihombing's avatar
Rosanny Sihombing committed
2
3

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

215
export = methods