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: [{
|
2022-02-25 09:33:47 +00:00
|
|
|
arg: 'workerFk',
|
2021-06-03 09:00:40 +00:00
|
|
|
type: 'number',
|
2019-03-22 07:28:57 +00:00
|
|
|
required: true,
|
|
|
|
},
|
2022-02-25 09:33:47 +00:00
|
|
|
{
|
|
|
|
arg: 'businessFk',
|
|
|
|
type: 'number',
|
|
|
|
required: false,
|
|
|
|
},
|
2019-03-22 07:28:57 +00:00
|
|
|
{
|
2021-06-03 09:00:40 +00:00
|
|
|
arg: 'year',
|
|
|
|
type: 'date',
|
2019-03-22 07:28:57 +00:00
|
|
|
required: true,
|
2022-02-23 13:50:59 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
arg: 'all',
|
|
|
|
type: 'boolean',
|
|
|
|
required: false,
|
2019-03-22 07:28:57 +00:00
|
|
|
}],
|
|
|
|
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'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-02-25 09:33:47 +00:00
|
|
|
Self.absences = async(ctx, workerFk, 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-11-18 10:17:30 +00:00
|
|
|
const myOptions = {};
|
2021-06-10 09:24:32 +00:00
|
|
|
|
|
|
|
if (typeof options == 'object')
|
|
|
|
Object.assign(myOptions, options);
|
|
|
|
|
2022-02-25 09:33:47 +00:00
|
|
|
let condition = {
|
|
|
|
and: [
|
|
|
|
{workerFk: workerFk},
|
|
|
|
{businessFk: businessFk}
|
|
|
|
]
|
|
|
|
};
|
|
|
|
|
|
|
|
if (businessFk)
|
|
|
|
condition.and.push({workerFk: workerFk});
|
2022-02-23 13:50:59 +00:00
|
|
|
|
2022-02-25 09:33:47 +00:00
|
|
|
const contracts = await models.WorkerLabour.find({
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}],
|
2022-02-25 09:33:47 +00:00
|
|
|
where: condition
|
2021-06-10 09:24:32 +00:00
|
|
|
}, myOptions);
|
2019-03-22 07:28:57 +00:00
|
|
|
|
2022-02-25 09:33:47 +00:00
|
|
|
if (!contracts) return;
|
2021-06-03 09:00:40 +00:00
|
|
|
|
2022-02-25 09:33:47 +00:00
|
|
|
const isSubordinate = await models.Worker.isSubordinate(ctx, 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 = [];
|
2022-02-25 09:33:47 +00:00
|
|
|
const holidays = [];
|
2019-03-22 07:28:57 +00:00
|
|
|
|
2022-02-25 09:33:47 +00:00
|
|
|
for (let contract of contracts) {
|
|
|
|
for (let absence of contract.absences()) {
|
|
|
|
absence.dated = new Date(absence.dated);
|
|
|
|
absence.dated.setHours(0, 0, 0, 0);
|
2020-06-19 07:58:09 +00:00
|
|
|
|
2022-02-25 09:33:47 +00:00
|
|
|
absences.push(absence);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (let day of contract.workCenter().holidays()) {
|
|
|
|
day.dated = new Date(day.dated);
|
|
|
|
day.dated.setHours(0, 0, 0, 0);
|
2019-03-22 07:28:57 +00:00
|
|
|
|
2022-02-25 09:33:47 +00:00
|
|
|
holidays.push(day);
|
|
|
|
}
|
2021-06-03 09:00:40 +00:00
|
|
|
}
|
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
|
|
|
};
|