-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSearch.Controller.js
More file actions
352 lines (331 loc) · 15.2 KB
/
Search.Controller.js
File metadata and controls
352 lines (331 loc) · 15.2 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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
'use strict';
var User = require('../models/User');
var Advert = require('../models/Advert');
var jwt = require('jwt-simple');
var request = require('request');
var http = require('http');
var xmlHTTP = require('xhr2');
var passport = require('passport');
var path = require('path');
/**
* @api {get} /search/searchByJWT Search using JWT Token
* @apiName Search by JWT
* @apiGroup Search
*
* @apiParam {String} req.headers JWT Token
* @apiParam {Integer} radius Radius to Search, defaults to 10 miles
* @apiParam {Date} start Date of availability start
* @apiParam {Date} endData Date of availabiliy end
*
* @apiSuccess [Employees] Returns Array of Employee Objects
*/
exports.searchByJWT = function (req, res1) {
if (!req.headers) {
return res.status(400).send();
} else {
var token = getToken(req.headers);
if (!token) {
res1.json({success: false, msg: 'Invalid JWT'}).send();
} else {
var employer = jwt.decode(token, 'secret');
var postcode = employer.postCode;
var radius = req.query.radius;
var wage = req.query.wage;
var startDate = req.query.startDate;
var endDate = req.query.endDate;
if (typeof radius === 'undefined') {
radius = 10;
}
populateAllEmployees();
var long = undefined;
var lat = undefined;
var matchedEmployees = [];
request.get('http://api.postcodes.io/postcodes/' + postcode, function (err, res) {
if (err) {
return res1.status(400).send("An Error Has Occured");
}
else {
var body = JSON.parse(res.body);
if (!body.result) {
return res1.status(400).send("Please Check Your PostCode");
} else {
long = body.result.longitude;
lat = body.result.latitude;
if (long && lat) {
User.find({'role': 'employee'})
.populate({
path: 'employee',
populate: {
path: 'availableTimes',
}
}).exec(function (err, emp) {
if (err) return res.status(400).send('error');
var arrayofemployees = emp;
for (var i = 0; i < emp.length; ++i) {
var employee = emp[i];
var distance = getDistance(lat, long, employee.latitude, employee.longitude);
if (distance >= radius) {
var index = arrayofemployees.indexOf(employee);
if (index !== -1) {
delete arrayofemployees[index];
}
}
if(employee.employee) {
if(employee.employee.wage) {
if (employee.employee.wage < wage) {
var index = arrayofemployees.indexOf(employee);
if (index !== -1) {
delete arrayofemployees[index];
}
}
}
}
if (startDate != undefined && typeof endDate != undefined) {
if (startDate > employee.employee.availableTimes.start || endDate < employee.employee.availableTimes.end) {
var index = arrayofemployees.indexOf(employee);
if (index !== -1) {
delete arrayofemployees[index];
}
}
}
}
var array = arrayofemployees.filter(function() { return true; });
return res1.json({success: true, employees: arrayofemployees}).end();
});
}
}
}
});
}
}
}
/**
* @api {get} /search/search Search
* @apiName Search
* @apiGroup Search
*
* @apiParam {Integer} req.query.postcode Postcode to use for searching
* @apiParam {Integer} req.query.radius Radius to search, defaults to 10 miles
* @apiParam {Integer} req.query.wage Max Wage
* @apiParam {Date} req.query.startDate Date of availabiliy start
* @apiParam {Date} req.query.endDate Date of availabiliy end
*
* @apiSuccess [Employees] Returns Array of Employee Objects
*/
exports.search = function (req, res1) {
if (!req.query) {
return res1.status(400).send('Wrong Format, Provide Query');
} else {
var postcode = req.query.postcode;
var radius = req.query.radius;
var wage = req.query.wage;
var startDate = req.query.startDate;
var endDate = req.query.endDate;
if (typeof radius === 'undefined') {
radius = 10;
}
populateAllEmployees();
var long = undefined;
var lat = undefined;
var matchedEmployees = [];
request.get('http://api.postcodes.io/postcodes/' + postcode, function (err, res) {
if (err) {
return res1.status(400).send("An Error Has Occured");
}
else {
var body = JSON.parse(res.body);
if (!body.result) {
return res1.status(400).send("Please Check Your PostCode");
} else {
long = body.result.longitude;
lat = body.result.latitude;
if (long && lat) {
User.find({'role': 'employee'})
.populate({
path: 'employee',
populate: {
path: 'availableTimes',
}
}).exec(function (err, emp) {
if (err) return res.status(400).send('error');
var arrayofemployees = emp;
for (var i = 0; i < emp.length; ++i) {
var employee = emp[i];
var distance = getDistance(lat, long, employee.latitude, employee.longitude);
if (distance >= radius) {
var index = arrayofemployees.indexOf(employee);
if (index !== -1) {
delete arrayofemployees[index];
}
}
if(employee.employee) {
if(employee.employee.wage) {
if (employee.employee.wage < wage) {
var index = arrayofemployees.indexOf(employee);
if (index !== -1) {
delete arrayofemployees[index];
}
}
}
}
if (startDate != undefined && typeof endDate != undefined) {
if (startDate > employee.employee.availableTimes.start || endDate < employee.employee.availableTimes.end) {
var index = arrayofemployees.indexOf(employee);
if (index !== -1) {
delete arrayofemployees[index];
}
}
}
}
var array = arrayofemployees.filter(function() { return true; });
return res1.json({success: true, employees: arrayofemployees}).end();
});
}
}
}
});
}
}
function getDistance(lat1, lon1, lat2, lon2) {
var R = 6371;
var dLat = deg2rad(lat2 - lat1);
var dLon = deg2rad(lon2 - lon1);
var a =
Math.sin(dLat / 2) * Math.sin(dLat / 2) +
Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) *
Math.sin(dLon / 2) * Math.sin(dLon / 2);
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
var d = R * c; // Distance in km
return d * 0.621371;
}
function deg2rad(deg) {
return deg * (Math.PI / 180)
}
function populateAllEmployees() {
var employees = [];
User.find({'role': 'employee'})
.populate('employee').exec(function (err, emp) {
if (err) return 'Error finding users';
emp.forEach(function (employee) {
var postcode = employee.postCode;
request.get('http://api.postcodes.io/postcodes/' + postcode, function (err, res) {
if (err) throw err
if (res.statusCode !== 200) {}
var body = JSON.parse(res.body);
if(body) {
if(body.result) {
if(body.result.longitude && body.result.latitude) {
var longitude = body.result.longitude;
var latitude = body.result.latitude;
employee.longitude = longitude;
employee.latitude = latitude;
employees.push(employee);
employee.save(function (err, emp) {})
}
}
}
});
});
});
return employees;
}
function getToken(headers) {
if (headers && headers.authorization) {
var parted = headers.authorization.split(' ');
if (parted.length === 2) {
return parted[1];
} else {
return null;
}
} else {
return null;
}
}
/**
* @api {get} /search/searchByAdvert Search by Advert
* @apiName Search by Advert
* @apiGroup Search
*
* @apiParam {Integer} req.query.id Id of Advert
*
* @apiSuccess [Employees] Returns Array of Employee Objects
*/
exports.searchByAdvert = function (req, res) {
if (!req.query) {
return res.status(400).send('Wrong format');
} else if (!req.query.id) {
return res.status(400).send('Provide ID');
} else {
var id = req.query.id;
Advert.findById(id, function (err, ad) {
if (err) return res.json({success: false, msg: 'Error thrown'}).send();
if (ad) {
var wage = ad.wage;
var postcode = ad.postcode;
var startDate = ad.start;
var endDate = ad.end;
var radius = 10;
populateAllEmployees();
var long = undefined;
var lat = undefined;
request.get('http://api.postcodes.io/postcodes/' + postcode, function (err, res1) {
if (err) {
console.log(err);
return res.status(400).json({success: false, msg: 'Cant Find postcode'}).send();
} else {
if(res1.body) {
var body = JSON.parse(res1.body);
if(body.result) {
long = body.result.longitude;
lat = body.result.latitude;
console.log("here");
if (long && lat) {
User.find({'role': 'employee'})
.populate({
path: 'employee',
populate: {
path: 'availableTimes',
model: 'AvailableTime'
}
}).exec(function (err, emp) {
if (err) return res.status(400).send();
else {
var arrayofemployees = emp;
for (var i = 0; i < emp.length; ++i) {
var employee = emp[i];
var distance = getDistance(lat, long, employee.latitude, employee.longitude);
if (distance >= radius) {
var index = arrayofemployees.indexOf(employee);
if (index !== -1) {
delete arrayofemployees[index];
}
}
if (typeof wage !== undefined && employee.employee.wage > wage) {
var index = arrayofemployees.indexOf(employee);
if (index !== -1) {
delete arrayofemployees[index];
}
}
if (typeof startDate !== 'undefined' && typeof endDate !== 'undefined') {
if (startDate > employee.employee.availableTimes.start ||
endDate < employee.employee.availableTimes.end) {
var index = arrayofemployees.indexOf(employee);
if (index !== -1) {
delete arrayofemployees[index];
}
}
}
}
var array = arrayofemployees.filter(function() { return true; });
return res.status(200).json({success: true, employees: array}).send();
}
});
}
}
}
}
});
}
});
}
}