salix/modules/worker/back/methods/calendar/absences.js

115 lines
3.1 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 contract',
accepts: [{
arg: 'businessFk',
type: 'number',
required: true,
},
{
arg: 'year',
type: 'date',
required: true,
}],
returns: [{
arg: 'absences',
type: 'number'
},
{
arg: 'holidays',
type: 'number'
}],
http: {
path: `/absences`,
verb: 'GET'
}
});
Self.absences = async(ctx, businessFk, year, options) => {
const models = Self.app.models;
const started = new Date();
started.setFullYear(year);
started.setMonth(0);
started.setDate(1);
const ended = new Date();
ended.setFullYear(year);
ended.setMonth(12);
ended.setDate(0);
const myOptions = {};
if (typeof options == 'object')
Object.assign(myOptions, options);
const contract = await models.WorkerLabour.findOne({
include: [{
relation: 'holidays',
scope: {
where: {year}
}
},
{
relation: 'absences',
scope: {
include: {
relation: 'absenceType',
},
where: {
dated: {between: [started, ended]}
}
}
},
{
relation: 'workCenter',
scope: {
include: {
relation: 'holidays',
scope: {
include: [{
relation: 'detail'
},
{
relation: 'type'
}],
where: {
dated: {between: [started, ended]}
}
}
}
}
}],
where: {businessFk}
}, myOptions);
if (!contract) return;
const isSubordinate = await models.Worker.isSubordinate(ctx, contract.workerFk, myOptions);
if (!isSubordinate)
throw new UserError(`You don't have enough privileges`);
const absences = [];
for (let absence of contract.absences()) {
absence.dated = new Date(absence.dated);
absence.dated.setHours(0, 0, 0, 0);
absences.push(absence);
}
// Workcenter holidays
const holidays = [];
const 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 [absences, holidays];
};
};