Skip to content

Commit bef932a

Browse files
committed
update boxes PUT requests and tests
1 parent fd2d30b commit bef932a

File tree

3 files changed

+56
-58
lines changed

3 files changed

+56
-58
lines changed

packages/models/src/box/box.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,8 @@ const DEVICE_COLUMNS_FOR_RETURNING = {
181181
createdAt: true,
182182
updatedAt: true,
183183
location: true,
184+
latitude: true,
185+
longitude: true,
184186
status: true
185187
};
186188

packages/models/src/device/index.js

Lines changed: 37 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const ModelError = require('../modelError');
88
const { inArray, arrayContains, sql, eq, asc, ilike } = require('drizzle-orm');
99
const { insertMeasurement, insertMeasurements } = require('../measurement');
1010
const SketchTemplater = require('@sensebox/sketch-templater');
11+
const { utcNow } = require('../utils');
1112

1213
const { max_boxes: pagination_max_boxes } = require('config').get('openSenseMap-API-models.pagination');
1314

@@ -274,41 +275,6 @@ const saveMeasurements = async function saveMeasurements (device, measurements)
274275
await insertMeasurements(measurements);
275276
};
276277

277-
async function updateLocation(deviceId, coords, timestamp = utcNow()) {
278-
if (!coords || coords.length !== 2) {
279-
throw new Error('Invalid coordinates provided');
280-
}
281-
282-
const [longitude, latitude] = coords;
283-
284-
// Check if the location already exists
285-
let existingLocation = await db.query.location.findFirst({
286-
where: eq(location.location, `POINT(${longitude} ${latitude})`),
287-
});
288-
289-
if (!existingLocation) {
290-
// Insert new location if not found
291-
const newLocation = await db.insert(location).values({
292-
location: `POINT(${longitude} ${latitude})`,
293-
}).returning();
294-
existingLocation = newLocation[0];
295-
}
296-
297-
// Insert into device_to_location linking table
298-
await db.insert(deviceToLocation).values({
299-
deviceId,
300-
locationId: existingLocation.id,
301-
time: timestamp,
302-
});
303-
304-
// Update the device's current location
305-
await db.update(device)
306-
.set({ latitude, longitude, updatedAt: timestamp })
307-
.where(eq(device.id, deviceId));
308-
309-
return existingLocation;
310-
}
311-
312278
const updateDevice = async function updateDevice (deviceId, args) {
313279
const {
314280
mqtt: {
@@ -410,15 +376,43 @@ const updateDevice = async function updateDevice (deviceId, args) {
410376
// box.addAddon(addonToAdd);
411377
// }
412378

413-
// TODO: run location update logic, if a location was provided.
414-
// const locPromise = location
415-
// ? box
416-
// .updateLocation(location)
417-
// .then((loc) => box.set({ currentLocation: loc }))
418-
// : Promise.resolve();
419-
// update location
379+
// run location update logic, if a location was provided.
380+
// if the provided location already exists, just update the relation
381+
// if the location does not exist, create it and update
420382
if (location) {
421-
await updateLocation(deviceId, location);
383+
const [geometry] = await db
384+
.insert(locationTable)
385+
.values({
386+
location: sql`ST_SetSRID(ST_MakePoint(${location[1]}, ${location[0]}), 4326)`
387+
})
388+
.onConflictDoNothing()
389+
.returning({ id: locationTable.id });
390+
391+
if (geometry) {
392+
// update location relation (update location id of device)
393+
await db
394+
.update(deviceToLocationTable)
395+
.set({ locationId: geometry.id })
396+
.where(eq(deviceToLocationTable.deviceId, deviceId));
397+
} else {
398+
// Get location id
399+
const geom = await db.query.locationTable.findFirst({
400+
columns: {
401+
id: true
402+
},
403+
where: sql`ST_Equals(${locationTable.location}, ST_SetSRID(ST_MakePoint(${location[1]}, ${location[0]}), 4326))`
404+
});
405+
406+
// update location relation (update location id of device)
407+
await db
408+
.update(deviceToLocationTable)
409+
.set({ locationId: geom.id })
410+
.where(eq(deviceToLocationTable.deviceId, deviceId));
411+
}
412+
413+
// also set latitude and longitude of device
414+
setColumns['longitude'] = location[0];
415+
setColumns['latitude'] = location[1];
422416
}
423417

424418
const device = await db

tests/tests/002-location_tests.js

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -182,12 +182,15 @@ describe('openSenseMap API locations tests', function () {
182182
.then(logResponseIfError)
183183
.then(function (response) {
184184
expect(response).to.have.status(200);
185-
expect(response.body.data.currentLocation).to.exist;
186-
expect(response.body.data.currentLocation.coordinates).to.deep.equal(loc);
187-
expect(response.body.data.currentLocation.timestamp).to.exist;
188-
expect(moment().diff(response.body.data.currentLocation.timestamp)).to.be.below(300);
185+
expect(response.body.data.latitude).to.exist;
186+
expect(response.body.data.longitude).to.exist;
187+
expect(response.body.data.longitude).to.deep.equal(loc[0]);
188+
expect(response.body.data.latitude).to.deep.equal(loc[1]);
189189

190-
submitTimeLoc1 = response.body.data.currentLocation.timestamp;
190+
expect(response.body.data.updatedAt).to.exist;
191+
expect(moment().diff(response.body.data.updatedAt)).to.be.below(300);
192+
193+
submitTimeLoc1 = response.body.data.updatedAt;
191194

192195
return chakram.wait();
193196
});
@@ -200,14 +203,12 @@ describe('openSenseMap API locations tests', function () {
200203
.then(logResponseIfError)
201204
.then(function (response) {
202205
expect(response).to.have.status(200);
203-
expect(response.body.data.currentLocation).to.exist;
204-
expect(response.body.data.currentLocation.coordinates).to.deep.equal([
205-
loc.lng,
206-
loc.lat,
207-
loc.height,
208-
]);
209-
expect(response.body.data.currentLocation.timestamp).to.exist;
210-
expect(moment().diff(response.body.data.currentLocation.timestamp)).to.be.below(300);
206+
expect(response.body.data.latitude).to.exist;
207+
expect(response.body.data.longitude).to.exist;
208+
expect(response.body.data.longitude).to.deep.equal(loc.lng);
209+
expect(response.body.data.latitude).to.deep.equal(loc.lat);
210+
expect(response.body.data.updatedAt).to.exist;
211+
expect(moment().diff(response.body.data.updatedAt)).to.be.below(300);
211212

212213
box = response.body.data;
213214

@@ -236,8 +237,9 @@ describe('openSenseMap API locations tests', function () {
236237
});
237238

238239
it('should return the current location in box.currentLocation', function () {
239-
expect(result.currentLocation).to.exist;
240-
expect(result.currentLocation).to.deep.equal(box.currentLocation);
240+
expect(result.latitude).to.exist;
241+
expect(result.longitude).to.exist;
242+
expect([result.longitude, result.latitude]).to.deep.equal([box.longitude, box.latitude]);
241243
});
242244

243245
it('should NOT return the whole location history in box.locations', function () {

0 commit comments

Comments
 (0)