UNPKG

consumerportal

Version:

mydna Custimised for you

277 lines (255 loc) 11.5 kB
module services { import IModalService = app.IModalService; export interface IPatientService { cases(force?: boolean): angular.IPromise<IPatientCase[]> genotypes(values?: IPatientGenoType[]): angular.IPromise<IPatientGenoType[]> download(incidentId: string, reportNo: string): angular.IPromise<void> getReportNavigation(): angular.IPromise<ReportNavigationCard[]> } export interface IPatientCase { CreateDate: string IncidentId: string OrderNumber: string ProductName: string ReferringClinician: string ReportStatus: string ReportType: string Status: string TestName: string TestFullName: string Tga: boolean Action: string } export interface IPatientGenoType { Color: string Gene: string LongDescription: string ShortPhonetype: string } export class ReportNavigationCard { public open: Function; constructor( public label: string, public type: string, public link: string, public report: string, public status: string ) { } } class PatientService implements IPatientService { static $inject = [ '$q', 'apiSrvc', 'mydnaApis', 'errorHandlerSrvc', '$rootScope', '$location', 'modalService' ]; storedCases: IPatientCase[]; storedMedications: IPatientMedication[]; storedGenotypes: IPatientGenoType[]; constructor( private $q: angular.IQService, private service: apiSrvc.IApiService, private api: ImyDNAApis, private errorService: errorHandlerSrvc.IErrorHandlerService, private $rootScope: angular.IRootScopeService, private $location: angular.ILocationService, private modalService: IModalService ) { $rootScope.$on('changeUser', (ev: any, user: IUser) => { if (!user) { this.storedCases = null; this.storedMedications = null; this.storedGenotypes = null; } }); } cases(forceRefresh: boolean = false): angular.IPromise<IPatientCase[]> { let defer = this.$q.defer(); let check: string = JSON.stringify(this.storedCases); this.get(this.api.PatientCases, 'storedCases', undefined, forceRefresh).then((values: IPatientCase[]) => { if (values) { this.storedCases = values = (values || []).filter((c: IPatientCase) => {return c.Status !== 'Canceled'}); values.forEach((pc: IPatientCase) => { if (pc.ReportStatus === 'Report Available') { pc.Action = pc.TestName === 'Fitness Report' || pc.TestName === 'Alcohol Report' ? 'view' : 'download'; } else if (pc.ReportStatus === 'Report Available to Health Professional') { if (pc.Tga) { pc.Action = 'tga'; } else { if (pc.TestName === 'Fitness Report' || pc.TestFullName === 'Alcohol Report') { pc.ReportStatus = 'Testing in Progress'; } pc.Action = 'waiting'; } } else { pc.Action = 'waiting'; } }); } if (check !== JSON.stringify(values)) { this.$rootScope.$broadcast('patientCasesUpdated', values); } defer.resolve(values); }); return defer.promise; } medications(values?: IPatientMedication[]): angular.IPromise<IPatientMedication[]> { if (values) { this.storedMedications = values; } return this.get(this.api.PatientMedications, 'storedMedications', [], false); } genotypes(values?: IPatientGenoType[]): angular.IPromise<IPatientGenoType[]> { if (values) { this.storedGenotypes = values; } return this.get(this.api.PatientGenotypes, 'storedGenotypes', [], false); } private get(url: string, store: string, defaultValue: any, forceReload: boolean): angular.IPromise<any[]> { let defer = this.$q.defer(); if (this[store] && !forceReload) { defer.resolve(this[store]); } else { this.service.get_request_params(url).then((values: any[]) => { this[store] = values || defaultValue; defer.resolve(this[store]); // always return something }, (err:any) => { this.errorService.errorHandler(err, "debug", ""); defer.resolve(defaultValue); // always return something }); } return defer.promise; } download(IncidentId: string, orderNumber: string): angular.IPromise<void> { let defer: angular.IDeferred<any> = this.$q.defer(); this.get( this.api.PatientCaseReport + '?caseNumber=' + IncidentId, null, null, true).then((data: any) => { if (data) { let blob = this.base64toBlob(data.Content, 'application/pdf', 512); saveAs(blob, orderNumber + ".pdf"); defer.resolve(); } else { defer.reject() } }); return defer.promise; } base64toBlob(b64Data, contentType, sliceSize) { contentType = contentType || ''; sliceSize = sliceSize || 512; let byteCharacters = atob(b64Data); let byteArrays = []; for (let offset = 0; offset < byteCharacters.length; offset += sliceSize) { let slice = byteCharacters.slice(offset, offset + sliceSize); let byteNumbers = new Array(slice.length); for (let i = 0; i < slice.length; i++) { byteNumbers[i] = slice.charCodeAt(i); } let byteArray = new Uint8Array(byteNumbers); byteArrays.push(byteArray); } return new Blob(byteArrays, { type: contentType }); } openReport(report: ReportNavigationCard) { console.log(report); if (report.status === 'Report Available') { this.$location.path(report.link); } else if (report.status === 'Report Available to Health Professional') { if (report.type === 'diet' || report.type === 'alcohol') { this.modalService.add('diettga'); } else { this.modalService.add('modal-notification', { title: 'Report Available to Healthcare Professional', content: `Please make an appointment to see your healthcare professional so they can deliver the results of your report. Once this has occurred you will be able to see your results online.` }); } } else if (report.status === 'Not Approved') { this.modalService.add('modal-notification.warning', { title: 'Access Not Approved', content: 'This report has not been released as approval was not given.' }); } else { this.modalService.add('modal-notification', { title: 'Your Sample is Currently Being Processed in the Lab', content: `Once this is complete and our clinical team has interpreted the findings, your report will be available for you to view. We’ll notify you as soon as it’s ready.` }); } } getReportNavigation(): angular.IPromise<ReportNavigationCard[]> { let defer = this.$q.defer(); this.cases().then((values:IPatientCase[]) => { let reports: ReportNavigationCard[] = []; let types: any = { meds: { label: 'Medication', type: 'meds', tests: [ 'Medications(Full)-PersonalisedMedicationReport', 'Medications-Pain', 'Medications-Mental Health', 'Medications-Gastro', 'Medications-Cardio', 'DNA Dose' ], link: '/meds/my' }, chemistWarehouse: { label: 'Weight Management', type: 'diet', tests: ['WeightManagementReport'], link: '/diet/WeightManagementReport' }, diet: { label: 'Diet', type: 'diet', tests: ['WellnessNutrition-DietReport'], link: '/diet/basic' }, dietv2: { label: 'Diet', type: 'diet', tests: ['WellnessNutrition-DietV2'], link: '/diet/WellnessNutrition-DietV2' }, fitness: { label: 'Fitness', type: 'fitness', tests: ['Fitness Report'], link: '/sports' }, alcohol: { label: 'Alcohol', type: 'alcohol', tests: ['Alcohol Report'], link: '/alcohol' } }; Object.keys(types).forEach((key: string) => { let obj: any = types[key]; let match: IPatientCase[] = values.filter((pc: IPatientCase) => { return obj.tests.indexOf(pc.TestName) > -1}); let states: string[] = obj.validStates || []; if (match.length > 0) { let rnc: ReportNavigationCard = new ReportNavigationCard(obj.label, obj.type, obj.link, match[0].TestName, match[0].ReportStatus); rnc.open = () => { this.openReport(rnc); }; reports.push(rnc); } }); defer.resolve(reports); }); return defer.promise; } } angular .module('app') .service('patientService', PatientService); }