where('head_user_id', $request->user()->id) ->first(); if (! $household || ! $household->assigned_drop_off_point_id) { return $this->ok([ 'household_assigned' => false, 'pickups' => [], ]); } $dopId = $household->assigned_drop_off_point_id; $trips = Trip::query() ->with(['route:id,name,code', 'team:id,name,driver_id', 'team.driver:id,first_name,last_name', 'truck:id,plate_number']) ->whereHas('route.stops', fn ($q) => $q->where('drop_off_point_id', $dopId)) ->where(function ($q) { $q->where('scheduled_date', '>=', now()->toDateString()) ->orWhereIn('status', [Trip::STATUS_IN_PROGRESS, Trip::STATUS_AT_DUMPSITE]); }) ->whereNotIn('status', [Trip::STATUS_CANCELLED, Trip::STATUS_COMPLETED]) ->orderBy('scheduled_date') ->orderBy('scheduled_start_time') ->limit(10) ->get(); $pickups = $trips->map(function (Trip $trip) use ($dopId) { $myStop = $trip->route?->stops()->where('drop_off_point_id', $dopId)->first(); return [ 'trip_id' => $trip->uuid, 'trip_number' => $trip->trip_number, 'status' => $trip->status, 'scheduled_date' => $trip->scheduled_date?->toDateString(), 'scheduled_start_time' => $trip->scheduled_start_time, 'route' => $trip->route?->name, 'truck_plate' => $trip->truck?->plate_number, 'driver_name' => $trip->team?->driver?->full_name, 'my_stop_sequence' => $myStop?->sequence, ]; }); return $this->ok([ 'household_assigned' => true, 'drop_off_point' => [ 'id' => $household->assignedDropOffPoint?->uuid, 'name' => $household->assignedDropOffPoint?->name, ], 'pickups' => $pickups, ]); } }