Add class-specific driver training profiles and metadata
This commit is contained in:
46
src/driver_profile_logic.js
Normal file
46
src/driver_profile_logic.js
Normal file
@@ -0,0 +1,46 @@
|
||||
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 : []).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;
|
||||
}
|
||||
Reference in New Issue
Block a user