diff --git a/modules/order/back/methods/order/getItemTypeAvailable.js b/modules/order/back/methods/order/getItemTypeAvailable.js new file mode 100644 index 000000000..977a2cf85 --- /dev/null +++ b/modules/order/back/methods/order/getItemTypeAvailable.js @@ -0,0 +1,57 @@ +const app = require('vn-loopback/server/server'); +const ParameterizedSQL = require('loopback-connector').ParameterizedSQL; + +module.exports = Self => { + Self.remoteMethod('getItemTypeAvailable', { + description: 'Gets the item types available for an rder and item category ', + accessType: 'READ', + accepts: [{ + arg: 'id', + type: 'number', + required: true, + description: 'order id', + http: {source: 'path'} + }, + { + arg: 'itemCategoryId', + type: 'number', + required: true + }], + returns: { + type: 'number', + root: true + }, + http: { + path: `/:id/getItemTypeAvailable`, + verb: 'GET' + } + }); + + Self.getItemTypeAvailable = async(orderId, itemCategoryId) => { + let stmts = []; + let stmt; + + let order = await app.models.Order.findById(orderId); + stmt = new ParameterizedSQL('call vn.available_calc(?, ?, ?)', [ + order.landed, + order.addressFk, + order.agencyModeFk + ]); + stmts.push(stmt); + stmt = new ParameterizedSQL(`SELECT it.id, it.name + FROM tmp.availableCalc ac + JOIN cache.available a ON a.calc_id = ac.calcFk + JOIN item i ON i.id = a.item_id + JOIN itemType it ON it.id = i.typeFk + WHERE it.categoryFk = ? + GROUP BY it.id`, [ + itemCategoryId + ]); + let categoriesIndex = stmts.push(stmt) - 1; + + let sql = ParameterizedSQL.join(stmts, ';'); + let result = await Self.rawStmt(sql); + + return result[categoriesIndex]; + }; +}; diff --git a/modules/order/back/methods/order/specs/getItemTypeAvailable.spec.js b/modules/order/back/methods/order/specs/getItemTypeAvailable.spec.js new file mode 100644 index 000000000..66be6462a --- /dev/null +++ b/modules/order/back/methods/order/specs/getItemTypeAvailable.spec.js @@ -0,0 +1,19 @@ +const app = require('vn-loopback/server/server'); + +describe('order getItemTypeAvailable()', () => { + it('should call the getItemTypeAvailable method with a valid order and item category', async() => { + let orderId = 11; + let itemCategoryId = 1; + let result = await app.models.Order.getItemTypeAvailable(orderId, itemCategoryId); + + expect(result.length).toEqual(1); + }); + + it('should call the getItemTypeAvailable method with the same order and different item category', async() => { + let orderId = 11; + let itemCategoryId = 4;// + let result = await app.models.Order.getItemTypeAvailable(orderId, itemCategoryId); + + expect(result.length).toEqual(0); + }); +}); diff --git a/modules/order/back/models/order.js b/modules/order/back/models/order.js index dc8b7c352..b2f9da85e 100644 --- a/modules/order/back/models/order.js +++ b/modules/order/back/models/order.js @@ -13,4 +13,5 @@ module.exports = Self => { require('../methods/order/updateBasicData')(Self); require('../methods/order/confirm')(Self); require('../methods/order/filter')(Self); + require('../methods/order/getItemTypeAvailable')(Self); }; diff --git a/modules/order/front/catalog/index.js b/modules/order/front/catalog/index.js index 77136d654..a5d7ee968 100644 --- a/modules/order/front/catalog/index.js +++ b/modules/order/front/catalog/index.js @@ -5,6 +5,7 @@ class Controller { constructor($scope, $state) { this.$scope = $scope; this.$state = $state; + this.$stateParams = $state.params; // Static autocomplete data this.wayList = [ diff --git a/modules/order/front/filter/index.js b/modules/order/front/filter/index.js index 5e54b5cbc..4305f2506 100644 --- a/modules/order/front/filter/index.js +++ b/modules/order/front/filter/index.js @@ -91,8 +91,12 @@ class Controller { * Refreshes item type dropdown data */ updateItemTypes() { - const query = `/item/api/ItemCategories/${this.category.id}/itemTypes`; - this.$http.get(query).then(res => { + let params = { + itemCategoryId: this.category.id + }; + + const query = `/api/Orders/${this.order.id}/getItemTypeAvailable`; + this.$http.get(query, {params}).then(res => { this.itemTypes = res.data; }); } @@ -195,6 +199,6 @@ ngModule.component('vnCatalogFilter', { catalog: '^vnOrderCatalog' }, bindings: { - order: '<', + order: '<' } }); diff --git a/modules/order/front/filter/index.spec.js b/modules/order/front/filter/index.spec.js index 6c9ecee34..fe7f3e9ed 100644 --- a/modules/order/front/filter/index.spec.js +++ b/modules/order/front/filter/index.spec.js @@ -1,7 +1,7 @@ import './index.js'; import crudModel from 'core/mocks/crud-model'; -describe('Order', () => { +fdescribe('Order', () => { describe('Component vnCatalogFilter', () => { let $scope; let $state; @@ -30,7 +30,7 @@ describe('Order', () => { describe('order() setter', () => { it(`should call scope $applyAsync() method and apply filters from state params`, () => { - $httpBackend.expect('GET', `/item/api/ItemCategories/1/itemTypes`).respond(); + $httpBackend.expect('GET', `/api/Orders/4/getItemTypeAvailable?itemCategoryId=1`).respond(); spyOn(controller.$scope, '$applyAsync').and.callThrough(); controller.order = {id: 4}; @@ -53,6 +53,7 @@ describe('Order', () => { it(`should set category property and then call updateStateParams() and applyFilters() methods`, () => { spyOn(controller, 'updateStateParams'); + controller._order = {id: 4}; controller.category = {id: 2, value: 'My category'}; expect(controller.updateStateParams).toHaveBeenCalledWith();