All files / src/services/api partnerAnalyticsApi.ts

100% Statements 26/26
87.5% Branches 7/8
100% Functions 3/3
100% Lines 25/25

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                                                                  7x       6x 6x 1x     6x       5x                   7x       7x 7x 1x     7x           6x 6x 6x 1x 1x 1x         6x 6x 6x 6x 6x 6x 6x 6x    
/**
 * Partner Analytics API Client
 * Story 8.1: Partner Attendance Dashboard — AC1, AC2, AC3, AC4
 *
 * Endpoints served by partner-coordination-service via API Gateway.
 * CRITICAL: Path is relative to baseURL (which already includes /api/v1).
 */
 
import apiClient from '@/services/api/apiClient';
 
// ─── Types ───────────────────────────────────────────────────────────────────
 
export interface AttendanceSummaryRecord {
  eventCode: string;
  eventTitle: string;
  eventDate: string; // ISO-8601 date-time
  totalAttendees: number;
  companyAttendees: number;
}
 
export interface PartnerDashboardData {
  attendanceSummary: AttendanceSummaryRecord[];
  costPerAttendee: number | null;
}
 
// ─── API functions ────────────────────────────────────────────────────────────
 
/**
 * Fetch the attendance dashboard for a partner company.
 *
 * @param companyName - Partner company name (ADR-003)
 * @param fromYear    - Earliest year to include (undefined → backend default: current year - 5)
 */
export const getAttendanceDashboard = async (
  companyName: string,
  fromYear?: number
): Promise<PartnerDashboardData> => {
  const params: Record<string, number> = {};
  if (fromYear !== undefined) {
    params.fromYear = fromYear;
  }
 
  const response = await apiClient.get<PartnerDashboardData>(
    `/partners/${companyName}/analytics/dashboard`,
    { params }
  );
  return response.data;
};
 
/**
 * Trigger Excel download for the attendance report.
 * Uses a temporary anchor element to initiate the browser download.
 *
 * @param companyName - Partner company name (ADR-003)
 * @param fromYear    - Earliest year to include
 */
export const exportAttendanceReport = async (
  companyName: string,
  fromYear?: number
): Promise<void> => {
  const params: Record<string, number> = {};
  if (fromYear !== undefined) {
    params.fromYear = fromYear;
  }
 
  const response = await apiClient.get(`/partners/${companyName}/analytics/export`, {
    params,
    responseType: 'blob',
  });
 
  // Extract filename from Content-Disposition header or use default
  const contentDisposition = response.headers['content-disposition'] as string | undefined;
  let filename = `attendance-${companyName}.xlsx`;
  if (contentDisposition) {
    const match = contentDisposition.match(/filename="?([^";\n]+)"?/);
    Eif (match) {
      filename = match[1];
    }
  }
 
  // Trigger browser download
  const url = URL.createObjectURL(new Blob([response.data as BlobPart]));
  const anchor = document.createElement('a');
  anchor.href = url;
  anchor.download = filename;
  document.body.appendChild(anchor);
  anchor.click();
  document.body.removeChild(anchor);
  setTimeout(() => URL.revokeObjectURL(url), 100);
};