/*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);
});
}
*/