refs #6274 refactor updateInTime
gitea/salix/pipeline/head This commit looks good Details

This commit is contained in:
Jorge Penadés 2023-11-23 13:19:59 +01:00
parent cf3b5efee9
commit f862ed7d8a
5 changed files with 68 additions and 76 deletions

View File

@ -0,0 +1,63 @@
module.exports = Self => {
Self.remoteMethodCtx('updateInTime', {
description: '',
accessType: 'WRITE',
accepts: [
{
arg: 'plate',
type: 'string',
}
],
http: {
path: `/updateInTime`,
verb: 'POST'
}
});
Self.updateInTime = 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`);
const {maxHours} = await models.MachineWorkerConfig.findOne({fields: ['maxHours']}, myOptions);
const machineWorker = await models.MachineWorker.findOne({
where: {
workerFk: userId,
inTime: {gte: new Date(Date.now() - maxHours * 60 * 60 * 1000)},
outTimed: null,
machineFk: machine.id,
}
});
if (machineWorker) {
await machineWorker.updateAttributes({
inTime: new Date(Date.now())
}, myOptions);
}
if (tx) await tx.commit();
} catch (e) {
if (tx) await tx.rollback();
throw e;
}
};
};

View File

@ -1,71 +0,0 @@
module.exports = Self => {
Self.remoteMethodCtx('updateMachine', {
description: '',
accessType: 'WRITE',
accepts: [
{
arg: 'plate',
type: 'string',
}
],
returns: {
type: 'object',
root: true
},
http: {
path: `/update-machine`,
verb: 'POST'
}
});
Self.updateMachine = 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 {
let insertState = false;
const machine = await models.Machine.findOne({
fields: ['id', 'plate'],
where: {plate}
}, myOptions);
if (machine) {
const {maxHours} = await models.MachineWorkerConfig.findOne({fields: ['maxHours']}, myOptions);
const machineWorker = await models.MachineWorker.findOne({
where: {
workerFk: userId,
inTime: {gte: new Date(Date.now() - maxHours * 60 * 60 * 1000)},
outTimed: null,
machineFk: machine.id,
}
});
if (machineWorker) {
await machineWorker.updateAttributes({
inTime: new Date(Date.now())
}, myOptions);
}
insertState = true;
}
if (tx) await tx.commit();
return insertState;
} catch (e) {
if (tx) await tx.rollback();
throw e;
}
};
};

View File

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

View File

@ -2,7 +2,4 @@ INSERT INTO `salix`.`ACL` (model, property, accessType, permission, principalTyp
VALUES
('Collection', 'assignCollection', 'WRITE', 'ALLOW', 'ROLE', 'employee'),
('ExpeditionPallet', 'getPallet', 'READ', 'ALLOW', 'ROLE', 'employee'),
('MachineWorker','updateMachine','WRITE','ALLOW','ROLE','employee');
INSERT INTO `vn`.`machineWorkerConfig` (id, maxHours)
VALUES (1, 12)
('MachineWorker','updateInTime','WRITE','ALLOW','ROLE','employee');

View File

@ -3671,3 +3671,6 @@ CALL itemShelvingSale_reserveByCollection(10101010);
UPDATE vn.collection
SET workerFk=9
WHERE id=10101010;
INSERT INTO `vn`.`machineWorkerConfig` (id, maxHours)
VALUES (1, 12)