/var/www/greso.tech/server/nsm/services
Edit: /var/www/greso.tech/server/nsm/services/rateArea.service.js (2022B)
const logger = require('../logger')({ service: 'OrderService' })
const { Op } = require('sequelize')
const PointInPolygon = require('point-in-polygon')
const { isSet, isArray } = require('../utils/validator.util')
module.exports = class RateAreaService {
constructor({ db, config }) {
this.db = db
this.interval = null
this.rateAreas = VARS.rateAreas
this.config = config
logger.child({ service: 'RateAreaService', method: 'Constructor' }).info('Success')
}
async Run() {
// load rate areas
var areasCount = 0;
var areas = await this.db.rateAreas.findAll()
if (areas && areas.length > 0) {
for (var x in areas) {
areasCount++
try {
areas[x].area = JSON.parse(areas[x].area)
this.rateAreas.push(areas[x]);
} catch (e) {
}
}
}
logger.child({ service: 'RateAreaService', method: 'Run' }).info('Success. Areas: ' + areasCount)
console.log('RateAreaService Run Success. Areas: ' + areasCount)
}
async Stop() {
this.interval = null
logger.child({ service: 'RateAreaService', method: 'Stop' }).info('Success')
}
////////////////////////////////////////////////////////////////////////////////////////
checkPoly(poly, lat, lon){
var found = false;
if(lat == 'undefined' || lat == '' || lat == '0.0') return found;
if(isArray(poly)){
if(isArray(poly[0]) && typeof poly[0][0][0] === 'number'){
found = PointInPolygon([parseFloat(lon), parseFloat(lat)], poly[0])
}else{
for(var i in poly){
found = this.checkPoly(poly[i], lat, lon)
if(found)return found
}
}
}
return found;
}
async GetRateArea({ latitude, longitude }) {
// console.log('Search area: '+latitude+' '+longitude)
for (let i in this.rateAreas) {
let polygons = this.rateAreas[i].area.coordinates
let found = this.checkPoly(polygons, latitude, longitude)
if (found) {
return [
{
'id': this.rateAreas[i].id,
'key': this.rateAreas[i].key,
'name': this.rateAreas[i].name,
}
]
}
}
return []
}
}