var NodeGeocoder = require('node-geocoder');
let express = require('express'),
router = express.Router(),
communityService = require('../model/search');
const verifyToken = require('../model/auth1');
const nodemailer =require('nodemailer');
var options = {
provider: 'google',
// Optional depending on the providers
httpAdapter: 'https', // Default
apiKey: 'AIzaSyCNLBs_RtZoI4jdrZg_CjBp9hEM6SBIh-4', // for Mapquest, OpenCage, Google Premier
formatter: null // 'gpx', 'string', ...
};
var geocoder = NodeGeocoder(options);
var smtpTransport = nodemailer.createTransport({
service: "gmail",
auth: {
user: "satish.webnexus@gmail.com",
pass: "satish@webnexus2019"
}
});
var GetType = [];
var IncentivesSeacrch = []
router.post('/search', (req, res) => {
GetType = [];
var legit = verifyToken.verify(req.headers.authorization);
var criteria = '', miles;
if (legit) {
if(req.body.zipcode && req.body.zipcode !=="")
{
criteria = req.body.zipcode
}
else if(req.body.city && req.body.city !=="")
{
criteria = req.body.city
}
else if(req.body.county && req.body.county !== "")
{
criteria = req.body.county
}
else if(req.body.state && req.body.state !=="")
{
criteria = req.body.state
}
if(req.body.miles && req.body.miles !=="")
{
miles = req.body.miles
}
geocoder.geocode(criteria, function(err, data) {
if(data)
{
if(data.length>0)
{
communityService.GetSaerchResult(data[0].latitude, data[0].longitude, miles, (err, getData, fields) => {
if (!err) {
if (getData.length > 0) {
forEachPromise(getData, GetTypeUser)
.then((result) => {
res.json({ status: 200, hassuccessed: true, msg: 'User is found', data: GetType })
})
}
else
{
res.json({ status: 200, hassuccessed: false, msg: 'No data'})
}
} else {
res.json({ status: 200, hassuccessed: false, message: 'Something went wrong.', error: err });
}
});
}
}
else
{
res.json({ status: 200, hassuccessed: false, msg: 'No data'})
}
});
}
else {
res.json({ status: 200, hassuccessed: false, message: 'Unauthorized request' });
}
});
function forEachPromise(items, fn) {
return items.reduce(function (promise, item) {
return promise.then(function () {
return fn(item);
});
}, Promise.resolve());
}
function GetTypeUser(data) {
var comapny1 = [];
return new Promise((resolve, reject) => {
process.nextTick(() => {
communityService.GetType(data.community_provider_id, (err, getData, fields) => {
if(getData)
{
new Promise(function(resolve, reject) {
communityService.getInsentives(data.community_provider_id, (err, incen)=>{
if (err) {
reject(err);
} else {
communityService.GetCompany(data.community_provider_id, (err, comapny)=>{
if (err) {
} else {
var data = [];
data.push(comapny);
data.push(incen);
resolve(data);
}
})
}
})
}).then((data)=>
{
getData[0].incentives = data[1];
getData[0].company = data[0];
var new_data = getData[0];
GetType.push(new_data);
resolve(GetType);
})
}
});
});
});
}
router.post('/ShareSearch', (req, res) => {
var legit = verifyToken.verify(req.headers.authorization);
if (legit) {
mailOptions={
from: legit.email,
to : req.body.email,
subject : "Share the Search",
html: "The given Search is:
"+
"Incentves name :"+ req.body.sharingData.incentive_name +"
"+
"Start Date :"+ req.body.sharingData.start_date +"
"+
"End Date :"+ req.body.sharingData.end_date +"
"+
"Incentive Limited :"+ req.body.sharingData.incentive_limited +"
"+
"Value :"+ "$"+req.body.sharingData.value +"
"+
"Description :"+ req.body.sharingData.description +"
"+
"Legal Desclaimer :"+ req.body.sharingData.legal_desclaimer +"
",
}
smtpTransport.sendMail(mailOptions, function(error, response){
if(error){
res.json({ status: 200, hassuccessed: true, message: 'Something went wrong',error: err });
}else{
res.json({ status: 200, hassuccessed: true, msg: 'Email is successfully sent' })
}
})
}
else {
res.json({status: 200, hassuccessed: false, message: 'Unauthorized request' });
}
});
router.post('/SaveSearch', (req, res) => {
var legit = verifyToken.verify(req.headers.authorization);
if (legit) {
req.body.user_id = legit.user_id;
communityService.getName(req.body,(err, data, fields) => {
if (!err) {
if(data && data.length>0)
{
res.json({ status: 200, hassuccessed: false, message: 'Already exist',error: err });
}
else
{
communityService.SaveSearch(req.body, (err, getData, fields) => {
if (!err) {
res.json({ status: 200, hassuccessed: true, msg: 'Search is save successfully', data: getData })
} else {
res.json({ status: 200, hassuccessed: false, message: 'Something went wrong.',error: err });
}
});
}
} else {
res.json({ status: 200, hassuccessed: false, message: 'Something went wrong.',error: err });
}
});
}
else {
res.json({status: 200, hassuccessed: false, message: 'Unauthorized request' });
}
});
router.get('/get-save-search', (req, res) => {
IncentivesSeacrch = [];
var legit = verifyToken.verify(req.headers.authorization);
if (legit) {
var user_id = legit.user_id;
communityService.GetSaveSearch(user_id, (err, getData, fields) => {
if (!err) {
if (getData.length > 0) {
forEachPromise(getData, GetSaveIncentive)
.then((result) => {
res.json({ status: 200, hassuccessed: true, msg: 'User is found', data: IncentivesSeacrch })
})
}
else
{
res.json({ status: 200, hassuccessed: false, msg: 'No data'})
}
// res.json({ status: 200, hassuccessed: true, msg: 'Search is get successfully', data: getData })
} else {
res.json({ status: 200, hassuccessed: false, message: 'Something went wrong.',error: err });
}
});
}
else {
res.json({status: 200, hassuccessed: false, message: 'Unauthorized request' });
}
});
router.get('/delete-save-search', (req, res) => {
IncentivesSeacrch = [];
var legit = verifyToken.verify(req.headers.authorization);
if (legit) {
var user_id = legit.user_id;
communityService.DeleteSaveSearch(user_id, (err, getData, fields) => {
if (!err) {
if (getData.length > 0) {
forEachPromise(getData, GetSaveIncentive)
.then((result) => {
res.json({ status: 200, hassuccessed: true, msg: 'User is found', data: IncentivesSeacrch })
})
}
else
{
res.json({ status: 200, hassuccessed: false, msg: 'No data'})
}
// res.json({ status: 200, hassuccessed: true, msg: 'Search is get successfully', data: getData })
} else {
res.json({ status: 200, hassuccessed: false, message: 'Something went wrong.',error: err });
}
});
}
else {
res.json({status: 200, hassuccessed: false, message: 'Unauthorized Request' });
}
});
function GetSaveIncentive(data) {
return new Promise((resolve, reject) => {
process.nextTick(() => {
communityService.ChechincetiveSearch(JSON.parse(data.incentive_id), (err, getData, fields) => {
if(getData)
{
var newdata = data;
newdata.incentives = getData;
IncentivesSeacrch.push(newdata);
resolve(IncentivesSeacrch);
}
});
});
});
}
router.post('/volunteer-search', (req, res) => {
// GetType = [];
// var legit = verifyToken.verify(req.headers.authorization);
// var criteria = '', miles;
// if (legit) {
// if(req.body.zipcode && req.body.zipcode !=="")
// {
// criteria = req.body.zipcode
// }
// else if(req.body.city && req.body.city !=="")
// {
// criteria = req.body.city
// }
// else if(req.body.county && req.body.county !== "")
// {
// criteria = req.body.county
// }
// else if(req.body.state && req.body.state !=="")
// {
// criteria = req.body.state
// }
// if(req.body.miles && req.body.miles !=="")
// {
// miles = req.body.miles
// }
// geocoder.geocode(criteria, function(err, data) {
// if(data)
// {
// if(data.length>0)
// {
// communityService.GetSaerchResult(data[0].latitude, data[0].longitude, miles, (err, getData, fields) => {
// if (!err) {
// if (getData.length > 0) {
// forEachPromise(getData, GetTypeUser)
// .then((result) => {
// res.json({ status: 200, hassuccessed: true, msg: 'User is found', data: GetType })
// })
// }
// else
// {
// res.json({ status: 200, hassuccessed: false, msg: 'No data'})
// }
// } else {
// res.json({ status: 200, hassuccessed: false, message: 'Something went wrong.', error: err });
// }
// });
// }
// }
// else
// {
// res.json({ status: 200, hassuccessed: false, msg: 'No data'})
// }
// });
// }
// else {
// res.json({ status: 200, hassuccessed: false, message: 'Unauthorized Request' });
// }
});
//Ended by ANkita
module.exports = router;