#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>
<mg-ajax
path="/client/api/Clients/{{edit.params.id}}/card"
actions="$ctrl.client = edit.model"
options="mgEdit">
</mg-ajax>
<vn-horizontal>
<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-auto>
<vn-one>

View File

@ -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'),

View File

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

View File

@ -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: '<'
}

View File

@ -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);

View File

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

View File

@ -9,8 +9,7 @@ class Controller {
set clientFk(value) {
if (!value) return;
this._getClient(value);
this._getClientDebt(value);
this.getCard(value);
}
set client(value) {
@ -39,15 +38,8 @@ class Controller {
return this._quicklinks;
}
_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 => {
Object.assign(this.client, response.data);
});

View File

@ -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);
});
});
});

View File

@ -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'),

View File

@ -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: {
$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);
});
});
});

View File

@ -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;
});

View File

@ -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);

View File

@ -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;
};
};

View File

@ -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);
});
});

View File

@ -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);