Skip to content

Commit 4b8c9ea

Browse files
scan city
1 parent ce005d9 commit 4b8c9ea

File tree

10 files changed

+325
-142
lines changed

10 files changed

+325
-142
lines changed

app/api/rooms.js

+162-114
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,66 @@ const config = require('../modules/config');
66
const error = require('../modules/error/api');
77
const aws = require('../modules/aws-amazon-s3');
88
const crypto = require('crypto');
9-
var moment = require('moment');
9+
const moment = require('moment');
1010
const async = require('async');
11-
var NodeGeocoder = require('node-geocoder');
11+
const NodeGeocoder = require('node-geocoder');
1212

13-
var options = {
13+
const GooglePlaces = require('google-places');
14+
15+
const places = new GooglePlaces(config.get('google:api:maps:key'));
16+
17+
const geocoder = NodeGeocoder({
1418
provider: 'google',
1519

1620
// Optional depending on the providers
1721
httpAdapter: 'https', // Default
1822
apiKey: config.get('google:api:maps:key'), // for Mapquest, OpenCage, Google Premier
19-
};
20-
var geocoder = NodeGeocoder(options);
23+
});
24+
25+
2126
module.exports = (API, redis) => {
27+
API.on('geoCode', true, (user, param, callback) => {
28+
if (!param.address && !param.keyword) {
29+
return callback && callback(null, {
30+
error: error.api('param.address or param.keyword is not valid', 'param', {}, 0),
31+
success: false
32+
});
33+
}
34+
35+
places.autocomplete({input: param.address || param.keyword}, function (err, response) {
36+
let result = [];
37+
async.map(response.predictions, function (data,callback) {
38+
places.details({reference: data.reference}, function (err, response) {
39+
let coutry = '';
40+
let district = null;
41+
for (let key in response.result.address_components) {
42+
if (response.result.address_components[key].types[0] === 'country')
43+
coutry = response.result.address_components[key].long_name;
44+
if (response.result.address_components[key].types[0] === 'administrative_area_level_1')
45+
district = response.result.address_components[key].long_name;
46+
47+
}
48+
if (response.result.types[0] === 'country' || response.result.types[0] === 'locality') {
49+
callback(null,{city:response.result.name,district:district,coutry:coutry});
50+
}else{
51+
callback()
52+
}
53+
});
54+
}, function(err, results) {
55+
let scan_res = results.filter(function (item) {
56+
if(item) return true;
57+
return false;
58+
});
59+
callback && callback(null,{results:scan_res,success:true});
60+
});
61+
62+
});
63+
}, {
64+
title: 'Get geo code to find',
65+
description: 'Get geo code to find',
66+
param: [{name: 'address', type: "string", title: 'address string ', default: ''}],
67+
response: []
68+
});
2269
API.on('GetRooms', true, (user, param, callback) => {
2370
let findPram = {};
2471
if (param.find && typeof param.find === 'object') findPram = param.find;
@@ -38,7 +85,7 @@ module.exports = (API, redis) => {
3885
if (param.page && !isNaN(param.page) && +param.page > 0) {
3986
skip = limit * (param.page - 1)
4087
}
41-
console.log(param);
88+
4289
async.parallel({
4390
count: function (count_callback) {
4491
db.rooms.count(findPram).then(function (count) {
@@ -90,6 +137,7 @@ module.exports = (API, redis) => {
90137
});
91138

92139
}).catch(function (err) {
140+
93141
return callback && callback({
94142
error: error.api(err.message, 'db', err, 5),
95143
success: false
@@ -207,120 +255,120 @@ module.exports = (API, redis) => {
207255
}
208256

209257
if (!param._id && !param._index) {
210-
geocoder.geocode(param.address_country + ',' + param.address_state + ',' + param.address_city+','+param.address_address).then(function (res) {
211-
212-
new db.rooms({
213-
214-
user: db.mongoose.Types.ObjectId(user._id),
215-
title: param.title,
216-
description: param.description,
217-
photo: param.photo,
218-
wallet: param.wallet,
219-
bathroom: param.bathroom,
220-
bathroom_count: param.bathroom_count,
221-
bed_count: param.bed_count,
222-
bedroom_count: param.bedroom_count,
223-
children_count: param.children_count,
224-
limit_time_min: param.limit_time_min,
225-
limit_time_max: param.limit_time_max,
226-
people_count: param.people_count,
227-
startTimeCheckIn: param.startTimeCheckIn,
228-
startTimeCheckOut: param.startTimeCheckOut,
229-
endTimeCheckIn: param.endTimeCheckIn,
230-
endTimeCheckOut: param.endTimeCheckOut,
231-
// dateRanges: param.dateRanges,
232-
address: {
233-
country: res[0].country,
234-
state: res[0].administrativeLevels.level1long || res[0].extra.neighborhood,
235-
city: res[0].city || res[0].extra.neighborhood,
236-
street: res[0].streetName,
237-
address: param.address_address,
238-
index: res[0].zipcode || param.address_index,
239-
},
240-
facilities: param.facilities,
241-
location: [param.location_latitude * 1, param.location_longitude * 1],
242-
txHash: null,
243-
status: param.status //0 - draft , 1 - wait confirm, 2 - send to blockchain, 3 -success public
244-
}).save().then(function (document) {
245-
246-
for (let i in param.dateRanges) {
247-
248-
249-
new db.scheduleRoom({
250-
room: db.mongoose.Types.ObjectId(document._doc._id),
251-
_scheduleIndex: null,
252-
startDate: moment.utc(param.dateRanges[i].startDate, 'DD.MM.YY').toDate(),
253-
endDate: moment.utc(param.dateRanges[i].endDate, 'DD.MM.YY').toDate(),
254-
dayPrice: +(+param.dateRanges[i].priceDay).toFixed(8),
255-
weekPrice: +(param.dateRanges[i].priceDay - (param.dateRanges[i].priceDay * param.dateRanges[i].discountWeek / 100)).toFixed(8),
256-
monthPrice: +(param.dateRanges[i].priceDay - (param.dateRanges[i].priceDay * param.dateRanges[i].discountMonth / 100)).toFixed(8),
257-
discountWeek: 1 * (1 * param.dateRanges[i].discountWeek).toFixed(8),
258-
discountMonth: 1 * (1 * param.dateRanges[i].discountMonth).toFixed(8),
259-
intervalDate: null,
260-
tx: {
261-
status: 1,
262-
hash: null,
263-
}
264-
}).save();
265-
266-
}
267-
// bathroom
268-
// bathroom_count
269-
// bed_count
270-
// bedroom_count
271-
// children_count
272-
// dateRanges
273-
// endTimeCheckIn
274-
// endTimeCheckOut
275-
// facilities
276-
// limit_time_max
277-
// limit_time_min
278-
// people_count
279-
// startTimeCheckIn
280-
// startTimeCheckOut
281-
return callback && callback(null, {
282-
room: filterObject(document._doc, [
283-
'_id',
284-
'_index',
285-
'_hash',
286-
'bathroom',
287-
'bathroom_count',
288-
'bed_count',
289-
'bedroom_count',
290-
'children_count',
291-
'endTimeCheckIn',
292-
'endTimeCheckOut',
293-
'startTimeCheckIn',
294-
'startTimeCheckOut',
295-
'facilities',
296-
'people_count',
297-
'limit_time_min',
298-
'limit_time_max',
299-
'title',
300-
'description',
301-
'photo',
302-
'wallet',
303-
'dateRanges',
304-
'address',
305-
'location',
306-
'status',
307-
'update_at',
308-
]),
309-
success: true
310-
});
311-
}).catch(function (err) {
312-
return callback && callback(null, {
313-
error: error.api(err.message, 'db', err, 5),
314-
success: false
315-
});
258+
geocoder.geocode(param.address_country + ',' + param.address_state + ',' + param.address_city + ',' + param.address_address).then(function (res) {
259+
260+
new db.rooms({
261+
262+
user: db.mongoose.Types.ObjectId(user._id),
263+
title: param.title,
264+
description: param.description,
265+
photo: param.photo,
266+
wallet: param.wallet,
267+
bathroom: param.bathroom,
268+
bathroom_count: param.bathroom_count,
269+
bed_count: param.bed_count,
270+
bedroom_count: param.bedroom_count,
271+
children_count: param.children_count,
272+
limit_time_min: param.limit_time_min,
273+
limit_time_max: param.limit_time_max,
274+
people_count: param.people_count,
275+
startTimeCheckIn: param.startTimeCheckIn,
276+
startTimeCheckOut: param.startTimeCheckOut,
277+
endTimeCheckIn: param.endTimeCheckIn,
278+
endTimeCheckOut: param.endTimeCheckOut,
279+
// dateRanges: param.dateRanges,
280+
address: {
281+
country: res[0].country,
282+
state: res[0].administrativeLevels.level1long || res[0].extra.neighborhood,
283+
city: res[0].city || res[0].extra.neighborhood,
284+
street: res[0].streetName,
285+
address: param.address_address,
286+
index: res[0].zipcode || param.address_index,
287+
},
288+
facilities: param.facilities,
289+
location: [param.location_latitude * 1, param.location_longitude * 1],
290+
txHash: null,
291+
status: param.status //0 - draft , 1 - wait confirm, 2 - send to blockchain, 3 -success public
292+
}).save().then(function (document) {
293+
294+
for (let i in param.dateRanges) {
295+
296+
297+
new db.scheduleRoom({
298+
room: db.mongoose.Types.ObjectId(document._doc._id),
299+
_scheduleIndex: null,
300+
startDate: moment.utc(param.dateRanges[i].startDate, 'DD.MM.YY').toDate(),
301+
endDate: moment.utc(param.dateRanges[i].endDate, 'DD.MM.YY').toDate(),
302+
dayPrice: +(+param.dateRanges[i].priceDay).toFixed(8),
303+
weekPrice: +(param.dateRanges[i].priceDay - (param.dateRanges[i].priceDay * param.dateRanges[i].discountWeek / 100)).toFixed(8),
304+
monthPrice: +(param.dateRanges[i].priceDay - (param.dateRanges[i].priceDay * param.dateRanges[i].discountMonth / 100)).toFixed(8),
305+
discountWeek: 1 * (1 * param.dateRanges[i].discountWeek).toFixed(8),
306+
discountMonth: 1 * (1 * param.dateRanges[i].discountMonth).toFixed(8),
307+
intervalDate: null,
308+
tx: {
309+
status: 1,
310+
hash: null,
311+
}
312+
}).save();
316313

317-
});
314+
}
315+
// bathroom
316+
// bathroom_count
317+
// bed_count
318+
// bedroom_count
319+
// children_count
320+
// dateRanges
321+
// endTimeCheckIn
322+
// endTimeCheckOut
323+
// facilities
324+
// limit_time_max
325+
// limit_time_min
326+
// people_count
327+
// startTimeCheckIn
328+
// startTimeCheckOut
329+
return callback && callback(null, {
330+
room: filterObject(document._doc, [
331+
'_id',
332+
'_index',
333+
'_hash',
334+
'bathroom',
335+
'bathroom_count',
336+
'bed_count',
337+
'bedroom_count',
338+
'children_count',
339+
'endTimeCheckIn',
340+
'endTimeCheckOut',
341+
'startTimeCheckIn',
342+
'startTimeCheckOut',
343+
'facilities',
344+
'people_count',
345+
'limit_time_min',
346+
'limit_time_max',
347+
'title',
348+
'description',
349+
'photo',
350+
'wallet',
351+
'dateRanges',
352+
'address',
353+
'location',
354+
'status',
355+
'update_at',
356+
]),
357+
success: true
358+
});
318359
}).catch(function (err) {
319360
return callback && callback(null, {
320-
error: error.api('Error geo data', 'param', err, 0),
361+
error: error.api(err.message, 'db', err, 5),
321362
success: false
322363
});
364+
323365
});
366+
}).catch(function (err) {
367+
return callback && callback(null, {
368+
error: error.api('Error geo data', 'param', err, 0),
369+
success: false
370+
});
371+
});
324372
} else {
325373

326374

app/modules/api/index.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,9 @@ API = {
9393
}
9494
json.latency_ms = (new Date()).getTime() - initTimestamp;
9595
if (!type) type = 'server';
96-
9796
new db.logsAPI({
9897
method: name,
99-
param: param,
98+
param:{url:querystring.stringify(param)},
10099
response: json,
101100
error: err,
102101
user: {
@@ -109,6 +108,9 @@ API = {
109108
json.latency_ms = (new Date()).getTime() - initTimestamp;
110109
json.requestId = res._id;
111110
}
111+
if(err)
112+
cb && cb(err.stack || err, json);
113+
else
112114
cb && cb(err, json);
113115
});
114116
});

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
"express-recaptcha": "^2.2.0",
4040
"express-recaptcha-middleware": "^1.0.1",
4141
"express-session": "^1.15.3",
42+
"google-places": "0.0.2",
4243
"helmet": "^3.6.1",
4344
"jade": "^1.11.0",
4445
"keythereum": "^1.0.1",

0 commit comments

Comments
 (0)