2024-05-09 07:46:40 +00:00
|
|
|
const moment = require('moment');
|
2024-06-13 11:43:28 +00:00
|
|
|
const UserError = require('vn-loopback/util/user-error');
|
2024-05-09 07:46:40 +00:00
|
|
|
|
2023-04-03 19:29:02 +00:00
|
|
|
module.exports = Self => {
|
|
|
|
Self.remoteMethodCtx('resendWeeklyHourEmail', {
|
2023-12-07 13:47:22 +00:00
|
|
|
description: 'Send the records for the week of the date provided',
|
2023-04-03 19:29:02 +00:00
|
|
|
accessType: 'WRITE',
|
|
|
|
accepts: [{
|
|
|
|
arg: 'id',
|
|
|
|
type: 'number',
|
|
|
|
description: 'The worker id',
|
|
|
|
http: {source: 'path'}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
arg: 'dated',
|
|
|
|
type: 'date',
|
|
|
|
required: true
|
|
|
|
}],
|
|
|
|
returns: [{
|
|
|
|
type: 'Object',
|
|
|
|
root: true
|
|
|
|
}],
|
|
|
|
http: {
|
|
|
|
path: `/:id/resendWeeklyHourEmail`,
|
|
|
|
verb: 'POST'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
Self.resendWeeklyHourEmail = async(ctx, workerId, dated, options) => {
|
|
|
|
const models = Self.app.models;
|
|
|
|
const myOptions = {};
|
|
|
|
|
|
|
|
if (typeof options == 'object')
|
|
|
|
Object.assign(myOptions, options);
|
|
|
|
|
|
|
|
const yearNumber = dated.getFullYear();
|
2024-05-09 07:46:40 +00:00
|
|
|
const weekNumber = moment(dated).isoWeek();
|
2024-06-25 15:27:00 +00:00
|
|
|
const isSubordinate = await models.Worker.isSubordinate(ctx, workerId, myOptions);
|
|
|
|
const isTeamBoss = await models.ACL.checkAccessAcl(ctx, 'Worker', 'isTeamBoss', 'WRITE');
|
2024-05-09 07:46:40 +00:00
|
|
|
|
2024-06-25 15:27:00 +00:00
|
|
|
if (!isSubordinate || (workerId === ctx.req.accessToken.userId && !isTeamBoss))
|
2024-06-13 11:43:28 +00:00
|
|
|
throw new UserError(`You don't have enough privileges`);
|
|
|
|
|
2023-04-03 19:29:02 +00:00
|
|
|
const workerTimeControlMail = await models.WorkerTimeControlMail.findOne({
|
|
|
|
where: {
|
|
|
|
workerFk: workerId,
|
|
|
|
year: yearNumber,
|
|
|
|
week: weekNumber
|
|
|
|
}
|
|
|
|
}, myOptions);
|
|
|
|
|
|
|
|
if (workerTimeControlMail && workerTimeControlMail.state != 'SENDED') {
|
2023-04-06 08:06:29 +00:00
|
|
|
const worker = await models.EmailUser.findById(workerId, null, myOptions);
|
2023-04-03 19:29:02 +00:00
|
|
|
ctx.args = {
|
|
|
|
recipient: worker.email,
|
|
|
|
year: yearNumber,
|
|
|
|
week: weekNumber,
|
|
|
|
workerId: workerId,
|
|
|
|
state: 'SENDED'
|
|
|
|
};
|
|
|
|
return models.WorkerTimeControl.weeklyHourRecordEmail(ctx, myOptions);
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
};
|