diff --git a/front/core/mocks/crud-model.js b/front/core/mocks/crud-model.js index af6406eec..7dd32e93f 100644 --- a/front/core/mocks/crud-model.js +++ b/front/core/mocks/crud-model.js @@ -1,4 +1,5 @@ -module.exports = { +const crudModel = { + _data: [1, 2, 3], data: [], filter: {}, order: {}, @@ -31,7 +32,28 @@ module.exports = { } }; }, - refresh: () => {}, - addFilter: () => {}, - applyFilter: () => {}, + refresh: () => { + return { + then: callback => { + return callback({data: {id: 1234}}); + } + }; + }, + addFilter: () => { + return { + then: callback => { + return callback({data: {id: 1234}}); + } + }; + }, + applyFilter: () => { + crudModel.data = crudModel._data; + return { + then: callback => { + return callback({data: {id: 1234}}); + } + }; + }, }; + +module.exports = crudModel; diff --git a/modules/client/back/methods/receipt/filter.js b/modules/client/back/methods/receipt/filter.js index 5c0d6c1dd..bf362f665 100644 --- a/modules/client/back/methods/receipt/filter.js +++ b/modules/client/back/methods/receipt/filter.js @@ -11,10 +11,15 @@ module.exports = Self => { description: 'Filter defining where, order, offset, and limit - must be a JSON-encoded string', http: {source: 'query'} }, { - arg: 'params', - type: 'Object', - description: 'clientFk', - http: {source: 'query'} + arg: 'clientFk', + type: 'Number', + description: 'The client id', + required: true, + }, { + arg: 'companyFk', + type: 'Number', + description: 'The company id', + required: true, } ], returns: { @@ -27,7 +32,7 @@ module.exports = Self => { } }); - Self.filter = async(filter, params) => { + Self.filter = async(filter, clientId, companyId) => { let stmt = new ParameterizedSQL( `SELECT * FROM ( SELECT @@ -72,10 +77,10 @@ module.exports = Self => { ORDER BY payed DESC, created DESC ) t ORDER BY payed DESC, created DESC`, [ - params.clientFk, - params.companyFk, - params.clientFk, - params.companyFk, + clientId, + companyId, + clientId, + companyId, ] ); diff --git a/modules/client/back/methods/receipt/specs/filter.spec.js b/modules/client/back/methods/receipt/specs/filter.spec.js index 5e8ccd807..2af3b3127 100644 --- a/modules/client/back/methods/receipt/specs/filter.spec.js +++ b/modules/client/back/methods/receipt/specs/filter.spec.js @@ -2,12 +2,10 @@ const app = require('vn-loopback/server/server'); describe('receipt filter()', () => { it('should return the receipts', async() => { - let filter = {limit: 20}; - let params = { - clientFk: 101, - companyFk: 442 - }; - let result = await app.models.Receipt.filter(filter, params); + const filter = {limit: 20}; + const clientId = 101; + const companyId = 442; + let result = await app.models.Receipt.filter(filter, clientId, companyId); expect(result.length).toBeGreaterThan(0); }); diff --git a/modules/client/front/balance/index/index.html b/modules/client/front/balance/index/index.html index 29f3d02e6..720668bcf 100644 --- a/modules/client/front/balance/index/index.html +++ b/modules/client/front/balance/index/index.html @@ -1,10 +1,8 @@ + data="$ctrl.balances"> { - if (response.data) { - this.clientRisks = response.data; - - this.getBalances(); + getData() { + return this.$.model.applyFilter(null, { + clientFk: this.$stateParams.id, + companyFk: this.companyFk + }).then(() => this.$.riskModel.applyFilter({ + where: { + clientFk: this.$stateParams.id, + companyFk: this.companyFk } - }); + })).then(() => this.getBalances()); } - get balances() { - return this._balances; - } getCurrentBalance() { - const selectedCompany = this.$.company.selection; - const currentBalance = this.clientRisks.find(balance => { - return balance.companyFk === selectedCompany.id; + const clientRisks = this.$.riskModel.data; + const selectedCompany = this.companyFk; + const currentBalance = clientRisks.find(balance => { + return balance.companyFk === selectedCompany; }); return currentBalance.amount; } getBalances() { - this.balances.forEach((balance, index) => { + const balances = this.$.model.data; + balances.forEach((balance, index) => { if (index === 0) balance.balance = this.getCurrentBalance(); if (index > 0) { - let previousBalance = this.balances[index - 1]; + let previousBalance = balances[index - 1]; balance.balance = previousBalance.balance - (previousBalance.debit - previousBalance.credit); } @@ -93,9 +72,7 @@ class Controller { openCreateDialog() { this.$.balanceCreateDialog.companyFk = this.companyFk; - this.$.balanceCreateDialog.onResponse = () => { - this.refresh(); - }; + this.$.balanceCreateDialog.onResponse = () => this.getData(); this.$.balanceCreateDialog.show(); } diff --git a/modules/client/front/balance/index/index.spec.js b/modules/client/front/balance/index/index.spec.js index 6de7bb3cb..a1b9a3ad7 100644 --- a/modules/client/front/balance/index/index.spec.js +++ b/modules/client/front/balance/index/index.spec.js @@ -3,41 +3,31 @@ import './index'; describe('Client', () => { describe('Component vnClientBalanceIndex', () => { let $componentController; - let $scope; - let $httpBackend; - let $httpParamSerializer; let controller; beforeEach(ngModule('client')); - beforeEach(angular.mock.inject((_$componentController_, $rootScope, _$httpBackend_, _$httpParamSerializer_) => { + beforeEach(angular.mock.inject((_$componentController_, $rootScope) => { $componentController = _$componentController_; - $httpBackend = _$httpBackend_; - $httpParamSerializer = _$httpParamSerializer_; - $scope = $rootScope.$new(); + let $scope = $rootScope.$new(); controller = $componentController('vnClientBalanceIndex', {$scope}); + controller._companyFk = 442; + controller.$stateParams.id = 101; + + controller.$.model = {applyFilter: () => {}}; + controller.$.riskModel = {applyFilter: () => {}}; })); - describe('balances() setter', () => { - it('should calculate the balance for each line from the oldest date to the newest', () => { - controller.getCurrentBalance = jasmine.createSpy(controller, 'getCurrentBalance').and.returnValue(1000); - let balances = [ - {credit: -100, debit: 0}, - {credit: 0, debit: 300}, - {credit: 100, debit: 0}, - {credit: 0, debit: -300} - ]; - const params = {filter: controller.filter}; - let serializedParams = $httpParamSerializer(params); - $httpBackend.when('GET', `ClientRisks?${serializedParams}`).respond(balances); - $httpBackend.expect('GET', `ClientRisks?${serializedParams}`); - controller.balances = balances; - $httpBackend.flush(); - - expect(controller.balances[0].balance).toEqual(1000); - expect(controller.balances[1].balance).toEqual(900); - expect(controller.balances[2].balance).toEqual(600); - expect(controller.balances[3].balance).toEqual(700); + describe('getData()', () => { + it('should apply the filters on he models and get the client balance', () => { + spyOn(controller, 'getBalances'); + spyOn(controller.$.model, 'applyFilter').and.returnValue(Promise.resolve()); + spyOn(controller.$.riskModel, 'applyFilter').and.returnValue(Promise.resolve()); + controller.getData().then(() => { + expect(controller.$.model.applyFilter).toHaveBeenCalledWith(null, {'clientFk': 101, 'companyFk': 442}); + expect(controller.$.riskModel.applyFilter).toHaveBeenCalledWith({'where': {'clientFk': 101, 'companyFk': 442}}); + expect(controller.getBalances).toHaveBeenCalledWith(); + }); }); }); });