salix/modules/worker/back/methods/worker-time-control/updateWorkerTimeControlMail.js

92 lines
2.5 KiB
JavaScript
Raw Normal View History

2022-11-07 12:57:54 +00:00
const UserError = require('vn-loopback/util/user-error');
module.exports = Self => {
Self.remoteMethodCtx('updateWorkerTimeControlMail', {
description: 'Updates the state of WorkerTimeControlMail',
accessType: 'WRITE',
accepts: [{
arg: 'workerId',
type: 'number',
required: true
},
{
arg: 'year',
type: 'number',
required: true
},
{
arg: 'week',
type: 'number',
required: true
},
{
arg: 'state',
type: 'string',
required: true
},
{
2022-11-08 07:46:36 +00:00
arg: 'reason',
type: 'string'
}],
returns: {
type: 'boolean',
root: true
},
http: {
path: `/updateWorkerTimeControlMail`,
verb: 'POST'
}
});
Self.updateWorkerTimeControlMail = async(ctx, options) => {
const models = Self.app.models;
const args = ctx.args;
2022-11-07 12:57:54 +00:00
const userId = ctx.req.accessToken.userId;
const myOptions = {};
if (typeof options == 'object')
Object.assign(myOptions, options);
const workerTimeControlMail = await models.WorkerTimeControlMail.findOne({
where: {
workerFk: args.workerId,
year: args.year,
week: args.week
}
}, myOptions);
2022-11-07 12:57:54 +00:00
if (!workerTimeControlMail) throw new UserError(`There aren't records for this week`);
const oldState = workerTimeControlMail.state;
2022-11-08 07:46:36 +00:00
const oldReason = workerTimeControlMail.reason;
2022-11-07 12:57:54 +00:00
await workerTimeControlMail.updateAttributes({
state: args.state,
2022-11-08 07:46:36 +00:00
reason: args.reason || null
}, myOptions);
2022-11-07 12:57:54 +00:00
if (args.state == 'SENDED') {
await workerTimeControlMail.updateAttributes({
sendedCounter: workerTimeControlMail.sendedCounter + 1
}, myOptions);
}
2022-11-07 12:57:54 +00:00
const logRecord = {
originFk: args.workerId,
userFk: userId,
action: 'update',
changedModel: 'WorkerTimeControlMail',
oldInstance: {
state: oldState,
2022-11-08 07:46:36 +00:00
reason: oldReason
2022-11-07 12:57:54 +00:00
},
newInstance: {
state: args.state,
2022-11-08 07:46:36 +00:00
reason: args.reason
2022-11-07 12:57:54 +00:00
}
};
return models.WorkerLog.create(logRecord, myOptions);
};
};