All files / src/services/api sessionApiClient.ts

88% Statements 44/50
69.56% Branches 32/46
87.5% Functions 7/8
88% Lines 44/50

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 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280                          5x                                                                                             6x 6x       1x     5x       2x   3x                                       2x 2x       1x     1x       1x                                           2x 2x     1x   1x                                                                           3x 3x   2x       1x   1x                                     3x 3x       1x   2x       1x   1x                           3x 3x       2x       1x   1x               7x     6x           6x 4x 1x   3x 3x         2x 6x       6x     1x         5x  
/**
 * Session API Client (Story BAT-17)
 *
 * HTTP client for Session management APIs in Event Management Service
 * Features:
 * - Update session details (title, description, duration)
 */
 
import apiClient from '@/services/api/apiClient';
import { AxiosError } from 'axios';
import type { Session, SessionMaterial, SessionSpeaker } from '@/types/event.types';
 
// API base path for session endpoints
const SESSION_API_PATH = '/events';
 
export interface SessionUpdateRequest {
  title?: string;
  description?: string;
  durationMinutes?: number;
}
 
export interface AssignSpeakerRequest {
  username: string;
  speakerRole: 'PRIMARY_SPEAKER' | 'CO_SPEAKER' | 'MODERATOR' | 'PANELIST';
}
 
export interface MaterialUploadItem {
  uploadId: string;
  materialType: string; // PRESENTATION, DOCUMENT, VIDEO, ARCHIVE, OTHER
  fileName: string;
  fileExtension: string;
  fileSize: number;
  mimeType: string;
}
 
export interface SessionMaterialAssociationRequest {
  materials: MaterialUploadItem[];
}
 
/**
 * Session API Client Class
 *
 * Handles all HTTP requests to the Session APIs
 */
class SessionApiClient {
  /**
   * Update session details (title, description, duration)
   *
   * PATCH /api/v1/events/{eventCode}/sessions/{sessionSlug}
   *
   * @param eventCode - Event code (e.g., "BATbern142")
   * @param sessionSlug - Session slug identifier
   * @param updates - Session update request
   * @returns Updated session
   */
  async updateSession(
    eventCode: string,
    sessionSlug: string,
    updates: SessionUpdateRequest
  ): Promise<Session> {
    try {
      const response = await apiClient.patch<Session>(
        `${SESSION_API_PATH}/${eventCode}/sessions/${sessionSlug}`,
        updates
      );
      return response.data;
    } catch (error) {
      // Re-throw AxiosError to preserve 401/403 auth errors
      if (
        error instanceof AxiosError &&
        (error.response?.status === 401 || error.response?.status === 403)
      ) {
        throw error;
      }
      throw this.transformError(error);
    }
  }
 
  /**
   * Associate uploaded materials with a session
   * Story 5.9: Session Materials Upload (AC5)
   *
   * POST /api/v1/events/{eventCode}/sessions/{sessionSlug}/materials
   *
   * @param eventCode - Event code (e.g., "BATbern142")
   * @param sessionSlug - Session slug identifier
   * @param materials - Material association request with uploadIds and materialTypes
   * @returns Created materials list
   */
  async associateMaterials(
    eventCode: string,
    sessionSlug: string,
    materials: SessionMaterialAssociationRequest
  ): Promise<SessionMaterial[]> {
    try {
      const response = await apiClient.post(
        `${SESSION_API_PATH}/${eventCode}/sessions/${sessionSlug}/materials`,
        materials
      );
      return response.data;
    } catch (error) {
      // Re-throw AxiosError to preserve auth errors
      Eif (
        error instanceof AxiosError &&
        (error.response?.status === 401 || error.response?.status === 403)
      ) {
        throw error;
      }
      throw this.transformError(error);
    }
  }
 
  /**
   * Get presigned download URL for a material
   * Story 5.9: Session Materials Upload (AC5)
   *
   * GET /api/v1/events/{eventCode}/sessions/{sessionSlug}/materials/{materialId}/download
   *
   * @param eventCode - Event code (e.g., "BATbern142")
   * @param sessionSlug - Session slug identifier
   * @param materialId - Material UUID
   * @returns Object with downloadUrl (presigned URL valid for 1 hour)
   */
  async getMaterialDownloadUrl(
    eventCode: string,
    sessionSlug: string,
    materialId: string
  ): Promise<{ downloadUrl: string }> {
    try {
      const response = await apiClient.get(
        `${SESSION_API_PATH}/${eventCode}/sessions/${sessionSlug}/materials/${materialId}/download`
      );
      return response.data;
    } catch (error) {
      throw this.transformError(error);
    }
  }
 
  /**
   * Delete a material from a session
   *
   * DELETE /api/v1/events/{eventCode}/sessions/{sessionSlug}/materials/{materialId}
   *
   * @param eventCode - Event code (e.g., "BATbern142")
   * @param sessionSlug - Session slug identifier
   * @param materialId - Material UUID
   */
  async deleteMaterial(eventCode: string, sessionSlug: string, materialId: string): Promise<void> {
    try {
      await apiClient.delete(
        `${SESSION_API_PATH}/${eventCode}/sessions/${sessionSlug}/materials/${materialId}`
      );
    } catch (error) {
      if (
        error instanceof AxiosError &&
        (error.response?.status === 401 || error.response?.status === 403)
      ) {
        throw error;
      }
      throw this.transformError(error);
    }
  }
 
  /**
   * Delete a session
   *
   * DELETE /api/v1/events/{eventCode}/sessions/{sessionSlug}
   *
   * @param eventCode - Event code (e.g., "BATbern142")
   * @param sessionSlug - Session slug identifier
   */
  async deleteSession(eventCode: string, sessionSlug: string): Promise<void> {
    try {
      await apiClient.delete(`${SESSION_API_PATH}/${eventCode}/sessions/${sessionSlug}`);
    } catch (error) {
      if (
        error instanceof AxiosError &&
        (error.response?.status === 401 || error.response?.status === 403)
      ) {
        throw error;
      }
      throw this.transformError(error);
    }
  }
 
  /**
   * Assign a speaker to a session
   *
   * POST /api/v1/events/{eventCode}/sessions/{sessionSlug}/speakers
   *
   * @param eventCode - Event code (e.g., "BATbern142")
   * @param sessionSlug - Session slug identifier
   * @param request - Speaker assignment request (username + speakerRole)
   * @returns Created session speaker
   */
  async assignSpeaker(
    eventCode: string,
    sessionSlug: string,
    request: AssignSpeakerRequest
  ): Promise<SessionSpeaker> {
    try {
      const response = await apiClient.post<SessionSpeaker>(
        `${SESSION_API_PATH}/${eventCode}/sessions/${sessionSlug}/speakers`,
        request
      );
      return response.data;
    } catch (error) {
      if (
        error instanceof AxiosError &&
        (error.response?.status === 401 || error.response?.status === 403)
      ) {
        throw error;
      }
      throw this.transformError(error);
    }
  }
 
  /**
   * Remove a speaker from a session
   *
   * DELETE /api/v1/events/{eventCode}/sessions/{sessionSlug}/speakers/{username}
   *
   * @param eventCode - Event code (e.g., "BATbern142")
   * @param sessionSlug - Session slug identifier
   * @param username - Speaker username to remove
   */
  async removeSpeaker(eventCode: string, sessionSlug: string, username: string): Promise<void> {
    try {
      await apiClient.delete(
        `${SESSION_API_PATH}/${eventCode}/sessions/${sessionSlug}/speakers/${username}`
      );
    } catch (error) {
      if (
        error instanceof AxiosError &&
        (error.response?.status === 401 || error.response?.status === 403)
      ) {
        throw error;
      }
      throw this.transformError(error);
    }
  }
 
  /**
   * Transform Axios errors into user-friendly error messages
   */
  private transformError(error: unknown): Error {
    if (error instanceof AxiosError) {
      // Extract error message from response
      const message =
        error.response?.data?.message ||
        error.response?.data?.error ||
        error.message ||
        'An unexpected error occurred';
 
      // Handle specific error cases
      if (error.response?.status === 404) {
        if (message.includes('Event')) {
          return new Error('Event not found');
        }
        Eif (message.includes('Session')) {
          return new Error('Session not found');
        }
      }
 
      // Include correlation ID if available
      const correlationId = error.response?.headers?.['x-correlation-id'];
      const errorMessage = correlationId
        ? `${message} (Correlation ID: ${correlationId})`
        : message;
 
      return new Error(errorMessage);
    }
 
    return error instanceof Error ? error : new Error('An unexpected error occurred');
  }
}
 
// Export singleton instance
export const sessionApiClient = new SessionApiClient();