Roadbound-BRR/supabase/functions/operations-schedule-meta/index.ts

116 lines
3.1 KiB
TypeScript

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 };
try {
body = await req.json();
} catch {
return fail("Invalid JSON body");
}
const channelId = (body.channel_id ?? "").trim();
if (!channelId) return fail("channel_id is required");
const { data: channel, error: channelError } = await client
.from("channels")
.select("id, type")
.eq("id", channelId)
.eq("type", "operations")
.maybeSingle();
if (channelError) return fail(channelError.message, 400);
if (!channel) return fail("forbidden", 403);
const { data: scheduleRow } = await client
.from("operations_schedules")
.select("id, version, source_file_name, parsed_at")
.eq("channel_id", channelId)
.eq("is_active", true)
.order("version", { ascending: false })
.limit(1)
.maybeSingle();
if (!scheduleRow) {
return json({ has_schedule: false });
}
const scheduleId = scheduleRow.id as string;
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)
.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;
}[];
const duties = [...new Set(trips.map((t) => t.duty_number))].sort();
const tripMeta = trips.map((t) => ({
trip_number: t.trip_number,
duty_number: t.duty_number,
}));
// get unique stop names
let stopNames: string[] = [];
if (trips.length > 0) {
const tripIds = trips.map((t) => t.id);
const { data: stopRows, error: stopError } = await client
.from("operations_trip_stops")
.select("stop_name")
.in("trip_id", tripIds);
if (stopError) return fail(stopError.message, 500);
stopNames = [
...new Set(
(stopRows ?? []).map((r: { stop_name: string }) =>
(r.stop_name ?? "").trim()
).filter((s: string) => s.length > 0)
),
].sort();
}
const { data: aliasRows } = await client
.from("operations_stop_aliases")
.select("raw_stop_name, alias_stop_name, source")
.eq("channel_id", channelId);
const aliases = (aliasRows ?? []).map((r: {
raw_stop_name: string;
alias_stop_name: string;
source: string;
}) => ({
raw_stop_name: r.raw_stop_name,
alias_stop_name: r.alias_stop_name,
source: r.source ?? "user",
}));
return json({
has_schedule: true,
schedule_id: scheduleId,
duties,
trips: tripMeta,
stop_names: stopNames,
aliases,
});
});