import { requireAuth } from "@/lib/jwt"; import { sql } from "@/lib/db"; // GET — the signed-in rider's ride history (completed + cancelled rides), // newest first, with the assigned driver (nullable via LEFT JOIN). This feeds // the "Recent Rides" / "All rides" lists; the active/in-progress ride is // tracked separately on the book-ride status screen. export async function GET(req: Request) { const auth = requireAuth(req); if ("error" in auth) return auth.error; try { const response = await sql` SELECT r.ride_id, r.origin_address, r.destination_address, r.origin_latitude, r.origin_longitude, r.destination_latitude, r.destination_longitude, r.ride_time, r.fare_price, r.payment_status, r.status, r.service, r.created_at, r.completed_at, r.cancelled_at, json_build_object( 'id', d.id, 'first_name', d.first_name, 'last_name', d.last_name, 'car_seats', d.car_seats, 'profile_image_url', d.profile_image_url, 'car_image_url', d.car_image_url, 'rating', d.rating, 'service', d.service, 'car_model', d.car_model ) AS driver FROM rides r LEFT JOIN drivers d ON d.id = r.driver_id WHERE r.user_id = ${auth.userId} AND r.status IN ('completed', 'cancelled') ORDER BY r.created_at DESC `; return Response.json({ data: response }); } catch (error) { console.error("[GET_RIDE_LIST]: ", error); return Response.json({ error: "Internal Server Error" }, { status: 500 }); } }