diff --git a/app/mixins/patient-id.js b/app/mixins/patient-id.js index 464eb8a0af..7570d1ef33 100644 --- a/app/mixins/patient-id.js +++ b/app/mixins/patient-id.js @@ -19,11 +19,11 @@ export default Ember.Mixin.create(PouchDbMixin, { const maxValue = this.get('maxValue'); const findUnusedId = (sequence) => { - let next, id; + let current, id; return config.getPatientPrefix() .then(function(prefix) { - next = sequence.incrementProperty('value'); - id = sequenceId(prefix, next); + current = sequence.get('value'); + id = sequenceId(prefix, current); const query = { startkey: [ id, null ], endkey: [ id, maxValue ] @@ -31,14 +31,16 @@ export default Ember.Mixin.create(PouchDbMixin, { return database.queryMainDB(query, 'patient_by_display_id'); }) .then(function(found) { - if (isEmpty(found.rows)) { - sequence.set('value', next); - } else { + if (!isEmpty(found.rows)) { + sequence.incrementProperty('value'); return findUnusedId(sequence); } - return sequence.save().then(function() { - return id; - }); + if (sequence.get('hasDirtyAttributes')) { + return sequence.save().then(function() { + return id; + }); + } + return id; }); }; @@ -48,7 +50,7 @@ export default Ember.Mixin.create(PouchDbMixin, { var store = this.get('store'); var sequence = store.push(store.normalize('sequence', { id: 'patient', - value: 0 + value: 1 })); return findUnusedId(sequence); }); diff --git a/app/patients/edit/controller.js b/app/patients/edit/controller.js index b49ab490d5..ad10964590 100644 --- a/app/patients/edit/controller.js +++ b/app/patients/edit/controller.js @@ -6,6 +6,8 @@ import ReturnTo from 'hospitalrun/mixins/return-to'; import SelectValues from 'hospitalrun/utils/select-values'; import UserSession from 'hospitalrun/mixins/user-session'; export default AbstractEditController.extend(BloodTypes, ReturnTo, UserSession, PatientNotes, { + config: Ember.inject.service(), + canAddAppointment: function() { return this.currentUserCan('add_appointment'); }.property(), @@ -526,14 +528,35 @@ export default AbstractEditController.extend(BloodTypes, ReturnTo, UserSession, this.send('closeModal'); }, + _updateSequence: function(record) { + const config = this.get('config'); + const friendlyId = record.get('friendlyId'); + return config.getPatientPrefix().then((prefix) => { + const re = new RegExp(`^${prefix}\\d{5}$`); + if (!re.test(friendlyId)) { + return; + } + return this.store.find('sequence', 'patient').then((sequence) => { + const sequenceNumber = sequence.get('value'); + const patientNumber = parseInt(friendlyId.slice(prefix.length)); + if (patientNumber > sequenceNumber) { + sequence.set('value', patientNumber); + return sequence.save(); + } + }); + }); + }, + afterUpdate: function(record) { - this.send('openModal', 'dialog', Ember.Object.create({ - title: this.get('i18n').t('patients.titles.savedPatient'), - message: this.get('i18n').t('patients.messages.savedPatient', record), - updateButtonAction: 'returnToPatient', - updateButtonText: this.get('i18n').t('patients.buttons.backToPatients'), - cancelButtonText: this.get('i18n').t('buttons.close') - })); + this._updateSequence(record).then(() => { + this.send('openModal', 'dialog', Ember.Object.create({ + title: this.get('i18n').t('patients.titles.savedPatient'), + message: this.get('i18n').t('patients.messages.savedPatient', record), + updateButtonAction: 'returnToPatient', + updateButtonText: this.get('i18n').t('patients.buttons.backToPatients'), + cancelButtonText: this.get('i18n').t('buttons.close') + })); + }); } });