salix/modules/worker/back/methods/worker-calendar/specs/absences.spec.js

168 lines
5.6 KiB
JavaScript
Raw Normal View History

const app = require('vn-loopback/server/server');
2020-01-03 07:43:03 +00:00
describe('Worker absences()', () => {
it('should get the absence calendar for a full year contract', async() => {
let ctx = {req: {accessToken: {userId: 106}}};
let workerFk = 106;
const started = new Date();
started.setHours(0, 0, 0, 0);
started.setMonth(0);
started.setDate(1);
const monthIndex = 11;
const ended = new Date();
ended.setHours(0, 0, 0, 0);
ended.setMonth(monthIndex + 1);
ended.setDate(0);
let result = await app.models.WorkerCalendar.absences(ctx, workerFk, started, ended);
let calendar = result[0];
let absences = result[1];
2019-04-23 11:29:52 +00:00
expect(calendar.totalHolidays).toEqual(27.5);
expect(calendar.holidaysEnjoyed).toEqual(5);
let firstType = absences[0].absenceType().name;
let sixthType = absences[5].absenceType().name;
2020-01-03 07:43:03 +00:00
expect(firstType).toMatch(/(Holidays|Leave of absence)/);
expect(sixthType).toMatch(/(Holidays|Leave of absence)/);
2019-04-23 11:29:52 +00:00
});
it('should get the absence calendar for a permanent contract', async() => {
2019-07-31 07:17:26 +00:00
let workerFk = 106;
let worker = await app.models.WorkerLabour.findById(workerFk);
let endedDate = worker.ended;
2019-12-11 14:01:01 +00:00
await app.models.WorkerLabour.rawSql(
`UPDATE postgresql.business SET date_end = ? WHERE business_id = ?`,
[null, worker.businessFk]
);
2019-04-23 11:29:52 +00:00
2019-07-31 07:17:26 +00:00
let ctx = {req: {accessToken: {userId: 9}}};
2019-04-23 11:29:52 +00:00
const started = new Date();
started.setHours(0, 0, 0, 0);
started.setMonth(0);
started.setDate(1);
const monthIndex = 11;
const ended = new Date();
ended.setHours(0, 0, 0, 0);
ended.setMonth(monthIndex + 1);
ended.setDate(0);
let result = await app.models.WorkerCalendar.absences(ctx, workerFk, started, ended);
let calendar = result[0];
let absences = result[1];
expect(calendar.totalHolidays).toEqual(27.5);
2019-04-23 11:29:52 +00:00
expect(calendar.holidaysEnjoyed).toEqual(5);
let firstType = absences[0].absenceType().name;
let sixthType = absences[5].absenceType().name;
2020-01-03 07:43:03 +00:00
expect(firstType).toMatch(/(Holidays|Leave of absence)/);
expect(sixthType).toMatch(/(Holidays|Leave of absence)/);
2019-04-23 11:29:52 +00:00
// restores the contract end date
2019-12-11 14:01:01 +00:00
await app.models.WorkerLabour.rawSql(
`UPDATE postgresql.business SET date_end = ? WHERE business_id = ?`,
[endedDate, worker.businessFk]
);
});
2019-04-23 11:29:52 +00:00
it('should give the same holidays as worked days since the holidays amount matches the amount of days in a year', async() => {
const today = new Date();
// getting how many days in a year
const yearStart = new Date();
yearStart.setHours(0, 0, 0, 0);
yearStart.setMonth(0);
yearStart.setDate(1);
const yearEnd = new Date();
yearEnd.setHours(23, 59, 59, 59);
yearEnd.setMonth(11);
yearEnd.setDate(31);
const startedTime = yearStart.getTime();
const endedTime = yearEnd.getTime();
const dayTimestamp = 1000 * 60 * 60 * 24;
const daysInYear = Math.floor((endedTime - startedTime) / dayTimestamp);
// sets the holidays per year to the amount of days in the current year
let holidaysConfig = await app.models.WorkCenterHoliday.findOne({
where: {
workCenterFk: 1,
year: today.getFullYear()
}});
let originalHolidaysValue = holidaysConfig.days;
await app.models.WorkCenterHoliday.updateAll(
{
workCenterFk: 1,
year: today.getFullYear()
},
{
days: daysInYear
}
);
// normal test begins
const contract = await app.models.WorkerLabour.findById(106);
const contractStartDate = contract.started;
const startingContract = new Date();
startingContract.setHours(0, 0, 0, 0);
startingContract.setMonth(today.getMonth());
startingContract.setDate(1);
2019-12-11 14:01:01 +00:00
await app.models.WorkerLabour.rawSql(
`UPDATE postgresql.business SET date_start = ? WHERE business_id = ?`,
[startingContract, contract.businessFk]
);
2019-04-23 11:29:52 +00:00
let ctx = {req: {accessToken: {userId: 106}}};
let workerFk = 106;
let result = await app.models.WorkerCalendar.absences(ctx, workerFk, yearStart, yearEnd);
2019-04-23 11:29:52 +00:00
let calendar = result[0];
let absences = result[1];
let remainingDays = 0;
for (let i = today.getMonth(); i < 12; i++) {
today.setMonth(i + 1);
today.setDate(0);
2019-04-23 11:29:52 +00:00
remainingDays += today.getDate();
}
2019-04-23 11:29:52 +00:00
expect(calendar.totalHolidays).toEqual(remainingDays);
expect(calendar.holidaysEnjoyed).toEqual(5);
let firstType = absences[0].absenceType().name;
let sixthType = absences[5].absenceType().name;
2020-01-03 07:43:03 +00:00
expect(firstType).toMatch(/(Holidays|Leave of absence)/);
expect(sixthType).toMatch(/(Holidays|Leave of absence)/);
// resets the holidays per year with originalHolidaysValue and the contract starting date
await app.models.WorkCenterHoliday.updateAll(
{
workCenterFk: 1,
year: today.getFullYear()
},
{
days: originalHolidaysValue
}
);
2019-12-11 14:01:01 +00:00
await app.models.WorkerLabour.rawSql(
`UPDATE postgresql.business SET date_start = ? WHERE business_id = ?`,
[contractStartDate, contract.businessFk]
);
});
});