diff --git a/services/loopback/common/methods/client/specs/updateFiscalData.spec.js b/services/loopback/common/methods/client/specs/updateFiscalData.spec.js new file mode 100644 index 000000000..d3deed3cc --- /dev/null +++ b/services/loopback/common/methods/client/specs/updateFiscalData.spec.js @@ -0,0 +1,51 @@ +const app = require(`${servicesDir}/client/server/server`); + +describe('Client updateFiscalData', () => { + afterAll(async() => { + let ctxOfAdmin = {req: {accessToken: {userId: 5}}}; + let validparams = {postcode: 46460}; + let idWithDataChecked = 101; + + await app.models.Client.updateFiscalData(ctxOfAdmin, validparams, idWithDataChecked); + }); + + it('should return an error if the user is not administrative and the isTaxDataChecked value is true', async() => { + let error; + + let ctxOfNoAdmin = {req: {accessToken: {userId: 1}}}; + let params = []; + let idWithDataChecked = 101; + + await app.models.Client.updateFiscalData(ctxOfNoAdmin, params, idWithDataChecked) + .catch(e => { + error = e; + }); + + expect(error.toString()).toContain(`You don't have enough privileges to do that`); + }); + + it('should return an error if the user is administrative and the isTaxDataChecked value is true BUT the params aint valid', async() => { + let error; + + let ctxOfAdmin = {req: {accessToken: {userId: 5}}}; + let invalidparams = {invalid: 'param for update'}; + let idWithDataChecked = 101; + + await app.models.Client.updateFiscalData(ctxOfAdmin, invalidparams, idWithDataChecked) + .catch(e => { + error = e; + }); + + expect(error.toString()).toContain(`You don't have enough privileges to do that`); + }); + + it('should update the client discal data and return the count if changes made', async() => { + let ctxOfAdmin = {req: {accessToken: {userId: 5}}}; + let validparams = {postcode: 12345}; + let idWithDataChecked = 101; + + let result = await app.models.Client.updateFiscalData(ctxOfAdmin, validparams, idWithDataChecked); + + expect(result).toEqual({count: 1}); + }); +}); diff --git a/services/loopback/common/methods/client/updateFiscalData.js b/services/loopback/common/methods/client/updateFiscalData.js index 3c74c44ea..148711034 100644 --- a/services/loopback/common/methods/client/updateFiscalData.js +++ b/services/loopback/common/methods/client/updateFiscalData.js @@ -31,13 +31,12 @@ module.exports = Self => { Self.updateFiscalData = async(ctx, params, id) => { let userId = ctx.req.accessToken.userId; let isAdministrative = await Self.app.models.Account.hasRole(userId, 'administrative'); - let [taxData] = await Self.app.models.Client.find({where: {id: id}, fields: ['isTaxDataChecked']}); + if (!isAdministrative && taxData.isTaxDataChecked) throw new UserError(`You don't have enough privileges to do that`); let validUpdateParams = [ - 'id', 'socialName', 'fi', 'street',