All files / src/components/organizer/EventPage AddParticipantDialog.tsx

85.71% Statements 42/49
70.83% Branches 17/24
77.77% Functions 7/9
89.36% Lines 42/47

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                                                                                2x 2x     3x     2x           48x 48x   48x 48x 48x 48x   48x   48x 2x 2x 2x 2x 2x     48x           48x 4x 4x 4x 4x 4x   2x 2x 2x   2x 4x 4x 4x   2x   1x 1x           1x 1x                     2x       48x                                   3x 3x 3x                                                   4x                            
/**
 * AddParticipantDialog (Epic 14 follow-up — organizer "Add participant")
 *
 * Lets an organizer add an EXISTING BATbern user onto an event directly as a `confirmed`
 * participant — no self-registration / email-confirmation step. The user is chosen via the
 * shared `UserAutocomplete` (the same picker the speaker kanban uses to promote a READY
 * speaker); there is deliberately no "add by email / new person" path.
 *
 * Capacity: a full event returns 409 with `details.code = "capacity_exceeded"`; the dialog
 * then offers an explicit "add anyway" that re-submits with `force=true`.
 */
 
import React, { useState } from 'react';
import {
  Alert,
  Button,
  Checkbox,
  Dialog,
  DialogActions,
  DialogContent,
  DialogTitle,
  FormControlLabel,
  Stack,
} from '@mui/material';
import { useQueryClient } from '@tanstack/react-query';
import { useTranslation } from 'react-i18next';
import { UserAutocomplete } from '@/components/shared/UserAutocomplete';
import type { UserSearchResponse } from '@/types/user.types';
import { addParticipant } from '@/services/api/eventRegistrationService';
 
interface AddParticipantDialogProps {
  open: boolean;
  onClose: () => void;
  eventCode: string;
  /** Notifies the parent so it can show a success snackbar. */
  onAdded?: (attendeeName: string) => void;
}
 
/** Narrow an axios-style error to the backend ErrorResponse `details.code`, if any. */
function errorDetailCode(err: unknown): string | undefined {
  const data = (err as { response?: { data?: { details?: { code?: string } } } })?.response?.data;
  return data?.details?.code;
}
function errorStatus(err: unknown): number | undefined {
  return (err as { response?: { status?: number } })?.response?.status;
}
 
export const AddParticipantDialog: React.FC<AddParticipantDialogProps> = ({
  open,
  onClose,
  eventCode,
  onAdded,
}) => {
  const { t } = useTranslation('events');
  const queryClient = useQueryClient();
 
  const [selectedUser, setSelectedUser] = useState<UserSearchResponse | null>(null);
  const [notify, setNotify] = useState(true);
  const [submitting, setSubmitting] = useState(false);
  const [error, setError] = useState<string | null>(null);
  // Set when the event is full → the primary button becomes "Add anyway" (force).
  const [capacityFull, setCapacityFull] = useState(false);
 
  const reset = (): void => {
    setSelectedUser(null);
    setNotify(true);
    setSubmitting(false);
    setError(null);
    setCapacityFull(false);
  };
 
  const handleClose = (): void => {
    if (submitting) return;
    reset();
    onClose();
  };
 
  const submit = async (force: boolean): Promise<void> => {
    Iif (!selectedUser) return;
    setSubmitting(true);
    setError(null);
    try {
      await addParticipant(eventCode, { username: selectedUser.id, force, notify });
      // Mirror the promote-from-waitlist invalidations so the list + count badge refresh.
      queryClient.invalidateQueries({ queryKey: ['event-registrations', eventCode] });
      queryClient.invalidateQueries({ queryKey: ['events', eventCode] });
      queryClient.invalidateQueries({ queryKey: ['event', eventCode] });
      const name =
        `${selectedUser.firstName ?? ''} ${selectedUser.lastName ?? ''}`.trim() || selectedUser.id;
      reset();
      onClose();
      onAdded?.(name);
    } catch (err) {
      if (errorStatus(err) === 409 && errorDetailCode(err) === 'capacity_exceeded') {
        // Offer the explicit override (NFR1 — consequential action confirmed).
        setCapacityFull(true);
        setError(
          t(
            'eventPage.participantsTab.addParticipantFull',
            'This event is full. Add this participant over capacity anyway?'
          )
        );
      } else if (errorStatus(err) === 409) {
        setError(
          t(
            'eventPage.participantsTab.addParticipantDuplicate',
            'This user is already registered for this event.'
          )
        );
      } else E{
        setError(
          t('eventPage.participantsTab.addParticipantError', 'Could not add this participant.')
        );
      }
      setSubmitting(false);
    }
  };
 
  return (
    <Dialog open={open} onClose={handleClose} maxWidth="sm" fullWidth>
      <DialogTitle>
        {t('eventPage.participantsTab.addParticipantTitle', 'Add participant')}
      </DialogTitle>
      <DialogContent>
        <Stack spacing={2} sx={{ mt: 1 }} data-testid="add-participant-dialog">
          {error && (
            <Alert
              severity={capacityFull ? 'warning' : 'error'}
              data-testid="add-participant-error"
            >
              {error}
            </Alert>
          )}
          <UserAutocomplete
            value={selectedUser}
            onChange={(u) => {
              setSelectedUser(u);
              setCapacityFull(false);
              setError(null);
            }}
            label={t('eventPage.participantsTab.addParticipantUserLabel', 'User')}
            data-testid="add-participant-user"
          />
          <FormControlLabel
            control={
              <Checkbox
                checked={notify}
                onChange={(e) => setNotify(e.target.checked)}
                data-testid="add-participant-notify"
              />
            }
            label={t(
              'eventPage.participantsTab.addParticipantNotify',
              'Notify the participant by email'
            )}
          />
        </Stack>
      </DialogContent>
      <DialogActions>
        <Button onClick={handleClose} disabled={submitting}>
          {t('common:actions.cancel', 'Cancel')}
        </Button>
        <Button
          variant="contained"
          onClick={() => submit(capacityFull)}
          disabled={!selectedUser || submitting}
          data-testid="add-participant-confirm"
        >
          {capacityFull
            ? t('eventPage.participantsTab.addParticipantForce', 'Add anyway')
            : t('eventPage.participantsTab.addParticipant', 'Add participant')}
        </Button>
      </DialogActions>
    </Dialog>
  );
};
 
export default AddParticipantDialog;