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

Commit

Permalink
fix: delete payments and line items when deleting invoices
Browse files Browse the repository at this point in the history
  • Loading branch information
aboma committed May 12, 2016
1 parent 9d2040d commit 05527ae
Showing 1 changed file with 38 additions and 27 deletions.
65 changes: 38 additions & 27 deletions app/patients/delete/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,18 @@ import PouchDbMixin from 'hospitalrun/mixins/pouchdb';
import Ember from 'ember';

function deleteMany(manyArray) {
if (!manyArray) {
return Ember.RSVP.resolve();
}
if (manyArray.then) {
// recursive call after resolving async model
return manyArray.then(deleteMany);
}
var recordsCount = manyArray.get('length');
if (!recordsCount) {
// empty array: no records to delete
return Ember.RSVP.resolve();
}
console.log('deleting ' + recordsCount + ' records');
return Ember.RSVP.all(manyArray.invoke('destroyRecord', 'async array deletion'));
}

Expand All @@ -24,44 +27,52 @@ export default AbstractDeleteController.extend(PatientVisitsMixin, PatientInvoic
// all related records before deleting patient record
// otherwise errors will occur
deletePatient: function() {
var controller = this;
var patient = this.get('model');
var promises = [];
promises.push(this.deleteVisits(patient));
promises.push(this.deleteInvoices(patient));
Ember.RSVP.all(promises)
.then(function() {
patient.destroyRecord();
});
this.send(this.get('afterDeleteAction'), patient);
var visits = this.getPatientVisits(patient);
var invoices = this.getPatientInvoices(patient);
// resolve all async models first since they reference each other, then delete
return Ember.RSVP.all([visits, invoices]).then(function(records) {
var promises = [];
promises.push(controller.deleteVisits(records[0]));
promises.push(controller.deleteInvoices(records[1]));
return Ember.RSVP.all(promises)
.then(function() {
return patient.destroyRecord();
});
});
},

deleteVisits: function(patient) {
return this.getPatientVisits(patient).then(function(visits) {
var promises = [];
visits.forEach(function(visit) {
promises.push(deleteMany(visit.get('labs')));
promises.push(deleteMany(visit.get('patientNotes')));
promises.push(deleteMany(visit.get('vitals')));
promises.push(deleteMany(visit.get('procedures')));
promises.push(deleteMany(visit.get('medication')));
promises.push(deleteMany(visit.get('imaging')));
});
return Ember.RSVP.all(promises).then(function(){
return deleteMany(visits);
});
deleteVisits: function(visits) {
var promises = [];
visits.forEach(function(visit) {
promises.push(deleteMany(visit.get('labs')));
promises.push(deleteMany(visit.get('patientNotes')));
promises.push(deleteMany(visit.get('vitals')));
promises.push(deleteMany(visit.get('procedures')));
promises.push(deleteMany(visit.get('medication')));
promises.push(deleteMany(visit.get('imaging')));
});
return Ember.RSVP.all(promises).then(function() {
return deleteMany(visits);
});
},

deleteInvoices: function(patient) {
this.getPatientInvoices(patient).then(function(invoices){
deleteMany(invoices);
deleteInvoices: function(invoices) {
var lineItems = invoices.get('lineItems');
var payments = invoices.get('payments');
return Ember.RSVP.all([lineItems, payments]).then(function() {
return deleteMany(invoices);
});
},

actions: {
// delete related records without modal dialogs
delete: function(patient) {
this.deletePatient(patient);
var controller = this;
this.deletePatient(patient).then(function() {
controller.send(controller.get('afterDeleteAction'), patient);
});
this.send('closeModal');
}
}
Expand Down

0 comments on commit 05527ae

Please sign in to comment.