#6276 createNewWarehouse methods migrated from silex to salix #1850
|
@ -0,0 +1,68 @@
|
|||
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`);
|
||||
|
||||
const twelveHoursAgo = Date.vnNew();
|
||||
twelveHoursAgo.setHours(twelveHoursAgo.getHours() - 12);
|
||||
|
||||
const isRegistered = await models.MachineWorker.findOne({
|
||||
where: {
|
||||
and: [
|
||||
{machineFk: machine.id},
|
||||
{workerFk: userId},
|
||||
{outTime: {gte: twelveHoursAgo}}
|
||||
]
|
||||
}
|
||||
}, myOptions);
|
||||
console.log(isRegistered);
|
||||
if (!isRegistered) await models.MachineWorker.create({machineFk: machine.id, workerFk: userId}, myOptions);
|
||||
else {
|
||||
await models.MachineWorker.updateAll(
|
||||
{or: [{workerFk: userId}, {machineFk: machine.id}]},
|
||||
{outTime: Date.vnNew()},
|
||||
myOptions
|
||||
);
|
||||
}
|
||||
|
||||
if (tx) await tx.commit();
|
||||
} catch (e) {
|
||||
if (tx) await tx.rollback();
|
||||
throw e;
|
||||
}
|
||||
};
|
||||
};
|
|
@ -1,6 +1,6 @@
|
|||
module.exports = Self => {
|
||||
Self.remoteMethodCtx('updateInTime', {
|
||||
description: '',
|
||||
description: 'Updates the corresponding registry if the worker has been registered in the last few hours',
|
||||
accessType: 'WRITE',
|
||||
accepts: [
|
||||
{
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
module.exports = Self => {
|
||||
require('../methods/machine-worker/updateInTime')(Self);
|
||||
require('../methods/machine-worker/add')(Self);
|
||||
};
|
||||
|
|
|
@ -3,4 +3,5 @@ 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');
|
Loading…
Reference in New Issue