All files / services invitations.service.ts

100% Statements 22/22
100% Branches 4/4
100% Functions 6/6
100% Lines 22/22

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                                                          3x 29x 29x 2x   29x 2x     29x 24x     3x 5x 4x     3x 3x     3x 3x 2x                                 3x 30x 24x     3x 15x 11x    
import { apiClient as api } from '@/lib/api';
 
export interface Invitation {
  id: string;
  account_id: string;
  account_code: string;
  email: string;
  invited_by: string;
  expires_at: string;
  accepted_at: string | null;
  created_at: string;
  email_sent: boolean;
  email_sent_at: string | null;
  status: 'pending' | 'accepted' | 'expired' | 'cancelled';
  token: string;
}
 
export interface InvitationListResponse {
  invitations: Invitation[];
  total: number;
}
 
export interface InvitationCreateRequest {
  email: string;
  role_id?: string;
  groups?: string[];
  account_id?: string;
}
 
export const getInvitations = async (status?: string, account_id?: string): Promise<InvitationListResponse> => {
  const params: Record<string, string> = {};
  if (status) {
    params.status_filter = status;
  }
  if (account_id) {
    params.account_id = account_id;
  }
  
  const response = await api.get<InvitationListResponse>('/invitations', { params });
  return response.data;
};
 
export const createInvitation = async (data: InvitationCreateRequest): Promise<Invitation> => {
  const response = await api.post<Invitation>('/invitations', data);
  return response.data;
};
 
export const cancelInvitation = async (invitationId: string): Promise<void> => {
  await api.delete(`/invitations/${invitationId}`);
};
 
export const resendInvitation = async (invitationId: string): Promise<{ message: string }> => {
  const response = await api.post<{ message: string }>(`/invitations/${invitationId}/resend`);
  return response.data;
};
 
export interface InvitationPublicResponse {
  email: string;
  account_name: string;
  invited_by_name: string;
  expires_at: string;
  is_valid: boolean;
}
 
export interface InvitationAcceptRequest {
  password?: string;
}
 
import type { AuthResponse } from '@/types/auth.types';
 
export const getInvitation = async (token: string): Promise<InvitationPublicResponse> => {
  const response = await api.get<InvitationPublicResponse>(`/invitations/${token}`);
  return response.data;
};
 
export const acceptInvitation = async (token: string, data: InvitationAcceptRequest): Promise<AuthResponse> => {
  const response = await api.post<AuthResponse>(`/invitations/${token}/accept`, data);
  return response.data;
};