diff --git a/client/client/src/billing-data/billing-data.spec.js b/client/client/src/billing-data/billing-data.spec.js index 89184c83c..b527b19f3 100644 --- a/client/client/src/billing-data/billing-data.spec.js +++ b/client/client/src/billing-data/billing-data.spec.js @@ -16,53 +16,48 @@ describe('Client', () => { $httpBackend = _$httpBackend_; $httpBackend.when('GET', /\/locale\/\w+\/[a-z]{2}\.json/).respond({}); $scope = $rootScope.$new(); - let submit = jasmine.createSpy('submit').and.returnValue(Promise.resolve()); - $scope.watcher = {submit}; + $scope.watcher = { + submit: () => { + return { + then: callback => { + callback(); + } + }; + } + }; $httpBackend.get = jasmine.createSpy('get').and.returnValue(Promise.resolve()); controller = $componentController('vnClientBillingData', {$scope: $scope}, {$http: $httpBackend}); + controller.client = {id: 101, name: 'Client name', payMethodFk: 4}; })); - describe('copyData()', () => { - it(`should define billData using client's data`, () => { - controller.client = { - dueDay: 0, - iban: null, - payMethodFk: 1 - }; - controller.billData = {}; - controller.copyData(controller.client); - - expect(controller.billData).toEqual(controller.client); + describe('client()', () => { + it(`should call setter client`, () => { + expect(controller.orgData).toEqual(controller.client); }); }); - describe('submit()', () => { - it(`should call submit() on the watcher then receive a callback`, done => { - spyOn(controller, 'checkPaymentChanges'); - controller.submit() - .then(() => { - expect(controller.$.watcher.submit).toHaveBeenCalledWith(); - expect(controller.checkPaymentChanges).toHaveBeenCalledWith(); - done(); - }); + describe('hasPaymethodChanged()', () => { + it(`should call hasPaymethodChanged() and return true if there are changes on payMethod data`, () => { + controller.client.payMethodFk = 5; + + expect(controller.hasPaymethodChanged()).toBeTruthy(); + }); + + it(`should call hasPaymethodChanged() and return false if there are no changes on payMethod data`, () => { + controller.client.payMethodFk = 4; + + expect(controller.hasPaymethodChanged()).toBeFalsy(); }); }); - describe('checkPaymentChanges()', () => { - it(`should not call sendMail.show() if there are no changes on billing data`, () => { - controller.billData = {marvelHero: 'Silver Surfer'}; - controller.client = {marvelHero: 'Silver Surfer'}; - controller.checkPaymentChanges(); + describe('onSubmit()', () => { + it(`should call notifyChanges() if there are changes on payMethod data`, () => { + spyOn(controller, 'notifyChanges'); + controller.client.payMethodFk = 5; + controller.onSubmit(); - expect(controller.$http.get).not.toHaveBeenCalled(); - }); - - it(`should call sendMail.show() if there are changes on billing data object`, () => { - controller.billData = {id: '123', marvelHero: 'Silver Surfer'}; - controller.client = {id: '123', marvelHero: 'Spider-Man'}; - controller.checkPaymentChanges(); - - expect(controller.$http.get).toHaveBeenCalled(); + expect(controller.hasPaymethodChanged()).toBeTruthy(); + expect(controller.notifyChanges).toHaveBeenCalledWith(); }); }); }); diff --git a/client/client/src/billing-data/index.html b/client/client/src/billing-data/index.html index 588ef2bc3..cf2672789 100644 --- a/client/client/src/billing-data/index.html +++ b/client/client/src/billing-data/index.html @@ -5,7 +5,7 @@ form="form" save="patch"> -
+ Pay method diff --git a/client/client/src/billing-data/index.js b/client/client/src/billing-data/index.js index 50fc4963e..947de8481 100644 --- a/client/client/src/billing-data/index.js +++ b/client/client/src/billing-data/index.js @@ -2,47 +2,42 @@ import ngModule from '../module'; export default class Controller { constructor($scope, $http, vnApp, $translate) { - this.$ = $scope; + this.$scope = $scope; this.$http = $http; this.vnApp = vnApp; this.translate = $translate; - this.billData = {}; - this.copyData(); } - $onChanges() { - this.copyData(); + set client(value) { + this._client = value; + + if (value) + this.orgData = Object.assign({}, value); } - copyData() { - if (this.client) { - this.billData.payMethodFk = this.client.payMethodFk; - this.billData.iban = this.client.iban; - this.billData.dueDay = this.client.dueDay; - } + get client() { + return this._client; } - submit() { - return this.$.watcher.submit().then( - () => this.checkPaymentChanges()); + onSubmit() { + this.$scope.watcher.submit().then(() => { + if (this.hasPaymethodChanged()) + this.notifyChanges(); + }); } - checkPaymentChanges() { - let equals = true; - Object.keys(this.billData).forEach( - val => { - if (this.billData[val] !== this.client[val]) { - this.billData[val] = this.client[val]; - equals = false; - } - } + notifyChanges() { + this.$http.get(`/mailer/notification/payment-update/${this.client.id}`).then( + () => this.vnApp.showMessage(this.translate.instant('Notification sent!')) ); + } - if (!equals) { - this.$http.get(`/mailer/notification/payment-update/${this.client.id}`).then( - () => this.vnApp.showMessage(this.translate.instant('Notification sent!')) - ); - } + hasPaymethodChanged() { + let payMethod = this.orgData.payMethodFk != this.client.payMethodFk; + let iban = this.orgData.iban != this.client.iban; + let dueDay = this.orgData.dueDay != this.client.dueDay; + + return payMethod || iban || dueDay; } } Controller.$inject = ['$scope', '$http', 'vnApp', '$translate']; diff --git a/client/ticket/src/create/card.spec.js b/client/ticket/src/create/card.spec.js index 3cd0d46ca..687aae7de 100644 --- a/client/ticket/src/create/card.spec.js +++ b/client/ticket/src/create/card.spec.js @@ -14,6 +14,7 @@ describe('Ticket', () => { beforeEach(angular.mock.inject((_$componentController_, $rootScope, _$httpBackend_) => { $componentController = _$componentController_; $httpBackend = _$httpBackend_; + $httpBackend.when('GET', /\/locale\/\w+\/[a-z]{2}\.json/).respond({}); $scope = $rootScope.$new(); controller = $componentController('vnTicketCreateCard', {$scope: $scope}); controller.item = {id: 3}; diff --git a/services/loopback/common/models/client.js b/services/loopback/common/models/client.js index c85a78571..9cd596be6 100644 --- a/services/loopback/common/models/client.js +++ b/services/loopback/common/models/client.js @@ -89,7 +89,9 @@ module.exports = Self => { Self.observe('before save', async function(ctx) { let changes = ctx.data || ctx.instance; + let orgData = ctx.currentInstance; let finalState = getFinalState(ctx); + let payMethodWithIban = 4; if (changes.salesPerson === null) { changes.credit = 0; @@ -97,7 +99,10 @@ module.exports = Self => { changes.payMethodFk = 5; // Credit card } - if (changes.payMethodFk !== undefined && changes.dueDay === undefined) + let payMethodFk = changes.payMethodFk || (orgData && orgData.payMethodFk); + let dueDay = changes.dueDay || (orgData && orgData.dueDay); + + if (payMethodFk == payMethodWithIban && dueDay == 0) changes.dueDay = 5; if (isMultiple(ctx)) return;