Skip to content

Commit 4dfb96f

Browse files
authored
Merge pull request #88 from OpenLMIS/OLMIS-8249-implement-changes-from-OE-206-to-Core
fix(OLMIS-8249): require positive adjustment quantities
2 parents 5dfe636 + f2c91d7 commit 4dfb96f

3 files changed

Lines changed: 107 additions & 3 deletions

File tree

src/openlmis-adjustments/adjustments-modal.controller.js

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,13 @@
3131
AdjustmentsModalController.$inject = [
3232
'$filter', '$q', 'modalDeferred', 'title', 'message', 'isDisabled', 'adjustments',
3333
'reasons', 'summaries', 'preSave', 'preCancel', 'filterReasons', 'showInDoses', 'lineItem',
34-
'quantityUnitCalculateService'
34+
'quantityUnitCalculateService', 'alertService'
3535
];
3636

3737
function AdjustmentsModalController($filter, $q, modalDeferred, title, message, isDisabled,
3838
adjustments, reasons, summaries, preSave, preCancel,
39-
filterReasons, showInDoses, lineItem, quantityUnitCalculateService) {
39+
filterReasons, showInDoses, lineItem, quantityUnitCalculateService,
40+
alertService) {
4041

4142
var vm = this;
4243

@@ -45,6 +46,7 @@
4546
vm.removeAdjustment = removeAdjustment;
4647
vm.cancel = cancel;
4748
vm.save = save;
49+
vm.hasInvalidAdjustments = hasInvalidAdjustments;
4850

4951
/**
5052
* @ngdoc property
@@ -180,6 +182,11 @@
180182
* Adds adjustment to line item.
181183
*/
182184
function addAdjustment() {
185+
if (vm.newAdjustment && !hasValidQuantity(vm.newAdjustment)) {
186+
alertService.error('openlmisAdjustments.quantityGreaterThanZero');
187+
return $q.when();
188+
}
189+
183190
vm.adjustments.push(vm.newAdjustment);
184191

185192
vm.newAdjustment = undefined;
@@ -223,6 +230,11 @@
223230
* back to the modal without applying any changes;
224231
*/
225232
function save() {
233+
if (hasInvalidAdjustments()) {
234+
alertService.error('openlmisAdjustments.quantityGreaterThanZero');
235+
return;
236+
}
237+
226238
if (preSave) {
227239
preSave(vm.adjustments)
228240
.then(function() {
@@ -250,6 +262,22 @@
250262
modalDeferred.reject();
251263
}
252264
}
265+
266+
function hasInvalidAdjustments() {
267+
return (vm.adjustments || []).some(function(adjustment) {
268+
return !hasValidQuantity(adjustment);
269+
});
270+
}
271+
272+
function hasValidQuantity(adjustment) {
273+
return adjustment !== undefined
274+
&& adjustment !== null
275+
&& adjustment.quantity !== undefined
276+
&& adjustment.quantity !== null
277+
&& adjustment.quantity !== ''
278+
&& !isNaN(adjustment.quantity)
279+
&& Number(adjustment.quantity) > 0;
280+
}
253281
}
254282

255283
})();

src/openlmis-adjustments/adjustments-modal.controller.spec.js

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ describe('AdjustmentsModalController', function() {
2222
this.$q = $injector.get('$q');
2323
this.$rootScope = $injector.get('$rootScope');
2424
this.$controller = $injector.get('$controller');
25+
this.alertService = $injector.get('alertService');
2526
});
2627

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

6869
spyOn(this.modalDeferred, 'resolve');
6970
spyOn(this.modalDeferred, 'reject');
71+
spyOn(this.alertService, 'error');
7072

7173
this.initController = initController;
7274
});
@@ -191,6 +193,52 @@ describe('AdjustmentsModalController', function() {
191193
expect(this.filterReasons).toHaveBeenCalledWith(this.vm.adjustments);
192194
});
193195

196+
it('should not add adjustment and should show error when quantity is zero', function() {
197+
this.vm.newAdjustment = {
198+
reason: this.vm.reasons[0],
199+
quantity: 0
200+
};
201+
202+
this.vm.addAdjustment();
203+
204+
expect(this.vm.adjustments.length).toBe(2);
205+
expect(this.alertService.error)
206+
.toHaveBeenCalledWith('openlmisAdjustments.quantityGreaterThanZero');
207+
});
208+
209+
it('should not add adjustment and should show error when quantity is empty', function() {
210+
this.vm.newAdjustment = {
211+
reason: this.vm.reasons[0],
212+
quantity: ''
213+
};
214+
215+
this.vm.addAdjustment();
216+
217+
expect(this.vm.adjustments.length).toBe(2);
218+
expect(this.alertService.error)
219+
.toHaveBeenCalledWith('openlmisAdjustments.quantityGreaterThanZero');
220+
});
221+
222+
it('should not add adjustment and should show error when quantity is negative', function() {
223+
this.vm.newAdjustment = {
224+
reason: this.vm.reasons[0],
225+
quantity: -5
226+
};
227+
228+
this.vm.addAdjustment();
229+
230+
expect(this.vm.adjustments.length).toBe(2);
231+
expect(this.alertService.error)
232+
.toHaveBeenCalledWith('openlmisAdjustments.quantityGreaterThanZero');
233+
});
234+
235+
it('should not show error when quantity is greater than zero', function() {
236+
this.vm.addAdjustment();
237+
238+
expect(this.vm.adjustments.length).toBe(3);
239+
expect(this.alertService.error).not.toHaveBeenCalled();
240+
});
241+
194242
});
195243

196244
describe('removeAdjustment', function() {
@@ -297,6 +345,33 @@ describe('AdjustmentsModalController', function() {
297345
expect(this.modalDeferred.resolve).not.toHaveBeenCalled();
298346
});
299347

348+
it('should not resolve modalDeferred and should show error if any adjustment has invalid quantity',
349+
function() {
350+
this.adjustments = [{
351+
reason: this.reasons[0],
352+
quantity: 0
353+
}];
354+
355+
this.initController();
356+
this.vm.$onInit();
357+
358+
this.vm.save();
359+
360+
expect(this.modalDeferred.resolve).not.toHaveBeenCalled();
361+
expect(this.alertService.error)
362+
.toHaveBeenCalledWith('openlmisAdjustments.quantityGreaterThanZero');
363+
});
364+
365+
it('should not show error if all adjustments have valid quantities', function() {
366+
this.initController();
367+
this.vm.$onInit();
368+
369+
this.vm.save();
370+
371+
expect(this.alertService.error).not.toHaveBeenCalled();
372+
expect(this.modalDeferred.resolve).toHaveBeenCalledWith(this.vm.adjustments);
373+
});
374+
300375
});
301376

302377
describe('cancel', function() {

src/openlmis-adjustments/messages_en.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@
1111
"openlmisAdjustments.total": "Total",
1212
"openlmisAdjustments.update": "Update",
1313
"openlmisAdjustments.actions": "Actions",
14-
"openlmisAdjustments.close": "Close"
14+
"openlmisAdjustments.close": "Close",
15+
"openlmisAdjustments.quantityGreaterThanZero": "Quantity must be greater than 0"
1516
}

0 commit comments

Comments
 (0)