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 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 | 295x 34x 19x 7x 28x 3x 3x 1x 3x 3x 3x 3x 4x 4x 4x 4x 3x 6x 3x 1x 4x 3x 10x 10x 1x 1x 2x 10x 2x 4x 10x 1x 1x 10x 10x 10x 10x 10x 10x 10x 10x 7x 3x 10x 10x 10x 10x 10x 10x 10x 8x 8x 8x 8x 8x 8x 7x 17x 8x 4x 2x 2x 2x | /**
* @fileoverview Voting sub-client for European Parliament API
*
* Handles voting records (roll-call votes) and plenary speeches.
*
* @module clients/ep/votingClient
*/
import { auditLogger, toErrorMessage } from '../../utils/auditLogger.js';
import type {
VotingRecord,
Speech,
PaginatedResponse,
} from '../../types/europeanParliament.js';
import { toSafeString as _toSafeString } from './jsonLdHelpers.js';
import {
transformVoteResult as _transformVoteResult,
transformSpeech as _transformSpeech,
} from './transformers.js';
import {
BaseEPClient,
APIError,
type EPClientConfig,
type EPSharedResources,
type JSONLDResponse,
} from './baseClient.js';
// ─── Voting Client ────────────────────────────────────────────────────────────
/**
* Sub-client for voting records and speeches EP API endpoints.
*
* @extends BaseEPClient
* @public
*/
export class VotingClient extends BaseEPClient {
constructor(config: EPClientConfig = {}, shared?: EPSharedResources) {
super(config, shared);
}
// ─── Transform helpers ────────────────────────────────────────────────────
private transformVoteResult(
apiData: Record<string, unknown>,
sessionId: string
): VotingRecord {
return _transformVoteResult(apiData, sessionId);
}
private transformSpeech(apiData: Record<string, unknown>): Speech {
return _transformSpeech(apiData);
}
// ─── Private helpers ──────────────────────────────────────────────────────
/**
* Fetches vote results for a specific sitting/session.
* @private
*/
private async fetchVoteResultsForSession(
sessionId: string,
apiParams: Record<string, unknown>
): Promise<VotingRecord[]> {
const response = await this.get<JSONLDResponse>(
`meetings/${sessionId}/vote-results`,
apiParams
);
return response.data.map((item) => this.transformVoteResult(item, sessionId));
}
/**
* Fetches vote results from recent meetings when no sessionId is given.
* @private
*/
private async fetchVoteResultsFromRecentMeetings(params: {
dateFrom?: string;
limit?: number;
}): Promise<VotingRecord[]> {
const meetingsParams: Record<string, unknown> = { limit: 5 };
if (params.dateFrom !== undefined && params.dateFrom !== '') {
meetingsParams['year'] = params.dateFrom.substring(0, 4);
}
const meetingsResponse = await this.get<JSONLDResponse>('meetings', meetingsParams);
const records: VotingRecord[] = [];
const recordLimit = params.limit ?? 50;
for (const meeting of meetingsResponse.data) {
const meetingId =
_toSafeString(meeting['activity_id']) ||
_toSafeString(meeting['id']) ||
'';
Iif (meetingId === '') continue;
try {
const voteResponse = await this.get<JSONLDResponse>(
`meetings/${meetingId}/vote-results`,
{ limit: recordLimit }
);
const transformed = voteResponse.data.map((item) =>
this.transformVoteResult(item, meetingId)
);
records.push(...transformed);
} catch (error: unknown) {
// Some meetings may not have vote results – continue with degraded result
auditLogger.logError('get_voting_records', { meetingId }, toErrorMessage(error));
}
Iif (records.length >= recordLimit) break;
}
return records;
}
/**
* Applies client-side filters to voting records.
* @private
*/
private filterVotingRecords(
records: VotingRecord[],
params: { topic?: string; dateFrom?: string; dateTo?: string }
): VotingRecord[] {
let filtered = records;
if (params.topic !== undefined && params.topic !== '') {
const topicLower = params.topic.toLowerCase();
filtered = filtered.filter((r) =>
r.topic.toLowerCase().includes(topicLower)
);
}
if (params.dateFrom !== undefined && params.dateFrom !== '') {
const fromDate = params.dateFrom;
filtered = filtered.filter((r) => r.date >= fromDate);
}
if (params.dateTo !== undefined && params.dateTo !== '') {
const toDate = params.dateTo;
filtered = filtered.filter((r) => r.date <= toDate);
}
return filtered;
}
// ─── Public methods ───────────────────────────────────────────────────────
/**
* Retrieves voting records with filtering by session, topic, and date.
*
* **EP API Endpoint:** `GET /meetings/{sitting-id}/vote-results`
*
* @param params - sessionId, topic, dateFrom, dateTo, limit, offset
* @returns Paginated voting records list
* @security Audit logged per GDPR Article 30
*/
async getVotingRecords(params: {
sessionId?: string;
topic?: string;
dateFrom?: string;
dateTo?: string;
limit?: number;
offset?: number;
}): Promise<PaginatedResponse<VotingRecord>> {
const action = 'get_voting_records';
try {
const apiParams: Record<string, unknown> = {};
if (params.limit !== undefined) apiParams['limit'] = params.limit;
if (params.offset !== undefined) apiParams['offset'] = params.offset;
const effectiveSessionId = params.sessionId ?? '';
let records: VotingRecord[];
if (effectiveSessionId !== '') {
records = await this.fetchVoteResultsForSession(effectiveSessionId, apiParams);
} else {
records = await this.fetchVoteResultsFromRecentMeetings(params);
}
// Apply client-side filters
records = this.filterVotingRecords(records, params);
// Apply pagination
const offset = params.offset ?? 0;
const limit = params.limit ?? 50;
const paginatedRecords = records.slice(offset, offset + limit);
const result: PaginatedResponse<VotingRecord> = {
data: paginatedRecords,
total: records.length,
limit,
offset,
hasMore: offset + paginatedRecords.length < records.length,
};
auditLogger.logDataAccess(action, params, result.data.length);
return result;
} catch (error) {
auditLogger.logError(
action,
params,
error instanceof Error ? error.message : 'Unknown error'
);
throw error;
}
}
/**
* Returns plenary speeches.
* **EP API Endpoint:** `GET /speeches`
*/
async getSpeeches(params: {
dateFrom?: string;
dateTo?: string;
limit?: number;
offset?: number;
} = {}): Promise<PaginatedResponse<Speech>> {
const limit = params.limit ?? 50;
const offset = params.offset ?? 0;
const apiParams: Record<string, unknown> = {
format: 'application/ld+json',
offset,
limit,
};
if (params.dateFrom !== undefined) apiParams['date-from'] = params.dateFrom;
if (params.dateTo !== undefined) apiParams['date-to'] = params.dateTo;
const response = await this.get<JSONLDResponse>('speeches', apiParams);
const items = Array.isArray(response.data) ? response.data : [];
const speeches = items.map((item) => this.transformSpeech(item));
return { data: speeches, total: speeches.length + offset, limit, offset, hasMore: speeches.length === limit };
}
/**
* Returns a single speech by ID.
* **EP API Endpoint:** `GET /speeches/{speech-id}`
*/
async getSpeechById(speechId: string): Promise<Speech> {
if (speechId.trim() === '') {
throw new APIError('Speech ID is required', 400);
}
const response = await this.get<Record<string, unknown>>(
`speeches/${speechId}`,
{ format: 'application/ld+json' }
);
return this.transformSpeech(response);
}
}
|