2023-11-23 12:19:59 +00:00
|
|
|
module.exports = Self => {
|
|
|
|
Self.remoteMethodCtx('updateInTime', {
|
2023-11-29 15:25:15 +00:00
|
|
|
description: 'Updates the corresponding registry if the worker has been registered in the last few hours',
|
2023-11-23 12:19:59 +00:00
|
|
|
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({
|
2024-01-03 11:50:53 +00:00
|
|
|
outTime: new Date(Date.now())
|
2023-11-23 12:19:59 +00:00
|
|
|
}, myOptions);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tx) await tx.commit();
|
|
|
|
} catch (e) {
|
|
|
|
if (tx) await tx.rollback();
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|