Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 | 8x 8x 48x 8x 8x 8x 8x 8x 6x 3x 3x 3x 6x 6x 6x 4x 4x 4x 4x 3x 6x 4x 4x 4x 4x 4x 4x 4x 4x 4x 3x 9x 6x 3x 3x 3x 6x 6x 6x 4x 3x 3x 3x 3x 4x 3x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 1x 1x 1x | import type { z } from 'zod';
import { CommitteeSchema } from '../schemas/europeanParliament.js';
import type {
WeeklyCorporateBodiesCache,
WeeklyMEPCache,
} from './weeklyDataCache.js';
type Committee = z.infer<typeof CommitteeSchema>;
type CommitteeMembership = NonNullable<Committee['memberships']>[number];
type CachedMEPDetails = WeeklyMEPCache['mepDetails'][string];
interface CachedCommitteeRoster {
memberships: Map<string, CommitteeMembership>;
members: Set<string>;
chairs: Set<string>;
viceChairs: Set<string>;
}
const FULL_MEMBER_ROLES = new Set(['MEMBER', 'CHAIR', 'CHAIR_VICE', 'VICE_CHAIR']);
const VICE_CHAIR_ROLES = new Set(['CHAIR_VICE', 'VICE_CHAIR']);
function referenceCode(value: string | undefined): string {
return value?.split('/').filter(Boolean).pop()?.toUpperCase() ?? '';
}
function isCommitteeClassification(value: string | undefined): boolean {
return referenceCode(value).startsWith('COMMITTEE_PARLIAMENTARY_');
}
function isCurrentMembership(membership: CommitteeMembership): boolean {
const today = new Date().toISOString().slice(0, 10);
const startDate = membership.memberDuring?.startDate;
const endDate = membership.memberDuring?.endDate;
return (startDate === undefined || startDate <= today)
&& (endDate === undefined || endDate >= today);
}
function currentCommitteeOrganizationIds(cache: WeeklyMEPCache): Set<string> {
const currentMEPIds = new Set(cache.meps.filter((mep) => mep.active).map((mep) => mep.id));
const organizations = new Set<string>();
const visitedMEPs = new Set<string>();
for (const detail of Object.values(cache.mepDetails)) {
Iif (!currentMEPIds.has(detail.id) || visitedMEPs.has(detail.id)) continue;
visitedMEPs.add(detail.id);
for (const membership of detail.hasMembership ?? []) {
Iif (!isCommitteeClassification(membership.membershipClassification)) continue;
Iif (!isCurrentMembership({ ...membership, member: detail.id })) continue;
const organizationId = referenceCode(membership.organization);
Eif (organizationId !== '') organizations.add(organizationId);
}
}
return organizations;
}
function matchingCommitteeMemberships(
detail: CachedMEPDetails,
organizationIds: ReadonlySet<string>,
): CommitteeMembership[] {
return (detail.hasMembership ?? [])
.filter((membership) => isCommitteeClassification(membership.membershipClassification))
.filter((membership) => organizationIds.has(referenceCode(membership.organization)))
.map((membership) => ({ ...membership, member: detail.id }))
.filter(isCurrentMembership);
}
function addMembershipToRoster(
roster: CachedCommitteeRoster,
membership: CommitteeMembership,
): void {
const role = referenceCode(membership.role);
const membershipKey = membership.id ?? membership.identifier
?? `${membership.member}|${membership.organization ?? ''}|${membership.role ?? ''}`;
roster.memberships.set(membershipKey, membership);
if (FULL_MEMBER_ROLES.has(role)) roster.members.add(membership.member);
if (role === 'CHAIR') roster.chairs.add(membership.member);
if (VICE_CHAIR_ROLES.has(role)) roster.viceChairs.add(membership.member);
}
function buildCachedCommitteeRoster(
committee: Committee,
cache: WeeklyMEPCache,
organizationAliases: readonly string[] = [],
): CachedCommitteeRoster {
const organizationIds = new Set(
[committee.id, committee.abbreviation, ...organizationAliases]
.map((value) => referenceCode(value)),
);
const currentMEPIds = new Set(cache.meps.filter((mep) => mep.active).map((mep) => mep.id));
const roster: CachedCommitteeRoster = {
memberships: new Map(),
members: new Set(),
chairs: new Set(),
viceChairs: new Set(),
};
const visitedMEPs = new Set<string>();
for (const detail of Object.values(cache.mepDetails)) {
Iif (!currentMEPIds.has(detail.id) || visitedMEPs.has(detail.id)) continue;
visitedMEPs.add(detail.id);
for (const membership of matchingCommitteeMemberships(detail, organizationIds)) {
addMembershipToRoster(roster, membership);
}
}
return roster;
}
export function enrichCommitteeFromMEPCache(
committee: Committee,
cache: WeeklyMEPCache,
organizationAliases: readonly string[] = [],
): Committee {
const roster = buildCachedCommitteeRoster(committee, cache, organizationAliases);
const sortedMembers = [...roster.members].sort();
const sortedMemberships = [...roster.memberships.values()].sort((left, right) =>
`${left.member}|${left.id ?? left.identifier ?? ''}`
.localeCompare(`${right.member}|${right.id ?? right.identifier ?? ''}`),
);
return {
...committee,
members: sortedMembers.length > 0 ? sortedMembers : committee.members,
memberships: sortedMemberships.length > 0 ? sortedMemberships : committee.memberships,
chair: [...roster.chairs].sort()[0] ?? committee.chair,
viceChairs: roster.viceChairs.size > 0
? [...roster.viceChairs].sort()
: (committee.viceChairs ?? []),
};
}
export function findCachedCommittee(
lookup: string,
cachedBodies: WeeklyCorporateBodiesCache,
cachedMEPs: WeeklyMEPCache | null,
): Committee | undefined {
const normalizedLookup = referenceCode(lookup);
const activeCommitteeIds = cachedMEPs === null
? new Set<string>()
: currentCommitteeOrganizationIds(cachedMEPs);
const matchingBodies = cachedBodies.corporateBodies.filter((body) =>
referenceCode(body.id) === normalizedLookup || referenceCode(body.abbreviation) === normalizedLookup,
);
const currentBody = matchingBodies.find((body) => activeCommitteeIds.has(referenceCode(body.id)));
const selectedBody = currentBody ?? matchingBodies[0];
Eif (selectedBody !== undefined) {
const detail = cachedBodies.corporateBodyDetails?.[selectedBody.id];
const committee = CommitteeSchema.parse(detail ?? selectedBody);
return cachedMEPs === null
? committee
: enrichCommitteeFromMEPCache(committee, cachedMEPs, [selectedBody.id]);
}
const aliasDetail = cachedBodies.corporateBodyDetails?.[lookup];
Iif (aliasDetail === undefined) return undefined;
const committee = CommitteeSchema.parse(aliasDetail);
return cachedMEPs === null ? committee : enrichCommitteeFromMEPCache(committee, cachedMEPs, [lookup]);
}
export function listCachedCurrentCorporateBodies(
cachedBodies: WeeklyCorporateBodiesCache,
cachedMEPs: WeeklyMEPCache | null,
): Committee[] {
return cachedBodies.corporateBodies
.map((body) => {
const committee = CommitteeSchema.parse(cachedBodies.corporateBodyDetails?.[body.id] ?? body);
return cachedMEPs === null
|| committee.responsibilities?.some(isCommitteeClassification) !== true
? committee
: enrichCommitteeFromMEPCache(committee, cachedMEPs, [body.id]);
})
.sort((left, right) => left.id.localeCompare(right.id));
}
|