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

132 lines
3.5 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: [{
arg: 'workerFk',
2021-06-03 09:00:40 +00:00
type: 'number',
2019-03-22 07:28:57 +00:00
required: true,
},
{
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,
}],
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'
}
});
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
2023-01-16 14:18:24 +00:00
const started = Date.vnNew();
2021-06-03 09:00:40 +00:00
started.setFullYear(year);
started.setMonth(0);
started.setDate(1);
started.setHours(0, 0, 0, 0);
2021-06-03 09:00:40 +00:00
2023-01-16 14:18:24 +00:00
const ended = Date.vnNew();
2021-06-03 09:00:40 +00:00
ended.setFullYear(year);
ended.setMonth(12);
ended.setDate(0);
const myOptions = {};
2021-06-10 09:24:32 +00:00
if (typeof options == 'object')
Object.assign(myOptions, options);
let condition = {
and: [
{workerFk: workerFk},
{businessFk: businessFk}
]
};
if (businessFk)
condition.and.push({workerFk: workerFk});
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
}
}
}
}
}],
where: condition
2021-06-10 09:24:32 +00:00
}, myOptions);
2019-03-22 07:28:57 +00:00
if (!contracts) return;
2021-06-03 09:00:40 +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 = [];
const holidays = [];
2019-03-22 07:28:57 +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
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
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
};