dbController.ts 7.03 KB
Newer Older
Rosanny Sihombing's avatar
Rosanny Sihombing committed
1
import { dbConnection } from '../config/dbconn'
Rosanny Sihombing's avatar
Rosanny Sihombing committed
2

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

Rosanny Sihombing's avatar
Rosanny Sihombing committed
208
export { dbController }