diff --git a/modules/ticket/back/methods/state/editableStates.js b/modules/ticket/back/methods/state/editableStates.js new file mode 100644 index 0000000000..4979f48195 --- /dev/null +++ b/modules/ticket/back/methods/state/editableStates.js @@ -0,0 +1,36 @@ + +module.exports = Self => { + Self.remoteMethodCtx('editableStates', { + description: 'Gets the editable states according the user role ', + accessType: 'READ', + returns: { + type: ['Object'], + root: true + }, + http: { + path: `/editableStates`, + verb: 'GET' + } + }); + + Self.editableStates = async ctx => { + let userId = ctx.req.accessToken.userId; + let models = Self.app.models; + let statesList = await models.State.find(); + + let isProduction = await models.Account.hasRole(userId, 'production'); + let isSalesPerson = await models.Account.hasRole(userId, 'salesPerson'); + let isAdministrative = await models.Account.hasRole(userId, 'administrative'); + + if (isProduction || isAdministrative) + return statesList; + + if (isSalesPerson) { + return statesList = statesList.filter(stateList => + stateList.alertLevel === 0 || stateList.code === 'PICKER_DESIGNED' + ); + } + + return statesList.filter(stateList => stateList.alertLevel === 0); + }; +}; diff --git a/modules/ticket/back/methods/state/specs/editableState.spec.js b/modules/ticket/back/methods/state/specs/editableState.spec.js new file mode 100644 index 0000000000..d352c7c892 --- /dev/null +++ b/modules/ticket/back/methods/state/specs/editableState.spec.js @@ -0,0 +1,35 @@ +const app = require('vn-loopback/server/server'); + +describe('ticket editableStates()', () => { + it('should call the editableStates method with the production role and check that the result contain the DELIVERED state', async() => { + const ctx = {req: {accessToken: {userId: 49}}}; + let result = await app.models.State.editableStates(ctx); + let codeExists = result.some(state => state.code == 'DELIVERED'); + + expect(codeExists).toBeTruthy(); + }); + + it(`should call the editableStates method with the salesPerson role and check that the result not contain the DELIVERED state`, async() => { + const ctx = {req: {accessToken: {userId: 18}}}; + let result = await app.models.State.editableStates(ctx); + let codeExists = result.some(state => state.code == 'DELIVERED'); + + expect(codeExists).toBeFalsy(); + }); + + it(`should call the editableStates method with the salesPerson role and check that the result contain the PICKER_DESIGNED state`, async() => { + const ctx = {req: {accessToken: {userId: 18}}}; + let result = await app.models.State.editableStates(ctx); + let codeExists = result.some(state => state.code == 'PICKER_DESIGNED'); + + expect(codeExists).toBeTruthy(); + }); + + it(`should call the editableStates method with the employee role and check that the result not contain the PICKER_DESIGNED state`, async() => { + const ctx = {req: {accessToken: {userId: 1}}}; + let result = await app.models.State.editableStates(ctx); + let codeExists = result.some(state => state.code == 'PICKER_DESIGNED'); + + expect(codeExists).toBeFalsy(); + }); +}); diff --git a/modules/ticket/back/models/state.js b/modules/ticket/back/models/state.js index aaa0351d6f..252768237a 100644 --- a/modules/ticket/back/models/state.js +++ b/modules/ticket/back/models/state.js @@ -1,4 +1,6 @@ module.exports = Self => { + require('../methods/state/editableStates')(Self); + /** * Checks if the alertLevel of a state is 0. * diff --git a/modules/ticket/back/models/state.json b/modules/ticket/back/models/state.json index 73154edcab..efa56abee7 100644 --- a/modules/ticket/back/models/state.json +++ b/modules/ticket/back/models/state.json @@ -28,11 +28,5 @@ "type": "String", "required": false } - }, - "scopes" : { - "alertLevelIs0" : { - "fields": ["id", "name"], - "where": {"alertLevel": "0"} - } } } diff --git a/modules/ticket/front/sale/index.html b/modules/ticket/front/sale/index.html index 993b81003d..936cfe1bd4 100644 --- a/modules/ticket/front/sale/index.html +++ b/modules/ticket/front/sale/index.html @@ -18,7 +18,7 @@ disabled="!$ctrl.isEditable" label="State" value-field="id" - url="/ticket/api/States/alertLevelIs0" + url="/api/States/editableStates" on-change="$ctrl.onStateChange(value)">