UNPKG

consumerportal

Version:

mydna Custimised for you

184 lines (160 loc) 6.76 kB
module services { export interface IMedicationService { search(input: string): angular.IPromise<IMedicationSearchResult[]> medications(values?: IPatientMedication[]): angular.IPromise<IPatientMedication[]> medication(value: string): angular.IPromise<IPatientMedication> addMedication(id: string): angular.IPromise<any> removeMedication(id: any, index: number): angular.IPromise<any> future(): angular.IPromise<IFutureMedication> navigation(): any[] } export interface IMedicationSearchResult { displayName: string genericName: string id: string tradename: string } export interface IPatientMedication { AlertType: string Comment: string Id: string Medication: IPatientMedicationMedication PredictedMetabolism: string Recommendation: string } export interface IPatientMedicationMedication { CategoryName: string CommonUsage: string DisplayName: string GenericName: string Id: string PGXClassification: string TradeName: string } export interface IFutureMedication { Categories: IFutureMedicationCategory[] } export interface IFutureMedicationCategory { Name: string Recomendations: IFutureMedicationCategoryRecommendation SubCategories: IFutureMedicationCategory[] } export interface IFutureMedicationCategoryRecommendation { AlertType: string Medications: IFutureMedicationCategoryRecommendationMedication[] RecommendationBody: string RecommendationName: string RecommendationTitle: string } export interface IFutureMedicationCategoryRecommendationMedication { DisplayName: string MedicationId: string } class MedicationService implements IMedicationService { static $inject = [ '$q', 'apiSrvc', 'mydnaApis', 'errorHandlerSrvc', '$rootScope' ]; storedMedications: IPatientMedication[]; storedFutureMedications: IFutureMedication[]; storedIndividualMeds: any = {}; constructor( private $q: angular.IQService, private service: apiSrvc.IApiService, private api: ImyDNAApis, private errorService: errorHandlerSrvc.IErrorHandlerService, private $rootScope: angular.IRootScopeService ) { $rootScope.$on('changeUser', () => { this.storedMedications = null; this.storedFutureMedications = null; }); } navigation(): any[] { return [ {value: '#/meds', label: 'Medications', title: 'Medication Impact Analysis', key: 'medications'}, {value: '#/meds/profile', label: 'Results', key: 'results'}, {value: '#/meds/report', label: 'Report', key: 'report'} ] } search(input: string): angular.IPromise<IMedicationSearchResult[]> { return this.get(this.api.Medicines + '?MedicationSearchQuery=' + input, null, [], false); } medications(values?: IPatientMedication[]): angular.IPromise<IPatientMedication[]> { if (values) { this.storedMedications = values; } return this.get(this.api.PatientMedications, 'storedMedications', [], false).then((values: IPatientMedication[]) => { if (values.length > 1 && values[0].Id == '00000000-0000-0000-0000-000000000000') { values.splice(0, 1); } return values; }); } medication(id: string): angular.IPromise<IPatientMedication> { let defer = this.$q.defer(); if (this.storedIndividualMeds[id]) { defer.resolve(this.storedIndividualMeds[id]) } else { this.get(this.api.PatientMedications + '?MedicationId=' + id, null, null, false).then((value: IPatientMedication) => { this.storedIndividualMeds[id] = value; defer.resolve(value); }); } return defer.promise; } addMedication(value: string) { return this.service.post_request(this.api.PatientMedications, {Medication: {id: value}}).then((data: any) => { this.storedMedications.push(data.data); if (this.storedMedications.length > 1 && this.storedMedications[0].Id === "00000000-0000-0000-0000-000000000000") { this.storedMedications.splice(0, 1); } return this.storedMedications; }, (err: any) => { if(err.status !== 409){ this.errorService.errorHandler(err, 'debug', ''); } return err; }); } removeMedication(id: string, index: number) { return this.service.delete_request(this.api.PatientMedications + '/' + id, "").then((data: any) => { this.storedMedications.splice(index, 1); return this.storedMedications; }, err => { this.errorService.errorHandler(err, "debug", ""); return err }); } future(values?: IFutureMedication[]): angular.IPromise<IFutureMedication> { if (values) { this.storedFutureMedications = values; } return this.get(this.api.FutureMedications, 'storedFutureMedications', null, false); } 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; } } angular .module('app') .service('medicationService', MedicationService); }