feat(houseofrc): fetch club events, classes and entries with transponders via the timing API (1.81.0)

This commit is contained in:
larssand committed 2026-11-08 15:53:57 +01:00
1 parent 9c1f5713e6
commit b0f2b92c04
15 files changed
+247 -149

No files matched your search

+50 -25
View File
@@ -31,6 +31,7 @@ function createHouseOfRc({ dataDir, env = process.env, fetchImpl = globalThis.fe
}
function setKey(value) {
account = null;
const key = String(value || "").trim();
if (!key) {
fs.rmSync(configPath, { force: true });
@@ -66,11 +67,19 @@ function createHouseOfRc({ dataDir, env = process.env, fetchImpl = globalThis.fe
}
}
// A member's key answers with `user`, a club's (timing) key with `organization`. The
// organization is what lists the club's events and opens their entries.
let account = null;
async function testConnection() {
const result = await get("/v1/me");
if (!result.ok) return result;
const body = result.data?.data || {};
return { ok: true, status: result.status, user: body.user || null, rateLimit: body.rate_limit ?? null };
account = { user: body.user || null, organization: body.organization || null };
return { ok: true, status: result.status, ...account, rateLimit: body.rate_limit ?? null };
}
async function getOrganizationId() {
if (!account) await testConnection();
return account?.organization?.id ?? null;
}
// Members owning these transponders (/timing/users, max 200 per call). The newest owner of a
@@ -102,43 +111,59 @@ function createHouseOfRc({ dataDir, env = process.env, fetchImpl = globalThis.fe
organizerId: event.organizer_id ?? null, seriesId: event.series_id ?? null, url: event.web_url || "",
});
// Upcoming events in a date range (the API has no country or text filter, so filter here).
// At most 5 pages of 100 to stay well inside the rate limit.
// Events in a date range: the club's own (hosted or organized) when the key is an
// organization's, otherwise every public event filtered by country. At most 5 pages of 100.
async function listEvents({ from, to, country = "" } = {}) {
const organizationId = await getOrganizationId();
const query = organizationId
? { parent_id: organizationId, from, to }
: { start_from: from, start_to: to, country: String(country || "").toUpperCase() || undefined };
const events = [];
for (let page = 1; page <= 5; page += 1) {
const result = await get("/v1/events", { start_from: from, start_to: to, sort: "start", per_page: 100, page });
const result = await get("/v1/events", { ...query, sort: "start", per_page: 100, page });
if (!result.ok) return result;
const rows = Array.isArray(result.data?.data) ? result.data.data : [];
events.push(...rows.map(pickEvent));
if (!result.data?.links?.next || rows.length < 100) break;
}
const wanted = String(country || "").toUpperCase();
return { ok: true, status: 200, events: events.filter((event) => !wanted || String(event.country).toUpperCase() === wanted) };
return { ok: true, status: 200, organizationId, events: events.filter((event) => event.type !== "series") };
}
// One event and its entered drivers. /results on an event that hasn't been run yet lists the
// entries (0 points); cancelled and waiting-list entries are already left out.
async function getEventEntries(eventId, eventclassId) {
// An id, or a pasted link like https://houseofrc.com/events/2253.
// One event with its racing classes and entries (/v1/timing/events/{id}/entries, needs the
// hosting or organizing club's key). The answer also holds the drivers' email, phone,
// emergency contact and age: those are dropped here and never reach the browser.
const NOT_RACING = new Set(["entrycancelled", "waitinglist"]);
async function getEventEntries(eventId) {
const text = String(eventId || "").trim();
const id = Number(text.match(/events\/(\d+)/)?.[1] || text.match(/^\d+$/)?.[0]);
if (!Number.isInteger(id) || id < 1) return { ok: false, status: 400, error: "invalid_event" };
const event = await get(`/v1/events/${id}`);
if (!event.ok) return event.status === 404 ? { ...event, error: "event_not_found" } : event;
const results = await get("/results", { event_id: id, eventclass_id: eventclassId || undefined });
if (!results.ok) return results;
const entries = (Array.isArray(results.data) ? results.data : []).map((row) => ({
houseOfRcId: row.user_id ?? null,
name: String(row.name || [row.firstname, row.lastname].filter(Boolean).join(" ")).trim(),
number: row.number ?? null,
club: String(row.club || "").trim(),
country: String(row.country || "").trim(),
car: [row.brand1 && !String(row.product || "").toLowerCase().startsWith(String(row.brand1).toLowerCase()) ? row.brand1 : "", row.product].filter(Boolean).join(" ").trim(),
brand: String(row.brand1 || "").trim(),
points: row.points ?? null,
})).filter((entry) => entry.name);
return { ok: true, status: 200, event: pickEvent(event.data?.data || {}), entries };
const result = await get(`/v1/timing/events/${id}/entries`);
if (!result.ok) {
if (result.status === 403) return { ...result, error: "not_your_event" };
if (result.status === 404) return { ...result, error: "event_not_found" };
return result;
}
const data = result.data?.data || {};
const users = new Map((data.users || []).map((user) => [user.id, user]));
const classes = (data.classes || []).map((item) => ({ id: item.id, name: String(item.name || "").trim(), order: item.order ?? null }));
const entries = (data.entries || []).filter((entry) => !NOT_RACING.has(entry.status)).map((entry) => {
const user = users.get(entry.user_id) || {};
const brand = String(entry.brands?.[0]?.name || "").trim();
const model = String(entry.car?.name || "").trim();
return {
entryId: entry.id,
classId: entry.class_id,
houseOfRcId: entry.user_id,
name: String(user.name || [user.firstname, user.lastname].filter(Boolean).join(" ")).trim(),
country: user.represents_country || user.country || "",
number: entry.driver_number ?? null,
transponders: (entry.transponders || []).map((value) => String(value).trim()).filter(Boolean),
club: String(entry.club?.name || "").trim(),
car: brand && !model.toLowerCase().startsWith(brand.toLowerCase()) ? `${brand} ${model}`.trim() : model || brand,
status: entry.status || "",
};
}).filter((entry) => entry.name);
return { ok: true, status: 200, event: pickEvent(data.event || { id }), classes, entries };
}
return { status, setKey, get, testConnection, lookupTransponders, listEvents, getEventEntries };