Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement fetching on change and dimplify internal request service design #55

Merged
merged 1 commit into from
May 11, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
61 changes: 51 additions & 10 deletions app/controllers/internalRequestController.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
app.controller('InternalRequestController', function ($controller, $scope, ApiResponseActions, InternalRequestRepo, InternalRequestsService, ProductRepo, RemoteProductsByProductIdService, ProductsService) {
app.controller('InternalRequestController', function ($controller, $scope, ApiResponseActions, InternalRequestRepo, InternalRequestsService, ProductRepo, ProductsService, WsApi) {

angular.extend(this, $controller('AbstractController', {
$scope: $scope
Expand All @@ -12,6 +12,10 @@ app.controller('InternalRequestController', function ($controller, $scope, ApiRe

$scope.featureRequestToPush = {};

$scope.products = [];
$scope.remoteProducts = {};
$scope.remoteProductsLoading = false;

$scope.resetInternalRequestForms = function () {
InternalRequestRepo.clearValidationResults();

Expand All @@ -27,11 +31,40 @@ app.controller('InternalRequestController', function ($controller, $scope, ApiRe
$scope.resetInternalRequestForms();

if ($scope.isManager() || $scope.isAdmin()) {
$scope.remoteProductsByProduct = RemoteProductsByProductIdService.remoteProducts();
$scope.products = ProductsService.getProducts();
$scope.selectRemoteProducts = function () {
if ($scope.remoteProductsLoading === false) {
$scope.remoteProducts = {};

if (angular.isDefined($scope.featureRequestToPush.productId) && $scope.featureRequestToPush.productId !== null) {
var productId = $scope.featureRequestToPush.productId;
var remoteProducts = ProductsService.getRemoteProducts();

if (angular.isDefined(remoteProducts[productId])) {
angular.extend($scope.remoteProducts, remoteProducts[productId]);
} else {
$scope.refreshRemoteProducts(productId);
}
}
}
};

$scope.refreshRemoteProducts = function (productId) {
if ($scope.remoteProductsLoading === false) {
$scope.remoteProductsLoading = true;
$scope.remoteProducts = {};

ProductsService.refreshRemoteProducts(productId).then(null, null, function (res) {
remoteProducts = ProductsService.getRemoteProducts();

$scope.refreshProductRemoteProducts = function () {
RemoteProductsByProductIdService.refreshRemoteProductsByProductId($scope.featureRequestToPush.productId);
if (angular.isDefined(remoteProducts[productId])) {
angular.extend($scope.remoteProducts, remoteProducts[productId]);
}

$scope.remoteProductsLoading = false;
}).catch(function() {
$scope.remoteProductsLoading = false;
});
}
};

$scope.createInternalRequest = function () {
Expand Down Expand Up @@ -123,13 +156,21 @@ app.controller('InternalRequestController', function ($controller, $scope, ApiRe
});
};

ProductRepo.listen([ApiResponseActions.CREATE, ApiResponseActions.DELETE, ApiResponseActions.UPDATE], function () {
var products = ProductRepo.getAll();
ProductsService.ready.then(null, null, function () {
$scope.products = ProductsService.getProducts();
});

$scope.products.length = 0;
WsApi.listen(apiMapping.Product.listen).then(null, null, function (res) {
var productId = angular.isDefined($scope.featureRequestToPush.productId) ? $scope.featureRequestToPush.productId : null;

for (var i in products) {
$scope.products.push(products[i]);
ProductsService.refreshProducts();

if ($scope.remoteProductsLoading === false && productId !== null) {
var apiRes = angular.fromJson(res.body);

if (apiRes.meta.status === 'SUCCESS') {
$scope.refreshRemoteProducts(productId);
}
}
});
}
Expand Down
70 changes: 56 additions & 14 deletions app/services/productsService.js
Original file line number Diff line number Diff line change
@@ -1,60 +1,102 @@
app.service('ProductsService', function ($q, ProductRepo, WsApi) {
var productsService = this;
var service = this;

var products = {};
var products = [];
var remoteProducts = {};

var defer = $q.defer();

var process = function (response) {
var apiRes = angular.fromJson(response.body);

if (apiRes.meta.status === 'SUCCESS') {
products.length = 0;
angular.extend(products, apiRes.payload['ArrayList<Product>']);
defer.resolve();
ProductRepo.reset();
defer.notify();
} else {
console.error(apiRes.meta);
throw "Unable to retrieve products";
}
};

var processRemoteProduct = function (response, productId, deferRefresh) {
var apiRes = angular.fromJson(response.body);

if (apiRes.meta.status === 'SUCCESS') {
if (angular.isDefined(remoteProducts[productId])) {
for (var key in remoteProducts[productId]) {
remoteProducts[productId][key] = undefined;
}
} else {
remoteProducts[productId] = {};
}

angular.extend(remoteProducts[productId], apiRes.payload.HashMap);
deferRefresh.notify();
} else {
throw "Unable to retrieve remote products for product " + productId;
}
};

WsApi.listen(apiMapping.Product.listen).then(null, null, function (response) {
process(response);
});

productsService.refreshProducts = function () {
service.refreshProducts = function () {
WsApi.fetch(apiMapping.Product.all).then(function (response) {
process(response);

for (var productId in remoteProducts) {
service.refreshRemoteProducts(productId);
}
});
};

productsService.getProducts = function () {
return products;
service.refreshRemoteProducts = function (productId) {
var deferRefresh = $q.defer();
var options = {
pathValues: {
productId: productId
}
};

WsApi.fetch(apiMapping.RemoteProducts.byProduct, options).then(function (response) {
processRemoteProduct(response, productId, deferRefresh);
});

return deferRefresh.promise;
};

service.getRemoteProducts = function () {
return remoteProducts;
};

productsService.getProductRemoteProducts = function (productId) {
var remoteProducts = {};
service.getProducts = function () {
return products;
};

service.getRemoteProductInfo = function (productId) {
for (var i in products) {
if (products[i].id == productId) {
return products[i].remoteProducts;
}
}
};

productsService.getById = function (id) {
service.getById = function (id) {
return $q(function (resolve, reject) {
productsService.ready.then(function () {
service.ready.then(function () {
for (var i in products) {
if (products[i].id == id) {
resolve(products[i]);
}
}
});
}.bind(productsService));
}.bind(service));
};

productsService.refreshProducts();
service.refreshProducts();

productsService.ready = defer.promise;
service.ready = defer.promise;

});
66 changes: 0 additions & 66 deletions app/services/remoteProductsByProductIdService.js

This file was deleted.

8 changes: 4 additions & 4 deletions app/views/modals/pushInternalRequestModal.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ <h4 class="modal-title">Push Internal Request (Feature Request)</h4>

<form name="internalRequestForms.push" ng-submit="pushFeatureRequest()" novalidate>

<alerts seconds="60" channels="internal/request/push" types="WARNING,ERROR" exclusive></alerts>
<alerts seconds="60" channels="internal/request/push,products" types="WARNING,ERROR" exclusive></alerts>
<validationmessage results="internalRequestForms.getResults()"></validationmessage>

<div class="modal-body">
Expand All @@ -33,20 +33,20 @@ <h4 class="modal-title">Push Internal Request (Feature Request)</h4>
disabled="'true'"
form="internalRequest.push"
validations="internalRequestForms.validations"
results="internalRequestForms.getResults()"
results="internalRequestForms.getResults()"e
autocomplete="off">
</validatedinput>

<div class="form-group">
<label for="productId">Associate Product</label>
<select name="productId" class="form-control" ng-model="featureRequestToPush.productId" ng-options="product.id as product.name for product in products" ng-change="refreshProductRemoteProducts()">
<select name="productId" class="form-control" ng-model="featureRequestToPush.productId" ng-options="product.id as product.name for product in products | orderBy:'name'" ng-change="selectRemoteProducts()" ng-disabled="remoteProductsLoading">
<option value="" selected>None</option>
</select>
</div>

<div class="form-group" ng-if="featureRequestToPush.productId">
<label for="scopeId">Associate Remote Product</label>
<select name="scopeId" class="form-control" ng-model="featureRequestToPush.scopeId" ng-options="remoteProduct.id as remoteProduct.name for remoteProduct in remoteProductsByProduct">
<select name="scopeId" class="form-control" ng-model="featureRequestToPush.scopeId" ng-options="remoteProduct.id as remoteProduct.name for remoteProduct in remoteProducts" ng-disabled="remoteProductsLoading">
<option value="" selected>None</option>
</select>
</div>
Expand Down
42 changes: 33 additions & 9 deletions tests/mock/service/mockProductsService.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,50 @@
var mockProductsService = function ($q, ProductRepo, WsApi) {
var mockProductsService = function ($q, $timeout) {
var service = mockService($q);

var products = {};
var remoteProducts = {};

service.getById = function (id) {
// @todo
return {};
return $q(function (resolve, reject) {
for (var i in dataProductRepo1) {
if (dataProductRepo1[i].id === scopeId) {
resolve(dataProductRepo1[i]);
}
}
reject(undefined);
});
}

service.getProductRemoteProducts = function (id) {
// @todo
return {};
};

service.getProducts = function () {
return products;
};

service.getRemoteProductInfo = function (productId) {
for (var i in products) {
if (products[i].id == productId) {
return products[i];
}
}
};

service.getRemoteProducts = function () {
return remoteProducts;
};

service.refreshProducts = function () {
// @todo
return messagePromise();
};

service.refreshRemoteProducts = function (productId) {
if (dataFeatureRequest1.id == productId) {
return notifyPromise($timeout, $q.defer(), dataFeatureRequest1);
}

return rejectPromise($q.defer());
};

service.ready = $q.defer().promise;

return service;
};

Expand Down