Skip to content

Commit

Permalink
Merge pull request #166 from OpenLMIS-Angola/OAM-273-frontend
Browse files Browse the repository at this point in the history
OAM-273: OpenLMIS kit unpack hangs during submission
  • Loading branch information
olewandowski1 authored Aug 7, 2024
2 parents efa617c + 75677c3 commit 0111379
Show file tree
Hide file tree
Showing 5 changed files with 251 additions and 3 deletions.
3 changes: 2 additions & 1 deletion src/admin-orderable-edit/messages_en.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@
"adminOrderableEdit.unitAssignment.factor": "factor",
"adminOrderableEdit.unitAssignment.description": "description",
"adminOrderableEdit.unitAssignment.noUnitsFound": "Orderable has no assigned units",
"adminOrderableEdit.assignUnit": "Assign unit"
"adminOrderableEdit.assignUnit": "Assign unit",
"adminOrderableEdit.unit": "Unit"
}
180 changes: 180 additions & 0 deletions src/admin-orderable-edit/orderable-edit-kit-unpack-list.controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
/*
* This program is part of the OpenLMIS logistics management information system platform software.
* Copyright © 2017 VillageReach
*
* This program is free software: you can redistribute it and/or modify it under the terms
* of the GNU Affero General Public License as published by the Free Software Foundation, either
* version 3 of the License, or (at your option) any later version.
*  
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
* See the GNU Affero General Public License for more details. You should have received a copy of
* the GNU Affero General Public License along with this program. If not, see
* http://www.gnu.org/licenses.  For additional information contact [email protected]
*/

(function() {

'use strict';

/**
* @ngdoc controller
* @name admin-orderable-edit.controller:OrderableEditKitUnpackListController
*
* @description
* Controller for managing orderable view screen.
*/
angular
.module('admin-orderable-edit')
.controller('OrderableEditKitUnpackListController', controller);

controller.$inject = [
'selectProductsModalService', 'orderable', 'children', 'OrderableResource', 'orderablesMap',
'FunctionDecorator', 'stateTrackerService'
];

function controller(selectProductsModalService, orderable, children, OrderableResource, orderablesMap,
FunctionDecorator, stateTrackerService) {

var vm = this;

vm.$onInit = onInit;
vm.goToOrderableList = goToOrderableList;
vm.addChild = addChild;
vm.removeChild = removeChild;
vm.saveOrderable = new FunctionDecorator()
.decorateFunction(saveOrderable)
.withSuccessNotification('adminOrderableEdit.kitUnpackListSavedSuccessfully')
.withErrorNotification('adminOrderableEdit.failedToSaveKitUnpackList')
.withLoading(true)
.getDecoratedFunction();

/**
* @ngdoc method
* @propertyOf admin-orderable-edit.controller:OrderableEditKitUnpackListController
* @name $onInit
*
* @description
* Method that is executed on initiating OrderableEditKitUnpackListController.
*/
function onInit() {
vm.orderable = orderable;
vm.children = children;
vm.orderablesMap = orderablesMap;
vm.unitOfOrderables = orderable.units;
}

/**
* @ngdoc method
* @propertyOf admin-orderable-edit.controller:OrderableEditKitUnpackListController
* @name addChild
*
* @description
* Method that displays a modal for selecting and adding a orderable to the UI
*/
function addChild() {
selectProducts({
selections: vm.orderablesMap
})
.then(addToOrderablesMap)
.then(updateChildrenList);
}

/**
* @ngdoc method
* @propertyOf admin-orderable-edit.controller:OrderableEditKitUnpackListController
* @name removeChild
*
* @description
* Method that removes kit constituent from the kit orderable
*
* @param {Object} a single child or kit constituent to be removed
*/
function removeChild(child) {
if (vm.children.indexOf(child) > -1) {
vm.children.splice(vm.children.indexOf(child), 1);
}

vm.orderable.children = vm.children;

Object.keys(vm.orderablesMap).forEach(function(key) {
if (key === child.orderable.id) {
delete vm.orderablesMap[child.orderable.id];
}
});
}

/**
* @ngdoc method
* @methodOf admin-orderable-edit.controller:OrderableEditKitUnpackListController
* @name goToOrderableList
*
* @description
* Redirects to orderable list screen.
*/
function goToOrderableList() {
stateTrackerService.goToPreviousState('openlmis.administration.orderables', {}, {
reload: true
});
}

/**
* @ngdoc method
* @methodOf admin-orderable-edit.controller:OrderableEditKitUnpackListController
* @name saveOrderable
*
* @description
* Updates the orderable and return to the orderable list on success.
*/
function saveOrderable() {
return new OrderableResource()
.update(vm.orderable)
.then(goToOrderableList);
}

function selectProducts(availableProducts) {
return selectProductsModalService.show(availableProducts);
}

function mapToChild(orderable) {
return {
orderable: {
id: orderable.id
},
quantity: undefined,
unit: undefined
};
}

function updateChildrenList(selectedOrderables) {
var childrenMap = vm.children.reduce(function(childrenMap, child) {
childrenMap[child.orderable.id] = child;
return childrenMap;
}, {});

clearChildrenList();

selectedOrderables.forEach(function(orderable) {
vm.children.push(childrenMap[orderable.id] ? childrenMap[orderable.id] : mapToChild(orderable));
});

vm.orderable.children = vm.children;
}

function addToOrderablesMap(orderables) {
vm.orderablesMap = orderables.reduce(function(orderablesMap, orderable) {
orderablesMap[orderable.id] = orderable;
return orderablesMap;
}, {});

return orderables;
}

function clearChildrenList() {
while (children.length) {
children.pop();
}
}

}
})();
52 changes: 52 additions & 0 deletions src/admin-orderable-edit/orderable-edit-kit-unpack-list.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<h3>{{'adminOrderableEdit.kitUnpackList' | message}}</h3>
<form name="saveKitUnpackListForm" id="saveKitUnpackList" class="form-inline" ng-submit="vm.saveOrderable()">
<section class="openlmis-table-container">
<div class="is-primary">
<button type="button" class="add" ng-click="vm.addChild()">
{{'adminOrderableEdit.addProduct' | message}}
</button>
</div>
<table>
<caption ng-if="!vm.children || vm.children.length === 0">
{{'adminOrderableEdit.noKitProductsFound' | message}}
</caption>
<thead>
<tr>
<th>{{'adminOrderableEdit.productCode' | message}}</th>
<th>{{'adminOrderableEdit.name' | message}}</th>
<th>{{'adminOrderableEdit.quantity' | message}}</th>
<!-- OAM-273: Start -->
<th>{{'adminOrderableEdit.unit' | message}}</th>
<!-- OAM-273: End -->
<th>{{'adminOrderableEdit.actions' | message}}</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="child in vm.childrenPage">
<td>{{vm.orderablesMap[child.orderable.id].productCode}}</td>
<td>{{vm.orderablesMap[child.orderable.id].fullProductName}}</td>
<td><input positive-integer id="quantity_{{$index}}" max="100" ng-model="child.quantity"
required>
</td>
<!-- OAM-273: Start -->
<td>
<select id="unitOfOrderable" ng-model="child.unit"
ng-options="unit.name for unit in vm.unitOfOrderables track by unit.id" required></select>
</td>
<!-- OAM-273: End -->
<td><button ng-click="vm.removeChild(child)" id="remove_{{$index}}" type="button"
class="danger">
{{'adminOrderableEdit.remove' | message}}</button></td>
</tr>
</tbody>
</table>
<openlmis-pagination list="vm.children" paged-list="vm.childrenPage" pagination-id="'kitUnpackList'" />
</section>
</form>

<div class="openlmis-toolbar">
<button ng-click="vm.goToOrderableList()">{{'adminOrderableEdit.cancel' | message}}</button>
<div class="button-group primary">
<button class="primary" type="submit" form="saveKitUnpackList">{{'adminOrderableEdit.saveKitUnpackList' | message}}</button>
</div>
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -724,14 +724,19 @@

function onSubmit() {
loadingModalService.open();

var addedLineItems = angular.copy(vm.addedLineItems);

generateKitConstituentLineItem(addedLineItems);
var lotPromises = [],
errorLots = [],
distinctLots = [];

addedLineItems.forEach(function(lineItem) {
lineItem.quantity = vm.getLineItemTotalQuantity(lineItem);
// OAM-273: add unitOfOrderableId to body of line items for unpack view.
lineItem.unitOfOrderableId = lineItem.unit.id;
// OAM-273: end

if (lineItem.lot && lineItem.$isNewItem && _.isUndefined(lineItem.lot.id) &&
!listContainsTheSameLot(distinctLots, lineItem.lot)) {
Expand Down Expand Up @@ -948,7 +953,9 @@
lineItem.orderable.children.forEach(function(constituent) {
constituent.reason = creditReason;
constituent.occurredDate = lineItem.occurredDate;
constituent.quantity = lineItem.quantity * constituent.quantity;
if (lineItem.unit.id === constituent.unit.id) {
constituent.quantity = lineItem.quantity * constituent.quantity;
}
constituentLineItems.push(constituent);
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ describe('StockAdjustmentCreationController', function() {
beforeEach(function() {

module('referencedata-lot');
module('openlmis-unit-add');
module('stock-adjustment-creation', function($provide) {
var stockEventRepositoryMock = jasmine.createSpyObj('stockEventRepository', ['create']);
$provide.factory('StockEventRepository', function() {
Expand Down Expand Up @@ -54,6 +55,8 @@ describe('StockAdjustmentCreationController', function() {
UNPACK_REASONS = $injector.get('UNPACK_REASONS');
LotResource = $injector.get('LotResource');

this.unitOfOrderableService = $injector.get('unitOfOrderableService');

this.OrderableDataBuilder = $injector.get('OrderableDataBuilder');
this.OrderableChildrenDataBuilder = $injector.get('OrderableChildrenDataBuilder');
this.offlineService = $injector.get('offlineService');
Expand Down Expand Up @@ -97,6 +100,9 @@ describe('StockAdjustmentCreationController', function() {
scope = rootScope.$new();
scope.productForm = jasmine.createSpyObj('productForm', ['$setUntouched', '$setPristine']);

// returns empty array, create a simple mockup if needed to test with mocked data.
spyOn(this.unitOfOrderableService, 'getAll').andReturn(q.resolve([]));

vm = initController(orderableGroups);
});
});
Expand Down Expand Up @@ -414,12 +420,14 @@ describe('StockAdjustmentCreationController', function() {
});

it('should redirect with proper state params after success', function() {
console.log('test start');
spyOn(stockAdjustmentCreationService, 'submitAdjustments');
stockAdjustmentCreationService.submitAdjustments.andReturn(q.resolve());

vm.submit();
rootScope.$apply();

console.log('test end - before expect');

expect(alertService.error).not.toHaveBeenCalled();
});

Expand Down

0 comments on commit 0111379

Please sign in to comment.