Skip to content
This repository has been archived by the owner on Jan 9, 2023. It is now read-only.

Commit

Permalink
Modified medication model (#1065)
Browse files Browse the repository at this point in the history
* Modified medication model

* Requested changes

* Deleted extra space
  • Loading branch information
hsorellana authored and jkleinsc committed May 18, 2017
1 parent 393127e commit f474589
Showing 1 changed file with 35 additions and 41 deletions.
76 changes: 35 additions & 41 deletions app/models/medication.js
Expand Up @@ -5,62 +5,55 @@ import DateFormat from 'hospitalrun/mixins/date-format';
import Ember from 'ember';
import MedicationDetails from 'hospitalrun/mixins/medication-details';

const { computed, get } = Ember;

export default AbstractModel.extend(CanEditRequested, DateFormat, MedicationDetails, {
inventoryItem: DS.belongsTo('inventory', {
async: true
}),
// Attributes
notes: DS.attr('string'),
patient: DS.belongsTo('patient', {
async: false
}),
prescription: DS.attr('string'),
prescriptionDate: DS.attr('date'),
quantity: DS.attr('number'),
refills: DS.attr('number'),
requestedDate: DS.attr('date'),
requestedBy: DS.attr('string'),
status: DS.attr('string'),
visit: DS.belongsTo('visit', {
async: false
}),

isRequested: function() {
let status = this.get('status');
return (status === 'Requested');
}.property('status'),
// Associations
inventoryItem: DS.belongsTo('inventory', { async: true }),
patient: DS.belongsTo('patient', { async: false }),
visit: DS.belongsTo('visit', { async: false }),

medicationName: function() {
isRequested: computed('status', function() {
return get(this, 'status') === 'Requested';
}),

medicationName: computed('medicationTitle', 'inventoryItem', function() {
return this.getMedicationName('inventoryItem');
}.property('medicationTitle', 'inventoryItem'),
}),

medicationPrice: function() {
medicationPrice: computed('priceOfMedication', 'inventoryItem', function() {
return this.getMedicationPrice('inventoryItem');
}.property('priceOfMedication', 'inventoryItem'),
}),

prescriptionDateAsTime: function() {
return this.dateToTime(this.get('prescriptionDate'));
}.property('prescriptionDate'),
prescriptionDateAsTime: computed('prescriptionDate', function() {
return this.dateToTime(get(this, 'prescriptionDate'));
}),

requestedDateAsTime: function() {
return this.dateToTime(this.get('requestedDate'));
}.property('requestedDate'),
requestedDateAsTime: computed('requestedDate', function() {
return this.dateToTime(get(this, 'requestedDate'));
}),

validations: {
prescription: {
acceptance: {
accept: true,
if(object) {
if (!object.get('hasDirtyAttributes') || object.get('isFulfilling')) {
return false;
}
let prescription = object.get('prescription');
let quantity = object.get('quantity');
if (Ember.isEmpty(prescription) && Ember.isEmpty(quantity)) {
// force validation to fail
return true;
} else {
if (!get(object, 'hasDirtyAttributes') || get(object, 'isFulfilling')) {
return false;
}
let prescription = get(object, 'prescription');
let quantity = get(object, 'quantity');
return Ember.isEmpty(prescription) && Ember.isEmpty(quantity);
},
message: 'Please enter a prescription or a quantity'
}
Expand All @@ -70,11 +63,11 @@ export default AbstractModel.extend(CanEditRequested, DateFormat, MedicationDeta
acceptance: {
accept: true,
if(object) {
if (!object.get('hasDirtyAttributes') || !object.get('isNew')) {
if (!get(object, 'hasDirtyAttributes') || !get(object, 'isNew')) {
return false;
}
let itemName = object.get('inventoryItem.name');
let itemTypeAhead = object.get('inventoryItemTypeAhead');
let itemName = get(object, 'inventoryItem.name');
let itemTypeAhead = get(object, 'inventoryItemTypeAhead');
if (Ember.isEmpty(itemName) || Ember.isEmpty(itemTypeAhead)) {
// force validation to fail
return true;
Expand All @@ -94,7 +87,7 @@ export default AbstractModel.extend(CanEditRequested, DateFormat, MedicationDeta
patientTypeAhead: {
presence: {
if(object) {
return (object.get('selectPatient'));
return get(object, 'selectPatient');
}
}
},
Expand All @@ -109,27 +102,28 @@ export default AbstractModel.extend(CanEditRequested, DateFormat, MedicationDeta
},
presence: {
if(object) {
let isFulfilling = object.get('isFulfilling');
return isFulfilling;
return get(object, 'isFulfilling');
}
},
acceptance: {
accept: true,
if(object) {
let isFulfilling = object.get('isFulfilling');
let requestQuantity = parseInt(object.get('quantity'));
let isFulfilling = get(object, 'isFulfilling');
let requestQuantity = parseInt(get(object, 'quantity'));
let quantityToCompare = null;

if (!isFulfilling) {
// no validation needed when not fulfilling
return false;
} else {
quantityToCompare = object.get('inventoryItem.quantity');
}

if (requestQuantity > quantityToCompare) {
// force validation to fail
return true;
} else {
// There is enough quantity on hand.
// There is enough quantity on hand
return false;
}
},
Expand Down

0 comments on commit f474589

Please sign in to comment.