From 2fbea6f9dd0525891ca31e83d4f83535df5a1b2b Mon Sep 17 00:00:00 2001 From: jorgep Date: Tue, 16 Jan 2024 15:43:34 +0100 Subject: [PATCH] feat: refs #6276 wip machineWorke_update --- back/methods/machine-worker/updateInTime.js | 23 +++++++++++++-------- loopback/locale/es.json | 7 ++++--- 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/back/methods/machine-worker/updateInTime.js b/back/methods/machine-worker/updateInTime.js index 278d1d08d..0ae968b83 100644 --- a/back/methods/machine-worker/updateInTime.js +++ b/back/methods/machine-worker/updateInTime.js @@ -1,3 +1,4 @@ +const UserError = require('vn-loopback/util/user-error'); module.exports = Self => { Self.remoteMethodCtx('updateInTime', { description: 'Updates the corresponding registry if the worker has been registered in the last few hours', @@ -34,25 +35,29 @@ module.exports = Self => { fields: ['id', 'plate'], where: {plate} }, myOptions); - if (!machine) throw new Error(`plate ${plate} does not exist`); const {maxHours} = await models.MachineWorkerConfig.findOne({fields: ['maxHours']}, myOptions); - - const machineWorker = await models.MachineWorker.findOne({ + const machineWorker = await Self.findOne({ where: { - workerFk: userId, - inTime: {gte: Date.vnNew() - (maxHours * 60 * 60 * 1000)}, - outTimed: null, machineFk: machine.id, + outTime: null, } + }, myOptions); - }); if (machineWorker) { + const hoursDifference = (Date.vnNew() - machineWorker.inTime) / (60 * 60 * 1000); + const isHimSelf = userId == machineWorker.workerFk; + + if (maxHours > hoursDifference && !isHimSelf) throw new UserError('Esta máquina ya está en uso.'); + await machineWorker.updateAttributes({ - outTime: Date.now() + outTime: Date.vnNew() }, myOptions); - } + + if (!isHimSelf) await models.MachineWorker.create({machineFk: machine.id, workerFk: userId}, myOptions); + } else + await models.MachineWorker.create({machineFk: machine.id, workerFk: userId}, myOptions); if (tx) await tx.commit(); } catch (e) { diff --git a/loopback/locale/es.json b/loopback/locale/es.json index 698303a26..5d57b1968 100644 --- a/loopback/locale/es.json +++ b/loopback/locale/es.json @@ -72,7 +72,7 @@ "The secret can't be blank": "La contraseña no puede estar en blanco", "We weren't able to send this SMS": "No hemos podido enviar el SMS", "This client can't be invoiced": "Este cliente no puede ser facturado", - "You must provide the correction information to generate a corrective invoice": "Debes informar la información de corrección para generar una factura rectificativa", + "You must provide the correction information to generate a corrective invoice": "Debes informar la información de corrección para generar una factura rectificativa", "This ticket can't be invoiced": "Este ticket no puede ser facturado", "You cannot add or modify services to an invoiced ticket": "No puedes añadir o modificar servicios a un ticket facturado", "This ticket can not be modified": "Este ticket no puede ser modificado", @@ -339,5 +339,6 @@ "The alias cant be modified": "Este alias de correo no puede ser modificado", "No tickets to invoice": "No hay tickets para facturar", "No hay tickets para sacar": "No hay tickets para sacar", - "There is no zone for these parameters 999999": "There is no zone for these parameters 999999" -} + "There is no zone for these parameters 999999": "There is no zone for these parameters 999999", + "Esta máquina ya está en uso.": "Esta máquina ya está en uso." +} \ No newline at end of file