'use strict'; const // debug = require('debug')('agm:obstacle'), Obstacle = require('../model/obstacles'), ObjectId = require('mongodb').ObjectId, { AppParamError } = require('../helpers/app_error'); async function near_post(req, res) { const ops = req.body; const miles = 32; // ~ 60km in radius const obs = await Obstacle.find( { geometry: { $geoWithin: { $centerSphere: [[ops.center.lng, ops.center.lat], miles / 3963.2] } }, "properties.agl": { $gte: (Number(ops.min) || 0) }, }, {}, { lean: false }); let retObs = obs ? obs : []; if (retObs.length) retObs = retObs.map(ob => ob.toObject()); res.json(retObs); } function createOstacle(obsInfo) { const obs = obsInfo.item; if (!obs || !obs.coors || !obs.coors.length || !ObjectId.isValid(obsInfo.pUser)) AppParamError.throw(); const obstacle = { properties: { name: obs.name || '', type: obs.type || 'USER', agl: Number(obs.agl || 0), amsl: Number(obs.amsl || 0) }, geometry: { type: 'Point', coordinates: obs.coors }, byUser: ObjectId(obsInfo.pUser) }; return obstacle; } async function createObstacle_post(req, res) { const obs = req.body; if (!obs) AppParamError.throw(); const newObs = createOstacle(obs); const ob = await Obstacle.create(newObs); res.json(ob.toObject()); } async function updateObstacle_put(req, res) { const obs = req.body; const ob = await Obstacle.findOneAndUpdate( { _id: req.params.ob_id }, { "properties.name": obs.name, "properties.agl": Number(obs.agl), "properties.amsl": Number(obs.amsl) }, { new: true }); res.json(ob && ob.toObject()); } async function deleteObstacle(req, res) { const ob = await Obstacle.findById(req.params.ob_id); if (ob) await ob.remove(); res.json({ message: 'Obstacle successfully deleted' }); } module.exports = { near_post, createObstacle_post, updateObstacle_put, deleteObstacle }