164 lines
5.6 KiB
JavaScript
164 lines
5.6 KiB
JavaScript
const app = require('vn-loopback/server/server');
|
|
|
|
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.Calendar.absences(ctx, workerFk, started, ended);
|
|
let calendar = result[0];
|
|
let absences = result[1];
|
|
|
|
expect(calendar.totalHolidays).toEqual(27.5);
|
|
expect(calendar.holidaysEnjoyed).toEqual(5);
|
|
|
|
let firstType = absences[0].absenceType().name;
|
|
let sixthType = absences[5].absenceType().name;
|
|
|
|
expect(firstType).toMatch(/(Holidays|Leave of absence)/);
|
|
expect(sixthType).toMatch(/(Holidays|Leave of absence)/);
|
|
});
|
|
|
|
it('should get the absence calendar for a permanent contract', async() => {
|
|
let workerFk = 106;
|
|
let worker = await app.models.WorkerLabour.findById(workerFk);
|
|
let endedDate = worker.ended;
|
|
|
|
await app.models.WorkerLabour.rawSql(
|
|
`UPDATE postgresql.business SET date_end = ? WHERE business_id = ?`,
|
|
[null, worker.businessFk]
|
|
);
|
|
|
|
let ctx = {req: {accessToken: {userId: 9}}};
|
|
|
|
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.Calendar.absences(ctx, workerFk, started, ended);
|
|
let calendar = result[0];
|
|
let absences = result[1];
|
|
|
|
expect(calendar.totalHolidays).toEqual(27.5);
|
|
expect(calendar.holidaysEnjoyed).toEqual(5);
|
|
|
|
let firstType = absences[0].absenceType().name;
|
|
let sixthType = absences[5].absenceType().name;
|
|
|
|
expect(firstType).toMatch(/(Holidays|Leave of absence)/);
|
|
expect(sixthType).toMatch(/(Holidays|Leave of absence)/);
|
|
|
|
// restores the contract end date
|
|
await app.models.WorkerLabour.rawSql(
|
|
`UPDATE postgresql.business SET date_end = ? WHERE business_id = ?`,
|
|
[endedDate, worker.businessFk]
|
|
);
|
|
});
|
|
|
|
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();
|
|
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;
|
|
|
|
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;
|
|
|
|
await holidaysConfig.updateAttribute('days', daysInYear);
|
|
|
|
// normal test begins
|
|
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);
|
|
|
|
await app.models.WorkerLabour.rawSql(
|
|
`UPDATE postgresql.business SET date_start = ?, date_end = ? WHERE business_id = ?`,
|
|
[startingContract, yearEnd, contract.businessFk]
|
|
);
|
|
|
|
let ctx = {req: {accessToken: {userId: userId}}};
|
|
|
|
let result = await app.models.Calendar.absences(ctx, userId, yearStart, yearEnd);
|
|
let calendar = result[0];
|
|
let absences = result[1];
|
|
|
|
let remainingDays = 0;
|
|
for (let i = today.getMonth(); i < 12; i++) {
|
|
today.setDate(1);
|
|
today.setMonth(i + 1);
|
|
today.setDate(0);
|
|
|
|
remainingDays += today.getDate();
|
|
}
|
|
|
|
expect(calendar.totalHolidays).toEqual(remainingDays);
|
|
expect(calendar.holidaysEnjoyed).toEqual(5);
|
|
|
|
let firstType = absences[0].absenceType().name;
|
|
let sixthType = absences[5].absenceType().name;
|
|
|
|
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
|
|
}
|
|
);
|
|
|
|
await app.models.WorkerLabour.rawSql(
|
|
`UPDATE postgresql.business SET date_start = ? WHERE business_id = ?`,
|
|
[contractStartDate, contract.businessFk]
|
|
);
|
|
});
|
|
});
|