2026-05-08 10:45:58 +02:00
|
|
|
import { ACTIVE_CLUB_STORAGE_KEY } from './api'
|
|
|
|
|
|
2026-05-09 10:42:56 +02:00
|
|
|
/** Nur Mitgliedschaften mit aktivem Vereinszugang (Backend: club_members.status). */
|
|
|
|
|
export function activeClubMemberships(clubs) {
|
|
|
|
|
return (clubs || []).filter(
|
|
|
|
|
(c) => (c.membership_status || 'active').toString().trim().toLowerCase() === 'active'
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-08 10:45:58 +02:00
|
|
|
/**
|
|
|
|
|
* Einheitliche Anzeige des aktiven Vereins: Abgleich mit effective_club_id, active_club_id,
|
2026-05-09 10:42:56 +02:00
|
|
|
* LocalStorage (Request-Header-Quelle), sonst erster **aktiver** Verein der Liste.
|
2026-05-08 10:45:58 +02:00
|
|
|
*/
|
|
|
|
|
export function getResolvedActiveClubIdForUi(user) {
|
2026-05-09 10:42:56 +02:00
|
|
|
const clubs = activeClubMemberships(user?.clubs)
|
2026-05-08 10:45:58 +02:00
|
|
|
if (!clubs.length) return null
|
|
|
|
|
|
|
|
|
|
const idInClubs = (id) =>
|
|
|
|
|
id != null &&
|
|
|
|
|
id !== '' &&
|
|
|
|
|
clubs.some((c) => Number(c.id) === Number(id))
|
|
|
|
|
|
|
|
|
|
const eff = user?.effective_club_id
|
|
|
|
|
if (idInClubs(eff)) return Number(eff)
|
|
|
|
|
|
|
|
|
|
const ac = user?.active_club_id
|
|
|
|
|
if (idInClubs(ac)) return Number(ac)
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const ls = localStorage.getItem(ACTIVE_CLUB_STORAGE_KEY)
|
|
|
|
|
if (ls && /^\d+$/.test(ls.trim()) && clubs.some((c) => String(c.id) === ls.trim())) {
|
|
|
|
|
return Number(ls.trim())
|
|
|
|
|
}
|
|
|
|
|
} catch {
|
|
|
|
|
/* ignore */
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Number(clubs[0].id)
|
|
|
|
|
}
|