#593 Refactor card/descriptor/descriptor-popover

This commit is contained in:
Carlos Jimenez 2018-09-14 13:43:51 +02:00
parent d66383dced
commit e90af6b851
15 changed files with 84 additions and 107 deletions

View File

@ -1,12 +1,7 @@
<vn-main-block> <vn-main-block>
<mg-ajax
path="/client/api/Clients/{{edit.params.id}}/card"
actions="$ctrl.client = edit.model"
options="mgEdit">
</mg-ajax>
<vn-horizontal> <vn-horizontal>
<vn-auto class="left-block"> <vn-auto class="left-block">
<vn-client-descriptor client="$ctrl.client" clientFk="$ctrl.client.id"></vn-client-descriptor> <vn-client-descriptor client="$ctrl.client"></vn-client-descriptor>
<vn-left-menu></vn-left-menu> <vn-left-menu></vn-left-menu>
</vn-auto> </vn-auto>
<vn-one> <vn-one>

View File

@ -1,16 +1,28 @@
import ngModule from '../module'; import ngModule from '../module';
export default class Controller { export default class Controller {
constructor($scope) { constructor($scope, $stateParams, $http) {
this.$scope = $scope; this.$scope = $scope;
this.$http = $http;
this.$stateParams = $stateParams;
this.client = null; this.client = null;
} }
$onInit() {
this.getCard();
}
getCard() {
this.$http.get(`/client/api/Clients/${this.$stateParams.id}/getCard`).then(response => {
this.client = response.data;
});
}
reload() { reload() {
this.$scope.edit.accept(); this.getCard();
} }
} }
Controller.$inject = ['$scope']; Controller.$inject = ['$scope', '$stateParams', '$http'];
ngModule.component('vnClientCard', { ngModule.component('vnClientCard', {
template: require('./index.html'), template: require('./index.html'),

View File

@ -1,7 +1,6 @@
<vn-popover vn-id="popover"> <vn-popover vn-id="popover">
<vn-client-descriptor <vn-client-descriptor
client="$ctrl.client" client="$ctrl.client"
clientFk="$ctrl.clientFk"
quicklinks="$ctrl.quicklinks"> quicklinks="$ctrl.quicklinks">
</vn-client-descriptor> </vn-client-descriptor>
</vn-popover> </vn-popover>

View File

@ -14,8 +14,7 @@ class Controller extends Component {
set clientFk(value) { set clientFk(value) {
if (value) { if (value) {
this._getClient(value); this.getCard(value);
this._getClientDebt(value);
} }
} }
@ -32,15 +31,8 @@ class Controller extends Component {
this.clientFk = null; this.clientFk = null;
} }
_getClientDebt(clientFk) { getCard(clientFk) {
this.$http.get(`/client/api/Clients/${clientFk}/getDebt`) this.$http.get(`/client/api/Clients/${clientFk}/getCard`)
.then(response => {
this.clientDebt = response.data.debt;
});
}
_getClient(clientFk) {
this.$http.get(`/client/api/Clients/${clientFk}/card`)
.then(response => { .then(response => {
this.client = response.data; this.client = response.data;
}); });
@ -64,7 +56,6 @@ ngModule.component('vnClientDescriptorPopover', {
template: require('./index.html'), template: require('./index.html'),
controller: Controller, controller: Controller,
bindings: { bindings: {
client: '<',
clientFk: '<', clientFk: '<',
quicklinks: '<' quicklinks: '<'
} }

View File

@ -21,25 +21,13 @@ describe('Client', () => {
controller = $componentController('vnClientDescriptorPopover', {$scope: $scope, $element: $element}); controller = $componentController('vnClientDescriptorPopover', {$scope: $scope, $element: $element});
})); }));
describe('_getClientDebt()', () => { describe('getCard()', () => {
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()', () => {
it(`should perform a get query to store the client data into the controller`, () => { it(`should perform a get query to store the client data into the controller`, () => {
let clientFk = 1; let clientFk = 1;
let response = {id: 1, name: 'name'}; let response = {id: 1, name: 'name', debt: 1000};
$httpBackend.when('GET', `/client/api/Clients/${clientFk}/card`).respond(response); $httpBackend.when('GET', `/client/api/Clients/${clientFk}/getCard`).respond(response);
$httpBackend.expect('GET', `/client/api/Clients/${clientFk}/card`); $httpBackend.expect('GET', `/client/api/Clients/${clientFk}/getCard`);
controller._getClient(clientFk); controller.getCard(clientFk);
$httpBackend.flush(); $httpBackend.flush();
expect(controller.client).toEqual(response); expect(controller.client).toEqual(response);

View File

@ -45,7 +45,7 @@
<vn-icon <vn-icon
vn-tooltip="Client has debt" vn-tooltip="Client has debt"
icon="icon-risk" icon="icon-risk"
ng-class="{bright: $ctrl.clientDebt > 0}"> ng-class="{bright: $ctrl.client.debt > 0}">
</vn-icon> </vn-icon>
<vn-icon <vn-icon
vn-tooltip="Client not checked" vn-tooltip="Client not checked"

View File

@ -9,8 +9,7 @@ class Controller {
set clientFk(value) { set clientFk(value) {
if (!value) return; if (!value) return;
this._getClient(value); this.getCard(value);
this._getClientDebt(value);
} }
set client(value) { set client(value) {
@ -39,15 +38,8 @@ class Controller {
return this._quicklinks; return this._quicklinks;
} }
_getClientDebt(clientFk) { getCard(clientFk) {
this.$http.get(`/client/api/Clients/${clientFk}/getDebt`) this.$http.get(`/client/api/Clients/${clientFk}/getCard`)
.then(response => {
this.clientDebt = response.data.debt;
});
}
_getClient(clientFk) {
this.$http.get(`/client/api/Clients/${clientFk}/card`)
.then(response => { .then(response => {
Object.assign(this.client, response.data); Object.assign(this.client, response.data);
}); });

View File

@ -17,29 +17,17 @@ describe('Descriptor', () => {
controller = $componentController('vnClientDescriptor'); controller = $componentController('vnClientDescriptor');
})); }));
describe('_getClientDebt()', () => { describe('getCard()', () => {
it(`should call _getClientDebt() and define the clientDebt value on the controller`, () => { it(`should perform a query and store the client into the controller`, () => {
controller.client = {}; controller.client = {};
let response = {debt: 100}; let clientFk = 101;
$httpBackend.whenGET(`/client/api/Clients/101/getDebt`).respond(response); let response = {id: 101, name: 'Batman', debt: 100};
$httpBackend.expectGET(`/client/api/Clients/101/getDebt`); $httpBackend.when('GET', `/client/api/Clients/${clientFk}/getCard`).respond(response);
controller._getClientDebt(101); $httpBackend.expect('GET', `/client/api/Clients/${clientFk}/getCard`);
controller.getCard(clientFk);
$httpBackend.flush(); $httpBackend.flush();
expect(controller.clientDebt).toEqual(100); expect(controller.client).toEqual(response);
});
});
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');
}); });
}); });
}); });

View File

@ -1,24 +1,29 @@
import ngModule from '../module'; import ngModule from '../module';
class Controller { class Controller {
constructor($http, $state) { constructor($http, $stateParams) {
this.$http = $http; this.$http = $http;
this.$state = $state; this.$stateParams = $stateParams;
this.item = null; this.item = null;
} }
_getCard() { $onInit() {
this.$http.get(`/item/api/Items/${this.$state.params.id}/getCard`).then(response => { this.getCard();
}
getCard() {
this.$http.get(`/item/api/Items/${this.$stateParams.id}/getCard`).then(response => {
this.item = response.data; this.item = response.data;
}); });
} }
$onInit() { reload() {
this._getCard(); this.getCard();
}
} }
Controller.$inject = ['$http', '$state']; }
Controller.$inject = ['$http', '$stateParams'];
ngModule.component('vnItemCard', { ngModule.component('vnItemCard', {
template: require('./index.html'), template: require('./index.html'),

View File

@ -4,30 +4,32 @@ describe('Item', () => {
describe('Component vnItemCard', () => { describe('Component vnItemCard', () => {
let $componentController; let $componentController;
let $httpBackend; let $httpBackend;
let $state; let $stateParams;
let controller; let controller;
beforeEach(() => { beforeEach(() => {
angular.mock.module('item'); angular.mock.module('item');
}); });
beforeEach(angular.mock.inject((_$componentController_, _$state_, _$httpBackend_) => { beforeEach(angular.mock.inject((_$componentController_, _$stateParams_, _$httpBackend_) => {
$componentController = _$componentController_; $componentController = _$componentController_;
$httpBackend = _$httpBackend_; $httpBackend = _$httpBackend_;
$httpBackend.when('GET', /\/locale\/\w+\/[a-z]{2}\.json/).respond({}); $httpBackend.when('GET', /\/locale\/\w+\/[a-z]{2}\.json/).respond({});
$state = { $stateParams = {
params: {
id: 123 id: 123
}
}; };
controller = $componentController('vnItemCard', {$state: $state}); controller = $componentController('vnItemCard', {$stateParams});
})); }));
describe('_getCard()', () => { describe('getCard()', () => {
it('should request to get the card', () => { it('should request to set the item into the controller', () => {
$httpBackend.expectGET('/item/api/Items/123/getCard').respond(); let response = {id: 123};
controller._getCard(); $httpBackend.when('GET', `/item/api/Items/123/getCard`).respond(response);
$httpBackend.expect('GET', `/item/api/Items/123/getCard`);
controller.getCard();
$httpBackend.flush(); $httpBackend.flush();
expect(controller.item).toEqual(response);
}); });
}); });
}); });

View File

@ -15,7 +15,7 @@ class Controller extends Component {
this._itemFk = id; this._itemFk = id;
if (id) { if (id) {
this._getItem(); this.getCard();
} else } else
this.clear(); this.clear();
} }
@ -38,7 +38,8 @@ class Controller extends Component {
this.$.popover.parent = this.parent; this.$.popover.parent = this.parent;
this.$.popover.show(); this.$.popover.show();
} }
_getItem() {
getCard() {
this.$http.get(`/item/api/Items/${this._itemFk}/getCard`).then(response => { this.$http.get(`/item/api/Items/${this._itemFk}/getCard`).then(response => {
this.item = response.data; this.item = response.data;
}); });

View File

@ -25,11 +25,11 @@ describe('Item', () => {
})); }));
describe('itemFk setter', () => { describe('itemFk setter', () => {
it(`shoud set _itemFk to a given value and call _getItem if the given value is not null`, () => { it(`should set _itemFk to a given value and call getCard if the given value is not null`, () => {
spyOn(controller, '_getItem'); spyOn(controller, 'getCard');
controller.itemFk = 5; controller.itemFk = 5;
expect(controller._getItem).toHaveBeenCalledWith(); expect(controller.getCard).toHaveBeenCalledWith();
expect(controller._itemFk).toEqual(5); expect(controller._itemFk).toEqual(5);
}); });
@ -74,11 +74,11 @@ describe('Item', () => {
}); });
}); });
describe('_getItem()', () => { describe('getCard()', () => {
it(`should make a query and set this.item`, () => { it(`should make a query and set this.item`, () => {
$httpBackend.whenGET(`/item/api/Items/1/getCard`).respond(true); $httpBackend.whenGET(`/item/api/Items/1/getCard`).respond(true);
$httpBackend.expectGET(`/item/api/Items/1/getCard`); $httpBackend.expectGET(`/item/api/Items/1/getCard`);
controller._getItem(); controller.getCard();
$httpBackend.flush(); $httpBackend.flush();
expect(controller.item).toEqual(true); expect(controller.item).toEqual(true);

View File

@ -1,6 +1,6 @@
module.exports = function(Self) { module.exports = function(Self) {
Self.remoteMethod('card', { Self.remoteMethod('getCard', {
description: 'Get client basic data', description: 'Get client basic data and debt',
accepts: { accepts: {
arg: 'id', arg: 'id',
type: 'number', type: 'number',
@ -14,12 +14,12 @@ module.exports = function(Self) {
}, },
http: { http: {
verb: 'GET', verb: 'GET',
path: '/:id/card' path: '/:id/getCard'
} }
}); });
Self.card = async function(id) { Self.getCard = async function(id) {
return await Self.findOne({ let client = await Self.findOne({
where: { where: {
id: id 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;
}; };
}; };

View File

@ -3,11 +3,10 @@ const app = require(`${servicesDir}/client/server/server`);
describe('Client card', () => { describe('Client card', () => {
it('should call the card() method to receive a formated card of Bruce Wayne', async() => { it('should call the card() method to receive a formated card of Bruce Wayne', async() => {
let id = 101; let id = 101;
let result = await app.models.Client.card(id); let result = await app.models.Client.getCard(id);
expect(result).toEqual(jasmine.objectContaining({ expect(result.id).toEqual(101);
id: 101, expect(result.name).toEqual('Bruce Wayne');
name: 'Bruce Wayne' expect(result.debt).toEqual(1048.76);
}));
}); });
}); });

View File

@ -5,7 +5,7 @@ var isMultiple = require('../helpers').isMultiple;
module.exports = Self => { module.exports = Self => {
// Methods // Methods
require('../methods/client/activate')(Self); require('../methods/client/activate')(Self);
require('../methods/client/card')(Self); require('../methods/client/getCard')(Self);
require('../methods/client/createWithUser')(Self); require('../methods/client/createWithUser')(Self);
require('../methods/client/listWorkers')(Self); require('../methods/client/listWorkers')(Self);
require('../methods/client/hasCustomerRole')(Self); require('../methods/client/hasCustomerRole')(Self);