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