Add bus detail functionality and update organization selection UI

This commit is contained in:
ImBenji
2026-03-29 19:12:49 +01:00
parent 55cd970173
commit 3dfea45afb
10 changed files with 425 additions and 97 deletions
+3
View File
@@ -48,3 +48,6 @@ verify_jwt = false
[functions.operations-stop-detail]
verify_jwt = false
[functions.operations-bus-detail]
verify_jwt = false
@@ -0,0 +1,81 @@
import { fail, handleOptions, json } from "../_shared/http.ts";
import { requireUser } from "../_shared/supabase.ts";
Deno.serve(async (req) => {
const preflight = handleOptions(req);
if (preflight) return preflight;
if (req.method !== "POST") return fail("Method not allowed", 405);
const { client, user, error: userError } = await requireUser(req);
if (!user) return fail(userError ?? "Unauthorized", 401);
let body: { channel_id?: string; schedule_id?: string; bus_work_number?: string };
try {
body = await req.json();
} catch {
return fail("Invalid JSON body");
}
const channelId = (body.channel_id ?? "").trim();
const scheduleId = (body.schedule_id ?? "").trim();
const busWorkNumber = (body.bus_work_number ?? "").trim();
if (!channelId) return fail("channel_id is required");
if (!scheduleId) return fail("schedule_id is required");
if (!busWorkNumber) return fail("bus_work_number is required");
const { data: schedule, error: scheduleError } = await client
.from("operations_schedules")
.select("id, channel_id")
.eq("id", scheduleId)
.eq("channel_id", channelId)
.maybeSingle();
if (scheduleError) return fail(scheduleError.message, 400);
if (!schedule) return fail("forbidden", 403);
const { data: tripRows, error: tripError } = await client
.from("operations_trips")
.select("id, trip_number, duty_number, bus_work_number, direction, sort_order")
.eq("schedule_id", scheduleId)
.eq("bus_work_number", busWorkNumber)
.order("sort_order", { ascending: true });
if (tripError) return fail(tripError.message, 500);
const trips = (tripRows ?? []) as {
id: string;
trip_number: string;
duty_number: string;
bus_work_number: string;
direction: string;
sort_order: number;
}[];
if (trips.length === 0) return json({ trips: [] });
const tripIds = trips.map((t) => t.id);
const { data: stopRows, error: stopError } = await client
.from("operations_trip_stops")
.select("id, trip_id, stop_sequence, stop_name, scheduled_time")
.in("trip_id", tripIds)
.order("stop_sequence", { ascending: true });
if (stopError) return fail(stopError.message, 500);
const stopsByTripId: Record<string, typeof stopRows> = {};
for (const stop of stopRows ?? []) {
const s = stop as { trip_id: string };
if (!stopsByTripId[s.trip_id]) stopsByTripId[s.trip_id] = [];
stopsByTripId[s.trip_id]!.push(stop);
}
const result = trips.map((trip) => ({
...trip,
stops: stopsByTripId[trip.id] ?? [],
}));
return json({ trips: result });
});
@@ -63,6 +63,7 @@ Deno.serve(async (req) => {
}[];
const duties = [...new Set(trips.map((t) => t.duty_number))].sort();
const busWorkNumbers = [...new Set(trips.map((t) => t.bus_work_number))].sort();
const tripMeta = trips.map((t) => ({
trip_number: t.trip_number,
@@ -109,6 +110,7 @@ Deno.serve(async (req) => {
has_schedule: true,
schedule_id: scheduleId,
duties,
bus_work_numbers: busWorkNumbers,
trips: tripMeta,
stop_names: stopNames,
aliases,
@@ -14,24 +14,23 @@ type OpenAiAlias = {
estimated?: string;
};
const prompt = `You are interpreting abbreviated station names from a rail replacement service display.
const prompt = `You are interpreting abbreviated stop names from a bus schedule display.
Each entry follows this structure:
* A 4-letter station code (derived from the real station name, often by removing vowels or compressing syllables)
* A 2-letter stop code (ignore this; it is ambiguous and not needed)
* A short stop code (typically 4 letters, derived from the stop name by removing vowels or compressing syllables)
* A 2-letter zone or bay code (ignore this; it is not needed)
* An optional "T" indicating terminus (ignore for naming purposes)
Your task is to infer the full station names from the 4-letter codes.
Your task is to infer the full stop names from the short codes.
Guidelines:
* Treat the 4-letter code as a compressed version of a real station name (e.g. consonant-heavy, missing vowels, or merged syllables)
* Use pattern recognition rather than strict decoding
* Prefer real-world plausibility over perfect letter matching
* Assume all stations are on the same rail corridor or geographically connected route
* Use the sequence of stops to inform your guesses (adjacent stations should make sense geographically)
* If a code is slightly irregular, prioritise what fits the route best over what matches the letters exactly
* Treat the code as a compressed version of a stop or locality name
* Use pattern recognition rather than strict letter matching
* Do not assume any specific country, region or city — infer purely from the codes and their sequence
* Use the sequence of stops to inform your guesses (adjacent stops should make sense as a connected route)
* If a code is ambiguous, prefer the interpretation that best fits the surrounding stops
Output:
Return JSON with shape {"aliases":[{"original":"<code>","estimated":"<name>"}]}.