Child: [38b632] (diff)

Download this file

mysql.js    273 lines (259 with data), 6.7 kB

  1
  2
  3
  4
  5
  6
  7
  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
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/*made by Miguel Rodrigues @ KBZ miguel.rodrigues@knowledgebiz.pt*/
var mysql = require('mysql');
var db = require ('../config/db');
var token = require ('../backend/token/tokenGenerator');
var con = require ('../backend/server');
var email = require('./emailer');
/*
Insert Modules
*/
module.exports.insertVapp= function(body, callback){
var sqlQuery = "SELECT appID FROM registration where appID = '"+body.appID+"'";
con.query(sqlQuery, function (err, result) {
if (err){
callback(false,err);
}
else{
if(result.length == 0 && body.appID != "null"){
var newToken = token.token_generator();
var sqlQuery = "INSERT INTO registration ( token, appID, developerID) VALUES ('"+newToken+"','"+body.appID+"','"+body.developerID+"')";
con.query(sqlQuery, function (err, result) {
if (err){
callback(false,result);
}
else{
email.sendEmailToken(body.developerID, newToken);
callback(true,{token: newToken});
}
});
}
else{
callback(false,"App is already registered or AppID is Null");
}
}
});
}
module.exports.insertVappByDeveloperid= function(body, callback){
var sqlQuery = "SELECT appID FROM registration where appID = '"+body.appID+"'";
con.query(sqlQuery, function (err, result) {
if (err){
callback(false,err);
}
else{
if(result.length == 0 && body.appID != "null"){
var newToken = token.token_generator();
var sqlQuery = "INSERT INTO registration ( token, appID, developerID) VALUES ('"+newToken+"','"+body.appID+"','"+body.developerID+"')";
con.query(sqlQuery, function (err, result) {
if (err){
callback(false,result);
}else{
email.sendEmailToken(body.developerID, newToken);
callback(true,{token: newToken});
}
});
}
else{
callback(false,"App is already registered or AppID is Null");
}
}
});
}
module.exports.insertNotification= function(body, callback){
date = new Date().toISOString().slice(0, 19).replace('T', ' ');
strSubject = body.subject.replace(/\s/g, '');
var sqlQuery = "INSERT INTO notificationlist ( emailTo, subject, date, token) VALUES ('"+body.emailTo+"','"+strSubject+"', '"+date+"', '"+body.token+"')";
con.query(sqlQuery, function (err, result) {
if (err){
callback(false,err);
}
else{
callback(true);
}
});
}
module.exports.insertNotificationRules= function(body, callback){
var sql = "SELECT token FROM registration where appID = '"+body.appID+"'";
con.query(sql, function (err, res) {
if (err){
callback(false,err);
}
else{
var sqlQuery = "INSERT INTO ruleslist ( description, parameters, parameter, conditionValue, controlValue, notificationType, token) VALUES ('"+body.description+"','"+body.parameters+"','"+body.parameter+"','"+body.conditionValue+"','"+body.controlValue+"',"+body.notificationType+",'"+res[0].token+"')";
con.query(sqlQuery, function (err, result) {
if (err){
callback(false,err);
}
else {
callback(true);
}
});
}
});
}
/*
Retrieves Modules
*/
module.exports.retrieveApp= function(developerid,callback){
var sqlQuery = "SELECT * FROM registration where developerID = '"+developerid+"'";
con.query(sqlQuery, function (err, result) {
if (err){
callback(false,err);
}
else {
if(result.length == 0){
callback(false,"DeveloperID incorrect");
}
else{
callback(true,result);
}
}
});
}
module.exports.retrieveNotificationList= function(appid,callback){
var sql = "SELECT token FROM registration where appID = '"+appid+"'";
con.query(sql, function (err, res) {
if (err){
callback(false,err);
}
else{
if(res.length == 0){
callback(false,"AppID incorrect");
}
else{
var sqlQuery = "SELECT emailTo, subject, date, token FROM notificationlist where token = '"+res[0].token+"'";
con.query(sqlQuery, function (err, rows) {
if (err){
callback(false,err);
}
else {
callback(true,rows);
}
});
}
}
});
}
module.exports.retrieveRulesList= function(appid,callback){
var sql = "SELECT token FROM registration where appID = '"+appid+"'";
con.query(sql, function (err, res) {
if (err){
callback(false,err);
}
else{
if(res.length == 0){
callback(false,"AppID incorrect");
}
else{
var sqlQuery = "SELECT * FROM ruleslist where token = '"+res[0].token+"'";
con.query(sqlQuery, function (err, rows) {
if (err){
callback(false,err);
}
else{
callback(true,rows);
}
});
}
}
});
}
/*
Get Rules List by Token
*/
module.exports.getRulesListByToken = function(token, callback){
var sqlQuery = "SELECT * FROM ruleslist where token = '"+token+"'";
con.query(sqlQuery, function (err, ruleslist) {
if (err) {
callback(false,err);
}
else{
callback(true, ruleslist);
}
});
}
/*
Check If Token Exists (true) or Not (false)
*/
module.exports.checkIfTokenExists = function(body, callback){
var sql = "SELECT token FROM registration where token = '"+body.token+"'";
con.query(sql, function (err, res) {
if (err){
callback(false);
}
else{
if(res.length == 1){
callback(true, res);
}
else{
callback(false, "Invalid Token");
}
}
});
}
/*
Delete Vapp and Rules
*/
module.exports.deleteVapp= function(appid, callback){
var sql = "SELECT token FROM registration where appID = '"+appid+"'";
con.query(sql, function (err, res) {
if (err){
callback(false,err);
}
else{
if(res.length == 0){
callback(false,"AppID incorrect");
}
else{
var sqlNotifcations = "DELETE FROM notificationlist where token = '"+res[0].token+"'";
con.query(sqlNotifcations, function (err, rows) {
if (err) {
callback(false,err);
}
else{
var sqlRules = "DELETE FROM ruleslist where token = '"+res[0].token+"'";
con.query(sqlRules, function (err, rows) {
if (err) {
callback(false,err);
}
else{
var sqlQuery = "DELETE FROM registration where token = '"+res[0].token+"'";
con.query(sqlQuery, function (err, rows) {
if (err){
callback(false,err);
}
else{
callback(true, rows);
}
});
}
});
}
});
}
}
});
}
module.exports.deleteRule= function(rulesid, callback){
var sqlQuery = "DELETE FROM ruleslist where rulesID = '"+rulesid+"'";
con.query(sqlQuery, function (err, rows) {
if (err) {
callback(false,err);
}
else{
if(rows.length == 0){
callback(false,"RuleID incorrect");
}
else{
callback(true,rows);
}
}
});
}
/*
module.exports.insertConfiguration= function(sql){
con.query(sql, function (err, result) {
if (err) throw err;
console.log(result);
});
}
*/