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

122 lines
4.3 KiB
JavaScript

const app = require('vn-loopback/server/server');
describe('Worker absences()', () => {
it('should get the absence calendar for a full year contract', async() => {
const ctx = {req: {accessToken: {userId: 1106}}};
const workerId = 1106;
const businessId = 1106;
const now = Date.vnNew();
const year = now.getFullYear();
const [absences] = await app.models.Calendar.absences(ctx, workerId, businessId, year);
const firstType = absences[0].absenceType().name;
const 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() => {
const businessId = 1106;
const ctx = {req: {accessToken: {userId: 9}}};
const now = Date.vnNew();
const year = now.getFullYear();
const tx = await app.models.Calendar.beginTransaction({});
try {
const options = {transaction: tx};
const worker = await app.models.WorkerLabour.findById(businessId, null, options);
await app.models.WorkerLabour.rawSql(
`UPDATE vn.business SET ended = ? WHERE id = ?`,
[null, worker.businessFk], options);
const [absences] = await app.models.Calendar.absences(ctx, worker.id, businessId, year, options);
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)/);
await tx.rollback();
} catch (e) {
await tx.rollback();
throw e;
}
});
it('should give the same holidays as worked days since the holidays amount matches the amount of days in a year', async() => {
const businessId = 1106;
const workerId = 1106;
const userId = 1106;
const today = Date.vnNew();
// getting how many days in a year
const yearStart = Date.vnNew();
yearStart.setHours(0, 0, 0, 0);
yearStart.setMonth(0);
yearStart.setDate(1);
const yearEnd = Date.vnNew();
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);
const tx = await app.models.Calendar.beginTransaction({});
try {
const options = {transaction: tx};
// sets the holidays per year to the amount of days in the current year
const holidaysConfig = await app.models.WorkCenterHoliday.findOne({
where: {
workCenterFk: 1,
year: today.getFullYear()
}
}, options);
await holidaysConfig.updateAttribute('days', daysInYear, options);
// normal test begins
const contract = await app.models.WorkerLabour.findById(businessId, null, options);
const startingContract = Date.vnNew();
startingContract.setHours(0, 0, 0, 0);
startingContract.setMonth(today.getMonth());
startingContract.setDate(1);
await app.models.WorkerLabour.rawSql(
`UPDATE vn.business SET started = ?, ended = ? WHERE id = ?`,
[startingContract, yearEnd, contract.businessFk], options
);
const ctx = {req: {accessToken: {userId: userId}}};
const [absences] = await app.models.Calendar.absences(ctx, workerId, businessId, currentYear);
const firstType = absences[0].absenceType().name;
const sixthType = absences[5].absenceType().name;
expect(firstType).toMatch(/(Holidays|Leave of absence)/);
expect(sixthType).toMatch(/(Holidays|Leave of absence)/);
await tx.rollback();
} catch (e) {
await tx.rollback();
throw e;
}
});
});