import { Injectable } from "@angular/core" ; import { ActivityDao } from "../../dao/activity/activity.dao" ; import { SyncedActivityModel } from "@elevate/shared/models" ; import * as _ from "lodash" ; import { AthleteSnapshotResolverService } from "../athlete-snapshot-resolver/athlete-snapshot-resolver.service" ; import { Subject } from "rxjs" ; import { LoggerService } from "../logging/logger.service" ; @ Injectable ( ) export class ActivityService { public athleteSettingsConsistency : Subject < boolean > ; constructor ( public activityDao , public athleteSnapshotResolverService , public logger ) { this . athleteSettingsConsistency = new Subject < boolean > ( ) ; } returns { Promise < SyncedActivityModel [ ] > } public fetch ( ) { return ( < Promise < SyncedActivityModel [ ] > > this . activityDao . fetch ( ) ) ; } param { SyncedActivityModel [ ] } syncedActivityModels returns { Promise < SyncedActivityModel [ ] > } public save ( syncedActivityModels ) { return ( < Promise < SyncedActivityModel [ ] > > this . activityDao . save ( syncedActivityModels ) ) ; } public clear ( ) { return this . activityDao . clear ( ) ; } param { number [ ] } activitiesToDelete returns { Promise < SyncedActivityModel [ ] > } public removeByIds ( activitiesToDelete ) { return this . activityDao . removeByIds ( activitiesToDelete ) ; } returns { Promise < boolean > } public isAthleteSettingsConsistent ( ) { return this . athleteSnapshotResolverService . update ( ) . then ( ( ) => { return this . activityDao . fetch ( ) ; } ) . then ( ( syncedActivityModels ) => { let isCompliant = true ; _ . forEachRight ( syncedActivityModels , ( syncedActivityModel ) => { const athleteModelFound = this . athleteSnapshotResolverService . resolve ( new Date ( syncedActivityModel . start_time ) ) ; if ( ! athleteModelFound . equals ( syncedActivityModel . athleteSnapshot ) ) { isCompliant = false ; return false ; } } ) ; return Promise . resolve ( isCompliant ) ; } ) ; } /**
     * Ask for athleteSettings consistency check and notify athleteSettingsConsistency subscribers of consistency
     */ public verifyConsistencyWithAthleteSettings ( ) { this . logger . debug ( "checking athlete settings consistency" ) ; this . isAthleteSettingsConsistent ( ) . then ( isConsistent => { this . athleteSettingsConsistency . next ( isConsistent ) ; this . logger . debug ( "Athlete settings consistent: " + isConsistent ) ; } , error => this . athleteSettingsConsistency . error ( error ) ) ; } /**
     * Provide local synced activity ids which are not compliant with current athlete settings
     */ public nonConsistentActivitiesWithAthleteSettings ( ) { return this . athleteSnapshotResolverService . update ( ) . then ( ( ) => { return this . activityDao . fetch ( ) ; } ) . then ( ( syncedActivityModels ) => { const nonConsistentIds = [ ] ; _ . forEachRight ( syncedActivityModels , ( syncedActivityModel ) => { const athleteModelFound = this . athleteSnapshotResolverService . resolve ( new Date ( syncedActivityModel . start_time ) ) ; if ( ! athleteModelFound . equals ( syncedActivityModel . athleteSnapshot ) ) { nonConsistentIds . push ( syncedActivityModel . id ) ; } } ) ; return Promise . resolve ( nonConsistentIds ) ; } ) ; } }