import { EventBus } from '../../core/modules/EventBus' ; import { Data } from '../Data/Data' ; import { ApiClient } from '../ApiClient/ApiClient' ; import { IMapRequest , IMapResponse , IRequest , TMethod } from '../ApiClient/ApiClient.types' ; import * as CONSTANTS from '../../constants' ; import { SetPurchaseAttributes } from './Api.types' ; export class Api { private readonly data : Data ; private readonly apiClient : ApiClient ; private readonly eventBus : EventBus ; constructor ( eventBus , data = new Data ( ) , apiClient = new ApiClient ( ) ) { this . eventBus = eventBus ; this . data = data ; this . apiClient = apiClient ; } public async checkDevice ( ) { const params = await this . getRequestParams ( ) ; return await this . apiClient . checkDevice ( params ) ; } public async checkDeviceSubscribeForPushNotifications ( useCache = true ) { let status = localStorage . getItem ( CONSTANTS . KEY_DEVICE_REGISTRATION_STATUS ) ; if ( typeof status === 'undefined' || ! useCache ) { const { exist , push_token_exist } = await this . checkDevice ( ) ; localStorage . setItem ( CONSTANTS . KEY_DEVICE_REGISTRATION_STATUS , exist && push_token_exist ? CONSTANTS . DEVICE_REGISTRATION_STATUS_REGISTERED : CONSTANTS . DEVICE_REGISTRATION_STATUS_UNREGISTERED ) ; status = localStorage . getItem ( CONSTANTS . KEY_DEVICE_REGISTRATION_STATUS ) ; } return status === CONSTANTS . DEVICE_REGISTRATION_STATUS_REGISTERED ; } public async getConfig ( features ) { const params = await this . getRequestParams ( ) ; return this . apiClient . getConfig ( { ... params , features , } ) ; } public async applicationOpen ( ) { const params = await this . getRequestParams ( ) ; await this . data . setLastOpenApplicationTime ( Date . now ( ) ) ; return this . apiClient . applicationOpen ( params ) ; } public async registerDevice ( ) { const isCommunicationDisabled = await this . data . getStatusCommunicationDisabled ( ) ; if ( isCommunicationDisabled ) { throw new Error ( `Can't register device: Communication is disabled!` ) ; } const params = await this . getRequestParams ( ) ; const tokens = await this . data . getTokens ( ) ; if ( ! tokens . pushToken ) { throw new Error ( `Can't register device: pushToken is not exist!` ) ; } const response = await this . apiClient . registerDevice ( { ... params , ... { push_token : tokens . pushToken , auth_token : tokens . authToken , public_key : tokens . publicKey , fcm_push_set : tokens . fcmPushSet , fcm_token : tokens . fcmToken , } , } ) ; await this . data . setStatusManualUnsubscribed ( false ) ; localStorage . setItem ( CONSTANTS . KEY_DEVICE_REGISTRATION_STATUS , CONSTANTS . DEVICE_REGISTRATION_STATUS_REGISTERED ) ; this . eventBus . dispatchEvent ( 'register' , { } ) ; return response ; } public async unregisterDevice ( ) { const params = await this . getRequestParams ( ) ; const response = this . apiClient . unregisterDevice ( params ) ; localStorage . setItem ( CONSTANTS . KEY_DEVICE_REGISTRATION_STATUS , CONSTANTS . DEVICE_REGISTRATION_STATUS_UNREGISTERED ) ; this . eventBus . dispatchEvent ( 'unsubscribe' , { } ) ; return response ; } public async deleteDevice ( ) { const params = await this . getRequestParams ( ) ; const response = this . apiClient . deleteDevice ( params ) ; await this . data . setStatusManualUnsubscribed ( true ) ; localStorage . setItem ( CONSTANTS . KEY_DEVICE_REGISTRATION_STATUS , CONSTANTS . DEVICE_REGISTRATION_STATUS_UNREGISTERED ) ; this . eventBus . dispatchEvent ( 'unsubscribe' , { } ) ; return response ; } public async messageDeliveryEvent ( hash , isTrackingLogOnFailure ? , metaData = { } ) { const startTime = Math . round ( + new Date ( ) ) ; const params = await this . getRequestParams ( ) ; return await this . apiClient . messageDeliveryEvent ( { ... params , hash , metaData } ) ; } public async pushStat ( hash , isTrackingLogOnFailure ? , metaData = { } ) { const startTime = Math . round ( + new Date ( ) ) ; const params = await this . getRequestParams ( ) ; return await this . apiClient . pushStat ( { ... params , hash , metaData , } ) ; } public async setTags ( tags ) { const params = await this . getRequestParams ( ) ; return this . apiClient . setTags ( { ... params , tags , } ) ; } public async getTags ( ) { const params = await this . getRequestParams ( ) ; return this . apiClient . getTags ( params ) ; } public async registerUser ( userId ) { const params = await this . getRequestParams ( ) ; const deviceType = await this . data . getDeviceType ( ) ; const id = `${ userId }` ; const response = await this . apiClient . registerUser ( { ... params , userId : id , ts_offset : - ( new Date ) . getTimezoneOffset ( ) * 60 , device_type : deviceType } ) ; await this . data . setUserId ( `${ userId }` ) ; await this . data . setStatusUserIdWasChanged ( true ) ; return response ; } public async postEvent ( event , attributes ) { const params = await this . getRequestParams ( ) ; const date = new Date ( ) ; const time = date . getTime ( ) ; const timestampUTC = Math . floor ( time / 1000 ) ; const timestampCurrent = timestampUTC - ( date . getTimezoneOffset ( ) / 60 * 3600 ) ; const lastOpenMessage = await this . data . getLastOpenMessage ( ) ; if ( lastOpenMessage && lastOpenMessage . expiry > Date . now ( ) ) { if ( attributes [ 'msgHash' ] ) { return Promise . reject ( 'attribute msgHash already defined' ) ; } attributes = { ... attributes , msgHash : lastOpenMessage . messageHash } ; } await this . data . setLastOpenMessage ( undefined ) ; const response = await this . apiClient . postEvent ( { ... params , event , timestampUTC , timestampCurrent , attributes , } ) ; if ( response && response . code ) { this . eventBus . dispatchEvent ( 'receive-in-app-code' , { code : response . code } ) ; } return response ; } public async getInboxMessages ( count = 0 ) { const params = await this . getRequestParams ( ) ; const lastCode = await this . data . getInboxLastRequestCode ( ) ; const lastRequestTime = await this . data . getInboxLastRequestTime ( ) ; return this . apiClient . getInboxMessages ( { ... params , ... { count , last_code : lastCode , last_request_time : lastRequestTime } , } ) ; } public async inboxStatus ( order , status ) { const params = await this . getRequestParams ( ) ; return this . apiClient . inboxStatus ( { ... params , ... { inbox_code : order , status , time : ( new Date ( ) ) . getTime ( ) , } } ) ; } public async triggerEvent ( ) { throw new Error ( `Method has been deprecated, because we don't aggregate this statistics.` ) ; } public async pageVisit ( config ) { const params = await this . getRequestParams ( ) ; const features = await this . data . getFeatures ( ) ; const url = features && features . page_visit && features . page_visit . entrypoint ; if ( ! url ) { return ; } return this . apiClient . pageVisit ( { ... params , ... config , } , url ) ; } public async getInApps ( ) { const params = await this . getRequestParams ( ) ; return this . apiClient . getInApps ( params ) ; } public async setPurchase ( attributes ) { const params = await this . getRequestParams ( ) ; return this . apiClient . setPurchase ( { ... params , ... attributes , } ) ; } public async getParams ( ) { const applicationCode = await this . data . getApplicationCode ( ) ; const hwid = await this . data . getHwid ( ) ; const apiParams = await this . data . getTokens ( ) ; const initParams = await this . data . getInitParams ( ) ; return { applicationCode , hwid , ... apiParams , ... initParams , } ; } public get params ( ) : void { throw new Error ( 'Property "Pushwoosh.api.params" have been deprecated. Use the async method "Pushwoosh.api.getParams()"' ) ; } private async getRequestParams ( ) { const applicationCode = await this . data . getApplicationCode ( ) ; const hwid = await this . data . getHwid ( ) ; const userId = await this . data . getUserId ( ) ; const deviceType = await this . data . getDeviceType ( ) ; const deviceModel = await this . data . getDeviceModel ( ) ; const language = await this . data . getLanguage ( ) ; const version = await this . data . getSdkVersion ( ) ; const timezone = - ( new Date ) . getTimezoneOffset ( ) * 60 ; return { application : applicationCode , hwid : hwid , userId : userId || hwid , device_type : deviceType , device_model : deviceModel , timezone : timezone , language : language , v : version , } ; } ; }