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

164 lines
5.6 KiB
JavaScript
Raw Normal View History

const app = require('vn-loopback/server/server');
2020-10-23 11:34:57 +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);
2020-08-10 12:29:25 +00:00
let result = await app.models.Calendar.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);
2020-08-10 12:29:25 +00:00
let result = await app.models.Calendar.absences(ctx, workerFk, started, ended);
2019-04-23 11:29:52 +00:00
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();
2020-10-23 11:34:57 +00:00
const currentYear = yearEnd.getFullYear();
yearEnd.setFullYear(currentYear + 1);
yearEnd.setHours(0, 0, 0, 0);
yearEnd.setMonth(0);
yearEnd.setDate(1);
const startedTime = yearStart.getTime();
const endedTime = yearEnd.getTime();
const dayTimestamp = 1000 * 60 * 60 * 24;
2020-10-23 11:34:57 +00:00
const daysInYear = Math.round((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;
2020-10-23 11:34:57 +00:00
await holidaysConfig.updateAttribute('days', daysInYear);
// normal test begins
2020-10-23 11:34:57 +00:00
const userId = 106;
const contract = await app.models.WorkerLabour.findById(userId);
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(
2020-10-23 11:34:57 +00:00
`UPDATE postgresql.business SET date_start = ?, date_end = ? WHERE business_id = ?`,
[startingContract, yearEnd, contract.businessFk]
2019-12-11 14:01:01 +00:00
);
2019-04-23 11:29:52 +00:00
2020-10-23 11:34:57 +00:00
let ctx = {req: {accessToken: {userId: userId}}};
2019-04-23 11:29:52 +00:00
2020-10-23 11:34:57 +00:00
let result = await app.models.Calendar.absences(ctx, userId, 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++) {
2020-01-30 10:20:41 +00:00
today.setDate(1);
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]
);
});
});