# All files / src/utils mepFetcher.ts

100% Statements 13/13
100% Branches 0/0
100% Functions 1/1
100% Lines 13/13

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

    24x
    24x
    24x
    24x
    24x
    35x
    35x
    32x
    32x
    32x

    3x
    3x

    21x

     /**
     * Shared utility for fetching all current MEPs via paginated batches.
     *
     * Used by OSINT tools that need full MEP counts (e.g., sentimentTracker,
     * analyzeCoalitionDynamics) to avoid per-group API calls that each trigger
     * a full multi-page fetch.
     *
     * @module utils/mepFetcher
     */

    import { epClient } from '../clients/europeanParliamentClient.js';
    import { auditLogger, toErrorMessage } from './auditLogger.js';
    import type { MEP } from '../types/europeanParliament.js';

    /**
     * Result of a paginated MEP fetch. Callers should inspect `complete` to
     * determine whether the returned `meps` array represents a full dataset
     * or only a partial snapshot (due to a pagination error).
     */
    export interface FetchMEPsResult {
      /** MEPs collected so far (may be partial on error). */
      meps: MEP[];
      /** `true` when all pages were fetched successfully; `false` on error. */
      complete: boolean;
      /** The offset at which a failure occurred, if any. */
      failureOffset?: number;
    }

    /**
     * Fetch all current MEPs by paginating until no more pages remain.
     *
     * If a page request fails mid-pagination, the error is logged via
     * {@link auditLogger.logError} and the function returns the MEPs
     * collected so far as partial results. Callers should check
     * {@link FetchMEPsResult.complete} to know whether the data is complete.
     *
     * @returns A {@link FetchMEPsResult} containing the MEPs, a `complete` flag,
     *   and an optional `failureOffset` when the fetch was incomplete.
     */
    export async function fetchAllCurrentMEPs(): Promise<FetchMEPsResult> {
      const batchSize = 100;
      const allMeps: MEP[] = [];
      let offset = 0;
      let hasMore = true;
      while (hasMore) {
        try {
          const page = await epClient.getCurrentMEPs({ limit: batchSize, offset });
          allMeps.push(...page.data);
          hasMore = page.hasMore;
          offset += batchSize;
        } catch (error: unknown) {
          auditLogger.logError('fetchAllCurrentMEPs', { offset, batchSize }, `Pagination failed at offset ${String(offset)}: ${toErrorMessage(error)}`);
          return { meps: allMeps, complete: false, failureOffset: offset };
        }
      }
      return { meps: allMeps, complete: true };
    }