import { createLocation, getLocationByQuery, getLocationByRadius, } from "../models/Location/operations"; import { aggregateAssignment } from "../models/Assignment/operations"; import { ObjectId } from "mongodb"; /* Sample Location Payload { "extras": {}, "activity": { "confidence": 100, "type": "still" }, "timestamp": "2026-04-25T17:31:25.320Z", "battery": { "level": 0.57, "is_charging": false }, "recorded_at": "2026-04-25T17:31:25.388Z", "age": 0.074, "is_moving": false, "event": "motionchange", "uuid": "4c476d94-b7db-4823-9270-805235ed0ec8", "coords": { "ellipsoidal_altitude": 0, "altitude": 0, "altitude_accuracy": 0.5, "heading_accuracy": -1, "heading": -1, "speed": 0, "accuracy": 5, "longitude": 77.48675, "speed_accuracy": 0.5, "latitude": 9.9707883 }, "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, event: location.event, heading: location.coords.heading, 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 }); res.status(200).json({ result: uploaded }); } catch (error) { console.log("createLocationController Error", error); res.status(500).json({ error: error.message }); } }; export const getLocationController = async (req, res) => { try { const location = await getLocationByQuery(req.query); res.status(200).json(location); } catch (error) { res.status(500).json({ error: error.message }); } }; export const getMyAssignedVenuesHandler = async (req, res, next) => { try { const { user } = res.locals; const pipeline = [ { $match: { user: ObjectId?.createFromHexString(user.id), isActive: true, }, }, { $lookup: { from: "venues", localField: "venue", foreignField: "_id", as: "venue", }, }, { $unwind: { path: "$venue", }, }, { $lookup: { from: "partners", localField: "venue.entity", foreignField: "_id", as: "partner", }, }, { $unwind: { path: "$partner", }, }, ]; const assignment = await aggregateAssignment(pipeline); res.status(200).json({ result: assignment }); } catch (error) { console.log("getMyAssignedVenuesHandler Error", error); next(error); } };