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>
74 lines
2.5 KiB
TypeScript
74 lines
2.5 KiB
TypeScript
import type { Route } from "./+types/api.v1.activities._index";
|
|
import { requireApiUser, apiError, apiJson } from "~/lib/api-guard.server";
|
|
import { listActivities, createActivity } from "~/lib/activities.server";
|
|
import {
|
|
PaginationQuerySchema,
|
|
CreateActivityRequestSchema,
|
|
ERROR_CODES,
|
|
zodIssuesToFieldErrors,
|
|
ActivityListResponseSchema,
|
|
CreateActivityResponseSchema,
|
|
} from "@trails-cool/api";
|
|
|
|
/** GET /api/v1/activities — paginated activity list */
|
|
export async function loader({ request }: Route.LoaderArgs) {
|
|
const user = await requireApiUser(request);
|
|
const url = new URL(request.url);
|
|
const query = PaginationQuerySchema.safeParse({
|
|
cursor: url.searchParams.get("cursor") ?? undefined,
|
|
limit: url.searchParams.get("limit") ?? undefined,
|
|
});
|
|
if (!query.success) {
|
|
return apiError(400, ERROR_CODES.VALIDATION_ERROR, "Invalid pagination params");
|
|
}
|
|
|
|
const allActivities = await listActivities(user.id);
|
|
const { cursor, limit } = query.data;
|
|
|
|
let startIdx = 0;
|
|
if (cursor) {
|
|
const idx = allActivities.findIndex((a) => a.id === cursor);
|
|
startIdx = idx >= 0 ? idx + 1 : 0;
|
|
}
|
|
|
|
const page = allActivities.slice(startIdx, startIdx + limit);
|
|
const nextCursor = startIdx + limit < allActivities.length ? page[page.length - 1]?.id ?? null : null;
|
|
|
|
return apiJson(ActivityListResponseSchema, {
|
|
activities: page.map((a) => ({
|
|
id: a.id,
|
|
name: a.name,
|
|
description: a.description ?? "",
|
|
sportType: a.sportType,
|
|
routeId: a.routeId,
|
|
routeName: null, // TODO: join route name
|
|
distance: a.distance,
|
|
duration: a.duration,
|
|
elevationGain: a.elevationGain,
|
|
elevationLoss: a.elevationLoss,
|
|
startedAt: a.startedAt?.toISOString() ?? null,
|
|
geojson: a.geojson,
|
|
createdAt: a.createdAt.toISOString(),
|
|
})),
|
|
nextCursor,
|
|
});
|
|
}
|
|
|
|
/** POST /api/v1/activities — create a new activity */
|
|
export async function action({ request }: Route.ActionArgs) {
|
|
if (request.method !== "POST") return new Response(null, { status: 405 });
|
|
const user = await requireApiUser(request);
|
|
|
|
const body = await request.json().catch(() => null);
|
|
const parsed = CreateActivityRequestSchema.safeParse(body);
|
|
if (!parsed.success) {
|
|
return apiError(400, ERROR_CODES.VALIDATION_ERROR, "Validation failed",
|
|
zodIssuesToFieldErrors(parsed.error));
|
|
}
|
|
|
|
const id = await createActivity(user.id, {
|
|
...parsed.data,
|
|
startedAt: parsed.data.startedAt ? new Date(parsed.data.startedAt) : null,
|
|
});
|
|
return apiJson(CreateActivityResponseSchema, { id }, { status: 201 });
|
|
}
|