From b74da9da849ddc9af689ba52f18aeed2d7f4c92c Mon Sep 17 00:00:00 2001 From: Gerard Date: Wed, 3 Oct 2018 12:53:30 +0200 Subject: [PATCH 1/2] Tarea #600 changed client.update for update attributes to make it compatible with logs --- .../methods/client/specs/addressesPropagateRe.spec.js | 6 ++++-- .../common/methods/client/specs/updateBasicData.spec.js | 6 +----- .../common/methods/client/specs/updateFiscalData.spec.js | 6 +----- services/loopback/common/methods/client/updateBasicData.js | 4 ++-- services/loopback/common/methods/client/updateFiscalData.js | 4 +++- 5 files changed, 11 insertions(+), 15 deletions(-) diff --git a/services/loopback/common/methods/client/specs/addressesPropagateRe.spec.js b/services/loopback/common/methods/client/specs/addressesPropagateRe.spec.js index e9d3b26a29..b95f7a8866 100644 --- a/services/loopback/common/methods/client/specs/addressesPropagateRe.spec.js +++ b/services/loopback/common/methods/client/specs/addressesPropagateRe.spec.js @@ -1,14 +1,16 @@ const app = require(`${servicesDir}/client/server/server`); describe('Client addressesPropagateRe', () => { + let client; beforeEach(async() => { + client = await app.models.Client.findById(101); await app.models.Address.update({clientFk: 101}, {isEqualizated: false}); - await app.models.Client.update({id: 101}, {hasToInvoiceByAddress: true}); + await client.updateAttributes({hasToInvoiceByAddress: true}); }); afterAll(async() => { await app.models.Address.update({clientFk: 101}, {isEqualizated: false}); - await app.models.Client.update({id: 101}, {hasToInvoiceByAddress: true}); + await client.updateAttributes({hasToInvoiceByAddress: true}); }); it('should propagate the isEqualizated on both addresses of Mr Wayne and set hasToInvoiceByAddress to false', async() => { diff --git a/services/loopback/common/methods/client/specs/updateBasicData.spec.js b/services/loopback/common/methods/client/specs/updateBasicData.spec.js index 4ce89f66ad..a38e179dba 100644 --- a/services/loopback/common/methods/client/specs/updateBasicData.spec.js +++ b/services/loopback/common/methods/client/specs/updateBasicData.spec.js @@ -32,10 +32,6 @@ describe('Client updateBasicData', () => { let result = await app.models.Client.updateBasicData(validparams, id); - expect(result).toEqual({count: 1}); - - let updatedClient = await app.models.Client.findById(101); - - expect(updatedClient.email).toEqual('myNewEmail@myDomain.es'); + expect(result.email).toEqual('myNewEmail@myDomain.es'); }); }); diff --git a/services/loopback/common/methods/client/specs/updateFiscalData.spec.js b/services/loopback/common/methods/client/specs/updateFiscalData.spec.js index 32aedd3757..878dad7cc3 100644 --- a/services/loopback/common/methods/client/specs/updateFiscalData.spec.js +++ b/services/loopback/common/methods/client/specs/updateFiscalData.spec.js @@ -50,10 +50,6 @@ describe('Client updateFiscalData', () => { let result = await app.models.Client.updateFiscalData(ctxOfAdmin, validparams, idWithDataChecked); - expect(result).toEqual({count: 1}); - - let updatedClient = await app.models.Client.findById(idWithDataChecked); - - expect(updatedClient.postcode).toEqual('12345'); + expect(result.postcode).toEqual('12345'); }); }); diff --git a/services/loopback/common/methods/client/updateBasicData.js b/services/loopback/common/methods/client/updateBasicData.js index d2618aa8e0..33721a5c57 100644 --- a/services/loopback/common/methods/client/updateBasicData.js +++ b/services/loopback/common/methods/client/updateBasicData.js @@ -43,7 +43,7 @@ module.exports = Self => { if (validUpdateParams.indexOf(key) === -1) throw new UserError(`You don't have enough privileges to do that`); } - - return await Self.app.models.Client.update({id: id}, params); + let client = await Self.app.models.Client.findById(id); + return await client.updateAttributes(params); }; }; diff --git a/services/loopback/common/methods/client/updateFiscalData.js b/services/loopback/common/methods/client/updateFiscalData.js index 148711034c..7384d4417b 100644 --- a/services/loopback/common/methods/client/updateFiscalData.js +++ b/services/loopback/common/methods/client/updateFiscalData.js @@ -61,6 +61,8 @@ module.exports = Self => { } params.id = id; - return await Self.app.models.Client.update({id: id}, params); + + let client = await Self.app.models.Client.findById(id); + return await client.updateAttributes(params); }; }; From 1972a40c480179720aea7ad2fa40c47cae62dbf7 Mon Sep 17 00:00:00 2001 From: Gerard Date: Wed, 3 Oct 2018 12:54:13 +0200 Subject: [PATCH 2/2] Tarea #600 make it test friendly --- services/loopback/common/models/loggable.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/services/loopback/common/models/loggable.js b/services/loopback/common/models/loggable.js index df1640a966..fc61d21751 100644 --- a/services/loopback/common/models/loggable.js +++ b/services/loopback/common/models/loggable.js @@ -125,8 +125,13 @@ module.exports = function(Self) { Object.assign(newInstance, ctx.hookState.newInstance) } + + let userFk; + if (!loopBackContext) userFk = 9; + else userFk = loopBackContext.active.accessToken.userId; + let action = setActionType(ctx); - let userFk = loopBackContext.active.accessToken.userId; + let logRecord = { originFk: originId,