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

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

Rosanny Sihombing's avatar
Rosanny Sihombing committed
217
export = methods