From 3ae41a8cd58925769113caf272849b2bb25f7707 Mon Sep 17 00:00:00 2001 From: Shibi Chakkaravarthy Date: Tue, 16 Jun 2026 00:56:15 +0530 Subject: [PATCH] location update with separate data structure for geofence events --- controllers/assignment.controller.js | 48 ++++++++++++++++++++++++++ controllers/location.controller.js | 51 +++++++++++++++++++++++++++- documentation/assignment.js | 20 +++++++++++ documentation/documentation.json | 22 ++++++++++++ routes/assignment.route.js | 22 +++++++++--- 5 files changed, 158 insertions(+), 5 deletions(-) diff --git a/controllers/assignment.controller.js b/controllers/assignment.controller.js index bfe03de..509fe4e 100644 --- a/controllers/assignment.controller.js +++ b/controllers/assignment.controller.js @@ -98,3 +98,51 @@ export const getAssignmentByVenueHandler = async (req, res) => { res.status(500).json({ message: "Error getting assignment", error }); } }; + +export const getAssignmentByIdHandler = async (req, res) => { + try { + const { id } = req.params; + + const pipeline = [ + { + $match: { + _id: ObjectId.createFromHexString(id), + }, + }, + { + $lookup: { + from: "venues", + localField: "venue", + foreignField: "_id", + as: "venue", + }, + }, + { + $unwind: { + path: "$venue", + preserveNullAndEmptyArrays: true, + }, + }, + { + $lookup: { + from: "partners", + localField: "venue.entity", + foreignField: "_id", + as: "partner", + }, + }, + { + $unwind: { + path: "$partner", + preserveNullAndEmptyArrays: true, + }, + }, + ]; + + const assignment = await aggregateAssignment(pipeline)[0]; + + res.status(200).json({ result: assignment }); + } catch (error) { + res.status(500).json({ message: "Error getting assignment", error }); + } +}; diff --git a/controllers/location.controller.js b/controllers/location.controller.js index 8c5e973..6c84a90 100644 --- a/controllers/location.controller.js +++ b/controllers/location.controller.js @@ -40,12 +40,49 @@ Sample Location Payload "odometer_error": 0, "odometer": 0 } + +Geofence Event { + event: 'geofence', + is_moving: true, + uuid: 'dfa0f17d-017f-4fd1-beca-67e62f0087d9', + timestamp: '2026-06-15T18:15:08.430Z', + recorded_at: '2026-06-15T18:15:08.607Z', + age: 0.179, + odometer: 345.61, + odometer_error: 16.68, + coords: { + latitude: 13.1243472, + longitude: 80.1428553, + accuracy: 3.45, + speed: 1.01, + speed_accuracy: 0.19, + heading: 280.72, + heading_accuracy: 12.79, + altitude: -51.9, + ellipsoidal_altitude: -51.9, + altitude_accuracy: 1.11 + }, + activity: { type: 'walking', confidence: 100 }, + battery: { is_charging: false, level: 0.69 }, + geofence: { + identifier: '6a2e128af72b4c6184fd53ea-6a1f658f8fc61da8a1809091-6a2e126bf72b4c6184fd5310', + action: 'EXIT', + timestamp: '2026-06-15T18:15:08.612Z', + extras: { + venue: '6a2e126bf72b4c6184fd5310', + partner: '6a1f658f8fc61da8a1809091' + } + }, + extras: {} */ export const createLocationController = async (req, res) => { try { const { user } = res.locals; const { location } = req.body; + if (location.event === "geofence") { + console.log("Geofence Event", location); + } const dbPayload = { battery: location.battery.level, coordinates: location.coords, @@ -54,8 +91,20 @@ export const createLocationController = async (req, res) => { activity: location?.activity?.type, recorded_at: location.recorded_at, }; + + if (location.event === "geofence") { + const [assignment, partner, venue] = + location?.geofence?.identifier?.split("-"); + const geofencePayload = { + assignment, + partner, + venue, + action: location?.geofence?.action, + }; + + dbPayload.geofence = geofencePayload; + } const uploaded = await createLocation({ ...dbPayload, user: user?.id }); - console.log("BG LOCATION DB DOC", uploaded, user); res.status(200).json({ result: uploaded }); } catch (error) { console.log("createLocationController Error", error); diff --git a/documentation/assignment.js b/documentation/assignment.js index 2fe3a48..6061a7d 100644 --- a/documentation/assignment.js +++ b/documentation/assignment.js @@ -78,6 +78,26 @@ export default { }, }, }, + get: { + tags: ["Assignment"], + summary: "Update an assignment", + parameters: [ + { + in: "path", + name: "id", + required: true, + schema: { + type: "string", + }, + }, + ], + responses: { + 200: { + description: "OK", + content: {}, + }, + }, + }, delete: { tags: ["Assignment"], summary: "Delete an assignment", diff --git a/documentation/documentation.json b/documentation/documentation.json index e0ee0dc..c921e9b 100644 --- a/documentation/documentation.json +++ b/documentation/documentation.json @@ -710,6 +710,28 @@ } } }, + "get": { + "tags": [ + "Assignment" + ], + "summary": "Update an assignment", + "parameters": [ + { + "in": "path", + "name": "id", + "required": true, + "schema": { + "type": "string" + } + } + ], + "responses": { + "200": { + "description": "OK", + "content": {} + } + } + }, "delete": { "tags": [ "Assignment" diff --git a/routes/assignment.route.js b/routes/assignment.route.js index 3fa13d6..4641806 100644 --- a/routes/assignment.route.js +++ b/routes/assignment.route.js @@ -5,15 +5,29 @@ import { getAssignmentsByUserHandler, deactivateAssignmentHandler, getAssignmentByVenueHandler, + getAssignmentByIdHandler, } from "../controllers/assignment.controller"; -import {authorize} from "../middlewares/jwt.middleware"; +import { authorize } from "../middlewares/jwt.middleware"; const router = new Router(); router.post("/", authorize("assignment", "write"), createAssignmentHandler); router.patch("/:id", authorize("assignment", "write"), updateAssignmentHandler); -router.get("/user/:id", authorize("assignment", "read"), getAssignmentsByUserHandler); -router.delete("/:id", authorize("assignment", "write"), deactivateAssignmentHandler); -router.get("/venue/:id", authorize("assignment", "read"), getAssignmentByVenueHandler); +router.patch("/:id", authorize("assignment", "read"), getAssignmentByIdHandler); +router.get( + "/user/:id", + authorize("assignment", "read"), + getAssignmentsByUserHandler, +); +router.delete( + "/:id", + authorize("assignment", "write"), + deactivateAssignmentHandler, +); +router.get( + "/venue/:id", + authorize("assignment", "read"), + getAssignmentByVenueHandler, +); export default router;