import { CoreEnv } from "../../config/core-env" ; import * as Q from "q" ; import * as _ from "lodash" ; import { AthleteUpdateModel } from "../models/athlete-update.model" ; import { SyncResultModel } from "@elevate/shared/models" ; import { HerokuEndpointResolver } from "../resolvers/heroku-endpoint.resolver" ; export class AthleteUpdate { param stravaId param name param version param isPremium param isPro param locale param hrMin param hrMax returns { AthleteUpdateModel } public static create ( stravaId , name , version , isPremium , isPro , locale ? , hrMin ? , hrMax ? ) { if ( stravaId < 1 || _ . isEmpty ( name ) || _ . isEmpty ( version ) || ! _ . isBoolean ( isPremium ) || ! _ . isBoolean ( isPro ) ) { return null ; } let status = 0 ; if ( isPremium ) { status = 1 ; } if ( isPro ) { status = 2 ; } const athleteUpdate = { stravaId , name : _ . isEmpty ( name ) ? null : name , version , status , hrMin , hrMax , } ; if ( ! _ . isEmpty ( locale ) ) { athleteUpdate . locale = locale ; } return athleteUpdate ; } public static commit ( athleteUpdate ) { const deferred = Q . defer < SyncResultModel > ( ) ; const endPoint = HerokuEndpointResolver . resolve ( CoreEnv . endPoint ) + "/api/athlete/update" ; $ . post ( { url : endPoint , data : JSON . stringify ( athleteUpdate ) , dataType : "json" , contentType : "application/json" , success : ( response ) => { deferred . resolve ( response ) ; } , error : ( jqXHR , textStatus , errorThrown ) => { console . warn ( "Endpoint <" + endPoint + "> not reachable" , jqXHR ) ; deferred . reject ( { textStatus , errorThrown } ) ; } , } ) ; return deferred . promise ; } }