This repository has been archived by the owner on Jan 9, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
medication.js
140 lines (125 loc) · 4.06 KB
/
medication.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import AbstractModel from 'hospitalrun/models/abstract';
import CanEditRequested from 'hospitalrun/mixins/can-edit-requested';
import DS from 'ember-data';
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, {
// Attributes
notes: DS.attr('string'),
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'),
// Associations
inventoryItem: DS.belongsTo('inventory', { async: true }),
patient: DS.belongsTo('patient', { async: false }),
visit: DS.belongsTo('visit', { async: false }),
isRequested: computed('status', function() {
return get(this, 'status') === 'Requested';
}),
medicationName: computed('medicationTitle', 'inventoryItem', function() {
return this.getMedicationName('inventoryItem');
}),
medicationPrice: computed('priceOfMedication', 'inventoryItem', function() {
return this.getMedicationPrice('inventoryItem');
}),
prescriptionDateAsTime: computed('prescriptionDate', function() {
return this.dateToTime(get(this, 'prescriptionDate'));
}),
requestedDateAsTime: computed('requestedDate', function() {
return this.dateToTime(get(this, 'requestedDate'));
}),
validations: {
prescription: {
acceptance: {
accept: true,
if(object) {
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'
}
},
inventoryItemTypeAhead: {
acceptance: {
accept: true,
if(object) {
if (!get(object, 'hasDirtyAttributes') || !get(object, 'isNew')) {
return false;
}
let itemName = get(object, 'inventoryItem.name');
let itemTypeAhead = get(object, 'inventoryItemTypeAhead');
if (Ember.isEmpty(itemName) || Ember.isEmpty(itemTypeAhead)) {
// force validation to fail
return true;
} else {
let typeAheadName = itemTypeAhead.substr(0, itemName.length);
if (itemName !== typeAheadName) {
return true;
}
}
// Inventory item is properly selected; don't do any further validation
return false;
},
message: 'Please select a valid medication'
}
},
patientTypeAhead: {
presence: {
if(object) {
return get(object, 'selectPatient');
}
}
},
quantity: {
numericality: {
allowBlank: true,
greaterThan: 0,
messages: {
greaterThan: 'must be greater than 0'
}
},
presence: {
if(object) {
return get(object, 'isFulfilling');
}
},
acceptance: {
accept: true,
if(object) {
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
return false;
}
},
message: 'The quantity must be less than or equal to the number of available medication.'
}
},
refills: {
numericality: {
allowBlank: true
}
}
}
});