From 48cd23750ec885f5e9feee19278b0cd7e66ad34d Mon Sep 17 00:00:00 2001 From: vicent Date: Tue, 18 Apr 2023 09:09:09 +0200 Subject: [PATCH] refs #5128 add testBack --- .../client/back/methods/client/setRating.js | 2 +- .../methods/client/specs/setRating.spec.js | 43 +++++++++++++++++++ modules/client/back/models/client.js | 2 +- .../front/credit-management/index.spec.js | 38 ---------------- 4 files changed, 45 insertions(+), 40 deletions(-) create mode 100644 modules/client/back/methods/client/specs/setRating.spec.js delete mode 100644 modules/client/front/credit-management/index.spec.js diff --git a/modules/client/back/methods/client/setRating.js b/modules/client/back/methods/client/setRating.js index 06e7ebf1e2..a57cdbd1cd 100644 --- a/modules/client/back/methods/client/setRating.js +++ b/modules/client/back/methods/client/setRating.js @@ -2,7 +2,7 @@ const UserError = require('vn-loopback/util/user-error'); module.exports = Self => { Self.remoteMethodCtx('setRating', { - description: 'Change role and hasGrant if user has setRating', + description: 'Change rating and recommendedCredit of a client', accepts: [ { arg: 'id', diff --git a/modules/client/back/methods/client/specs/setRating.spec.js b/modules/client/back/methods/client/specs/setRating.spec.js new file mode 100644 index 0000000000..a7d0fb03a0 --- /dev/null +++ b/modules/client/back/methods/client/specs/setRating.spec.js @@ -0,0 +1,43 @@ +const models = require('vn-loopback/server/server').models; +const LoopBackContext = require('loopback-context'); + +describe('Client setRating()', () => { + const financialId = 73; + const activeCtx = { + accessToken: {userId: financialId}, + http: { + req: { + headers: {origin: 'http://localhost'} + } + } + }; + const ctx = {req: activeCtx}; + + beforeAll(async() => { + spyOn(LoopBackContext, 'getCurrentContext').and.returnValue({ + active: activeCtx + }); + }); + + it('should change rating and recommendedCredit', async() => { + const tx = await models.Ticket.beginTransaction({}); + + try { + const options = {transaction: tx}; + + const clientId = 1101; + const newRating = 10; + const newRecommendedCredit = 20; + + const updatedClient = await models.Client.setRating(ctx, clientId, newRating, newRecommendedCredit, options); + + expect(updatedClient.rating).toEqual(newRating); + expect(updatedClient.recommendedCredit).toEqual(newRecommendedCredit); + + await tx.rollback(); + } catch (e) { + await tx.rollback(); + throw e; + } + }); +}); diff --git a/modules/client/back/models/client.js b/modules/client/back/models/client.js index 94c583dcca..5cabd18778 100644 --- a/modules/client/back/models/client.js +++ b/modules/client/back/models/client.js @@ -281,7 +281,7 @@ module.exports = Self => { await Self.changeCredit(ctx, finalState, changes); // Credit management changes - if (orgData.rating != changes.rating || orgData.recommendedCredit != changes.recommendedCredit) + if (orgData?.rating != changes.rating || orgData?.recommendedCredit != changes.recommendedCredit) await Self.changeCreditManagement(ctx, finalState, changes); const oldInstance = {}; diff --git a/modules/client/front/credit-management/index.spec.js b/modules/client/front/credit-management/index.spec.js deleted file mode 100644 index 0f6460a033..0000000000 --- a/modules/client/front/credit-management/index.spec.js +++ /dev/null @@ -1,38 +0,0 @@ -import './index'; - -describe('client unpaid', () => { - describe('Component vnClientUnpaid', () => { - let controller; - - beforeEach(ngModule('client')); - - beforeEach(inject($componentController => { - const $element = angular.element(''); - controller = $componentController('vnClientUnpaid', {$element}); - })); - - describe('setDefaultDate()', () => { - it(`should not set today date if has dated`, () => { - const hasData = true; - const yesterday = Date.vnNew(); - yesterday.setDate(yesterday.getDate() - 1); - - controller.clientUnpaid = { - dated: yesterday - }; - controller.setDefaultDate(hasData); - - expect(controller.clientUnpaid.dated).toEqual(yesterday); - }); - - it(`should set today if not has dated`, () => { - const hasData = true; - - controller.clientUnpaid = {}; - controller.setDefaultDate(hasData); - - expect(controller.clientUnpaid.dated).toBeDefined(); - }); - }); - }); -});