trails/apps/journal/app/routes/api.v1.activities.$id.ts
Ullrich Schäfer 2cb32cd2d3
activity-sport-type: schema, contract, write/read paths, federation
Implements the activity-sport-type change (specs/activity-sport-type):

- db: nullable `sport_type` column on journal.activities + SportType /
  SPORT_TYPES (text().$type<> convention).
- api: optional sportType on the activity read + create schemas (mirrored
  SPORT_TYPES; @trails-cool/api stays zod-only).
- write: ActivityInput + createActivity persist it; mapSportType() normalizes
  provider strings (Komoot bulk import passes tour.sport; Garmin unset);
  threaded through the unified importActivity.
- read/display: sportType added to the detail/feed/profile loaders and the v1
  REST endpoints; shared SportBadge (glyph + i18n label) on detail, feed, and
  profile; sport-aware feed verb; create-form <select>.
- i18n: journal.activities.sport.* (labels + verbs) in en + de.
- federation: `sport` PropertyValue on the Note when set.

Tests: mapSportType unit table; federation asserts the sport attachment is
present when set and omitted when unset. typecheck + lint + unit all green.
E2E (create→badge) still to add.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-12 14:11:41 +02:00

47 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 ?? "",
sportType: activity.sportType,
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 });
}