UNPKG

consumerportal

Version:

mydna Custimised for you

361 lines (311 loc) 13.2 kB
module services { import IAppStorage = app.IAppStorage; export interface ISportsService { details(): angular.IPromise<any> updateMetrics(goal: string, level: string): angular.IPromise<any> navigation(): any[] diaryForCurrentMetrics(): SportsWorkoutCombined[] warmupForCurrentMetrics(): SportsWorkoutCombined cooldownForCurrentMetrics(): SportsWorkoutCombined } export interface ISportsResult { title: string gene: string description: string notes: string tabs: ISportsResultTab[] tab: string result: string color: string } export interface ISportsResultTab { value: string label: string pc: string visPC: number content: ISportsResultTabContent[] } export interface ISportsResultTabContent { title: string content: string } export interface ISportsDetails { Phenotype: string PowerEndurance: string Profile: ISportsDetailsProfile Genes: ISportsDetailsGene goal: string level: string results: ISportsResult[] } export interface ISportsDetailsProfile { Stamina: number Injury: number Recovery: number Power: number Endurance: number } export interface ISportsDetailsGene { ACTN3: string AGT: string AMPD1: string PPARGC1A: string COL1A1: string COL5A1: string IL6: string } export interface ISportsDiary { goal: boolean phenotype: string level: string plans: ISportsDiaryPlan[] } export interface ISportsDiaryPlan { workout: string level: string } export interface ISportsWorkout { key: string name?: string sessions?: string[][] reps?: number; } export interface ISportsWorkoutLevel { keys: string[] level: string description: string notes: string[] reps: number totalreps: number sets: number rest: number totalrest: number difficulty: number time: string intensity: string } export class SportsWorkoutExercise { public key: string; public name: string; public video: string; public description: string; public image: string; } export class SportsWorkoutCombined { public key: string; public name: string; public reps: number; public sets: number; public difficulty: number; public rest: number; public sessions: SportsWorkoutExercise[][] = []; public description: string; public notes: string[]; public totalreps: number; public totalrest: number; public time: string; public intensity: string; } class SportsService implements ISportsService { static $inject = [ '$q', 'apiSrvc', 'userService', 'mydnaApis', 'errorHandlerSrvc', '$rootScope', '$routeParams', 'appLocalStorage' ]; private storeDetails: ISportsDetails; private storeDiary: ISportsDiary[]; private storeWorkouts: any[]; private storeWorkoutLevels: any[]; private storeExercises: SportsWorkoutExercise[]; private user: IUser; constructor( private $q: angular.IQService, private service: apiSrvc.IApiService, private userService: IUserService, private api: ImyDNAApis, private errorService: errorHandlerSrvc.IErrorHandlerService, private $rootScope: angular.IRootScopeService, private $routeParams: angular.route.IRouteParamsService, private storage: IAppStorage ) { $rootScope.$on('changeUser', (ev: any, user: any) => { this.storeDetails = this.storeWorkoutLevels = this.storeExercises = this.storeWorkouts = null; this.loading = false; this.deferred = null; }); } navigation(): any[] { return [ {value: '#/sports', label: 'Dashboard', key: 'dashboard'}, {value: '#/sports/training', label: 'Training Program', key: 'training'}, {value: '#/sports/results', label: 'Results', key: 'results'}, {value: '#/sports/science', label: 'Science', key: 'science'} ] } private deferred: angular.IDeferred<any>; private loading: boolean = false; private detailsCount: number = -1; private DETAILS_COMPLETE: number = 6; private detailsResolve(inc: boolean = true) { // removes hierarchy of data calls in details() if (inc) { this.detailsCount++; } if (this.detailsCount === this.DETAILS_COMPLETE) { this.loading = false; this.deferred.resolve(this.storeDetails); this.deferred = null; } } details(): angular.IPromise<any> { let defer: angular.IDeferred<any> = this.deferred = this.$q.defer(); if (!this.loading) { if (!this.storeDetails) { this.loading = true; this.get(this.api.Sports, null, {}, false).then((value: ISportsDetails) => { value.Profile.Endurance = 100 - value.Profile.Power; this.detailsCount = 0; this.storeDetails = value; this.userService.user().then((user: IUser) => { this.user = user; this.storeDetails.goal = this.storage.getItem(user.Username + '-sports-goal') || 'strength'; this.storeDetails.level = this.storage.getItem(user.Username + '-sports-level') || 'beginner'; this.detailsResolve(); }); this.data('diary').then((value: any[]) => { this.storeDiary = value; this.detailsResolve(); }); this.data('workouts').then((workouts: any[]) => { this.storeWorkouts = workouts; this.detailsResolve(); }); this.data('workoutLevels').then((levels: any[]) => { this.storeWorkoutLevels = levels; this.detailsResolve(); }); this.data('exercises').then((exercises: SportsWorkoutExercise[]) => { this.storeExercises = exercises; this.detailsResolve(); }); this.data('results').then((values: ISportsResult[]) => { values.forEach((result: ISportsResult) => { result.result = this.storeDetails.Genes[result.gene]; }); this.storeDetails.results = values; this.detailsResolve(); }); }); } else { this.detailsResolve(false); } } return defer.promise; } public updateMetrics(goal: string, level: string) { this.storeDetails.goal = goal; this.storeDetails.level = level; this.storage.setItem(this.user.Username + '-sports-goal', goal); this.storage.setItem(this.user.Username + '-sports-level', level); let defer = this.$q.defer(); defer.resolve(); return defer.promise; } private populateExercises(sessions: string[][], replace?: any) { let popSessions: SportsWorkoutExercise[][] = []; sessions.forEach((session: string[]) => { let cSession: SportsWorkoutExercise[] = []; session.forEach((exercise: string) => { let ex: SportsWorkoutExercise = this.storeExercises.filter((e: SportsWorkoutExercise) => { return e.key === exercise })[0]; let newEx: SportsWorkoutExercise = new SportsWorkoutExercise(); for(let k in ex) newEx[k]=ex[k]; if (replace) { Object.keys(replace).forEach((key: string) => { newEx.name = newEx.name.replace(new RegExp('{{' + key + '}}', 'g'), replace[key]); }); } cSession.push(newEx); }); popSessions.push(cSession); }); return popSessions; } private createCombinedWorkout(name: string): SportsWorkoutCombined { let combined: SportsWorkoutCombined = new SportsWorkoutCombined(); let workout: ISportsWorkout = this.storeWorkouts.filter((workout: ISportsWorkout) => { return name === workout.key })[0]; combined.key = name; combined.name = workout.name; let replace: any = (workout.reps) ? { reps: 'x ' + workout.reps} : null; combined.sessions = this.populateExercises(workout.sessions, replace); return combined; } diaryForCurrentMetrics() { let plans: ISportsDiaryPlan[] = this.storeDiary.filter((d: any) => { return d.goal === this.storeDetails.goal && d.level === this.storeDetails.level && d.phenotype === this.storeDetails.Phenotype })[0].plans; let diary: SportsWorkoutCombined[] = []; plans.forEach((plan: ISportsDiaryPlan) => { let combined: SportsWorkoutCombined = this.createCombinedWorkout(plan.workout); let levels: ISportsWorkoutLevel[] = this.storeWorkoutLevels.filter((l: ISportsWorkoutLevel) => { return l.keys.indexOf(combined.key) > -1 }); if (levels.length > 1) { levels = levels.filter((l: ISportsWorkoutLevel) => { return l.level.indexOf(this.storeDetails.level) > -1 }); } ['sets', 'reps', 'rest', 'description', 'difficulty', 'notes', 'totalreps', 'totalrest', 'time', 'intensity'].forEach((key: string) => { combined[key] = levels[0][key]; }); diary.push(combined); }); return diary; } cooldownForCurrentMetrics() { let type: string = this.storeDetails.Genes.COL1A1 === 'TT' ? 'good' : 'bad'; return this.createCombinedWorkout('cooldown' + type); } warmupForCurrentMetrics() { let type: string = this.storeDetails.Genes.COL1A1 === 'TT' ? 'good' : 'bad'; return this.createCombinedWorkout('warmup' + type); } private data(key: string): angular.IPromise<any> { let defer = this.$q.defer(); this.get_local('data/sports/' + key.replace(/[, +]/g, '').toLowerCase() + '.json').then((val: any) => { defer.resolve(val); }); return defer.promise; } private get(url: string, store: string, defaultValue: any, forceReload: boolean): angular.IPromise<any> { let defer = this.$q.defer(); if (store && this[store] && !forceReload) { defer.resolve(this[store]); } else { this.service.get_request_params(url).then((values: any[]) => { if (store) { this[store] = values || defaultValue; } defer.resolve(this[store] || values || defaultValue); }, (err:any) => { this.errorService.errorHandler(err, "debug", ""); defer.resolve([]); }); } return defer.promise; } private get_local(url: any) { let defer = this.$q.defer(); this.service.get_request_params_otherResource(location.href.replace(location.hash, '') + url + '?<!--MYDNA-UUID-->').then((value: any) => { defer.resolve(value); }, (err:any) => { this.errorService.errorHandler(err, "debug", ""); defer.resolve({}); }); return defer.promise; } } angular .module('app') .service('sportsService', SportsService); }