Skip to content

Commit 2502491

Browse files
(api) handle geojson format in api for getBox
1 parent 099691a commit 2502491

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

packages/api/lib/controllers/boxesController.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,16 @@ const getBox = async function getBox (req, res, next) {
326326
const { format, boxId } = req._userParams;
327327

328328
try {
329-
res.send(await Box.findBoxById(boxId, { format }));
329+
const box = await Box.findBoxById(boxId);
330+
331+
if (format === 'geojson') {
332+
const coordinates = box.currentLocation.coordinates;
333+
box.currentLocation = undefined;
334+
box.loc = undefined;
335+
336+
return res.send(point(coordinates, box));
337+
}
338+
res.send(box);
330339
} catch (err) {
331340
handleError(err, next);
332341
}

tests/tests/005-create-boxes-test.js

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,26 @@ describe('openSenseMap API Routes: /boxes', function () {
181181
});
182182
});
183183

184+
it('should return a box as geojson', function () {
185+
return chakram.get(`${BASE_URL}/boxes/${boxId}?format=geojson`)
186+
.then(function (response) {
187+
expect(response).to.have.status(200);
188+
const geojsonBox = response.body;
189+
expect(geojsonBox).an('object');
190+
expect(geojsonBox.type).equal('Feature');
191+
expect(geojsonBox.geometry).an('object');
192+
193+
expect(geojsonBox.geometry.coordinates).an('array');
194+
for (const coord of geojsonBox.geometry.coordinates) {
195+
expect(coord).a('number');
196+
}
197+
expect(Math.abs(geojsonBox.geometry.coordinates[0])).most(180);
198+
expect(Math.abs(geojsonBox.geometry.coordinates[1])).most(90);
199+
200+
return chakram.wait();
201+
});
202+
});
203+
184204
it('should let users retrieve their arduino sketch', function () {
185205
return chakram.get(`${BASE_URL}/boxes/${boxId}/script`, { headers: { 'Authorization': `Bearer ${jwt}` } })
186206
.then(function (response) {
@@ -423,7 +443,8 @@ describe('openSenseMap API Routes: /boxes', function () {
423443
expect(response).to.comprise.of.json('data.exposure', update_payload.exposure);
424444
expect(response).to.comprise.of.json('data.grouptag', update_payload.grouptag);
425445
expect(response).to.comprise.of.json('data.description', update_payload.description);
426-
expect(response).to.comprise.of.json('data.currentLocation', { type: 'Point',
446+
expect(response).to.comprise.of.json('data.currentLocation', {
447+
type: 'Point',
427448
coordinates: [update_payload.location.lng, update_payload.location.lat]
428449
});
429450

0 commit comments

Comments
 (0)