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

69 lines
2.2 KiB
JavaScript
Raw Normal View History

module.exports = Self => {
Self.remoteMethodCtx('resendWeeklyHourEmail', {
description: 'Adds a new hour registry',
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();
const weekNumber = getWeekNumber(dated);
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);
ctx.args = {
recipient: worker.email,
year: yearNumber,
week: weekNumber,
workerId: workerId,
state: 'SENDED'
};
return models.WorkerTimeControl.weeklyHourRecordEmail(ctx, myOptions);
}
return false;
};
function getWeekNumber(date) {
const tempDate = new Date(date);
let dayOfWeek = tempDate.getDay();
dayOfWeek = (dayOfWeek === 0) ? 7 : dayOfWeek;
const firstDayOfWeek = new Date(tempDate.getFullYear(), tempDate.getMonth(), tempDate.getDate() - (dayOfWeek - 1));
const firstDayOfYear = new Date(tempDate.getFullYear(), 0, 1);
const differenceInMilliseconds = firstDayOfWeek.getTime() - firstDayOfYear.getTime();
const weekNumber = Math.floor(differenceInMilliseconds / (1000 * 60 * 60 * 24 * 7)) + 1;
return weekNumber;
}
};