2019-03-22 11:51:44 +00:00
|
|
|
const app = require('vn-loopback/server/server');
|
|
|
|
|
2020-10-23 11:34:57 +00:00
|
|
|
describe('Worker absences()', () => {
|
2019-11-20 13:22:44 +00:00
|
|
|
it('should get the absence calendar for a full year contract', async() => {
|
2021-06-23 11:24:23 +00:00
|
|
|
const ctx = {req: {accessToken: {userId: 1106}}};
|
2022-02-25 09:33:47 +00:00
|
|
|
const workerId = 1106;
|
2021-06-23 11:24:23 +00:00
|
|
|
const businessId = 1106;
|
2019-03-22 11:51:44 +00:00
|
|
|
|
2021-06-10 14:05:10 +00:00
|
|
|
const now = new Date();
|
|
|
|
const year = now.getFullYear();
|
2019-03-22 11:51:44 +00:00
|
|
|
|
2022-02-25 09:33:47 +00:00
|
|
|
const [absences] = await app.models.Calendar.absences(ctx, workerId, businessId, year);
|
2019-03-22 11:51:44 +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
|
|
|
|
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
|
|
|
});
|
|
|
|
|
2019-11-20 13:22:44 +00:00
|
|
|
it('should get the absence calendar for a permanent contract', async() => {
|
2021-06-23 11:24:23 +00:00
|
|
|
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
|
|
|
|
2021-06-10 14:05:10 +00:00
|
|
|
const now = new Date();
|
|
|
|
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 postgresql.business SET date_end = ? WHERE business_id = ?`,
|
|
|
|
[null, worker.businessFk], options);
|
2019-04-23 11:29:52 +00:00
|
|
|
|
2022-02-25 09:33:47 +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-11-20 13:22:44 +00:00
|
|
|
});
|
2019-04-23 11:29:52 +00:00
|
|
|
|
2019-11-20 13:22:44 +00:00
|
|
|
it('should give the same holidays as worked days since the holidays amount matches the amount of days in a year', async() => {
|
2021-06-23 11:24:23 +00:00
|
|
|
const businessId = 1106;
|
2022-02-25 09:33:47 +00:00
|
|
|
const workerId = 1106;
|
|
|
|
|
2021-06-23 11:24:23 +00:00
|
|
|
const userId = 1106;
|
2019-11-20 13:22:44 +00:00
|
|
|
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);
|
2019-11-20 13:22:44 +00:00
|
|
|
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);
|
2019-11-20 13:22:44 +00:00
|
|
|
|
2021-06-10 14:05:10 +00:00
|
|
|
const tx = await app.models.Calendar.beginTransaction({});
|
|
|
|
try {
|
|
|
|
const options = {transaction: tx};
|
2019-11-20 13:22:44 +00:00
|
|
|
|
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);
|
2019-11-20 13:22:44 +00:00
|
|
|
|
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);
|
2019-11-20 13:22:44 +00:00
|
|
|
|
2021-06-10 14:05:10 +00:00
|
|
|
const startingContract = new Date();
|
|
|
|
startingContract.setHours(0, 0, 0, 0);
|
|
|
|
startingContract.setMonth(today.getMonth());
|
|
|
|
startingContract.setDate(1);
|
2019-11-20 13:22:44 +00:00
|
|
|
|
2021-06-10 14:05:10 +00:00
|
|
|
await app.models.WorkerLabour.rawSql(
|
|
|
|
`UPDATE postgresql.business SET date_start = ?, date_end = ? WHERE business_id = ?`,
|
|
|
|
[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
|
|
|
|
2022-02-25 09:33:47 +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)/);
|
2019-03-22 11:51:44 +00:00
|
|
|
|
2021-06-10 14:05:10 +00:00
|
|
|
await tx.rollback();
|
|
|
|
} catch (e) {
|
|
|
|
await tx.rollback();
|
|
|
|
throw e;
|
|
|
|
}
|
2019-03-22 11:51:44 +00:00
|
|
|
});
|
|
|
|
});
|