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

81 lines
2.1 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('updateMailState', {
description: 'Updates the state of WorkerTimeControlMail',
accessType: 'WRITE',
accepts: [{
arg: 'id',
type: 'number',
description: 'The worker id',
http: {source: 'path'}
},
{
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: `/:id/updateMailState`,
verb: 'POST'
}
});
Self.updateMailState = async(ctx, id, options) => {
const models = Self.app.models;
const args = ctx.args;
const myOptions = {};
if (typeof options == 'object')
Object.assign(myOptions, options);
const [sent] = await models.WorkerTimeControlMail.find({
where: {
year: args.year,
week: args.week,
},
limit: 1
}, myOptions);
if (!sent) throw new UserError(`There aren't records for this week`);
2022-11-07 12:57:54 +00:00
const workerTimeControlMail = await models.WorkerTimeControlMail.upsertWithWhere(
{
year: args.year,
week: args.week,
workerFk: id
},
{
state: args.state,
2024-06-07 07:08:21 +00:00
reason: args.reason,
year: args.year,
week: args.week,
workerFk: id
},
myOptions);
2022-11-07 12:57:54 +00:00
if (args.state == 'SENDED') {
await workerTimeControlMail.updateAttributes({
sendedCounter: workerTimeControlMail.sendedCounter ? workerTimeControlMail.sendedCounter + 1 : 1
}, myOptions);
}
};
};