feat(houseofrc): preview and send results to the event's entries, heats or final placings (1.83.0)

This commit is contained in:
larssand committed 2026-11-08 20:44:23 +01:00
1 parent f4a62b92d2
commit ebdc6104e6
17 files changed
+295 -15

No files matched your search

+30 -6
View File
@@ -44,8 +44,9 @@ function createHouseOfRc({ dataDir, env = process.env, fetchImpl = globalThis.fe
return status();
}
// GET against the API with the club key. Returns { ok, status, data, error, retryAfter }.
async function get(apiPath, query = {}) {
// A call to the API with the club key (GET, or POST with a JSON body).
// Returns { ok, status, data, error, retryAfter }; a refused write (422) keeps its answer in data.
async function get(apiPath, query = {}, { method = "GET", body } = {}) {
const key = getKey();
if (!key) return { ok: false, status: 0, error: "not_configured" };
const url = new URL(baseUrl + apiPath);
@@ -54,14 +55,16 @@ function createHouseOfRc({ dataDir, env = process.env, fetchImpl = globalThis.fe
});
try {
const response = await fetchImpl(url, {
headers: { Authorization: `Bearer ${key}`, Accept: "application/json" },
method,
headers: { Authorization: `Bearer ${key}`, Accept: "application/json", ...(body ? { "Content-Type": "application/json" } : {}) },
body: body ? JSON.stringify(body) : undefined,
signal: AbortSignal.timeout(TIMEOUT_MS),
});
const data = await response.json().catch(() => null);
if (response.ok) return { ok: true, status: response.status, data };
const error = response.status === 401 || response.status === 403 ? "unauthorized"
: response.status === 429 ? "rate_limited" : "http_error";
return { ok: false, status: response.status, error, retryAfter: Number(response.headers.get("retry-after")) || 0 };
: response.status === 429 ? "rate_limited" : response.status === 422 ? "rejected" : "http_error";
return { ok: false, status: response.status, error, ...(response.status === 422 ? { data } : {}), retryAfter: Number(response.headers.get("retry-after")) || 0 };
} catch (error) {
return { ok: false, status: 0, error: error?.name === "TimeoutError" ? "timeout" : "unreachable" };
}
@@ -166,7 +169,28 @@ function createHouseOfRc({ dataDir, env = process.env, fetchImpl = globalThis.fe
return { ok: true, status: 200, event: pickEvent(data.event || { id }), classes, entries };
}
return { status, setKey, get, testConnection, lookupTransponders, listEvents, getEventEntries };
// Results back to the event's entries (all or nothing, see the API docs). entries:
// [{ id, ranking?, qualification?, tq?, points?, laps_time?, data? }]. Only the count and
// each entry's id and ranking come back to the browser.
const RESULT_FIELDS = ["ranking", "qualification", "tq", "points", "laps_time", "data"];
async function postResults(eventId, entries) {
const id = Number(eventId);
if (!Number.isInteger(id) || id < 1) return { ok: false, status: 400, error: "invalid_event" };
if (!Array.isArray(entries) || !entries.length || entries.length > 2000) return { ok: false, status: 400, error: "invalid_entries" };
const body = { entries: entries.map((entry) => Object.fromEntries([["id", Number(entry?.id)], ...RESULT_FIELDS.filter((field) => entry && field in entry).map((field) => [field, entry[field]])])) };
if (body.entries.some((entry) => !Number.isInteger(entry.id) || entry.id < 1)) return { ok: false, status: 400, error: "invalid_entries" };
const result = await get(`/v1/timing/events/${id}/entries`, {}, { method: "POST", body });
if (!result.ok) {
if (result.status === 403) return { ...result, error: "not_your_event" };
if (result.status === 404) return { ...result, error: "event_not_found" };
if (result.status === 422) return { ok: false, status: 422, error: "rejected", message: String(result.data?.message || ""), errors: result.data?.errors || {} };
return result;
}
const data = result.data?.data || {};
return { ok: true, status: 200, updated: Number(data.updated) || 0, entries: (data.entries || []).map((entry) => ({ id: entry.id, ranking: entry.ranking ?? null })) };
}
return { status, setKey, get, testConnection, lookupTransponders, listEvents, getEventEntries, postResults };
}
module.exports = { createHouseOfRc, DEFAULT_BASE_URL };