All files / src/components/admin AdminInviteRequests.vue

89.05% Statements 366/411
58.82% Branches 30/51
73.33% Functions 11/15
89.05% Lines 366/411

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 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 4911x 1x 1x 1x 1x 1x 1x 1x 1x 12x 1x 1x 1x 1x 1x 1x 1x 1x 1x   12x 1x 1x 1x 1x             1x 1x 1x 1x 1x 1x 1x 1x   1x 1x 1x 1x   1x 1x 1x 1x 1x 1x 1x   1x 1x 1x 1x   1x 1x 1x 1x 1x   1x 1x   1x 1x 1x 1x 1x   1x       1x 1x   1x 1x 1x 1x 1x 1x 1x 1x   1x 1x 1x 1x   1x 1x 1x 1x   1x 1x 1x 1x   1x 1x 1x 1x   1x 1x 1x 1x   1x 1x 1x 1x 1x 1x 1x 1x   1x 1x 1x 1x 1x 1x 1x 1x 1x 1x   1x 1x 1x 1x 1x 1x 1x 1x   1x 1x 1x 1x 1x             1x   1x 1x 1x 1x 1x 1x   1x 1x 1x 1x   3x 1x           1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x   1x 1x 1x 1x 1x 1x   1x 1x 1x 1x 1x 1x   1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x     1x       1x 1x 1x 3x 3x 3x 3x 3x   3x 1x 1x 1x 1x 1x 1x 1x   1x 1x 20x 20x 20x 20x 20x 1x       1x 1x 1x 1x   1x 1x 1x 1x   1x   1x   1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x   1x 1x 1x 2x 2x 2x 2x 2x 1x 2x   2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x       1x 2x     2x 1x 1x 2x   1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x       1x 1x     1x 1x 1x 1x 1x   1x 1x 1x                                             1x 1x 14x 14x   14x 14x 14x 14x 14x 14x 14x   14x   14x       14x 14x     14x 14x 14x 14x   1x 1x 14x 14x 14x 14x 14x 14x   14x 14x 14x 14x     14x   1x 1x 1x 1x 1x 1x 1x 1x 1x   1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x       1x   1x 1x 1x 1x   1x 1x 1x 1x 1x   1x 1x 1x 1x 1x 1x 1x 1x   1x       1x 1x 1x       1x   1x 1x 19x 19x 19x 19x 19x 19x 19x   1x 1x 12x 1x    
<template>
  <div>
    <div class="flex justify-between items-center mb-6">
      <h2 class="text-xl font-bold text-fg">Invite Requests</h2>
      <div class="flex items-center gap-4">
        <!-- Stats -->
        <div v-if="stats" class="text-sm text-fg-muted">
          <span class="font-medium">{{ stats.pending }}</span> pending /
          <span class="font-medium">{{ stats.total }}</span> total
        </div>
        <!-- Filters -->
        <label class="flex items-center gap-2 text-sm text-fg">
          <input
            v-model="iosBetaOnly"
            type="checkbox"
            data-testid="ios-beta-filter"
            class="h-4 w-4 rounded border-line"
            @change="fetchRequests"
          />
          iPhone beta only
        </label>
        <select
          v-model="statusFilter"
          @change="fetchRequests"
          class="px-3 py-2 bg-card border border-line rounded-md text-sm text-fg focus:outline-none focus:ring-2 focus:ring-brand-500"
        >
          <option value="">All Requests</option>
          <option value="pending">Pending</option>
          <option value="approved">Approved</option>
          <option value="rejected">Rejected</option>
          <option value="existing_user">Already a user</option>
        </select>
        <!-- Preview the email Reject sends (SB-1407) -->
        <button
          type="button"
          data-testid="test-rejection-email"
          class="px-3 py-2 text-sm rounded-md border border-line text-fg hover:bg-surface-alt"
          :disabled="sendingTestEmail"
          @click="sendTestRejectionEmail"
        >
          {{ sendingTestEmail ? 'Sending…' : 'Send test rejection email' }}
        </button>
      </div>
    </div>
 
    <!-- Loading State -->
    <div v-if="loading" class="text-center py-8">
      <div
        class="inline-block animate-spin rounded-full h-8 w-8 border-b-2 border-brand-600"
      ></div>
      <p class="mt-2 text-fg-muted">Loading requests...</p>
    </div>
 
    <!-- Error State -->
    <div
      v-else-if="error"
      class="bg-red-50 dark:bg-red-500/10 border border-red-200 dark:border-red-900 rounded-md p-4"
    >
      <p class="text-red-800 dark:text-red-300">{{ error }}</p>
      <button
        @click="fetchRequests"
        class="mt-2 text-sm text-red-600 hover:text-red-800 dark:text-red-400 dark:hover:text-red-300 underline"
      >
        Try again
      </button>
    </div>
 
    <!-- Empty State -->
    <div v-else-if="requests.length === 0" class="text-center py-12">
      <div class="text-fg-muted text-5xl mb-4">📬</div>
      <h3 class="text-lg font-medium text-fg mb-2">No invite requests</h3>
      <p class="text-fg-muted">
        {{
          statusFilter || iosBetaOnly
            ? `No ${[statusFilter, iosBetaOnly && 'iPhone beta'].filter(Boolean).join(' ')} requests found.`
            : 'No one has requested an invite yet.'
        }}
      </p>
    </div>
 
    <!-- Requests Table -->
    <div v-else class="overflow-x-auto">
      <table class="min-w-full divide-y divide-line">
        <thead class="bg-surface-alt">
          <tr>
            <th
              class="px-4 py-3 text-left text-xs font-medium text-fg-muted uppercase tracking-wider"
            >
              Requester
            </th>
            <th
              class="px-4 py-3 text-left text-xs font-medium text-fg-muted uppercase tracking-wider"
            >
              Team
            </th>
            <th
              class="px-4 py-3 text-left text-xs font-medium text-fg-muted uppercase tracking-wider"
            >
              Reason
            </th>
            <th
              class="px-4 py-3 text-left text-xs font-medium text-fg-muted uppercase tracking-wider"
            >
              Status
            </th>
            <th
              class="px-4 py-3 text-left text-xs font-medium text-fg-muted uppercase tracking-wider"
            >
              Date
            </th>
            <th
              class="px-4 py-3 text-left text-xs font-medium text-fg-muted uppercase tracking-wider"
            >
              Actions
            </th>
          </tr>
        </thead>
        <tbody class="bg-card divide-y divide-line">
          <tr
            v-for="request in requests"
            :key="request.id"
            class="hover:bg-surface-alt"
          >
            <td class="px-4 py-4">
              <div class="text-sm font-medium text-fg">
                {{ request.name }}
              </div>
              <div class="text-sm text-fg-muted">{{ request.email }}</div>
              <span
                v-if="request.wants_ios_beta"
                data-testid="ios-beta-badge"
                class="inline-block mt-1 px-2 py-0.5 text-xs font-medium rounded-full bg-blue-100 text-blue-800"
              >
                iPhone beta
              </span>
            </td>
            <td class="px-4 py-4 text-sm text-fg-muted">
              {{ request.team || '-' }}
            </td>
            <td
              class="px-4 py-4 text-sm text-fg-muted max-w-xs truncate"
              :title="request.reason"
            >
              {{ request.reason || '-' }}
            </td>
            <td class="px-4 py-4">
              <span
                :class="{
                  'bg-yellow-100 text-yellow-800': request.status === 'pending',
                  'bg-green-100 text-green-800': request.status === 'approved',
                  'bg-red-100 text-red-800': request.status === 'rejected',
                  'bg-blue-100 text-blue-800':
                    request.status === 'existing_user',
                }"
                class="px-2 py-1 text-xs font-medium rounded-full"
              >
                {{ STATUS_LABELS[request.status] || request.status }}
              </span>
              <div
                v-if="request.invitation"
                data-testid="linked-invite"
                class="mt-1 text-xs text-fg-muted"
              >
                <div class="font-mono">
                  {{ linkedInviteSummary(request.invitation) }}
                </div>
                <div v-if="request.invitation.testflight_status">
                  TestFlight:
                  <span
                    :class="{
                      'text-green-700':
                        request.invitation.testflight_status === 'added',
                      'text-red-700':
                        request.invitation.testflight_status === 'failed',
                    }"
                    :title="request.invitation.testflight_error || ''"
                    >{{ request.invitation.testflight_status }}</span
                  >
                </div>
              </div>
            </td>
            <td class="px-4 py-4 text-sm text-fg-muted">
              {{ formatDate(request.created_at) }}
            </td>
            <td class="px-4 py-4 text-sm">
              <div v-if="request.status === 'pending'" class="flex gap-2">
                <button
                  v-if="!request.invitation_id"
                  data-testid="approve-request"
                  class="text-green-600 hover:text-green-800 font-medium"
                  @click="emit('approve', request)"
                >
                  Approve
                </button>
                <button
                  data-testid="reject-request"
                  class="text-red-600 hover:text-red-800 font-medium"
                  @click="rejectRequest(request)"
                >
                  Reject
                </button>
                <button
                  data-testid="existing-user-request"
                  class="text-blue-600 hover:text-blue-800 font-medium"
                  @click="markExistingUser(request)"
                >
                  Already a user
                </button>
              </div>
              <div v-else class="flex flex-col gap-1 text-xs">
                <span v-if="request.reviewed_at" class="text-fg-muted">
                  {{ formatDate(request.reviewed_at) }}
                </span>
                <div class="flex gap-3">
                  <button
                    v-if="request.invitation?.status === 'pending'"
                    data-testid="resend-invite"
                    :disabled="resendingIds.has(request.id)"
                    class="text-blue-600 hover:text-blue-800 font-medium disabled:text-fg-muted"
                    @click="resendInviteEmail(request)"
                  >
                    {{
                      resendingIds.has(request.id)
                        ? 'Sending…'
                        : 'Resend invite'
                    }}
                  </button>
                  <!-- A linked request's invitation exists; it can't be undone here. -->
                  <button
                    v-if="!request.invitation_id"
                    data-testid="reset-request"
                    class="text-fg-muted hover:text-fg font-medium"
                    @click="resetToPending(request.id)"
                  >
                    Reset
                  </button>
                </div>
              </div>
            </td>
          </tr>
        </tbody>
      </table>
    </div>
 
    <!-- Pagination placeholder -->
    <div
      v-if="requests.length > 0"
      class="mt-4 text-sm text-fg-muted text-center"
    >
      Showing {{ requests.length }} requests
    </div>
  </div>
</template>
 
<script setup>
import { ref, onMounted } from 'vue';
import { useAuthStore } from '@/stores/auth';
import { getApiBaseUrl } from '../../config/api';
import { linkedInviteSummary } from '../../utils/inviteRequests';
 
// "Approve" hands the request to AdminPanel, which opens the invite form
// pre-filled from it (SB-1312). Sending that invite is what approves the
// request (SB-1392) - there is no approval without an invitation.
const emit = defineEmits(['approve']);
 
const STATUS_LABELS = { existing_user: 'already a user' };
 
const authStore = useAuthStore();
 
// State
const requests = ref([]);
const stats = ref(null);
const loading = ref(false);
const error = ref(null);
const statusFilter = ref('');
const iosBetaOnly = ref(false);
// Track per-row "resend in flight" so we can disable the button while the
// POST is pending. Set works fine without a deep reactive wrapper.
const resendingIds = ref(new Set());
const sendingTestEmail = ref(false);
 
// Send the rejection email to any address, so the template can be checked
// before a real Reject (SB-1407). Touches no invite request.
const sendTestRejectionEmail = async () => {
  const toEmail = window.prompt(
    'Send the rejection email to which address?',
    ''
  );
  if (!toEmail) return;
  const name =
    window.prompt('Name to greet in the email:', 'Test User') || 'Test User';
 
  sendingTestEmail.value = true;
  try {
    const response = await fetch(
      `${getApiBaseUrl()}/api/invite-requests/test-rejection-email`,
      {
        method: 'POST',
        headers: {
          ...authStore.getAuthHeaders(),
          'Content-Type': 'application/json',
        },
        body: JSON.stringify({ to_email: toEmail, name }),
      }
    );
    if (!response.ok) {
      const detail = await response.text();
      throw new Error(detail || response.statusText);
    }
    alert(`Sent — check ${toEmail}.`);
  } catch (err) {
    console.error('Test rejection email failed:', err);
    alert(`Failed to send: ${err.message}`);
  } finally {
    sendingTestEmail.value = false;
  }
};
 
// Re-send the linked invitation's email (e.g. the requester missed it).
const resendInviteEmail = async request => {
  if (!confirm(`Resend the invite email to ${request.email}?`)) return;
  resendingIds.value.add(request.id);
  // Trigger reactivity by reassigning the Set.
  resendingIds.value = new Set(resendingIds.value);
  try {
    const response = await fetch(
      `${getApiBaseUrl()}/api/invites/admin/${request.invitation.id}/resend-email`,
      { method: 'POST', headers: authStore.getAuthHeaders() }
    );
    if (!response.ok) {
      const detail = await response.text();
      throw new Error(detail || response.statusText);
    }
    alert(`Resent the invite email to ${request.email}.`);
  } catch (err) {
    console.error('Resend invite email failed:', err);
    alert(`Failed to resend: ${err.message}`);
  } finally {
    resendingIds.value.delete(request.id);
    resendingIds.value = new Set(resendingIds.value);
  }
};
 
// Set an already-decided request back to pending so the admin can
// approve or reject it again.
const resetToPending = async requestId => {
  if (!confirm('Reset this request to pending? Clears the review.')) return;
  try {
    const headers = {
      ...authStore.getAuthHeaders(),
      'Content-Type': 'application/json',
    };
    const response = await fetch(
      `${getApiBaseUrl()}/api/invite-requests/${requestId}/status`,
      {
        method: 'PUT',
        headers,
        body: JSON.stringify({ status: 'pending' }),
      }
    );
    if (!response.ok) throw new Error('Failed to reset request');
    await Promise.all([fetchRequests(), fetchStats()]);
  } catch (err) {
    console.error('Reset to pending failed:', err);
    alert('Failed to reset request. Please try again.');
  }
};
 
// Fetch invite requests
const fetchRequests = async () => {
  loading.value = true;
  error.value = null;
 
  try {
    const headers = authStore.getAuthHeaders();
    const params = new URLSearchParams();
    if (statusFilter.value) params.set('status', statusFilter.value);
    if (iosBetaOnly.value) params.set('ios_beta', 'true');
    const query = params.toString();
    const url = `${getApiBaseUrl()}/api/invite-requests${query ? `?${query}` : ''}`;
 
    const response = await fetch(url, { headers });
 
    if (!response.ok) {
      throw new Error('Failed to fetch invite requests');
    }
 
    requests.value = await response.json();
  } catch (err) {
    console.error('Error fetching invite requests:', err);
    error.value = err.message;
  } finally {
    loading.value = false;
  }
};
 
// Fetch stats
const fetchStats = async () => {
  try {
    const headers = authStore.getAuthHeaders();
    const response = await fetch(
      `${getApiBaseUrl()}/api/invite-requests/stats`,
      { headers }
    );
 
    if (response.ok) {
      stats.value = await response.json();
    }
  } catch (err) {
    console.error('Error fetching stats:', err);
  }
};
 
// Close a request from someone who already has an account (SB-1408): no
// email, no invite - Reject would tell them they weren't approved.
const markExistingUser = async request => {
  if (
    !confirm(
      `Close this request? ${request.email} already has an account. No email is sent.`
    )
  )
    return;
 
  try {
    const response = await fetch(
      `${getApiBaseUrl()}/api/invite-requests/${request.id}/status`,
      {
        method: 'PUT',
        headers: {
          ...authStore.getAuthHeaders(),
          'Content-Type': 'application/json',
        },
        body: JSON.stringify({ status: 'existing_user' }),
      }
    );
    if (!response.ok) throw new Error('Failed to close request');
    await Promise.all([fetchRequests(), fetchStats()]);
  } catch (err) {
    console.error('Error closing request:', err);
    alert('Failed to close request. Please try again.');
  }
};
 
// Reject a request; the backend emails the requester (SB-1392).
const rejectRequest = async request => {
  if (!confirm(`Reject this request? ${request.email} will be emailed.`))
    return;
 
  try {
    const headers = {
      ...authStore.getAuthHeaders(),
      'Content-Type': 'application/json',
    };
 
    const response = await fetch(
      `${getApiBaseUrl()}/api/invite-requests/${request.id}/status`,
      {
        method: 'PUT',
        headers,
        body: JSON.stringify({ status: 'rejected' }),
      }
    );
 
    if (!response.ok) {
      throw new Error('Failed to reject request');
    }
 
    // Refresh data
    await Promise.all([fetchRequests(), fetchStats()]);
  } catch (err) {
    console.error(`Error rejecting request:`, err);
    alert('Failed to reject request. Please try again.');
  }
};
 
// Utility functions
const formatDate = dateString => {
  if (!dateString) return '-';
  return new Date(dateString).toLocaleDateString('en-US', {
    month: 'short',
    day: 'numeric',
    year: 'numeric',
  });
};
 
// Lifecycle
onMounted(async () => {
  await Promise.all([fetchRequests(), fetchStats()]);
});
</script>