From e90af6b85196c1ba811a61ad801bcab485bd03cf Mon Sep 17 00:00:00 2001 From: Carlos Jimenez <=> Date: Fri, 14 Sep 2018 13:43:51 +0200 Subject: [PATCH] #593 Refactor card/descriptor/descriptor-popover --- client/client/src/card/index.html | 7 +---- client/client/src/card/index.js | 18 ++++++++++-- .../client/src/descriptor-popover/index.html | 1 - client/client/src/descriptor-popover/index.js | 15 ++-------- .../src/descriptor-popover/index.spec.js | 22 ++++----------- client/client/src/descriptor/index.html | 2 +- client/client/src/descriptor/index.js | 14 ++-------- client/client/src/descriptor/index.spec.js | 28 ++++++------------- client/item/src/card/index.js | 19 ++++++++----- client/item/src/card/index.spec.js | 24 ++++++++-------- client/item/src/descriptor-popover/index.js | 5 ++-- .../item/src/descriptor-popover/index.spec.js | 10 +++---- .../methods/client/{card.js => getCard.js} | 15 ++++++---- .../specs/{card.spec.js => getCard.spec.js} | 9 +++--- services/loopback/common/models/client.js | 2 +- 15 files changed, 84 insertions(+), 107 deletions(-) rename services/loopback/common/methods/client/{card.js => getCard.js} (80%) rename services/loopback/common/methods/client/specs/{card.spec.js => getCard.spec.js} (53%) diff --git a/client/client/src/card/index.html b/client/client/src/card/index.html index e25e97d3a..3a5d3ea65 100644 --- a/client/client/src/card/index.html +++ b/client/client/src/card/index.html @@ -1,12 +1,7 @@ - - - + diff --git a/client/client/src/card/index.js b/client/client/src/card/index.js index 1e3e4a754..a17a01883 100644 --- a/client/client/src/card/index.js +++ b/client/client/src/card/index.js @@ -1,16 +1,28 @@ import ngModule from '../module'; export default class Controller { - constructor($scope) { + constructor($scope, $stateParams, $http) { this.$scope = $scope; + this.$http = $http; + this.$stateParams = $stateParams; this.client = null; } + $onInit() { + this.getCard(); + } + + getCard() { + this.$http.get(`/client/api/Clients/${this.$stateParams.id}/getCard`).then(response => { + this.client = response.data; + }); + } + reload() { - this.$scope.edit.accept(); + this.getCard(); } } -Controller.$inject = ['$scope']; +Controller.$inject = ['$scope', '$stateParams', '$http']; ngModule.component('vnClientCard', { template: require('./index.html'), diff --git a/client/client/src/descriptor-popover/index.html b/client/client/src/descriptor-popover/index.html index e0f1bd33d..9ae559ba0 100644 --- a/client/client/src/descriptor-popover/index.html +++ b/client/client/src/descriptor-popover/index.html @@ -1,7 +1,6 @@ diff --git a/client/client/src/descriptor-popover/index.js b/client/client/src/descriptor-popover/index.js index fc1333851..abe165b43 100644 --- a/client/client/src/descriptor-popover/index.js +++ b/client/client/src/descriptor-popover/index.js @@ -14,8 +14,7 @@ class Controller extends Component { set clientFk(value) { if (value) { - this._getClient(value); - this._getClientDebt(value); + this.getCard(value); } } @@ -32,15 +31,8 @@ class Controller extends Component { this.clientFk = null; } - _getClientDebt(clientFk) { - this.$http.get(`/client/api/Clients/${clientFk}/getDebt`) - .then(response => { - this.clientDebt = response.data.debt; - }); - } - - _getClient(clientFk) { - this.$http.get(`/client/api/Clients/${clientFk}/card`) + getCard(clientFk) { + this.$http.get(`/client/api/Clients/${clientFk}/getCard`) .then(response => { this.client = response.data; }); @@ -64,7 +56,6 @@ ngModule.component('vnClientDescriptorPopover', { template: require('./index.html'), controller: Controller, bindings: { - client: '<', clientFk: '<', quicklinks: '<' } diff --git a/client/client/src/descriptor-popover/index.spec.js b/client/client/src/descriptor-popover/index.spec.js index 6f55db527..d746dc69c 100644 --- a/client/client/src/descriptor-popover/index.spec.js +++ b/client/client/src/descriptor-popover/index.spec.js @@ -21,25 +21,13 @@ describe('Client', () => { controller = $componentController('vnClientDescriptorPopover', {$scope: $scope, $element: $element}); })); - describe('_getClientDebt()', () => { - it(`should perform a get query to store the client debt into the controller`, () => { - let clientFk = 1; - $httpBackend.when('GET', `/client/api/Clients/${clientFk}/getDebt`).respond({debt: 100}); - $httpBackend.expect('GET', `/client/api/Clients/${clientFk}/getDebt`); - controller._getClientDebt(clientFk); - $httpBackend.flush(); - - expect(controller.clientDebt).toEqual(100); - }); - }); - - describe('_getClient()', () => { + describe('getCard()', () => { it(`should perform a get query to store the client data into the controller`, () => { let clientFk = 1; - let response = {id: 1, name: 'name'}; - $httpBackend.when('GET', `/client/api/Clients/${clientFk}/card`).respond(response); - $httpBackend.expect('GET', `/client/api/Clients/${clientFk}/card`); - controller._getClient(clientFk); + let response = {id: 1, name: 'name', debt: 1000}; + $httpBackend.when('GET', `/client/api/Clients/${clientFk}/getCard`).respond(response); + $httpBackend.expect('GET', `/client/api/Clients/${clientFk}/getCard`); + controller.getCard(clientFk); $httpBackend.flush(); expect(controller.client).toEqual(response); diff --git a/client/client/src/descriptor/index.html b/client/client/src/descriptor/index.html index 4e002c428..8c51cdc7b 100644 --- a/client/client/src/descriptor/index.html +++ b/client/client/src/descriptor/index.html @@ -45,7 +45,7 @@ + ng-class="{bright: $ctrl.client.debt > 0}"> { - this.clientDebt = response.data.debt; - }); - } - - _getClient(clientFk) { - this.$http.get(`/client/api/Clients/${clientFk}/card`) + getCard(clientFk) { + this.$http.get(`/client/api/Clients/${clientFk}/getCard`) .then(response => { Object.assign(this.client, response.data); }); diff --git a/client/client/src/descriptor/index.spec.js b/client/client/src/descriptor/index.spec.js index 58d348f43..3be937790 100644 --- a/client/client/src/descriptor/index.spec.js +++ b/client/client/src/descriptor/index.spec.js @@ -17,29 +17,17 @@ describe('Descriptor', () => { controller = $componentController('vnClientDescriptor'); })); - describe('_getClientDebt()', () => { - it(`should call _getClientDebt() and define the clientDebt value on the controller`, () => { + describe('getCard()', () => { + it(`should perform a query and store the client into the controller`, () => { controller.client = {}; - let response = {debt: 100}; - $httpBackend.whenGET(`/client/api/Clients/101/getDebt`).respond(response); - $httpBackend.expectGET(`/client/api/Clients/101/getDebt`); - controller._getClientDebt(101); + let clientFk = 101; + let response = {id: 101, name: 'Batman', debt: 100}; + $httpBackend.when('GET', `/client/api/Clients/${clientFk}/getCard`).respond(response); + $httpBackend.expect('GET', `/client/api/Clients/${clientFk}/getCard`); + controller.getCard(clientFk); $httpBackend.flush(); - expect(controller.clientDebt).toEqual(100); - }); - }); - - describe('_getClient()', () => { - it(`should call _getClient() and define the client value on the controller`, () => { - controller.client = {}; - let response = {id: 101, name: 'Batman'}; - $httpBackend.whenGET(`/client/api/Clients/101/card`).respond(response); - $httpBackend.expectGET(`/client/api/Clients/101/card`); - controller._getClient(101); - $httpBackend.flush(); - - expect(controller.client.name).toEqual('Batman'); + expect(controller.client).toEqual(response); }); }); }); diff --git a/client/item/src/card/index.js b/client/item/src/card/index.js index 0be6be0f0..d3ce4a208 100644 --- a/client/item/src/card/index.js +++ b/client/item/src/card/index.js @@ -1,24 +1,29 @@ import ngModule from '../module'; class Controller { - constructor($http, $state) { + constructor($http, $stateParams) { this.$http = $http; - this.$state = $state; + this.$stateParams = $stateParams; this.item = null; } - _getCard() { - this.$http.get(`/item/api/Items/${this.$state.params.id}/getCard`).then(response => { + $onInit() { + this.getCard(); + } + + getCard() { + this.$http.get(`/item/api/Items/${this.$stateParams.id}/getCard`).then(response => { this.item = response.data; }); } - $onInit() { - this._getCard(); + reload() { + this.getCard(); } + } -Controller.$inject = ['$http', '$state']; +Controller.$inject = ['$http', '$stateParams']; ngModule.component('vnItemCard', { template: require('./index.html'), diff --git a/client/item/src/card/index.spec.js b/client/item/src/card/index.spec.js index 2e654661d..fc20736b1 100644 --- a/client/item/src/card/index.spec.js +++ b/client/item/src/card/index.spec.js @@ -4,30 +4,32 @@ describe('Item', () => { describe('Component vnItemCard', () => { let $componentController; let $httpBackend; - let $state; + let $stateParams; let controller; beforeEach(() => { angular.mock.module('item'); }); - beforeEach(angular.mock.inject((_$componentController_, _$state_, _$httpBackend_) => { + beforeEach(angular.mock.inject((_$componentController_, _$stateParams_, _$httpBackend_) => { $componentController = _$componentController_; $httpBackend = _$httpBackend_; $httpBackend.when('GET', /\/locale\/\w+\/[a-z]{2}\.json/).respond({}); - $state = { - params: { - id: 123 - } + $stateParams = { + id: 123 }; - controller = $componentController('vnItemCard', {$state: $state}); + controller = $componentController('vnItemCard', {$stateParams}); })); - describe('_getCard()', () => { - it('should request to get the card', () => { - $httpBackend.expectGET('/item/api/Items/123/getCard').respond(); - controller._getCard(); + describe('getCard()', () => { + it('should request to set the item into the controller', () => { + let response = {id: 123}; + $httpBackend.when('GET', `/item/api/Items/123/getCard`).respond(response); + $httpBackend.expect('GET', `/item/api/Items/123/getCard`); + controller.getCard(); $httpBackend.flush(); + + expect(controller.item).toEqual(response); }); }); }); diff --git a/client/item/src/descriptor-popover/index.js b/client/item/src/descriptor-popover/index.js index cf7697ec2..644a14c1d 100644 --- a/client/item/src/descriptor-popover/index.js +++ b/client/item/src/descriptor-popover/index.js @@ -15,7 +15,7 @@ class Controller extends Component { this._itemFk = id; if (id) { - this._getItem(); + this.getCard(); } else this.clear(); } @@ -38,7 +38,8 @@ class Controller extends Component { this.$.popover.parent = this.parent; this.$.popover.show(); } - _getItem() { + + getCard() { this.$http.get(`/item/api/Items/${this._itemFk}/getCard`).then(response => { this.item = response.data; }); diff --git a/client/item/src/descriptor-popover/index.spec.js b/client/item/src/descriptor-popover/index.spec.js index e392fde6b..08754858c 100644 --- a/client/item/src/descriptor-popover/index.spec.js +++ b/client/item/src/descriptor-popover/index.spec.js @@ -25,11 +25,11 @@ describe('Item', () => { })); describe('itemFk setter', () => { - it(`shoud set _itemFk to a given value and call _getItem if the given value is not null`, () => { - spyOn(controller, '_getItem'); + it(`should set _itemFk to a given value and call getCard if the given value is not null`, () => { + spyOn(controller, 'getCard'); controller.itemFk = 5; - expect(controller._getItem).toHaveBeenCalledWith(); + expect(controller.getCard).toHaveBeenCalledWith(); expect(controller._itemFk).toEqual(5); }); @@ -74,11 +74,11 @@ describe('Item', () => { }); }); - describe('_getItem()', () => { + describe('getCard()', () => { it(`should make a query and set this.item`, () => { $httpBackend.whenGET(`/item/api/Items/1/getCard`).respond(true); $httpBackend.expectGET(`/item/api/Items/1/getCard`); - controller._getItem(); + controller.getCard(); $httpBackend.flush(); expect(controller.item).toEqual(true); diff --git a/services/loopback/common/methods/client/card.js b/services/loopback/common/methods/client/getCard.js similarity index 80% rename from services/loopback/common/methods/client/card.js rename to services/loopback/common/methods/client/getCard.js index 5832a9b6e..6a9331a86 100644 --- a/services/loopback/common/methods/client/card.js +++ b/services/loopback/common/methods/client/getCard.js @@ -1,6 +1,6 @@ module.exports = function(Self) { - Self.remoteMethod('card', { - description: 'Get client basic data', + Self.remoteMethod('getCard', { + description: 'Get client basic data and debt', accepts: { arg: 'id', type: 'number', @@ -14,12 +14,12 @@ module.exports = function(Self) { }, http: { verb: 'GET', - path: '/:id/card' + path: '/:id/getCard' } }); - Self.card = async function(id) { - return await Self.findOne({ + Self.getCard = async function(id) { + let client = await Self.findOne({ where: { id: id }, @@ -57,5 +57,10 @@ module.exports = function(Self) { } ] }); + + let query = `SELECT vn.clientGetDebt(?, CURDATE()) AS debt`; + client.debt = (await Self.rawSql(query, [id]))[0].debt; + + return client; }; }; diff --git a/services/loopback/common/methods/client/specs/card.spec.js b/services/loopback/common/methods/client/specs/getCard.spec.js similarity index 53% rename from services/loopback/common/methods/client/specs/card.spec.js rename to services/loopback/common/methods/client/specs/getCard.spec.js index 4a8afbeab..6b5f0e2b7 100644 --- a/services/loopback/common/methods/client/specs/card.spec.js +++ b/services/loopback/common/methods/client/specs/getCard.spec.js @@ -3,11 +3,10 @@ const app = require(`${servicesDir}/client/server/server`); describe('Client card', () => { it('should call the card() method to receive a formated card of Bruce Wayne', async() => { let id = 101; - let result = await app.models.Client.card(id); + let result = await app.models.Client.getCard(id); - expect(result).toEqual(jasmine.objectContaining({ - id: 101, - name: 'Bruce Wayne' - })); + expect(result.id).toEqual(101); + expect(result.name).toEqual('Bruce Wayne'); + expect(result.debt).toEqual(1048.76); }); }); diff --git a/services/loopback/common/models/client.js b/services/loopback/common/models/client.js index 9a246fac1..41149b30a 100644 --- a/services/loopback/common/models/client.js +++ b/services/loopback/common/models/client.js @@ -5,7 +5,7 @@ var isMultiple = require('../helpers').isMultiple; module.exports = Self => { // Methods require('../methods/client/activate')(Self); - require('../methods/client/card')(Self); + require('../methods/client/getCard')(Self); require('../methods/client/createWithUser')(Self); require('../methods/client/listWorkers')(Self); require('../methods/client/hasCustomerRole')(Self);