refs #6276 machineWorker_update
gitea/salix/pipeline/head This commit looks good Details

This commit is contained in:
Jorge Penadés 2023-11-22 14:25:19 +01:00
parent 40bbad04c9
commit fac6aab26c
6 changed files with 121 additions and 1 deletions

View File

@ -0,0 +1,71 @@
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

@ -79,9 +79,15 @@
"Language": {
"dataSource": "vn"
},
"Machine": {
"dataSource": "vn"
},
"MachineWorker": {
"dataSource": "vn"
},
"MachineWorkerConfig": {
"dataSource": "vn"
},
"MobileAppVersionControl": {
"dataSource": "vn"
},

View File

@ -0,0 +1,18 @@
{
"name": "MachineWorkerConfig",
"base": "VnModel",
"options": {
"mysql": {
"table": "vn.machineWorkerConfig"
}
},
"properties": {
"id": {
"type": "number",
"id": true
},
"maxHours": {
"type": "number"
}
}
}

View File

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

18
back/models/machine.json Normal file
View File

@ -0,0 +1,18 @@
{
"name": "Machine",
"base": "VnModel",
"options": {
"mysql": {
"table": "vn.machine"
}
},
"properties": {
"id": {
"type": "number",
"id": true
},
"plate": {
"type": "string"
}
}
}

View File

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