UNPKG

consumerportal

Version:

mydna Custimised for you

249 lines (218 loc) 8.63 kB
module services { export interface IAlcoholService { details(ADH1B?: string, ALDH2?: string): angular.IPromise<any> navigation(): any[] getMetrics(): IAlcoholMetric getConsideration(): IAlcoholConsideration getReport(): IAlcoholReport } export interface IAlcoholDetails { Phenotype: string Profile: IAlcoholDetailsProfile Genes: IAlcoholDetailsGene Results: IAlcoholResult[] } export interface IAlcoholMetric { ADH1B: string ALDH2: string Sensitivity: string SideEffects: string Dependence: string Binge: string Hangover: string Light: string Heavy: string Asian: any } export interface IAlcoholConsideration { ADH1B: string ALDH2: string Recommendation: string Dashboard: string Light: number Heavy: number Asian?: any } export interface IAlcoholReport { ADH1B: string ALDH2: string Outcome: string Phenotype: string Interpretation: string Recommendation: string } export interface IAlcoholResult { title: string gene: string description: string notes: string tabs: IAlcoholResultTab[] tab: string result: string color: string } export interface IAlcoholResultTab { value: string label: string pc: string visPC: number content: IAlcoholResultTabContent[] } export interface IAlcoholResultTabContent { title: string content: string } export interface IAlcoholDetailsProfile { Sensitivity: number SideEffects: number Dependence: number BingeDrinking: number Hangover: number } export interface IAlcoholDetailsGene { ADH1B: string ALDH2: string } class AlcoholService implements IAlcoholService { static $inject = [ '$q', 'apiSrvc', 'mydnaApis', 'errorHandlerSrvc', '$rootScope', ]; private storeDetails: IAlcoholDetails; private storeMetrics: IAlcoholMetric[]; private storeConsiderations: IAlcoholConsideration[]; private storeReports: IAlcoholReport[]; constructor( private $q: angular.IQService, private service: apiSrvc.IApiService, private api: ImyDNAApis, private errorService: errorHandlerSrvc.IErrorHandlerService, private $rootScope: angular.IRootScopeService, ) { $rootScope.$on('changeUser', (ev: any, user: IUser) => { if (!user) { this.storeDetails = null; this.loading = false; this.deferred = null; } }); } private deferred: angular.IDeferred<any>; private loading: boolean = false; private detailsCount: number = -1; private DETAILS_COMPLETE: number = 4; 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; } } navigation(): any[] { return [ {value: '#/alcohol', label: 'Dashboard', key: 'dashboard'}, {value: '#/alcohol/implications', label: 'Recommendations', title: 'Health Recommendations', key: 'implications'}, {value: '#/alcohol/results', label: 'Results', title: 'Gene Results', key: 'results'}, {value: '#/alcohol/science', label: 'Science', key: 'science'} ] } details(ADH1B?: string, ALDH2?: string): 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.Alcohol, null, {}, false).then((value: IAlcoholDetails) => { this.detailsCount = 0; if (ADH1B) { value.Genes.ADH1B = ADH1B; } if (ALDH2) { value.Genes.ALDH2 = ALDH2; } this.storeDetails = value; this.data('results').then((values: IAlcoholResult[]) => { values.forEach((result: IAlcoholResult) => { result.result = this.storeDetails.Genes[result.gene]; }); this.storeDetails.Results = values; this.detailsResolve(); }); this.data('metrics').then((values: IAlcoholMetric[]) => { this.storeMetrics = values; this.detailsResolve(); }); this.data('implications').then((values: IAlcoholConsideration[]) => { this.storeConsiderations = values; this.detailsResolve(); }); this.data('report').then((values: IAlcoholReport[]) => { this.storeReports = values; this.detailsResolve(); }) }); } else { this.detailsResolve(false); } } return defer.promise; } getMetrics(): IAlcoholMetric { return this.storeMetrics.filter((m: IAlcoholMetric) => { return m.ADH1B === this.storeDetails.Genes.ADH1B && m.ALDH2 === this.storeDetails.Genes.ALDH2 })[0]; } getConsideration(): IAlcoholConsideration { return this.storeConsiderations.filter((m: IAlcoholConsideration) => { return m.ADH1B === this.storeDetails.Genes.ADH1B && m.ALDH2 === this.storeDetails.Genes.ALDH2 })[0]; } getReport(): IAlcoholReport { return this.storeReports.filter((r: IAlcoholReport) => { return r.ADH1B === this.storeDetails.Genes.ADH1B && r.ALDH2 === this.storeDetails.Genes.ALDH2 })[0]; } private data(key: string): angular.IPromise<any> { let defer = this.$q.defer(); this.get_local('data/alcohol/' + 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('alcoholService', AlcoholService); }