From 7fc18c89df0d506d178b95424148241f391d9747 Mon Sep 17 00:00:00 2001 From: vicent Date: Mon, 7 Nov 2022 13:57:54 +0100 Subject: [PATCH] feat: error handlers --- loopback/locale/es.json | 10 ++++--- .../specs/sendMail.spec.js | 2 +- .../updateWorkerTimeControlMail.js | 28 ++++++++++++++++++- 3 files changed, 34 insertions(+), 6 deletions(-) diff --git a/loopback/locale/es.json b/loopback/locale/es.json index afcdf31ab..1805682af 100644 --- a/loopback/locale/es.json +++ b/loopback/locale/es.json @@ -237,7 +237,9 @@ "Modifiable user details only by an administrator": "Detalles de usuario modificables solo por un administrador", "Modifiable password only via recovery or by an administrator": "Contraseña modificable solo a través de la recuperación o por un administrador", "Not enough privileges to edit a client": "No tienes suficientes privilegios para editar un cliente", - "Claim pickup order sent": "Reclamación Orden de recogida enviada [({{claimId}})]({{{claimUrl}}}) al cliente *{{clientName}}*", - "You don't have grant privilege": "No tienes privilegios para dar privilegios", - "You don't own the role and you can't assign it to another user": "No eres el propietario del rol y no puedes asignarlo a otro usuario" -} + "Claim pickup order sent": "Reclamación Orden de recogida enviada [({{claimId}})]({{{claimUrl}}}) al cliente *{{clientName}}*", + "You don't have grant privilege": "No tienes privilegios para dar privilegios", + "You don't own the role and you can't assign it to another user": "No eres el propietario del rol y no puedes asignarlo a otro usuario", + "Already has this status": "Ya tiene este estado", + "There aren't records for this week": "No existen registros para esta semana" +} \ No newline at end of file diff --git a/modules/worker/back/methods/worker-time-control/specs/sendMail.spec.js b/modules/worker/back/methods/worker-time-control/specs/sendMail.spec.js index ec3012e82..c9cbb5da8 100644 --- a/modules/worker/back/methods/worker-time-control/specs/sendMail.spec.js +++ b/modules/worker/back/methods/worker-time-control/specs/sendMail.spec.js @@ -1,6 +1,6 @@ const models = require('vn-loopback/server/server').models; -fdescribe('workerTimeControl sendMail()', () => { +describe('workerTimeControl sendMail()', () => { const workerId = 18; const ctx = { req: { diff --git a/modules/worker/back/methods/worker-time-control/updateWorkerTimeControlMail.js b/modules/worker/back/methods/worker-time-control/updateWorkerTimeControlMail.js index 23dcce262..3bb2b9013 100644 --- a/modules/worker/back/methods/worker-time-control/updateWorkerTimeControlMail.js +++ b/modules/worker/back/methods/worker-time-control/updateWorkerTimeControlMail.js @@ -1,3 +1,4 @@ +const UserError = require('vn-loopback/util/user-error'); module.exports = Self => { Self.remoteMethodCtx('updateWorkerTimeControlMail', { description: 'Updates the state of WorkerTimeControlMail', @@ -39,6 +40,7 @@ module.exports = Self => { Self.updateWorkerTimeControlMail = async(ctx, options) => { const models = Self.app.models; const args = ctx.args; + const userId = ctx.req.accessToken.userId; const myOptions = {}; @@ -53,9 +55,33 @@ module.exports = Self => { } }, myOptions); - return workerTimeControlMail.updateAttributes({ + if (!workerTimeControlMail) throw new UserError(`There aren't records for this week`); + + const oldState = workerTimeControlMail.state; + const oldEmailResponse = workerTimeControlMail.emailResponse; + + if (oldState == args.state) throw new UserError('Already has this status'); + + await workerTimeControlMail.updateAttributes({ state: args.state, emailResponse: args.emailResponse || null }, myOptions); + + const logRecord = { + originFk: args.workerId, + userFk: userId, + action: 'update', + changedModel: 'WorkerTimeControlMail', + oldInstance: { + state: oldState, + emailResponse: oldEmailResponse + }, + newInstance: { + state: args.state, + emailResponse: args.emailResponse + } + }; + + return models.WorkerLog.create(logRecord, myOptions); }; };