All files / src/hooks useSessionSpeakers.ts

100% Statements 10/10
100% Branches 0/0
100% Functions 6/6
100% Lines 10/10

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                                                          9x   9x                 5x   3x 3x                         7x   7x                 4x   3x 3x        
/**
 * Session Speaker React Query Hooks
 *
 * Custom hooks for assigning and removing speakers from sessions.
 * Speakers are stored in the session_users table (entity SessionUser, exposed in the
 * API as SessionSpeaker). speaker_pool owns the workflow state machine only; from the
 * CONTACTED → READY transition onward, session_users is the canonical record for
 * speaker meta (confirmation, role, presentation title).
 *
 * On success, both mutations invalidate two query keys:
 * - ['event', eventCode] — so session.speakers[] is refreshed in the parent session list
 * - ['speakerPool', 'list', eventCode] — so the organizer drawer / kanban refetches
 *   the pool list. The server-side response derives the speaker's identity from the
 *   session-overlay (Phase A); without this invalidation the FE keeps showing the
 *   pre-reassign identity even though the server has the new value.
 */
 
import { useMutation, useQueryClient } from '@tanstack/react-query';
import { sessionApiClient, type AssignSpeakerRequest } from '@/services/api/sessionApiClient';
import { speakerPoolKeys } from '@/hooks/useSpeakerPool';
 
/**
 * Hook to assign a speaker to a session (ORGANIZER only)
 *
 * @example
 * const assign = useAssignSpeaker();
 * assign.mutate({ eventCode: 'BATbern142', sessionSlug: 'cloud-talk', request: { username: 'john.doe', speakerRole: 'PRIMARY_SPEAKER' } });
 */
export function useAssignSpeaker() {
  const queryClient = useQueryClient();
 
  return useMutation({
    mutationFn: ({
      eventCode,
      sessionSlug,
      request,
    }: {
      eventCode: string;
      sessionSlug: string;
      request: AssignSpeakerRequest;
    }) => sessionApiClient.assignSpeaker(eventCode, sessionSlug, request),
    onSuccess: (_data, variables) => {
      queryClient.invalidateQueries({ queryKey: ['event', variables.eventCode] });
      queryClient.invalidateQueries({ queryKey: speakerPoolKeys.list(variables.eventCode) });
    },
  });
}
 
/**
 * Hook to remove a speaker from a session (ORGANIZER only)
 *
 * @example
 * const remove = useRemoveSpeaker();
 * remove.mutate({ eventCode: 'BATbern142', sessionSlug: 'cloud-talk', username: 'john.doe' });
 */
export function useRemoveSpeaker() {
  const queryClient = useQueryClient();
 
  return useMutation({
    mutationFn: ({
      eventCode,
      sessionSlug,
      username,
    }: {
      eventCode: string;
      sessionSlug: string;
      username: string;
    }) => sessionApiClient.removeSpeaker(eventCode, sessionSlug, username),
    onSuccess: (_data, variables) => {
      queryClient.invalidateQueries({ queryKey: ['event', variables.eventCode] });
      queryClient.invalidateQueries({ queryKey: speakerPoolKeys.list(variables.eventCode) });
    },
  });
}