2022-11-07 12:57:54 +00:00
|
|
|
const UserError = require('vn-loopback/util/user-error');
|
2022-11-02 13:22:18 +00:00
|
|
|
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',
|
2022-11-02 13:22:18 +00:00
|
|
|
type: 'string'
|
|
|
|
}],
|
|
|
|
returns: {
|
|
|
|
type: 'boolean',
|
|
|
|
root: true
|
|
|
|
},
|
|
|
|
http: {
|
|
|
|
path: `/updateWorkerTimeControlMail`,
|
|
|
|
verb: 'POST'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-10-03 12:03:52 +00:00
|
|
|
Self.updateWorkerTimeControlMail = async(ctx, workerId, year, week, state, reason, options) => {
|
2022-11-02 13:22:18 +00:00
|
|
|
const models = Self.app.models;
|
|
|
|
|
|
|
|
const myOptions = {};
|
|
|
|
|
|
|
|
if (typeof options == 'object')
|
|
|
|
Object.assign(myOptions, options);
|
|
|
|
|
|
|
|
const workerTimeControlMail = await models.WorkerTimeControlMail.findOne({
|
|
|
|
where: {
|
2023-10-03 12:03:52 +00:00
|
|
|
workerFk: workerId,
|
|
|
|
year,
|
|
|
|
week
|
2022-11-02 13:22:18 +00:00
|
|
|
}
|
|
|
|
}, myOptions);
|
|
|
|
|
2023-10-03 12:03:52 +00:00
|
|
|
console.log('workerTimeControlMail: ', workerTimeControlMail);
|
2022-11-07 12:57:54 +00:00
|
|
|
if (!workerTimeControlMail) throw new UserError(`There aren't records for this week`);
|
|
|
|
|
|
|
|
await workerTimeControlMail.updateAttributes({
|
2023-10-03 12:03:52 +00:00
|
|
|
state,
|
|
|
|
reason: reason || null
|
2022-11-02 13:22:18 +00:00
|
|
|
}, myOptions);
|
2022-11-07 12:57:54 +00:00
|
|
|
|
2023-10-03 12:03:52 +00:00
|
|
|
if (state == 'SENDED') {
|
2023-03-30 06:14:17 +00:00
|
|
|
await workerTimeControlMail.updateAttributes({
|
|
|
|
sendedCounter: workerTimeControlMail.sendedCounter + 1
|
|
|
|
}, myOptions);
|
|
|
|
}
|
2022-11-02 13:22:18 +00:00
|
|
|
};
|
|
|
|
};
|