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

122 lines
4.3 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() => {
const ctx = {req: {accessToken: {userId: 1106}}};
const workerId = 1106;
const businessId = 1106;
2023-01-16 14:18:24 +00:00
const now = Date.vnNew();
2021-06-10 14:05:10 +00:00
const year = now.getFullYear();
const [absences] = await app.models.Calendar.absences(ctx, workerId, businessId, year);
2021-06-10 14:05:10 +00:00
const firstType = absences[0].absenceType().name;
const sixthType = absences[5].absenceType().name;
2019-04-23 11:29:52 +00:00
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() => {
const businessId = 1106;
2021-06-10 14:05:10 +00:00
const ctx = {req: {accessToken: {userId: 9}}};
2019-12-11 14:01:01 +00:00
2023-01-16 14:18:24 +00:00
const now = Date.vnNew();
2021-06-10 14:05:10 +00:00
const year = now.getFullYear();
2019-04-23 11:29:52 +00:00
2021-06-10 14:05:10 +00:00
const tx = await app.models.Calendar.beginTransaction({});
2019-04-23 11:29:52 +00:00
2021-06-10 14:05:10 +00:00
try {
const options = {transaction: tx};
2019-04-23 11:29:52 +00:00
2021-06-10 14:05:10 +00:00
const worker = await app.models.WorkerLabour.findById(businessId, null, options);
2019-04-23 11:29:52 +00:00
2021-06-10 14:05:10 +00:00
await app.models.WorkerLabour.rawSql(
`UPDATE vn.business SET ended = ? WHERE id = ?`,
2021-06-10 14:05:10 +00:00
[null, worker.businessFk], options);
2019-04-23 11:29:52 +00:00
const [absences] = await app.models.Calendar.absences(ctx, worker.id, businessId, year, options);
2019-04-23 11:29:52 +00:00
2021-06-10 14:05:10 +00:00
let firstType = absences[0].absenceType().name;
let sixthType = absences[5].absenceType().name;
2019-04-23 11:29:52 +00:00
2021-06-10 14:05:10 +00:00
expect(firstType).toMatch(/(Holidays|Leave of absence)/);
expect(sixthType).toMatch(/(Holidays|Leave of absence)/);
await tx.rollback();
} catch (e) {
await tx.rollback();
throw e;
}
});
2019-04-23 11:29:52 +00:00
2023-09-22 07:40:37 +00:00
it('Should have an equal number of holidays and workdays, as they both total the days in a year', async() => {
const businessId = 1106;
const workerId = 1106;
const userId = 1106;
2023-01-16 14:18:24 +00:00
const today = Date.vnNew();
// getting how many days in a year
2023-01-16 14:18:24 +00:00
const yearStart = Date.vnNew();
yearStart.setHours(0, 0, 0, 0);
yearStart.setMonth(0);
yearStart.setDate(1);
2023-01-16 14:18:24 +00:00
const yearEnd = Date.vnNew();
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);
2021-06-10 14:05:10 +00:00
const tx = await app.models.Calendar.beginTransaction({});
try {
const options = {transaction: tx};
2021-06-10 14:05:10 +00:00
// 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);
2021-06-10 14:05:10 +00:00
await holidaysConfig.updateAttribute('days', daysInYear, options);
2020-10-23 11:34:57 +00:00
2021-06-10 14:05:10 +00:00
// normal test begins
const contract = await app.models.WorkerLabour.findById(businessId, null, options);
2023-01-16 14:18:24 +00:00
const startingContract = Date.vnNew();
2021-06-10 14:05:10 +00:00
startingContract.setHours(0, 0, 0, 0);
startingContract.setMonth(today.getMonth());
startingContract.setDate(1);
2021-06-10 14:05:10 +00:00
await app.models.WorkerLabour.rawSql(
`UPDATE vn.business SET started = ?, ended = ? WHERE id = ?`,
2021-06-10 14:05:10 +00:00
[startingContract, yearEnd, contract.businessFk], options
);
2019-04-23 11:29:52 +00:00
2021-06-10 14:05:10 +00:00
const ctx = {req: {accessToken: {userId: userId}}};
2019-04-23 11:29:52 +00:00
const [absences] = await app.models.Calendar.absences(ctx, workerId, businessId, currentYear);
2019-04-23 11:29:52 +00:00
2021-06-10 14:05:10 +00:00
const firstType = absences[0].absenceType().name;
const sixthType = absences[5].absenceType().name;
2019-04-23 11:29:52 +00:00
2021-06-10 14:05:10 +00:00
expect(firstType).toMatch(/(Holidays|Leave of absence)/);
expect(sixthType).toMatch(/(Holidays|Leave of absence)/);
2021-06-10 14:05:10 +00:00
await tx.rollback();
} catch (e) {
await tx.rollback();
throw e;
}
});
});