consumerportal
Version:
mydna Custimised for you
172 lines (150 loc) • 5.54 kB
text/typescript
/// <reference path="../../includes.ts" />
module medsMyCtrl{
import IPatientService = services.IPatientService;
import IMedicationService = services.IMedicationService;
import IMedicationSearchResult = services.IMedicationSearchResult;
import IPatientMedication = services.IPatientMedication;
import IFutureMedication = services.IFutureMedication;
import IModalService = app.IModalService;
interface IMedsMyController {
getPatientMedications(): any;
showDetails(key: any): any;
medsSearch(): any;
addMedicine(key: any): any;
removeMedicine(item: IPatientMedication, key: any): void;
getFutureMedications(): any;
goto(cat:any):void
}
interface IMedsMyScope extends angular.IScope{
medicinesrch: any;
$sce: angular.ISCEService;
}
class MedsMyController implements IMedsMyController {
static $inject = [
'apiSrvc',
'errorHandlerSrvc',
'$scope',
'mydnaApis',
'$sce',
'appMemorySrvc',
'$location',
'patientService',
'medicationService',
'modalService'
];
PatientMedications: any;
LoadingInProgress = false;
medicinesrch: any;
Medicines: any;
keyToRemove: any;
Medication: any;
FutureMedications: IFutureMedication;
Disclaimer: string;
defaultMedicationID: string;
selectedMedicine: any;
medicationsLoaded = false;
constructor(
private apiSrvc: apiSrvc.IApiService,
private errorHandlerSrvc: errorHandlerSrvc.IErrorHandlerService,
private $scope: IMedsMyScope,
private mydnaApis: ImyDNAApis,
private $sce: angular.ISCEService,
private appMemorySrvc: appMemorySrvc.IAppMemoryService,
private $location: angular.ILocationService,
private patientService: IPatientService,
private medicationService: IMedicationService,
private modalService: IModalService
) {
const vm = this;
vm.Disclaimer = "";
vm.Medication = {};
vm.medicinesrch = false;
vm.defaultMedicationID = "00000000-0000-0000-0000-000000000000";
this.getPatientMedications();
this.getFutureMedications();
}
medsSearch() {
if (this.$scope.medicinesrch.length > 2) {
this.LoadingInProgress = true;
this.medicationService.search(this.$scope.medicinesrch).then((values: IMedicationSearchResult[]) => {
this.Medicines = values;
this.LoadingInProgress = false;
});
}
}
addMedicine(itm) {
this.$scope.$emit("disable-on-save-on");
this.Medication.Medication = {};
this.Medication.Medication.id = itm.id;
this.apiSrvc.post_request(
this.mydnaApis.PatientMedications
, this.Medication)
.then(data => {
this.PatientMedications.push(data.data);
if (this.PatientMedications.length > 1 && this.PatientMedications[0].Id == this.defaultMedicationID) {
this.PatientMedications.splice(0, 1);
}
this.appMemorySrvc.setPatientMedicationsToMemory(this.PatientMedications);
this.$scope.$emit("disable-on-save-off");
}, err => {
if(err.status == 409){
this.modalService.add('modal-notification', {
title: 'Already Exists',
content: `Medicine ${itm.displayName} already exists. You cannot add it again.`
}).then((index: number) => {
console.log(index);
})
}
else{
this.errorHandlerSrvc.errorHandler(err, 'debug', '');
}
this.$scope.$emit("disable-on-save-off");
});
this.$scope.medicinesrch = null;
}
removeMedicine(item: IPatientMedication, medIndex: number) {
this.modalService.add('modal-notification', {
title: 'Are you sure?',
content: `This will remove <b>${item.Medication.DisplayName}</b> from your medications.`,
buttons: ['Ok', {label: 'Cancel', className: 'secondary'}]
}).then((index: number) => {
if (index === 0) {
this.$scope.$emit("disable-on-save-on");
this.apiSrvc.delete_request(this.mydnaApis.PatientMedications + '/' + item.Id, "").then((data: any) => {
this.PatientMedications.splice(medIndex, 1);
this.medicationService.medications(this.PatientMedications);
}, err => {
this.errorHandlerSrvc.errorHandler(err, "debug", "");
});
}
})
}
getPatientMedications() {
this.medicationService.medications().then((data: IPatientMedication[]) => {
if (data.length > 1 && data[0].Id == this.defaultMedicationID) {
data.splice(0, 1);
}
this.PatientMedications = data;
this.Disclaimer = this.mydnaApis.MedsDisclaimer;
this.medicationsLoaded = true;
});
}
showDetails(item: any){
this.modalService.add('modal-notification', {
title: item.PredictedMetabolism,
content: item.Comment,
});
}
getFutureMedications() {
this.medicationService.future().then((data: IFutureMedication) => {
this.FutureMedications = data;
this.Disclaimer = this.mydnaApis.MedsDisclaimer;
});
}
goto(cat) {
this.appMemorySrvc.setSelectedMedicationCat(cat);
this.$location.path('meds/browser');
}
}
angular.module('app').controller('medsMyCtrl', MedsMyController);
}