From 7eea9d1d2c08b43be68d475b4710dc9738210191 Mon Sep 17 00:00:00 2001 From: Bernat Date: Mon, 19 Aug 2019 07:56:20 +0200 Subject: [PATCH] Refactor #1630 state.js --- back/models/account.js | 13 +++++----- .../05_tracking_state.spec.js | 21 +++------------- e2e/paths/05-ticket-module/16_summary.spec.js | 8 ------ front/salix/components/main-menu/main-menu.js | 5 ++-- .../methods/ticket-tracking/changeState.js | 19 ++++++-------- .../ticket-tracking/specs/changeState.spec.js | 25 ++++--------------- modules/ticket/back/models/state.js | 4 --- modules/ticket/front/locale/es.yml | 2 +- modules/ticket/front/tracking/edit/index.html | 2 +- modules/ticket/front/tracking/edit/index.js | 1 + 10 files changed, 29 insertions(+), 71 deletions(-) diff --git a/back/models/account.js b/back/models/account.js index 12d1aca65..40b7e9c12 100644 --- a/back/models/account.js +++ b/back/models/account.js @@ -19,8 +19,8 @@ module.exports = Self => { next(); }); - Self.remoteMethod('getCurrentUserName', { - description: 'Gets the current user name', + Self.remoteMethod('getCurrentUserData', { + description: 'Gets the current user data', accepts: [ { arg: 'context', @@ -31,21 +31,22 @@ module.exports = Self => { } ], returns: { - type: 'string', + type: 'object', root: true }, http: { verb: 'GET', - path: '/getCurrentUserName' + path: '/getCurrentUserData' } }); - Self.getCurrentUserName = async function(ctx) { + Self.getCurrentUserData = async function(ctx) { let filter = {fields: ['name']}; let userId = ctx.req.accessToken.userId; let account = await Self.findById(userId, filter); + let worker = await Self.app.models.Worker.findOne({where: {userFk: userId}, fields: ['id']}); - return account.name; + return {accountName: account.name, workerId: worker.id}; }; /** diff --git a/e2e/paths/05-ticket-module/05_tracking_state.spec.js b/e2e/paths/05-ticket-module/05_tracking_state.spec.js index a67c3fc1d..5797b6798 100644 --- a/e2e/paths/05-ticket-module/05_tracking_state.spec.js +++ b/e2e/paths/05-ticket-module/05_tracking_state.spec.js @@ -30,17 +30,6 @@ describe('Ticket Create new tracking state path', () => { expect(result).toEqual('State cannot be blank'); }); - it(`should attempt create a new state then clear and save it`, async() => { - let result = await nightmare - .autocompleteSearch(selectors.createStateView.stateAutocomplete, '¿Fecha?') - .waitToClick(selectors.createStateView.clearStateInputButton) - .waitToClick(selectors.createStateView.saveStateButton) - .waitForLastSnackbar(); - - expect(result).toEqual('State cannot be blank'); - }); - - it(`should create a new state`, async() => { let result = await nightmare .autocompleteSearch(selectors.createStateView.stateAutocomplete, '¿Fecha?') @@ -77,18 +66,16 @@ describe('Ticket Create new tracking state path', () => { expect(result).toEqual(`You don't have enough privileges`); }); - it(`should attempt to create an state for the type salesPerson has rights but fail as worker is blank`, async() => { + it(`should make sure the worker gets autocomplete uppon selecting the assigned state`, async() => { let result = await nightmare .autocompleteSearch(selectors.createStateView.stateAutocomplete, 'asignado') - .waitToClick(selectors.createStateView.saveStateButton) - .waitForLastSnackbar(); + .waitToGetProperty(`${selectors.createStateView.workerAutocomplete} input`, 'value'); - expect(result).toEqual(`Worker cannot be blank`); + expect(result).toEqual('salesPersonNick'); }); - it(`should create a new state with all it's data`, async() => { + it(`should succesfully create a valid state`, async() => { let result = await nightmare - .autocompleteSearch(selectors.createStateView.workerAutocomplete, 'replenisher') .waitToClick(selectors.createStateView.saveStateButton) .waitForLastSnackbar(); diff --git a/e2e/paths/05-ticket-module/16_summary.spec.js b/e2e/paths/05-ticket-module/16_summary.spec.js index d532bc139..0fd3b8b7c 100644 --- a/e2e/paths/05-ticket-module/16_summary.spec.js +++ b/e2e/paths/05-ticket-module/16_summary.spec.js @@ -69,14 +69,6 @@ describe('Ticket Summary path', () => { expect(exists).toBeTruthy(); }); - it('should click on the SET OK button and throw a privileges error', async() => { - let result = await nightmare - .waitToClick(selectors.ticketSummary.setOk) - .waitForLastSnackbar(); - - expect(result).toEqual(`You don't have enough privileges`); - }); - it('should log in as production then navigate to the summary of the same ticket', async() => { let url = await nightmare .loginAndModule('production', 'ticket') diff --git a/front/salix/components/main-menu/main-menu.js b/front/salix/components/main-menu/main-menu.js index 287c89d97..19118a86c 100644 --- a/front/salix/components/main-menu/main-menu.js +++ b/front/salix/components/main-menu/main-menu.js @@ -16,9 +16,10 @@ export default class MainMenu { } getCurrentUserName() { - this.$http.get('/api/Accounts/getCurrentUserName') + this.$http.get('/api/Accounts/getCurrentUserData') .then(json => { - this.$.currentUserName = json.data; + this.$.currentUserName = json.data.accountName; + window.localStorage.currentUserWorkerId = json.data.workerId; }); } diff --git a/modules/ticket/back/methods/ticket-tracking/changeState.js b/modules/ticket/back/methods/ticket-tracking/changeState.js index 9a4c63ece..065692137 100644 --- a/modules/ticket/back/methods/ticket-tracking/changeState.js +++ b/modules/ticket/back/methods/ticket-tracking/changeState.js @@ -35,29 +35,24 @@ module.exports = Self => { params.stateFk = state.id; } - let isProduction = await models.Account.hasRole(userId, 'production'); - let isSalesPerson = await models.Account.hasRole(userId, 'salesPerson'); + if (!params.workerFk) { + let worker = await models.Worker.findOne({where: {userFk: userId}}); + params.workerFk = worker.id; + } let ticket = await models.TicketState.findById( params.ticketFk, {fields: ['stateFk']} ); - let oldState = await models.State.findById(ticket.stateFk); - let newState = await models.State.findById(params.stateFk); + let oldStateAllowed = await models.State.isEditable(ctx, ticket.stateFk); + let newStateAllowed = await models.State.isEditable(ctx, params.stateFk); - let isAllowed = isProduction || isSalesPerson - && oldState.isEditable() - && newState.isEditable(); + let isAllowed = oldStateAllowed && newStateAllowed; if (!isAllowed) throw new UserError(`You don't have enough privileges`, 'ACCESS_DENIED'); - if (newState.code != 'PICKER_DESIGNED') { - let worker = await models.Worker.findOne({where: {userFk: userId}}); - params.workerFk = worker.id; - } - return await models.TicketTracking.create(params); }; }; diff --git a/modules/ticket/back/methods/ticket-tracking/specs/changeState.spec.js b/modules/ticket/back/methods/ticket-tracking/specs/changeState.spec.js index 07f76ce01..74a1ebde0 100644 --- a/modules/ticket/back/methods/ticket-tracking/specs/changeState.spec.js +++ b/modules/ticket/back/methods/ticket-tracking/specs/changeState.spec.js @@ -31,24 +31,9 @@ describe('ticket changeState()', () => { expect(errCode).toBe('ACCESS_DENIED'); }); - it('should throw an error if the state is assigned and theres not worker in params', async() => { - let ctx = {req: {accessToken: {userId: 18}}}; - let assignedState = await app.models.State.findOne({where: {code: 'PICKER_DESIGNED'}}); - let params = {ticketFk: 11, stateFk: assignedState.id}; - - let errCode; - try { - await app.models.TicketTracking.changeState(ctx, params); - } catch (e) { - errCode = e.details.codes.workerFk[0]; - } - - expect(errCode).toEqual('presence'); - }); - - it('should throw an error if a worker thats not production tries to change the state to one thats not assigned', async() => { - let ctx = {req: {accessToken: {userId: 110}}}; - let params = {ticketFk: 11, stateFk: 3}; + it('should throw an error if a worker with employee role attemps to a forbidden state', async() => { + let ctx = {req: {accessToken: {userId: 1}}}; + let params = {ticketFk: 11, stateFk: 13}; let errCode; try { @@ -72,7 +57,7 @@ describe('ticket changeState()', () => { expect(res.__data.id).toBeDefined(); }); - it('return an array with the created ticket tracking line', async() => { + it('should return an array with the created ticket tracking line', async() => { let ctx = {req: {accessToken: {userId: 49}}}; let params = {ticketFk: ticket.id, stateFk: 3}; let res = await app.models.TicketTracking.changeState(ctx, params); @@ -83,7 +68,7 @@ describe('ticket changeState()', () => { expect(res.__data.id).toBeDefined(); }); - it('return an array with the created ticket tracking line when the user is salesperson, uses the state assigned and thes a workerFk given', async() => { + it('should return an array with the created ticket tracking line when the user is salesperson, uses the state assigned and thes a workerFk given', async() => { let ctx = {req: {accessToken: {userId: 18}}}; let assignedState = await app.models.State.findOne({where: {code: 'PICKER_DESIGNED'}}); let params = {ticketFk: ticket.id, stateFk: assignedState.id, workerFk: 1}; diff --git a/modules/ticket/back/models/state.js b/modules/ticket/back/models/state.js index 452091ccf..5aa0ffabc 100644 --- a/modules/ticket/back/models/state.js +++ b/modules/ticket/back/models/state.js @@ -17,8 +17,4 @@ module.exports = Self => { ); return result[0].alertLevel == 0; }; - - Self.prototype.isEditable = function() { - return this.code == 'PICKER_DESIGNED' || this.alertLevel == 0; - }; }; diff --git a/modules/ticket/front/locale/es.yml b/modules/ticket/front/locale/es.yml index a1abc81fc..e796407d1 100644 --- a/modules/ticket/front/locale/es.yml +++ b/modules/ticket/front/locale/es.yml @@ -73,7 +73,7 @@ Volume: Volumen Expedition: Expedición New state: Nuevo estado Packages: Embalajes -Tracking: Revisión +Tracking: Estados Sale checked: Control clientes Components: Componentes Sale tracking: Líneas preparadas diff --git a/modules/ticket/front/tracking/edit/index.html b/modules/ticket/front/tracking/edit/index.html index ce7019d92..e7368ba7a 100644 --- a/modules/ticket/front/tracking/edit/index.html +++ b/modules/ticket/front/tracking/edit/index.html @@ -16,9 +16,9 @@