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

Fixes #485: Suggest next patient ID based on the largest number used #667

Merged
merged 2 commits into from
Sep 28, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 12 additions & 10 deletions app/mixins/patient-id.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,28 @@ 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 ]
};
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;
});
};

Expand All @@ -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);
});
Expand Down
37 changes: 30 additions & 7 deletions app/patients/edit/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down Expand Up @@ -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')
}));
});
}

});