trails/apps/journal/app/routes/api.v1.activities.$id.ts
Ullrich Schäfer 61a2d0085b
one source of truth for Route/Activity shapes; enforce api contracts
Route and Activity existed three times: hand-written interfaces in
packages/types, Zod contracts in packages/api, and Drizzle columns in
packages/db — each with different fields and nullability. The
hand-written ones had drifted so far they had zero importers; the Zod
contracts were advisory because v1 handlers hand-rolled Response.json
shapes nothing validated.

- packages/types keeps only what both apps actually share (Waypoint,
  WaypointPoiTags) and documents where row types and wire contracts
  live; the dead Route/RouteMetadata/RouteVersion/Activity interfaces
  are gone
- packages/db exports canonical inferred row types (RouteRow,
  ActivityRow, RouteVersionRow, UserRow)
- packages/api contracts are reconciled with the real wire format
  (RouteVersionSchema gains the id and createdBy fields the endpoint
  has always returned) and gain Create*ResponseSchemas
- apiJson(schema, payload) in api-guard parses every v1 response
  through its contract: drift is now a thrown ZodError in tests/CI,
  unknown keys are stripped, and payloads are compile-checked as
  z.input of the schema

Enforcement immediately caught two real drifts: nullable DB
descriptions could ship null where the contract promises string (now
coalesced at the boundary), and GET /api/v1/activities/:id was missing
the routeName and photos fields its contract declares.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-06-10 07:39:50 +02:00

46 lines
1.8 KiB
TypeScript

import type { Route } from "./+types/api.v1.activities.$id";
import { requireApiUser, apiError, apiJson } from "~/lib/api-guard.server";
import { getActivity, deleteActivity } from "~/lib/activities.server";
import { loadOwnedActivity } from "~/lib/ownership.server";
import { ERROR_CODES, ActivityDetailSchema } from "@trails-cool/api";
/** GET /api/v1/activities/:id — full activity detail */
export async function loader({ request, params }: Route.LoaderArgs) {
const user = await requireApiUser(request);
const activity = await getActivity(params.id);
if (!activity || activity.ownerId !== user.id) {
return apiError(404, ERROR_CODES.NOT_FOUND, "Activity not found");
}
return apiJson(ActivityDetailSchema, {
id: activity.id,
name: activity.name,
description: activity.description ?? "",
routeId: activity.routeId,
routeName: null, // TODO: join route name (matches the list endpoint)
photos: [], // no photos on this surface yet; contract field
distance: activity.distance,
duration: activity.duration,
elevationGain: activity.elevationGain,
elevationLoss: activity.elevationLoss,
startedAt: activity.startedAt?.toISOString() ?? null,
gpx: activity.gpx,
geojson: activity.geojson,
createdAt: activity.createdAt.toISOString(),
});
}
/** DELETE /api/v1/activities/:id */
export async function action({ request, params }: Route.ActionArgs) {
if (request.method !== "DELETE") return new Response(null, { status: 405 });
const user = await requireApiUser(request);
const result = await loadOwnedActivity(params.id, user.id);
if (!result.ok) {
return apiError(404, ERROR_CODES.NOT_FOUND, "Activity not found");
}
await deleteActivity(result.entity);
return new Response(null, { status: 204 });
}