141 lines
4.0 KiB
JavaScript
141 lines
4.0 KiB
JavaScript
const UserError = require('vn-loopback/util/user-error');
|
|
|
|
module.exports = Self => {
|
|
Self.remoteMethodCtx('absences', {
|
|
description: 'Returns an array of absences from an specified worker',
|
|
accepts: [{
|
|
arg: 'workerFk',
|
|
type: 'Number',
|
|
required: true,
|
|
},
|
|
{
|
|
arg: 'started',
|
|
type: 'Date',
|
|
required: true,
|
|
},
|
|
{
|
|
arg: 'ended',
|
|
type: 'Date',
|
|
required: true,
|
|
}],
|
|
returns: [{
|
|
arg: 'calendar'
|
|
},
|
|
{
|
|
arg: 'absences',
|
|
type: 'Number'
|
|
},
|
|
{
|
|
arg: 'holidays',
|
|
type: 'Number'
|
|
}],
|
|
http: {
|
|
path: `/absences`,
|
|
verb: 'GET'
|
|
}
|
|
});
|
|
|
|
Self.absences = async(ctx, workerFk, yearStarted, yearEnded) => {
|
|
const models = Self.app.models;
|
|
const isSubordinate = await models.Worker.isSubordinate(ctx, workerFk);
|
|
|
|
if (!isSubordinate)
|
|
throw new UserError(`You don't have enough privileges`);
|
|
|
|
const calendar = {totalHolidays: 0, holidaysEnjoyed: 0};
|
|
const holidays = [];
|
|
|
|
// Get absences of year
|
|
let absences = await Self.find({
|
|
include: {
|
|
relation: 'absenceType'
|
|
},
|
|
where: {
|
|
workerFk: workerFk,
|
|
dated: {between: [yearStarted, yearEnded]}
|
|
}
|
|
});
|
|
|
|
absences.forEach(absence => {
|
|
if (absence.absenceType().id === 1)
|
|
calendar.holidaysEnjoyed++;
|
|
|
|
absence.dated = new Date(absence.dated);
|
|
absence.dated.setHours(0, 0, 0, 0);
|
|
});
|
|
|
|
// Get active contracts on current year
|
|
const year = yearStarted.getFullYear();
|
|
const contracts = await models.WorkerLabour.find({
|
|
include: [{
|
|
relation: 'holidays',
|
|
scope: {
|
|
where: {year}
|
|
}
|
|
},
|
|
{
|
|
relation: 'workCenter',
|
|
scope: {
|
|
include: {
|
|
relation: 'holidays',
|
|
scope: {
|
|
include: [{
|
|
relation: 'detail'
|
|
},
|
|
{
|
|
relation: 'type'
|
|
}],
|
|
where: {
|
|
dated: {between: [yearStarted, yearEnded]}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}],
|
|
where: {
|
|
and: [
|
|
{workerFk: workerFk},
|
|
{or: [{
|
|
ended: {gte: [yearStarted]}
|
|
}, {ended: null}]}
|
|
],
|
|
|
|
}
|
|
});
|
|
|
|
// Get number of total holidays
|
|
contracts.forEach(contract => {
|
|
calendar.totalHolidays += getHolidaysByContract(contract, yearEnded);
|
|
|
|
let holidayList = contract.workCenter().holidays();
|
|
for (let day of holidayList) {
|
|
day.dated = new Date(day.dated);
|
|
day.dated.setHours(0, 0, 0, 0);
|
|
|
|
holidays.push(day);
|
|
}
|
|
});
|
|
|
|
return [calendar, absences, holidays];
|
|
};
|
|
|
|
function getHolidaysByContract(contract, endOfYear) {
|
|
const dayTimestamp = 1000 * 60 * 60 * 24;
|
|
|
|
const started = contract.started;
|
|
const ended = contract.ended;
|
|
const startedTime = started.getTime();
|
|
const endedTime = ended && ended.getTime() || endOfYear;
|
|
|
|
const contractDays = Math.floor((endedTime - startedTime) / dayTimestamp);
|
|
|
|
if (contractDays < 365) {
|
|
let holidays = Math.round(2 * contract.holidays().days * (contractDays + 1) / 365) / 2;
|
|
|
|
return holidays;
|
|
}
|
|
|
|
return contract.holidays().days;
|
|
}
|
|
};
|