import type { AxiosError , AxiosRequestConfig } from 'axios' ; import { TeamAPI } from '../team/' ; import { HttpClient , BackendErrorLabel , BackendError } from '../../http/' ; import type { NewTeamInvitation , TeamInvitation , TeamInvitationChunk } from '../invitation/' ; import { StatusCodes as StatusCode } from 'http-status-codes' ; import { InvitationInvalidPhoneError , InvitationInvalidEmailError , InvitationEmailExistsError , InvitationPhoneExistsError , InvitationNotFoundError , InvitationMultipleError , } from './InvitationError' ; export class TeamInvitationAPI { public static readonly MAX_CHUNK_SIZE = 100 ; public static readonly URL = { INFO : 'info' , INVITATIONS : 'invitations' , EMAIL : 'by-email' , } ; constructor ( private readonly client ) { } public async getInvitation ( teamId , invitationId ) { const config = { method : 'get' , url : `${ TeamAPI . URL . TEAMS }/${ teamId }/${ TeamInvitationAPI . URL . INVITATIONS }/${ invitationId }` , } ; const response = await this . client . sendJSON < TeamInvitation > ( config ) ; return response . data ; } public async getAllInvitations ( teamId ) { let allInvitations = [ ] ; let invitationChunk = await this . getInvitations ( teamId , undefined ) ; allInvitations = allInvitations . concat ( invitationChunk . invitations ) ; while ( invitationChunk . has_more ) { const invitations = invitationChunk . invitations ; const lastInvitation = invitations [ invitations . length - 1 ] || { } ; const lastChunkId = ( lastInvitation as TeamInvitation ) . id ; invitationChunk = await this . getInvitations ( teamId , lastChunkId ) ; allInvitations = allInvitations . concat ( invitationChunk . invitations ) ; } return allInvitations ; } public async getInvitations ( teamId , startId ? , limit = TeamInvitationAPI . MAX_CHUNK_SIZE ) { const config = { method : 'get' , params : { size : limit , start : startId , } , url : `${ TeamAPI . URL . TEAMS }/${ teamId }/${ TeamInvitationAPI . URL . INVITATIONS }` , } ; const response = await this . client . sendJSON < TeamInvitationChunk > ( config ) ; return response . data ; } public async deleteInvitation ( teamId , invitationId ) { const config = { method : 'delete' , url : `${ TeamAPI . URL . TEAMS }/${ teamId }/${ TeamInvitationAPI . URL . INVITATIONS }/${ invitationId }` , } ; await this . client . sendJSON ( config ) ; } public async headInvitation ( email ) { const config = { method : 'head' , params : { email , } , url : `${ TeamAPI . URL . TEAMS }/${ TeamInvitationAPI . URL . INVITATIONS }/${ TeamInvitationAPI . URL . EMAIL }` , } ; try { await this . client . sendJSON ( config ) ; } catch ( error ) { const status = ( error as AxiosError ) . response ?. status ; switch ( status ) { case StatusCode . NOT_FOUND : { throw new InvitationNotFoundError ( 'Invitation not found' ) ; } case StatusCode . CONFLICT : { throw new InvitationMultipleError ( 'Multiple invitations found' ) ; } } throw error ; } } public async postInvitation ( teamId , invitation ) { const config = { data : invitation , method : 'post' , url : `${ TeamAPI . URL . TEAMS }/${ teamId }/${ TeamInvitationAPI . URL . INVITATIONS }` , } ; try { const response = await this . client . sendJSON < TeamInvitation > ( config ) ; return response . data ; } catch ( error ) { const backendError = error as BackendError ; switch ( backendError . label ) { case BackendErrorLabel . INVITE_EMAIL_EXISTS : { throw new InvitationEmailExistsError ( backendError . message ) ; } case BackendErrorLabel . BAD_REQUEST : { throw new InvitationInvalidPhoneError ( backendError . message ) ; } case BackendErrorLabel . INVALID_EMAIL : { throw new InvitationInvalidEmailError ( backendError . message ) ; } case BackendErrorLabel . PHONE_EXISTS : { throw new InvitationPhoneExistsError ( backendError . message ) ; } } throw error ; } } public async getInvitationFromCode ( invitationCode ) { const config = { method : 'get' , params : { code : invitationCode , } , url : `${ TeamAPI . URL . TEAMS }/${ TeamInvitationAPI . URL . INVITATIONS }/${ TeamInvitationAPI . URL . INFO }` , } ; const response = await this . client . sendJSON < TeamInvitation > ( config ) ; return response . data ; } }