methods.js 6.4 KB
Newer Older
Rosanny Sihombing's avatar
Rosanny Sihombing committed
1
2
3
4
5
6
7
const dbconn = require('./dbconn');

var methods = {
    // test method
	currentDate: function() {
		console.log('Current Date is: ' + new Date().toISOString().slice(0, 10));
    },
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
    // ===================== user db =====================
    registerNewUser: function(data, callback) {
        dbconn.user.beginTransaction(function(err) { // START TRANSACTION
            if (err) {
                throw err
            }
            // insert profile
            dbconn.user.query('INSERT INTO user SET ?', data.profile, function (err, results, fields) {
                if (err) {
                  return dbconn.user.rollback(function() {
                    throw err
                  });
                }
                var newUserId = results.insertId
                // set password
                var credentialData = {
                    user_id: newUserId,
                    password: data.password
                }
                dbconn.user.query('INSERT INTO credential SET ?', credentialData, function (err, results, fields) {
                    if (err) {
                        return dbconn.user.rollback(function() {
                            throw err
                        });
                    }
                    // set default user-project-role
                    var projectRoleData = {
                        project_id: 1,  //M4_LAB
                        role_id: 2,     // USER
                        user_id: newUserId
                    }
                    dbconn.user.query('INSERT INTO user_project_role SET ?', projectRoleData, function (err, results, fields) {
                        if (err) {
                            return dbconn.user.rollback(function() {
                                throw err
                            });
                        }
                        // COMMIT
                        dbconn.user.commit(function(err) {
                            if (err) {
                                return dbconn.user.rollback(function() {
                                    throw err
                                });
                            }
                        });
                    })
                });
            });
            callback(err)
        })
    },
    getUserByEmail: function(email, callback) {
Rosanny Sihombing's avatar
Rosanny Sihombing committed
60
        dbconn.user.query('SELECT salutation, title, firstname, lastname, industry, organisation, speciality FROM user WHERE email = "' +email+'"', function (err, rows, fields) {
61
62
63
64
65
66
67
68
69
70
71
            if (err) {
                throw err;
            }
            else {
                if ( rows.length > 0) {
                    user = rows[0];
                }
            }
            callback(user, err);
        });
    },
Rosanny Sihombing's avatar
Rosanny Sihombing committed
72
73
	checkUserEmail: function(email, callback) {
        var user;
74
        dbconn.user.query('SELECT id, email FROM user WHERE email = "' +email+'"', function (err, rows, fields) {
Rosanny Sihombing's avatar
Rosanny Sihombing committed
75
76
77
78
79
80
81
82
83
84
85
            if (err) {
                throw err;
            }
            else {
                if ( rows.length > 0) {
                    user = rows[0];
                }
            }
            callback(err, user);
        });
    },
86
    getUserByToken: function(token, callback) {
Rosanny Sihombing's avatar
Rosanny Sihombing committed
87
        var user;
88
89
90
91
92
93
94
95
96
97
        dbconn.user.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(), function (err, rows, fields) {
                if (err) {
                    throw err;
                }
                else {
                    if ( rows.length > 0) {
                        user = rows[0]
                        console.log(user)
                    }
Rosanny Sihombing's avatar
Rosanny Sihombing committed
98
                }
99
                callback(err, user);
Rosanny Sihombing's avatar
Rosanny Sihombing committed
100
            }
101
        );
Rosanny Sihombing's avatar
Rosanny Sihombing committed
102
103
104
105
106
107
108
    },
	updateUser: function(userData, callback) {
		dbconn.user.query('UPDATE user SET ? WHERE email = "' +userData.email+'"', userData, function (err, rows, fields) {
            if (err) throw err;
            callback(err);
        })
    },
109
110
    updateCredential: function(data, callback) {
        dbconn.user.query('UPDATE credential SET ? WHERE user_id = ' +data.user_id, data, function (err, rows, fields) {
Rosanny Sihombing's avatar
Rosanny Sihombing committed
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
            if (err) throw err;
            callback(err);
        })
    },
    getUserIdByEmail: function(email, callback) {
        var userId
        dbconn.user.query('SELECT id FROM user WHERE email = "' +email+'"', function (err, rows, fields) {
            if (err) {
                throw err;
            }
            else {
                if ( rows.length > 0) {
                    userId = rows[0].id;
                }
            }
            callback(userId, err);
        });
    },
    getUserProjectRole: function(userId, callback) {
        dbconn.user.query('SELECT project_id, role_id FROM user_project_role WHERE user_id = "' +userId+'"', function (err, rows, fields) {
            if (err) throw err;
            callback(rows, err);
        });
    },
Wolfgang Knopki's avatar
Wolfgang Knopki committed
135
136
137
138
139
140
    addUserProjectRole: function(data, callback) {
        dbconn.user.query('INSERT INTO user_project_role SET ?', data, function (err, results, fields){
            if (err) throw err;
            callback(err);
        })
    },
Rosanny Sihombing's avatar
Rosanny Sihombing committed
141
142
143
144
145
146
    // ======================= project db =======================
    getAllProjects: function(callback) {
        dbconn.project.query('CALL getAllprojects', function (err, rows, fields){
            if (err) throw err;
            callback(rows[0], err);
        })
Wolfgang Knopki's avatar
Wolfgang Knopki committed
147
148
149
150
151
    },
    getAllMailinglists: function(callback) {
        dbconn.project.query('CALL getAllLists', function (err, rows, fields){
            if (err) throw err;
            callback(rows[0], err);
Wolfgang Knopki's avatar
Wolfgang Knopki committed
152
153
154
155
156
157
158
159
160
161
162
163
164
	    })
    },
    getProjectOverviewById: function(projectId, callback) {
        dbconn.project.query('CALL GetProjectInformationByProjectID(' + projectId+ ')', function (err, rows, fields){
            if (err) throw err;
            callback(rows[0], err);
    	})
    },
    getProjectImagesById: function(projectId, callback) {
        dbconn.project.query('CALL getImagesByProjectID(' + projectId+ ')', function (err, rows, fields){
            if (err) throw err;
            callback(rows[0], err);
        })
Wolfgang Knopki's avatar
Wolfgang Knopki committed
165
    },
166
    addProjectOverview: function(data, callback) {
Wolfgang Knopki's avatar
Wolfgang Knopki committed
167
        dbconn.project.query('INSERT INTO project_overview SET ?', data, function (err, results, fields){
Rosanny Sihombing's avatar
Rosanny Sihombing committed
168
169
170
171
            if (err) {
                console.error(err);
            }
            callback(results, err);
Wolfgang Knopki's avatar
Wolfgang Knopki committed
172
        })
Rosanny Sihombing's avatar
Rosanny Sihombing committed
173
174
175
176
    }
};

module.exports = methods;