feat: refs #6276 machineWorke_update
gitea/salix/pipeline/head There was a failure building this commit Details

This commit is contained in:
Jorge Penadés 2024-01-17 08:17:55 +01:00
parent 07e9b737c5
commit 53a10dda58
4 changed files with 4 additions and 63 deletions

View File

@ -1,57 +0,0 @@
module.exports = Self => {
Self.remoteMethodCtx('add', {
description: 'Insert log if the worker has not logged anything in the last 12 hours',
accessType: 'READ',
accepts: [
{
arg: 'plate',
type: 'string',
}
],
http: {
path: `/add`,
verb: 'POST'
}
});
Self.add = async(ctx, plate, options) => {
const models = Self.app.models;
const userId = ctx.req.accessToken.userId;
let tx;
const myOptions = {};
if (typeof options == 'object')
Object.assign(myOptions, options);
if (!myOptions.transaction) {
tx = await Self.beginTransaction({});
myOptions.transaction = tx;
}
try {
const machine = await models.Machine.findOne({
fields: ['id', 'plate'],
where: {plate}
}, myOptions);
if (!machine) throw new Error(`plate ${plate} does not exist`);
await models.MachineWorker.updateAll(
{
or: [{workerFk: userId}, {machineFk: machine.id}],
and: [{outTime: null }]
},
{outTime: Date.vnNew()},
myOptions
);
await models.MachineWorker.create({machineFk: machine.id, workerFk: userId}, myOptions);
if (tx) await tx.commit();
} catch (e) {
if (tx) await tx.rollback();
throw e;
}
};
};

View File

@ -35,9 +35,8 @@ module.exports = Self => {
fields: ['id', 'plate'],
where: {plate}
}, myOptions);
if (!machine) throw new Error(`plate ${plate} does not exist`);
if (!machine) throw new Error(`plate ${plate} does not exist.`);
const {maxHours} = await models.MachineWorkerConfig.findOne({fields: ['maxHours']}, myOptions);
const machineWorker = await Self.findOne({
where: {
machineFk: machine.id,
@ -46,10 +45,11 @@ module.exports = Self => {
}, myOptions);
if (machineWorker) {
const hoursDifference = (Date.vnNew() - machineWorker.inTime) / (60 * 60 * 1000);
const {maxHours} = await models.MachineWorkerConfig.findOne({fields: ['maxHours']}, myOptions);
const hoursDifference = (Date.vnNow() - machineWorker.inTime.getTime()) / (60 * 60 * 1000);
const isHimSelf = userId == machineWorker.workerFk;
if (maxHours > hoursDifference && !isHimSelf) throw new UserError('Esta máquina ya está en uso.');
if (maxHours > hoursDifference && !isHimSelf) throw new UserError('This machine is already in use.');
await machineWorker.updateAttributes({
outTime: Date.vnNew()

View File

@ -1,4 +1,3 @@
module.exports = Self => {
require('../methods/machine-worker/updateInTime')(Self);
require('../methods/machine-worker/add')(Self);
};

View File

@ -3,7 +3,6 @@ INSERT INTO `salix`.`ACL` (model, property, accessType, permission, principalTyp
('Collection', 'assignCollection', 'WRITE', 'ALLOW', 'ROLE', 'employee'),
('ExpeditionPallet', 'getPallet', 'READ', 'ALLOW', 'ROLE', 'employee'),
('MachineWorker','updateInTime','WRITE','ALLOW','ROLE','employee'),
('MachineWorker','add','READ','ALLOW','ROLE','employee'),
('MobileAppVersionControl','getVersion','READ','ALLOW','ROLE','employee'),
('SaleTracking','deleteTracking','WRITE','ALLOW','ROLE','employee'),
('SaleTracking','updateTracking','WRITE','ALLOW','ROLE','employee'),