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

115 lines
3.1 KiB
JavaScript
Raw Normal View History

2019-03-22 07:28:57 +00:00
const UserError = require('vn-loopback/util/user-error');
module.exports = Self => {
Self.remoteMethodCtx('absences', {
2021-06-10 09:24:32 +00:00
description: 'Returns an array of absences from an specified contract',
2019-03-22 07:28:57 +00:00
accepts: [{
2021-06-03 09:00:40 +00:00
arg: 'businessFk',
type: 'number',
2019-03-22 07:28:57 +00:00
required: true,
},
{
2021-06-03 09:00:40 +00:00
arg: 'year',
type: 'date',
2019-03-22 07:28:57 +00:00
required: true,
}],
returns: [{
2019-04-23 11:29:52 +00:00
arg: 'absences',
2021-06-03 09:00:40 +00:00
type: 'number'
2019-03-22 07:28:57 +00:00
},
{
2019-04-23 11:29:52 +00:00
arg: 'holidays',
2021-06-03 09:00:40 +00:00
type: 'number'
2019-03-22 07:28:57 +00:00
}],
http: {
path: `/absences`,
verb: 'GET'
}
});
2021-06-10 09:24:32 +00:00
Self.absences = async(ctx, businessFk, year, options) => {
2019-03-22 07:28:57 +00:00
const models = Self.app.models;
2021-06-03 09:00:40 +00:00
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);
2021-06-10 09:24:32 +00:00
let myOptions = {};
if (typeof options == 'object')
Object.assign(myOptions, options);
2021-06-03 09:00:40 +00:00
const contract = await models.WorkerLabour.findOne({
2019-03-22 07:28:57 +00:00
include: [{
relation: 'holidays',
scope: {
where: {year}
}
},
2021-06-10 09:24:32 +00:00
{
relation: 'absences',
scope: {
include: {
relation: 'absenceType',
},
where: {
dated: {between: [started, ended]}
}
}
},
2019-03-22 07:28:57 +00:00
{
relation: 'workCenter',
scope: {
include: {
relation: 'holidays',
scope: {
include: [{
relation: 'detail'
},
{
relation: 'type'
}],
where: {
2021-06-03 09:00:40 +00:00
dated: {between: [started, ended]}
2019-03-22 07:28:57 +00:00
}
}
}
}
}],
2021-06-03 09:00:40 +00:00
where: {businessFk}
2021-06-10 09:24:32 +00:00
}, myOptions);
2019-03-22 07:28:57 +00:00
2021-06-03 09:00:40 +00:00
if (!contract) return;
2021-06-10 09:24:32 +00:00
const isSubordinate = await models.Worker.isSubordinate(ctx, contract.workerFk, myOptions);
2021-06-03 09:00:40 +00:00
if (!isSubordinate)
throw new UserError(`You don't have enough privileges`);
2020-08-10 12:29:25 +00:00
2021-06-10 09:24:32 +00:00
const absences = [];
for (let absence of contract.absences()) {
2020-08-10 12:29:25 +00:00
absence.dated = new Date(absence.dated);
absence.dated.setHours(0, 0, 0, 0);
2019-03-22 07:28:57 +00:00
2021-06-10 09:24:32 +00:00
absences.push(absence);
}
2020-06-19 07:58:09 +00:00
2021-06-03 09:00:40 +00:00
// Workcenter holidays
2021-06-10 09:24:32 +00:00
const holidays = [];
const holidayList = contract.workCenter().holidays();
2021-06-03 09:00:40 +00:00
for (let day of holidayList) {
day.dated = new Date(day.dated);
day.dated.setHours(0, 0, 0, 0);
2019-03-22 07:28:57 +00:00
2021-06-03 09:00:40 +00:00
holidays.push(day);
}
2019-03-22 07:28:57 +00:00
2021-06-10 09:24:32 +00:00
return [absences, holidays];
2019-11-15 09:44:11 +00:00
};
2019-03-22 07:28:57 +00:00
};