77 lines
3.5 KiB
JavaScript
77 lines
3.5 KiB
JavaScript
function normalizeProfileName(value) {
|
|
return String(value || "").trim().toLocaleLowerCase().replace(/\s+/g, " ");
|
|
}
|
|
|
|
export function getDriverProfileId(driver) {
|
|
const explicitId = String(driver?.profileId || "").trim();
|
|
if (explicitId) return explicitId;
|
|
const legacyName = normalizeProfileName(driver?.name);
|
|
return legacyName ? `legacy:${legacyName}` : `driver:${String(driver?.id || "unknown")}`;
|
|
}
|
|
|
|
export function getDriverProfileGroups(state) {
|
|
const groups = new Map();
|
|
(Array.isArray(state?.drivers) ? state.drivers : []).filter((driver) => !driver.guestEventId).forEach((driver) => {
|
|
const profileId = getDriverProfileId(driver);
|
|
const group = groups.get(profileId) || { id: profileId, drivers: [] };
|
|
group.drivers.push(driver);
|
|
groups.set(profileId, group);
|
|
});
|
|
|
|
return [...groups.values()].map((group) => ({
|
|
...group,
|
|
representative: group.drivers.find((driver) => String(driver.id || "") === group.id) || group.drivers[0] || null,
|
|
club: String(group.drivers.find((driver) => String(driver.club || "").trim())?.club || "").trim(),
|
|
country: String(group.drivers.find((driver) => String(driver.country || "").trim())?.country || "").trim(),
|
|
driverIds: group.drivers.map((driver) => String(driver.id || "")).filter(Boolean),
|
|
classIds: [...new Set(group.drivers.map((driver) => String(driver.classId || "")).filter(Boolean))],
|
|
}));
|
|
}
|
|
|
|
export function getDriverProfile(state, reference) {
|
|
const normalizedReference = String(reference || "");
|
|
return getDriverProfileGroups(state).find((group) => (
|
|
group.id === normalizedReference
|
|
|| group.driverIds.includes(normalizedReference)
|
|
)) || null;
|
|
}
|
|
|
|
export function getDriverProfileReferences(state, reference) {
|
|
const profile = getDriverProfile(state, reference);
|
|
const references = new Set([String(reference || "")].filter(Boolean));
|
|
if (!profile) return references;
|
|
references.add(profile.id);
|
|
profile.driverIds.forEach((driverId) => references.add(driverId));
|
|
return references;
|
|
}
|
|
|
|
// A transponder is one physical chip, so the same value legitimately repeats across a
|
|
// single person's own multi-class driver entries (same profile) - that is not a
|
|
// conflict. It only means something went wrong when it also shows up on a driver
|
|
// belonging to a DIFFERENT profile, since two different people can't share one chip:
|
|
// most often a typo at registration that quietly grabbed someone else's transponder.
|
|
export function findTransponderConflict(drivers, { transponder, profileId, excludeDriverId } = {}) {
|
|
const value = String(transponder || "").trim();
|
|
if (!value) return null;
|
|
const list = Array.isArray(drivers) ? drivers : [];
|
|
return list.find((driver) => (
|
|
String(driver.id || "") !== String(excludeDriverId || "")
|
|
&& String(driver.transponder || "").trim() === value
|
|
&& getDriverProfileId(driver) !== String(profileId || "")
|
|
)) || null;
|
|
}
|
|
|
|
// The same transponder twice in one class (one person's two cars entered as two drivers): the
|
|
// laps can't be told apart - the decoder only sees the chip - so that is never allowed. Drive
|
|
// both cars as one entry, or merge the two entries.
|
|
export function findSameClassTransponder(drivers, { transponder, classId, excludeDriverId } = {}) {
|
|
const value = String(transponder || "").trim();
|
|
if (!value || !classId) return null;
|
|
return (Array.isArray(drivers) ? drivers : []).find((driver) => (
|
|
String(driver.id || "") !== String(excludeDriverId || "")
|
|
&& !driver.guestEventId
|
|
&& String(driver.classId || "") === String(classId)
|
|
&& String(driver.transponder || "").trim() === value
|
|
)) || null;
|
|
}
|