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'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
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;
|
2022-11-02 13:22:18 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
if (oldState == args.state) throw new UserError('Already has this status');
|
|
|
|
|
|
|
|
await workerTimeControlMail.updateAttributes({
|
2022-11-02 13:22:18 +00:00
|
|
|
state: args.state,
|
2022-11-08 07:46:36 +00:00
|
|
|
reason: args.reason || null
|
2022-11-02 13:22:18 +00:00
|
|
|
}, 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);
|
2022-11-02 13:22:18 +00:00
|
|
|
};
|
|
|
|
};
|