import axios , { AxiosRequestConfig } from 'axios' ; import { ArrayUtil } from '@wireapp/commons' ; import { TeamAPI } from '../team/TeamAPI' ; import { BackendError , handleProgressEvent , HttpClient , ProgressCallback , RequestCancelable , SyntheticErrorLabel , } from '../../http/' ; import type { MemberData , Members } from '../member/' ; import { RequestCancellationError } from '../../user' ; import { MemberCSVResponse } from './MemberCSVResponse' ; import { UpdatedMemberData } from './UpdatedMemberData' ; export class MemberAPI { private static readonly DEFAULT_MEMBERS_CHUNK_SIZE = 1600 ; constructor ( private readonly client ) { } public static readonly URL = { MEMBERS : 'members' , CSV : 'csv' , MEMBERS_BY_ID_LIST : 'get-members-by-ids-using-post' , } ; public async getMember ( teamId , userId ) { const config = { method : 'get' , url : `${ TeamAPI . URL . TEAMS }/${ teamId }/${ MemberAPI . URL . MEMBERS }/${ userId }` , } ; const response = await this . client . sendJSON < MemberData > ( config ) ; return response . data ; } public async deleteMember ( teamId , userId , password ) { const config = { data : { password , } , method : 'delete' , url : `${ TeamAPI . URL . TEAMS }/${ teamId }/${ MemberAPI . URL . MEMBERS }/${ userId }` , } ; await this . client . sendJSON ( config ) ; } public async postMembers ( teamId , member ) { const config = { data : { member : member , } , method : 'post' , url : `${ TeamAPI . URL . TEAMS }/${ teamId }/${ MemberAPI . URL . MEMBERS }` , } ; await this . client . sendJSON ( config ) ; } public async putMembers ( teamId , member ) { const config = { data : { member : member , } , method : 'put' , url : `${ TeamAPI . URL . TEAMS }/${ teamId }/${ MemberAPI . URL . MEMBERS }` , } ; await this . client . sendJSON ( config ) ; } /**
     * This endpoint returns all members of the a team unless it's a large team (>2000 team member).
     * If the queried team is a large team the `hasMore` flag will switch to `true`.
     */ public async getAllMembers ( teamId ) { const config = { method : 'get' , url : `${ TeamAPI . URL . TEAMS }/${ teamId }/${ MemberAPI . URL . MEMBERS }` , } ; const response = await this . client . sendJSON < Members > ( config ) ; return response . data ; } public async getMembers ( teamId , parameters , limit = MemberAPI . DEFAULT_MEMBERS_CHUNK_SIZE ) { const { ids } = parameters ; if ( ids . length ) { const uniqueIds = ArrayUtil . removeDuplicates ( ids ) ; const idChunks = ArrayUtil . chunk ( uniqueIds , limit ) ; const resolvedTasks = await Promise . all ( idChunks . map ( async ( idChunk ) => { const result = await this . _getMembers ( teamId , { ids : idChunk } ) ; return result . members ; } ) ) ; return ArrayUtil . flatten ( resolvedTasks ) ; } return [ ] ; } private async _getMembers ( teamId , parameters ) { const config = { data : { user_ids : parameters . ids } , method : 'post' , url : `${ TeamAPI . URL . TEAMS }/${ teamId }/${ MemberAPI . URL . MEMBERS_BY_ID_LIST }` , } ; const response = await this . client . sendJSON < Members > ( config ) ; return response . data ; } public async getMemberListCSV ( teamId , progressCallback ? ) { const cancelSource = axios . CancelToken . source ( ) ; const config = { cancelToken : cancelSource . token , method : 'get' , onDownloadProgress : handleProgressEvent ( progressCallback ) , responseType : 'arraybuffer' , url : `${ TeamAPI . URL . TEAMS }/${ teamId }/${ MemberAPI . URL . MEMBERS }/${ MemberAPI . URL . CSV }` , } ; const handleRequest = async ( ) : Promise < MemberCSVResponse > => { try { const response = await this . client . sendRequest < ArrayBuffer > ( config , true ) ; return { buffer : response . data , mimeType : response . headers [ 'content-type' ] , } ; } catch ( error ) { if ( ( error as BackendError ) . message === SyntheticErrorLabel . REQUEST_CANCELLED ) { throw new RequestCancellationError ( 'Member CSV download got cancelled.' ) ; } throw error ; } } ; return { cancel : ( ) => cancelSource . cancel ( SyntheticErrorLabel . REQUEST_CANCELLED ) , response : handleRequest ( ) , } ; } }