import { requireDriverProfile } from "@/lib/driver"; import { sql } from "@/lib/db"; // POST — driver location heartbeat. Each ping updates lat/lng/last_seen and // keeps the driver marked online. The client (use-driver-location) fires this // every few seconds while the driver's online toggle is on; going offline is // an explicit PATCH to /driver/profile, not the absence of pings. export async function POST(req: Request) { const result = await requireDriverProfile(req); if ("error" in result) return result.error; try { const body = await req.json(); const { latitude, longitude } = body; if ( typeof latitude !== "number" || typeof longitude !== "number" || Number.isNaN(latitude) || Number.isNaN(longitude) ) { return Response.json( { error: "latitude and longitude must be numbers." }, { status: 400 }, ); } const { driverId } = result; const rows = await sql` UPDATE drivers SET latitude = ${latitude}, longitude = ${longitude}, last_seen = CURRENT_TIMESTAMP, online = TRUE WHERE id = ${driverId} RETURNING id, latitude, longitude, last_seen, online `; return Response.json({ data: rows[0] }); } catch (error) { console.error("[DRIVER_LOCATION]: ", error); return Response.json({ error: "Internal Server Error" }, { status: 500 }); } }