89 lines
3.0 KiB
JavaScript
89 lines
3.0 KiB
JavaScript
const UserError = require('vn-loopback/util/user-error');
|
|
const ParameterizedSQL = require('loopback-connector').ParameterizedSQL;
|
|
|
|
module.exports = Self => {
|
|
Self.remoteMethodCtx('holidays', {
|
|
description: 'Returns the holidays available whitin a contract or a year',
|
|
accepts: [{
|
|
arg: 'id',
|
|
type: 'number',
|
|
required: true,
|
|
description: 'The worker id',
|
|
http: {source: 'path'}
|
|
},
|
|
{
|
|
arg: 'year',
|
|
type: 'date',
|
|
required: true,
|
|
},
|
|
{
|
|
arg: 'businessFk',
|
|
type: 'number',
|
|
required: false
|
|
}],
|
|
returns: [{
|
|
type: 'object',
|
|
root: true
|
|
}],
|
|
http: {
|
|
path: `/:id/holidays`,
|
|
verb: 'GET'
|
|
}
|
|
});
|
|
|
|
Self.holidays = async(ctx, id, options) => {
|
|
const models = Self.app.models;
|
|
const args = ctx.args;
|
|
const conn = Self.dataSource.connector;
|
|
const stmts = [];
|
|
const myOptions = {};
|
|
|
|
if (typeof options == 'object')
|
|
Object.assign(myOptions, options);
|
|
|
|
const isSubordinate = await models.Worker.isSubordinate(ctx, id, myOptions);
|
|
if (!isSubordinate)
|
|
throw new UserError(`You don't have enough privileges`);
|
|
|
|
const started = Date.vnNew();
|
|
started.setFullYear(args.year);
|
|
started.setMonth(0);
|
|
started.setDate(1);
|
|
started.setHours(0, 0, 0, 0);
|
|
|
|
const ended = Date.vnNew();
|
|
ended.setFullYear(args.year);
|
|
ended.setMonth(12);
|
|
ended.setDate(0);
|
|
ended.setHours(23, 59, 59, 59);
|
|
|
|
const filter = {where: {businessFk: args.businessFk}};
|
|
const contract = await models.WorkerLabour.findOne(filter, myOptions);
|
|
const payedHolidays = contract.payedHolidays;
|
|
|
|
let queryIndex;
|
|
const year = started.getFullYear();
|
|
|
|
if (args.businessFk) {
|
|
stmts.push(new ParameterizedSQL('CALL vn.workerCalendar_calculateBusiness(?,?)', [year, args.businessFk]));
|
|
queryIndex = stmts.push('SELECT * FROM tmp.workerCalendarCalculateBusiness') - 1;
|
|
stmts.push('DROP TEMPORARY TABLE tmp.workerCalendarCalculateBusiness');
|
|
} else {
|
|
stmts.push(new ParameterizedSQL('CALL vn.workerCalendar_calculateYear(?,?)', [year, id]));
|
|
queryIndex = stmts.push('SELECT * FROM tmp.workerCalendarCalculateYear') - 1;
|
|
stmts.push('DROP TEMPORARY TABLE tmp.workerCalendarCalculateYear');
|
|
}
|
|
|
|
const sql = ParameterizedSQL.join(stmts, ';');
|
|
const result = await conn.executeStmt(sql, myOptions);
|
|
const [holidays] = result[queryIndex];
|
|
|
|
const totalHolidays = holidays.days;
|
|
const holidaysEnjoyed = holidays.daysEnjoyed;
|
|
const totalHours = holidays.hours;
|
|
const hoursEnjoyed = holidays.hoursEnjoyed;
|
|
|
|
return {totalHolidays, holidaysEnjoyed, totalHours, hoursEnjoyed, payedHolidays};
|
|
};
|
|
};
|