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
|
|
|
|
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-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
|
|
|
|
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(
|
2022-09-19 07:45:28 +00:00
|
|
|
`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
|
|
|
|
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;
|
2023-01-16 14:18:24 +00:00
|
|
|
const today = Date.vnNew();
|
2019-11-20 13:22:44 +00:00
|
|
|
|
|
|
|
// getting how many days in a year
|
2023-01-16 14:18:24 +00:00
|
|
|
const yearStart = Date.vnNew();
|
2019-11-20 13:22:44 +00:00
|
|
|
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);
|
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
|
|
|
|
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);
|
2019-11-20 13:22:44 +00:00
|
|
|
|
2021-06-10 14:05:10 +00:00
|
|
|
await app.models.WorkerLabour.rawSql(
|
2022-09-19 07:45:28 +00:00
|
|
|
`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
|
|
|
|
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
|
|
|
});
|
|
|
|
});
|