location update with separate data structure for geofence events

This commit is contained in:
Shibi Chakkaravarthy
2026-06-16 00:56:15 +05:30
parent 36e1bf664f
commit 3ae41a8cd5
5 changed files with 158 additions and 5 deletions
+48
View File
@@ -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 });
}
};
+50 -1
View File
@@ -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);
+20
View File
@@ -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",
+22
View File
@@ -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"
+17 -3
View File
@@ -5,6 +5,7 @@ import {
getAssignmentsByUserHandler,
deactivateAssignmentHandler,
getAssignmentByVenueHandler,
getAssignmentByIdHandler,
} from "../controllers/assignment.controller";
import { authorize } from "../middlewares/jwt.middleware";
@@ -12,8 +13,21 @@ 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;