import _ from 'lodash' ; import Log from '../util/Logger' ; import * as queries from '../scripts/attendeeQueries' ; import NI from '../util/NetworkInterface' ; import { IUser } from '../interfaces/IUser' ; import { IContactInfo } from '../interfaces/IContactInfo' ; import { IAttendee , IAttendeeData , IAttendeeOptions , IAttendeeDataFull , IAttendeeWithPhasesData , IAttendeeWithPhaseGroupsData } from '../interfaces/IAttendee' ; import { IPhaseGroup , IPhaseGroupData } from '../interfaces/IPhaseGroup' ; import { IPhase , IPhaseData } from '../interfaces/IPhase' ; import { User } from './User' ; import { Phase } from './Phase' ; import { PhaseGroup } from './PhaseGroup' ; export class Attendee implements IAttendee { public static parse ( data ) { const eventIds = data . events . map ( event => event . id ) ; return new Attendee ( data . id , data . gamerTag , data . prefix , data . createdAt , data . claimed , data . verified , data . playerId , data . phoneNumber , data . connectedAccounts , data . contactInfo , eventIds ) ; } public static parseFull ( data ) { return this . parse ( data . participant ) ; } public static eq ( a1 , a2 ) { return a1 . getGamerTag ( ) === a2 . getGamerTag ( ) && a1 . getSponsor ( ) === a2 . getSponsor ( ) && a1 . getPlayerId ( ) === a2 . getPlayerId ( ) ; } public static getDefaultAttendeeOptions ( ) { return { areSeedsPublished : true , page : null , perPage : 1 , sortBy : null , filter : null } ; } private id : number ; private gamerTag : string ; private prefix : string | null ; private createdAt : number | null ; private claimed : boolean | null ; private verified : boolean | null ; private playerId : number | null ; private phoneNumber : number | null ; private connectedAccounts : object | null ; private contactInfo : IContactInfo | null ; private eventIds : number [ ] | null ; constructor ( id , gamerTag , prefix , createdAt , claimed , verified , playerId , phoneNumber , connectedAccounts , contactInfo , eventIds ) { this . id = id ; this . gamerTag = gamerTag ; this . prefix = prefix ; this . createdAt = createdAt ; this . claimed = claimed ; this . verified = verified ; this . playerId = playerId ; this . phoneNumber = phoneNumber ; this . contactInfo = contactInfo ; this . connectedAccounts = connectedAccounts ; this . eventIds = eventIds ; } public getId ( ) { return this . id ; } public getGamerTag ( ) { return this . gamerTag ; } public getSponsor ( ) { return this . prefix ; } public getCreatedAt ( ) { return this . createdAt ; } public getClaimed ( ) { return this . claimed ; } public getVerified ( ) { return this . verified ; } public getPlayerId ( ) { return this . playerId ; } public getPhoneNumber ( ) { return this . phoneNumber ; } public getContactInfo ( ) { return this . contactInfo ; } public getCity ( ) { if ( this . contactInfo ) return this . contactInfo . city ; else return null ; } public getState ( ) { if ( this . contactInfo ) return this . contactInfo . state ; else return null ; } public getStateId ( ) { if ( this . contactInfo ) return this . contactInfo . stateId ; else return null ; } public getCountry ( ) { if ( this . contactInfo ) return this . contactInfo . country ; else return null ; } public getCountryId ( ) { if ( this . contactInfo ) return this . contactInfo . countryId ; else return null ; } public getContactName ( ) { if ( this . contactInfo ) return this . contactInfo . name ; else return null ; } public getFirstName ( ) { if ( this . contactInfo ) return this . contactInfo . nameFirst ; else return null ; } public getLastName ( ) { if ( this . contactInfo ) return this . contactInfo . nameLast ; else return null ; } public getZipcode ( ) { if ( this . contactInfo ) return this . contactInfo . zipcode ; else return null ; } public getConnectedAccounts ( ) { return this . connectedAccounts ; } public async getUserAccount ( ) { Log . info ( 'Getting User account that Attendee %s (Participant %s) entered' , this . gamerTag , this . id ! ) ; return await User . getById ( this . playerId ! ) ; } public async getEnteredPhases ( ) { Log . info ( 'Getting Phases that Attendee %s (Participant %s) entered' , this . gamerTag , this . id ) ; const data = await NI . query ( queries . getAttendeePhases , { id : this . id } ) ; const seedData = _ . flatten ( data . participant . entrants . map ( entrant => entrant . seeds ) ) ; const phaseData = _ . flatten ( seedData . map ( seed => seed . phase ) ) ; const phases = phaseData . map ( phase => Phase . parse ( phase ) ) ; return phases ; } public async getEnteredPhaseGroups ( ) { Log . info ( 'Getting Phase Groups that Attendee %s (Participant %s) entered' , this . gamerTag , this . id ) ; const data = await NI . query ( queries . getAttendeePhaseGroups , { id : this . id } ) ; const seedData = _ . flatten ( data . participant . entrants . map ( entrant => entrant . seeds ) ) ; const groupData = _ . flatten ( seedData . map ( seed => seed . phaseGroup ) ) ; const groups = groupData . map ( group => PhaseGroup . parse ( group ) ) ; return groups ; } }