diff --git a/app/appointments/edit/route.js b/app/appointments/edit/route.js index d9f6433b6c..aae30ff91d 100644 --- a/app/appointments/edit/route.js +++ b/app/appointments/edit/route.js @@ -1,4 +1,5 @@ import AbstractEditRoute from 'hospitalrun/routes/abstract-edit-route'; +import AddToPatient from 'hospitalrun/mixins/add-to-patient-route'; import Ember from 'ember'; import moment from 'moment'; import PatientListRoute from 'hospitalrun/mixins/patient-list-route'; @@ -6,12 +7,13 @@ import { translationMacro as t } from 'ember-i18n'; const { get, + isEmpty, RSVP: { resolve } } = Ember; -export default AbstractEditRoute.extend(PatientListRoute, { +export default AbstractEditRoute.extend(AddToPatient, PatientListRoute, { editTitle: t('appointments.editTitle'), modelName: 'appointment', newButtonText: t('appointments.buttons.newButton'), @@ -26,7 +28,7 @@ export default AbstractEditRoute.extend(PatientListRoute, { selectPatient: true, startDate: new Date() }; - if (!Ember.isEmpty(idParam) && params[idParam] === 'newsurgery') { + if (!isEmpty(idParam) && params[idParam] === 'newsurgery') { newData.appointmentType = 'Surgery'; newData.allDay = false; newData.endDate = moment().add('1', 'hours').toDate(); @@ -53,10 +55,19 @@ export default AbstractEditRoute.extend(PatientListRoute, { model(params) { let idParam = this.get('idParam'); let modelId = params[idParam]; - if (!Ember.isEmpty(idParam) && (modelId.indexOf('new') === 0)) { - return this._createNewRecord(params); + if (!isEmpty(idParam) && (modelId.indexOf('new') === 0)) { + if (!isEmpty(params.forPatientId)) { + let modelPromise = this._super(params); + return this._setPatientOnModel(modelPromise, params.forPatientId); + } else if (!isEmpty(params.forVisitId)) { + let modelPromise = this._super(params); + return this._setVisitOnModel(modelPromise, params.forVisitId); + } else { + return this._createNewRecord(params); + } } else { return this._super(params); } } + }); diff --git a/app/imaging/edit/route.js b/app/imaging/edit/route.js index e84f3a243b..4d5bab420d 100644 --- a/app/imaging/edit/route.js +++ b/app/imaging/edit/route.js @@ -1,10 +1,11 @@ import { translationMacro as t } from 'ember-i18n'; import AbstractEditRoute from 'hospitalrun/routes/abstract-edit-route'; +import AddToPatientRoute from 'hospitalrun/mixins/add-to-patient-route'; import ChargeRoute from 'hospitalrun/mixins/charge-route'; import Ember from 'ember'; import moment from 'moment'; import PatientListRoute from 'hospitalrun/mixins/patient-list-route'; -export default AbstractEditRoute.extend(ChargeRoute, PatientListRoute, { +export default AbstractEditRoute.extend(AddToPatientRoute, ChargeRoute, PatientListRoute, { editTitle: t('imaging.titles.editTitle'), modelName: 'imaging', newTitle: t('imaging.titles.editTitle'), diff --git a/app/labs/edit/route.js b/app/labs/edit/route.js index 9b654c7e37..0338bbc9ab 100644 --- a/app/labs/edit/route.js +++ b/app/labs/edit/route.js @@ -1,11 +1,12 @@ -import Ember from 'ember'; import AbstractEditRoute from 'hospitalrun/routes/abstract-edit-route'; +import AddToPatientRoute from 'hospitalrun/mixins/add-to-patient-route'; import ChargeRoute from 'hospitalrun/mixins/charge-route'; +import Ember from 'ember'; import moment from 'moment'; import PatientListRoute from 'hospitalrun/mixins/patient-list-route'; import { translationMacro as t } from 'ember-i18n'; -export default AbstractEditRoute.extend(ChargeRoute, PatientListRoute, { +export default AbstractEditRoute.extend(AddToPatientRoute, ChargeRoute, PatientListRoute, { editTitle: t('labs.editTitle'), modelName: 'lab', newTitle: t('labs.newTitle'), diff --git a/app/medication/edit/route.js b/app/medication/edit/route.js index 2f8c7f131a..e8ae0b4b0d 100644 --- a/app/medication/edit/route.js +++ b/app/medication/edit/route.js @@ -1,12 +1,16 @@ import { translationMacro as t } from 'ember-i18n'; import AbstractEditRoute from 'hospitalrun/routes/abstract-edit-route'; +import AddToPatientRoute from 'hospitalrun/mixins/add-to-patient-route'; import Ember from 'ember'; import FulfillRequest from 'hospitalrun/mixins/fulfill-request'; import InventoryLocations from 'hospitalrun/mixins/inventory-locations'; // inventory-locations mixin is needed for fulfill-request mixin! import moment from 'moment'; import PatientListRoute from 'hospitalrun/mixins/patient-list-route'; import uuid from 'npm:uuid'; -export default AbstractEditRoute.extend(FulfillRequest, InventoryLocations, PatientListRoute, { + +const { isEmpty } = Ember; + +export default AbstractEditRoute.extend(AddToPatientRoute, FulfillRequest, InventoryLocations, PatientListRoute, { editTitle: t('medication.titles.editMedicationRequest'), modelName: 'medication', newTitle: t('medication.titles.newMedicationRequest'), @@ -27,8 +31,15 @@ export default AbstractEditRoute.extend(FulfillRequest, InventoryLocations, Pati model(params) { let idParam = this.get('idParam'); + let modelPromise = this._super(params); if (!Ember.isEmpty(idParam) && params[idParam] === 'new' || params[idParam] === 'dispense') { - return this._createNewRecord(params); + if (!isEmpty(params.forPatientId)) { + return this._setPatientOnModel(modelPromise, params.forPatientId); + } else if (!isEmpty(params.forVisitId)) { + return this._setVisitOnModel(modelPromise, params.forVisitId); + } else { + return this._createNewRecord(params); + } } else { return this._super(params); } diff --git a/app/mixins/add-to-patient-route.js b/app/mixins/add-to-patient-route.js new file mode 100644 index 0000000000..865816d2c7 --- /dev/null +++ b/app/mixins/add-to-patient-route.js @@ -0,0 +1,61 @@ +import Ember from 'ember'; + +const { get, isEmpty, Mixin } = Ember; + +export default Mixin.create({ + queryParams: { + forPatientId: { + refreshModel: false + }, + forVisitId: { + refreshModel: false + } + }, + + model(params) { + let idParam = get(this, 'idParam'); + let modelPromise = this._super(params); + if (params[idParam] === 'new') { + if (!isEmpty(params.forPatientId)) { + return this._setPatientOnModel(modelPromise, params.forPatientId); + } else if (!isEmpty(params.forVisitId)) { + return this._setVisitOnModel(modelPromise, params.forVisitId); + } else { + return this._createNewRecord(params); + } + } else { + return modelPromise; + } + }, + + /** + * Resolves the model promise and then sets the patient information on the model. + */ + _setPatientOnModel(modelPromise, patientId) { + let store = get(this, 'store'); + return modelPromise.then((model) => { + return store.find('patient', patientId).then((patient) => { + model.set('patient', patient); + model.set('returnToPatient', patientId); + model.set('selectPatient', false); + return model; + }); + }); + }, + + /** + * Resolves the model promise and then sets the visit information on the model. + */ + _setVisitOnModel(modelPromise, visitId) { + let store = get(this, 'store'); + return modelPromise.then((model) => { + return store.find('visit', visitId).then((visit) => { + model.set('visit', visit); + model.set('returnToVisit', visitId); + model.set('selectPatient', false); + model.set('patient', visit.get('patient')); + return model; + }); + }); + } +}); diff --git a/app/patients/edit/controller.js b/app/patients/edit/controller.js index 0e7eacddc9..45e854b78b 100644 --- a/app/patients/edit/controller.js +++ b/app/patients/edit/controller.js @@ -540,16 +540,16 @@ export default AbstractEditController.extend(BloodTypes, DiagnosisActions, Retur }, _addChildObject(route, afterTransition) { - this.transitionToRoute(route, 'new').then(function(newRoute) { - newRoute.currentModel.setProperties({ - patient: this.get('model'), - returnToPatient: this.get('model.id'), - selectPatient: false - }); + let options = { + queryParams: { + forPatientId: this.get('model.id') + } + }; + this.transitionToRoute(route, 'new', options).then((newRoute) => { if (afterTransition) { afterTransition(newRoute); } - }.bind(this)); + }); }, _showEditSocial(editAttributes, modelName, route) { diff --git a/app/patients/operative-plan/route.js b/app/patients/operative-plan/route.js index 96c292d465..f3f0fde73b 100644 --- a/app/patients/operative-plan/route.js +++ b/app/patients/operative-plan/route.js @@ -1,4 +1,5 @@ import AbstractEditRoute from 'hospitalrun/routes/abstract-edit-route'; +import AddToPatientRoute from 'hospitalrun/mixins/add-to-patient-route'; import Ember from 'ember'; import moment from 'moment'; import { translationMacro as t } from 'ember-i18n'; @@ -8,7 +9,7 @@ const { inject } = Ember; -export default AbstractEditRoute.extend({ +export default AbstractEditRoute.extend(AddToPatientRoute, { editTitle: t('operativePlan.titles.editTitle'), modelName: 'operative-plan', newTitle: t('operativePlan.titles.newTitle'), diff --git a/app/procedures/edit/route.js b/app/procedures/edit/route.js index cfac071baf..b2e54299ce 100644 --- a/app/procedures/edit/route.js +++ b/app/procedures/edit/route.js @@ -1,9 +1,10 @@ import AbstractEditRoute from 'hospitalrun/routes/abstract-edit-route'; +import AddToPatient from 'hospitalrun/mixins/add-to-patient-route'; import ChargeRoute from 'hospitalrun/mixins/charge-route'; import Ember from 'ember'; import { translationMacro as t } from 'ember-i18n'; -export default AbstractEditRoute.extend(ChargeRoute, { +export default AbstractEditRoute.extend(AddToPatient, ChargeRoute, { editTitle: t('procedures.titles.edit'), modelName: 'procedure', newTitle: t('procedures.titles.new'), diff --git a/app/reports/edit/route.js b/app/reports/edit/route.js index 1aa5943735..19ac6dd505 100644 --- a/app/reports/edit/route.js +++ b/app/reports/edit/route.js @@ -1,8 +1,9 @@ import AbstractEditRoute from 'hospitalrun/routes/abstract-edit-route'; +import AddToPatientRoute from 'hospitalrun/mixins/add-to-patient-route'; import Ember from 'ember'; import { translationMacro as t } from 'ember-i18n'; -export default AbstractEditRoute.extend({ +export default AbstractEditRoute.extend(AddToPatientRoute, { modelName: 'report', customForms: Ember.inject.service(), @@ -15,23 +16,18 @@ export default AbstractEditRoute.extend({ return customForms.setDefaultCustomForms(['opdReport', 'dischargeReport'], newReportData); }, - afterModel(model) { - if (model.get('isNew')) { - let visit = this.modelFor('visits.edit'); - if (!visit) { - return this.transitionTo('patients'); - } - model.set('visit', visit); - } - model.setProperties({ returnToVisit: model.get('visit.id') }); - }, - getScreenTitle(model) { let state = model.get('isNew') ? 'new' : 'edit'; let type = model.get('visit.outPatient') ? 'opd' : 'discharge'; return t(`reports.${type}.titles.${state}`); }, + afterModel(model) { + if (!model.get('visit')) { + return this.transitionTo('patients'); + } + }, + setupController(controller, model) { this._super(controller, model); } diff --git a/app/visits/edit/controller.js b/app/visits/edit/controller.js index cc31422291..dabd6a47f7 100644 --- a/app/visits/edit/controller.js +++ b/app/visits/edit/controller.js @@ -171,17 +171,16 @@ export default AbstractEditController.extend(AddNewPatient, ChargeActions, Diagn }.property('visitTypes', 'model.outPatient'), _addChildObject(route, afterTransition) { - this.transitionToRoute(route, 'new').then(function(newRoute) { - newRoute.currentModel.setProperties({ - patient: this.get('model.patient'), - visit: this.get('model'), - selectPatient: false, - returnToVisit: this.get('model.id') - }); + let options = { + queryParams: { + forVisitId: this.get('model.id') + } + }; + this.transitionToRoute(route, 'new', options).then((newRoute) => { if (afterTransition) { afterTransition(newRoute); } - }.bind(this)); + }); }, _finishAfterUpdate() { diff --git a/tests/acceptance/operative-test.js b/tests/acceptance/operative-test.js index 8c424bc39d..054665fd44 100644 --- a/tests/acceptance/operative-test.js +++ b/tests/acceptance/operative-test.js @@ -56,7 +56,7 @@ test('Plan and report creation', function(assert) { waitToAppear('span.secondary-diagnosis:contains(Tennis Elbow)'); }); andThen(() =>{ - assert.equal(currentURL(), '/patients/operative-plan/new', 'New operative plan URL is correct'); + assert.equal(currentURL(), '/patients/operative-plan/new?forPatientId=C87BFCB2-F772-7A7B-8FC7-AD00C018C32A', 'New operative plan URL is correct'); assert.equal(find('.patient-name .ps-info-data').text(), 'Joe Bagadonuts', 'Joe Bagadonuts patient header displays'); assert.equal(find('.view-current-title').text(), 'New Operative Plan', 'New operative plan title is correct'); assert.equal(find('span.primary-diagnosis:contains(Broken Arm)').length, 1, 'Primary diagnosis appears as read only'); diff --git a/tests/acceptance/visit-test.js b/tests/acceptance/visit-test.js index b3fe28c347..97edebdb5b 100644 --- a/tests/acceptance/visit-test.js +++ b/tests/acceptance/visit-test.js @@ -68,7 +68,7 @@ test('Edit visit', function(assert) { click('button:contains(New Medication)'); }); andThen(function() { - assert.equal(currentURL(), '/medication/edit/new', 'New medication url is correct'); + assert.equal(currentURL(), '/medication/edit/new?forVisitId=03C7BF8B-04E0-DD9E-9469-96A5604F5340', 'New medication url is correct'); assert.equal(find('.patient-name .ps-info-data').text(), 'Joe Bagadonuts', 'New medication prepopulates with patient'); click('button:contains(Cancel)'); }); @@ -76,7 +76,7 @@ test('Edit visit', function(assert) { click('button:contains(New Lab)'); }); andThen(function() { - assert.equal(currentURL(), '/labs/edit/new', 'New lab url is correct'); + assert.equal(currentURL(), '/labs/edit/new?forVisitId=03C7BF8B-04E0-DD9E-9469-96A5604F5340', 'New lab url is correct'); assert.equal(find('.patient-name .ps-info-data').text(), 'Joe Bagadonuts', 'New lab prepopulates with patient'); click('button:contains(Cancel)'); }); @@ -84,7 +84,7 @@ test('Edit visit', function(assert) { click('button:contains(New Imaging)'); }); andThen(function() { - assert.equal(currentURL(), '/imaging/edit/new', 'New imaging url is correct'); + assert.equal(currentURL(), '/imaging/edit/new?forVisitId=03C7BF8B-04E0-DD9E-9469-96A5604F5340', 'New imaging url is correct'); assert.equal(find('.patient-name .ps-info-data').text(), 'Joe Bagadonuts', 'New imaging prepopulates with patient'); click('button:contains(Cancel)'); });