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

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