feat: origin_stop_id y destination_stop_id en routes - motor de llegadas ahora usa parada de inicio explicita como referencia, admin puede configurar inicio/fin por ruta
This commit is contained in:
@ -68,17 +68,17 @@ export const busStopsService = {
|
|||||||
* Real-time bus arrival calculator for a specific stop.
|
* Real-time bus arrival calculator for a specific stop.
|
||||||
*
|
*
|
||||||
* Logic:
|
* Logic:
|
||||||
* 1. Get all routes passing through this stop + their travel_time_minutes to the stop
|
* 1. Get all routes through this stop + their travel_time_minutes
|
||||||
* 2. Get today's published schedules for those routes
|
* 2. Get route's origin_stop_id → get its travel_time_minutes as the "base"
|
||||||
* 3. For each schedule: arrivalAtStop = departure_time + travel_time_minutes
|
* 3. Effective travel time = stop.travel_time - origin.travel_time (always >= 0)
|
||||||
* 4. With GPS: filter to buses not yet passed, calculate wait minutes
|
* 4. arrivalAtStop = departure_time + effective_travel_time
|
||||||
* Without GPS: show all future arrivals as raw times for today (option A)
|
* 5. With GPS: filter to future buses, return wait minutes
|
||||||
* 5. Return sorted by arrivalTime asc, max 8 results within next 2 hours
|
* Without GPS (option A): show raw schedule times for today
|
||||||
*/
|
*/
|
||||||
async getNextBusesForStop(stopId: string, userLat?: number, userLng?: number): Promise<BusArrival[]> {
|
async getNextBusesForStop(stopId: string, userLat?: number, userLng?: number): Promise<BusArrival[]> {
|
||||||
const hasGps = userLat !== undefined && userLng !== undefined
|
const hasGps = userLat !== undefined && userLng !== undefined
|
||||||
|
|
||||||
// ── STEP 1: Get route_stops entries for this stop (includes travel_time_minutes) ──
|
// ── STEP 1: Get route_stops entry for THIS stop on every route it belongs to ──
|
||||||
const { data: routeStopsData, error: rsError } = await supabase
|
const { data: routeStopsData, error: rsError } = await supabase
|
||||||
.from('route_stops')
|
.from('route_stops')
|
||||||
.select('route_id, travel_time_minutes, stop_delay_minutes')
|
.select('route_id, travel_time_minutes, stop_delay_minutes')
|
||||||
@ -89,24 +89,59 @@ export const busStopsService = {
|
|||||||
|
|
||||||
const routeIds = routeStopsData.map((rs: any) => rs.route_id)
|
const routeIds = routeStopsData.map((rs: any) => rs.route_id)
|
||||||
|
|
||||||
// Build a lookup: routeId → travel_time_minutes to THIS stop
|
// Build a lookup: routeId → raw travel_time_minutes of THIS stop
|
||||||
const travelTimeMap: Record<string, number> = {}
|
const rawTravelTimeMap: Record<string, number> = {}
|
||||||
for (const rs of routeStopsData as any[]) {
|
for (const rs of routeStopsData as any[]) {
|
||||||
travelTimeMap[rs.route_id] = (rs.travel_time_minutes || 0) + (rs.stop_delay_minutes || 0)
|
rawTravelTimeMap[rs.route_id] = (rs.travel_time_minutes || 0) + (rs.stop_delay_minutes || 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ── STEP 2: Get routes metadata (name) ──
|
// ── STEP 2: Get route metadata including origin_stop_id ──
|
||||||
const { data: routesData, error: routesError } = await supabase
|
const { data: routesData, error: routesError } = await supabase
|
||||||
.from('routes')
|
.from('routes')
|
||||||
.select('id, name')
|
.select('id, name, origin_stop_id')
|
||||||
.in('id', routeIds)
|
.in('id', routeIds)
|
||||||
|
|
||||||
if (routesError) throw new Error(routesError.message)
|
if (routesError) throw new Error(routesError.message)
|
||||||
|
|
||||||
const routeNameMap: Record<string, string> = {}
|
const routeNameMap: Record<string, string> = {}
|
||||||
|
const originStopIdMap: Record<string, string | null> = {} // routeId → origin_stop_id
|
||||||
for (const r of (routesData || []) as any[]) {
|
for (const r of (routesData || []) as any[]) {
|
||||||
routeNameMap[r.id] = r.name
|
routeNameMap[r.id] = r.name
|
||||||
|
originStopIdMap[r.id] = r.origin_stop_id || null
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ── STEP 3: Get travel_time_minutes of the origin stop for each route ──
|
||||||
|
// This gives us the "base offset" so we can normalize travel times correctly.
|
||||||
|
// If origin_stop has travel_time=5 and user's stop has travel_time=30, effective = 25 min.
|
||||||
|
const originStopIds = [...new Set(Object.values(originStopIdMap).filter(Boolean))] as string[]
|
||||||
|
const originTravelTimeMap: Record<string, number> = {} // routeId → origin stop travel_time
|
||||||
|
|
||||||
|
if (originStopIds.length > 0) {
|
||||||
|
// Fetch travel_time_minutes for origin stops across all relevant routes
|
||||||
|
const { data: originRsData } = await supabase
|
||||||
|
.from('route_stops')
|
||||||
|
.select('route_id, stop_id, travel_time_minutes')
|
||||||
|
.in('route_id', routeIds)
|
||||||
|
.in('stop_id', originStopIds)
|
||||||
|
|
||||||
|
for (const ors of (originRsData || []) as any[]) {
|
||||||
|
// Match route to its origin stop
|
||||||
|
if (originStopIdMap[ors.route_id] === ors.stop_id) {
|
||||||
|
originTravelTimeMap[ors.route_id] = ors.travel_time_minutes || 0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Compute EFFECTIVE travel time = user's stop travel_time - origin stop travel_time
|
||||||
|
// This is always >= 0 if the data is correct (origin is before the user's stop)
|
||||||
|
const effectiveTravelTimeMap: Record<string, number> = {}
|
||||||
|
for (const routeId of routeIds) {
|
||||||
|
const rawTime = rawTravelTimeMap[routeId] ?? 0
|
||||||
|
const originTime = originTravelTimeMap[routeId] ?? 0
|
||||||
|
effectiveTravelTimeMap[routeId] = Math.max(0, rawTime - originTime)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// ── STEP 3: Get today's schedules ──
|
// ── STEP 3: Get today's schedules ──
|
||||||
const now = new Date()
|
const now = new Date()
|
||||||
const todayDay = DAY_MAP[now.getDay()]
|
const todayDay = DAY_MAP[now.getDay()]
|
||||||
@ -132,9 +167,10 @@ export const busStopsService = {
|
|||||||
|
|
||||||
|
|
||||||
const depMinutes = timeToMinutes(sched.departure_time)
|
const depMinutes = timeToMinutes(sched.departure_time)
|
||||||
const travelMins = travelTimeMap[sched.route_id] ?? 0
|
const travelMins = effectiveTravelTimeMap[sched.route_id] ?? 0
|
||||||
const arrivalMinutes = depMinutes + travelMins
|
const arrivalMinutes = depMinutes + travelMins
|
||||||
|
|
||||||
|
|
||||||
// Handle midnight crossover (e.g. departure 23:50 + 20min travel = 00:10 next day)
|
// Handle midnight crossover (e.g. departure 23:50 + 20min travel = 00:10 next day)
|
||||||
const arrivalMinutesNormalized = arrivalMinutes % 1440
|
const arrivalMinutesNormalized = arrivalMinutes % 1440
|
||||||
|
|
||||||
|
|||||||
@ -12,6 +12,8 @@ export interface Route {
|
|||||||
direction: string
|
direction: string
|
||||||
origin_city?: string
|
origin_city?: string
|
||||||
destination_city?: string
|
destination_city?: string
|
||||||
|
origin_stop_id?: string | null // ID de la parada donde inicia la ruta
|
||||||
|
destination_stop_id?: string | null // ID de la parada donde finaliza la ruta
|
||||||
distance_km?: number
|
distance_km?: number
|
||||||
estimated_duration_minutes?: number
|
estimated_duration_minutes?: number
|
||||||
average_speed_kmh?: number
|
average_speed_kmh?: number
|
||||||
|
|||||||
@ -107,6 +107,35 @@
|
|||||||
<input v-model="selectedRoute.color" @change="updateRouteDetails" type="color">
|
<input v-model="selectedRoute.color" @change="updateRouteDetails" type="color">
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<!-- Paradas de inicio/fin — crítico para el cálculo de llegadas -->
|
||||||
|
<div class="origin-dest-row">
|
||||||
|
<div class="form-group origin-stop-group">
|
||||||
|
<label>
|
||||||
|
<span class="material-icons md-16">trip_origin</span>
|
||||||
|
Parada de Inicio (donde sale el bus)
|
||||||
|
</label>
|
||||||
|
<select v-model="selectedRoute.origin_stop_id" @change="updateRouteDetails">
|
||||||
|
<option :value="null">— Sin definir —</option>
|
||||||
|
<option v-for="stop in allStops" :key="stop.id" :value="stop.id">
|
||||||
|
{{ stop.name }}
|
||||||
|
</option>
|
||||||
|
</select>
|
||||||
|
</div>
|
||||||
|
<div class="form-group dest-stop-group">
|
||||||
|
<label>
|
||||||
|
<span class="material-icons md-16">place</span>
|
||||||
|
Parada de Fin (destino final)
|
||||||
|
</label>
|
||||||
|
<select v-model="selectedRoute.destination_stop_id" @change="updateRouteDetails">
|
||||||
|
<option :value="null">— Sin definir —</option>
|
||||||
|
<option v-for="stop in allStops" :key="stop.id" :value="stop.id">
|
||||||
|
{{ stop.name }}
|
||||||
|
</option>
|
||||||
|
</select>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
<section class="stops-section">
|
<section class="stops-section">
|
||||||
@ -403,7 +432,9 @@ async function updateRouteDetails() {
|
|||||||
destination_city: selectedRoute.value.destination_city,
|
destination_city: selectedRoute.value.destination_city,
|
||||||
average_speed_kmh: selectedRoute.value.average_speed_kmh,
|
average_speed_kmh: selectedRoute.value.average_speed_kmh,
|
||||||
status: selectedRoute.value.status,
|
status: selectedRoute.value.status,
|
||||||
color: selectedRoute.value.color
|
color: selectedRoute.value.color,
|
||||||
|
origin_stop_id: selectedRoute.value.origin_stop_id ?? null,
|
||||||
|
destination_stop_id: selectedRoute.value.destination_stop_id ?? null,
|
||||||
})
|
})
|
||||||
} catch (err: any) {
|
} catch (err: any) {
|
||||||
console.error('Error updating route:', err)
|
console.error('Error updating route:', err)
|
||||||
@ -646,11 +677,26 @@ h1 { font-size: 1.5rem; font-weight: 800; color: #FEE715; margin: 0; }
|
|||||||
|
|
||||||
.route-details-form { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
|
.route-details-form { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
|
||||||
.form-group { display: flex; flex-direction: column; gap: 6px; }
|
.form-group { display: flex; flex-direction: column; gap: 6px; }
|
||||||
.form-group label { font-size: 0.75rem; color: #94a3b8; font-weight: 700; }
|
.form-group label { font-size: 0.75rem; color: #94a3b8; font-weight: 700; display: flex; align-items: center; gap: 4px; }
|
||||||
.form-group input, .form-group select {
|
.form-group input, .form-group select {
|
||||||
background: #1e293b; border: 1px solid #334155; border-radius: 8px; padding: 10px; color: white; font-size: 0.9rem;
|
background: #1e293b; border: 1px solid #334155; border-radius: 8px; padding: 10px; color: white; font-size: 0.9rem;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Paradas de inicio/fin — sección crítica para el motor de llegadas */
|
||||||
|
.origin-dest-row {
|
||||||
|
display: grid;
|
||||||
|
grid-template-columns: 1fr 1fr;
|
||||||
|
gap: 16px;
|
||||||
|
margin-top: 16px;
|
||||||
|
padding: 16px;
|
||||||
|
background: rgba(254, 231, 21, 0.04);
|
||||||
|
border: 1px solid rgba(254, 231, 21, 0.2);
|
||||||
|
border-radius: 12px;
|
||||||
|
}
|
||||||
|
.origin-stop-group label { color: #4ade80; }
|
||||||
|
.dest-stop-group label { color: #f87171; }
|
||||||
|
@media (max-width: 600px) { .origin-dest-row { grid-template-columns: 1fr; } }
|
||||||
|
|
||||||
.section-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 16px; }
|
.section-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 16px; }
|
||||||
.save-changes-btn { background: #FEE715; color: #101820; border: none; padding: 8px 16px; border-radius: 8px; font-weight: 800; cursor: pointer; }
|
.save-changes-btn { background: #FEE715; color: #101820; border: none; padding: 8px 16px; border-radius: 8px; font-weight: 800; cursor: pointer; }
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user