Skip to content
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
32 changes: 30 additions & 2 deletions src/openlmis-adjustments/adjustments-modal.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,13 @@
AdjustmentsModalController.$inject = [
'$filter', '$q', 'modalDeferred', 'title', 'message', 'isDisabled', 'adjustments',
'reasons', 'summaries', 'preSave', 'preCancel', 'filterReasons', 'showInDoses', 'lineItem',
'quantityUnitCalculateService'
'quantityUnitCalculateService', 'alertService'
];

function AdjustmentsModalController($filter, $q, modalDeferred, title, message, isDisabled,
adjustments, reasons, summaries, preSave, preCancel,
filterReasons, showInDoses, lineItem, quantityUnitCalculateService) {
filterReasons, showInDoses, lineItem, quantityUnitCalculateService,
alertService) {

var vm = this;

Expand All @@ -45,6 +46,7 @@
vm.removeAdjustment = removeAdjustment;
vm.cancel = cancel;
vm.save = save;
vm.hasInvalidAdjustments = hasInvalidAdjustments;

/**
* @ngdoc property
Expand Down Expand Up @@ -180,6 +182,11 @@
* Adds adjustment to line item.
*/
function addAdjustment() {
if (vm.newAdjustment && !hasValidQuantity(vm.newAdjustment)) {
alertService.error('openlmisAdjustments.quantityGreaterThanZero');
return $q.when();
}

vm.adjustments.push(vm.newAdjustment);

vm.newAdjustment = undefined;
Expand Down Expand Up @@ -223,6 +230,11 @@
* back to the modal without applying any changes;
*/
function save() {
if (hasInvalidAdjustments()) {
alertService.error('openlmisAdjustments.quantityGreaterThanZero');
return;
}

if (preSave) {
preSave(vm.adjustments)
.then(function() {
Expand Down Expand Up @@ -250,6 +262,22 @@
modalDeferred.reject();
}
}

function hasInvalidAdjustments() {
return (vm.adjustments || []).some(function(adjustment) {
return !hasValidQuantity(adjustment);
});
}

function hasValidQuantity(adjustment) {
return adjustment !== undefined
&& adjustment !== null
&& adjustment.quantity !== undefined
&& adjustment.quantity !== null
&& adjustment.quantity !== ''
&& !isNaN(adjustment.quantity)
&& Number(adjustment.quantity) > 0;
}
}

})();
75 changes: 75 additions & 0 deletions src/openlmis-adjustments/adjustments-modal.controller.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ describe('AdjustmentsModalController', function() {
this.$q = $injector.get('$q');
this.$rootScope = $injector.get('$rootScope');
this.$controller = $injector.get('$controller');
this.alertService = $injector.get('alertService');
});

this.modalDeferred = this.$q.defer();
Expand Down Expand Up @@ -67,6 +68,7 @@ describe('AdjustmentsModalController', function() {

spyOn(this.modalDeferred, 'resolve');
spyOn(this.modalDeferred, 'reject');
spyOn(this.alertService, 'error');

this.initController = initController;
});
Expand Down Expand Up @@ -191,6 +193,52 @@ describe('AdjustmentsModalController', function() {
expect(this.filterReasons).toHaveBeenCalledWith(this.vm.adjustments);
});

it('should not add adjustment and should show error when quantity is zero', function() {
this.vm.newAdjustment = {
reason: this.vm.reasons[0],
quantity: 0
};

this.vm.addAdjustment();

expect(this.vm.adjustments.length).toBe(2);
expect(this.alertService.error)
.toHaveBeenCalledWith('openlmisAdjustments.quantityGreaterThanZero');
});

it('should not add adjustment and should show error when quantity is empty', function() {
this.vm.newAdjustment = {
reason: this.vm.reasons[0],
quantity: ''
};

this.vm.addAdjustment();

expect(this.vm.adjustments.length).toBe(2);
expect(this.alertService.error)
.toHaveBeenCalledWith('openlmisAdjustments.quantityGreaterThanZero');
});

it('should not add adjustment and should show error when quantity is negative', function() {
this.vm.newAdjustment = {
reason: this.vm.reasons[0],
quantity: -5
};

this.vm.addAdjustment();

expect(this.vm.adjustments.length).toBe(2);
expect(this.alertService.error)
.toHaveBeenCalledWith('openlmisAdjustments.quantityGreaterThanZero');
});

it('should not show error when quantity is greater than zero', function() {
this.vm.addAdjustment();

expect(this.vm.adjustments.length).toBe(3);
expect(this.alertService.error).not.toHaveBeenCalled();
});

});

describe('removeAdjustment', function() {
Expand Down Expand Up @@ -297,6 +345,33 @@ describe('AdjustmentsModalController', function() {
expect(this.modalDeferred.resolve).not.toHaveBeenCalled();
});

it('should not resolve modalDeferred and should show error if any adjustment has invalid quantity',
function() {
this.adjustments = [{
reason: this.reasons[0],
quantity: 0
}];

this.initController();
this.vm.$onInit();

this.vm.save();

expect(this.modalDeferred.resolve).not.toHaveBeenCalled();
expect(this.alertService.error)
.toHaveBeenCalledWith('openlmisAdjustments.quantityGreaterThanZero');
});

it('should not show error if all adjustments have valid quantities', function() {
this.initController();
this.vm.$onInit();

this.vm.save();

expect(this.alertService.error).not.toHaveBeenCalled();
expect(this.modalDeferred.resolve).toHaveBeenCalledWith(this.vm.adjustments);
});

});

describe('cancel', function() {
Expand Down
3 changes: 2 additions & 1 deletion src/openlmis-adjustments/messages_en.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@
"openlmisAdjustments.total": "Total",
"openlmisAdjustments.update": "Update",
"openlmisAdjustments.actions": "Actions",
"openlmisAdjustments.close": "Close"
"openlmisAdjustments.close": "Close",
"openlmisAdjustments.quantityGreaterThanZero": "Quantity must be greater than 0"
}
Loading