Worker calendar rework #645

Merged
carlosjr merged 15 commits from 2567-calendar_rework into dev 2021-06-14 10:04:45 +00:00
4 changed files with 186 additions and 149 deletions
Showing only changes of commit 3f3e5c2735 - Show all commits

View File

@ -2,78 +2,56 @@ const app = require('vn-loopback/server/server');
describe('Worker absences()', () => { describe('Worker absences()', () => {
it('should get the absence calendar for a full year contract', async() => { it('should get the absence calendar for a full year contract', async() => {
let ctx = {req: {accessToken: {userId: 106}}}; const ctx = {req: {accessToken: {userId: 106}}};
let workerFk = 106; const businessId = 106;
const started = new Date(); const now = new Date();
started.setHours(0, 0, 0, 0); const year = now.getFullYear();
started.setMonth(0);
started.setDate(1);
const monthIndex = 11; const [absences] = await app.models.Calendar.absences(ctx, businessId, year);
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); const firstType = absences[0].absenceType().name;
let calendar = result[0]; const sixthType = absences[5].absenceType().name;
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(firstType).toMatch(/(Holidays|Leave of absence)/);
expect(sixthType).toMatch(/(Holidays|Leave of absence)/); expect(sixthType).toMatch(/(Holidays|Leave of absence)/);
}); });
it('should get the absence calendar for a permanent contract', async() => { it('should get the absence calendar for a permanent contract', async() => {
let workerFk = 106; const businessId = 106;
let worker = await app.models.WorkerLabour.findById(workerFk); const ctx = {req: {accessToken: {userId: 9}}};
let endedDate = worker.ended;
const now = new Date();
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( await app.models.WorkerLabour.rawSql(
`UPDATE postgresql.business SET date_end = ? WHERE business_id = ?`, `UPDATE postgresql.business SET date_end = ? WHERE business_id = ?`,
[null, worker.businessFk] [null, worker.businessFk], options);
);
let ctx = {req: {accessToken: {userId: 9}}}; const [absences] = await app.models.Calendar.absences(ctx, businessId, year, options);
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 firstType = absences[0].absenceType().name;
let sixthType = absences[5].absenceType().name; let sixthType = absences[5].absenceType().name;
expect(firstType).toMatch(/(Holidays|Leave of absence)/); expect(firstType).toMatch(/(Holidays|Leave of absence)/);
expect(sixthType).toMatch(/(Holidays|Leave of absence)/); expect(sixthType).toMatch(/(Holidays|Leave of absence)/);
await tx.rollback();
// restores the contract end date } catch (e) {
await app.models.WorkerLabour.rawSql( await tx.rollback();
`UPDATE postgresql.business SET date_end = ? WHERE business_id = ?`, throw e;
[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() => { it('should give the same holidays as worked days since the holidays amount matches the amount of days in a year', async() => {
const businessId = 106;
const userId = 106;
const today = new Date(); const today = new Date();
// getting how many days in a year // getting how many days in a year
@ -94,21 +72,22 @@ describe('Worker absences()', () => {
const daysInYear = Math.round((endedTime - startedTime) / dayTimestamp); 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 // sets the holidays per year to the amount of days in the current year
let holidaysConfig = await app.models.WorkCenterHoliday.findOne({ const holidaysConfig = await app.models.WorkCenterHoliday.findOne({
where: { where: {
workCenterFk: 1, workCenterFk: 1,
year: today.getFullYear() year: today.getFullYear()
}}); }
}, options);
let originalHolidaysValue = holidaysConfig.days; await holidaysConfig.updateAttribute('days', daysInYear, options);
await holidaysConfig.updateAttribute('days', daysInYear);
// normal test begins // normal test begins
const userId = 106; const contract = await app.models.WorkerLabour.findById(businessId, null, options);
const contract = await app.models.WorkerLabour.findById(userId);
const contractStartDate = contract.started;
const startingContract = new Date(); const startingContract = new Date();
startingContract.setHours(0, 0, 0, 0); startingContract.setHours(0, 0, 0, 0);
@ -117,47 +96,23 @@ describe('Worker absences()', () => {
await app.models.WorkerLabour.rawSql( await app.models.WorkerLabour.rawSql(
`UPDATE postgresql.business SET date_start = ?, date_end = ? WHERE business_id = ?`, `UPDATE postgresql.business SET date_start = ?, date_end = ? WHERE business_id = ?`,
[startingContract, yearEnd, contract.businessFk] [startingContract, yearEnd, contract.businessFk], options
); );
let ctx = {req: {accessToken: {userId: userId}}}; const ctx = {req: {accessToken: {userId: userId}}};
let result = await app.models.Calendar.absences(ctx, userId, yearStart, yearEnd); const [absences] = await app.models.Calendar.absences(ctx, businessId, currentYear);
let calendar = result[0];
let absences = result[1];
let remainingDays = 0; const firstType = absences[0].absenceType().name;
for (let i = today.getMonth(); i < 12; i++) { const sixthType = absences[5].absenceType().name;
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(firstType).toMatch(/(Holidays|Leave of absence)/);
expect(sixthType).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 tx.rollback();
await app.models.WorkCenterHoliday.updateAll( } catch (e) {
{ await tx.rollback();
workCenterFk: 1, throw e;
year: today.getFullYear()
},
{
days: originalHolidaysValue
} }
);
await app.models.WorkerLabour.rawSql(
`UPDATE postgresql.business SET date_start = ? WHERE business_id = ?`,
[contractStartDate, contract.businessFk]
);
}); });
}); });

View File

@ -5,18 +5,34 @@ describe('Worker createAbsence()', () => {
const workerId = 18; const workerId = 18;
it('should return an error for a user without enough privileges', async() => { it('should return an error for a user without enough privileges', async() => {
const ctx = {req: {accessToken: {userId: 18}}}; const ctx = {
const absenceTypeId = 1; req: {accessToken: {userId: 18}},
const dated = new Date(); args: {
businessFk: 18,
absenceTypeId: 1,
dated: new Date()
}
};
const tx = await app.models.Calendar.beginTransaction({});
try {
const options = {transaction: tx};
let error; let error;
await app.models.Worker.createAbsence(ctx, workerId, absenceTypeId, dated).catch(e => { await app.models.Worker.createAbsence(ctx, workerId, options).catch(e => {
error = e; error = e;
}).finally(() => { }).finally(() => {
expect(error.message).toEqual(`You don't have enough privileges`); expect(error.message).toEqual(`You don't have enough privileges`);
}); });
expect(error).toBeDefined(); expect(error).toBeDefined();
await tx.rollback();
} catch (e) {
await tx.rollback();
throw e;
}
}); });
it('should create a new absence', async() => { it('should create a new absence', async() => {
@ -24,7 +40,14 @@ describe('Worker createAbsence()', () => {
accessToken: {userId: 19}, accessToken: {userId: 19},
headers: {origin: 'http://localhost'} headers: {origin: 'http://localhost'}
}; };
const ctx = {req: activeCtx}; const ctx = {
req: activeCtx,
args: {
businessFk: 18,
absenceTypeId: 1,
dated: new Date()
}
};
ctx.req.__ = value => { ctx.req.__ = value => {
return value; return value;
}; };
@ -32,9 +55,12 @@ describe('Worker createAbsence()', () => {
active: activeCtx active: activeCtx
}); });
const absenceTypeId = 1; const tx = await app.models.Calendar.beginTransaction({});
const dated = new Date();
const createdAbsence = await app.models.Worker.createAbsence(ctx, workerId, absenceTypeId, dated); try {
const options = {transaction: tx};
const createdAbsence = await app.models.Worker.createAbsence(ctx, workerId, options);
const expectedBusinessId = 18; const expectedBusinessId = 18;
const expectedAbsenceTypeId = 1; const expectedAbsenceTypeId = 1;
@ -42,7 +68,10 @@ describe('Worker createAbsence()', () => {
expect(createdAbsence.businessFk).toEqual(expectedBusinessId); expect(createdAbsence.businessFk).toEqual(expectedBusinessId);
expect(createdAbsence.dayOffTypeFk).toEqual(expectedAbsenceTypeId); expect(createdAbsence.dayOffTypeFk).toEqual(expectedAbsenceTypeId);
// Restores await tx.rollback();
await app.models.Calendar.destroyById(createdAbsence.id); } catch (e) {
await tx.rollback();
throw e;
}
}); });
}); });

View File

@ -12,45 +12,68 @@ describe('Worker deleteAbsence()', () => {
ctx.req.__ = value => { ctx.req.__ = value => {
return value; return value;
}; };
let createdAbsence;
beforeEach(async() => { beforeEach(async() => {
spyOn(LoopBackContext, 'getCurrentContext').and.returnValue({ spyOn(LoopBackContext, 'getCurrentContext').and.returnValue({
active: activeCtx active: activeCtx
}); });
createdAbsence = await app.models.Calendar.create({
businessFk: businessId,
dayOffTypeFk: 1,
dated: new Date()
});
});
afterEach(async() => {
await app.models.Calendar.destroyById(createdAbsence.id);
}); });
it('should return an error for a user without enough privileges', async() => { it('should return an error for a user without enough privileges', async() => {
activeCtx.accessToken.userId = 106; activeCtx.accessToken.userId = 106;
const tx = await app.models.Calendar.beginTransaction({});
try {
const options = {transaction: tx};
const createdAbsence = await app.models.Calendar.create({
businessFk: businessId,
dayOffTypeFk: 1,
dated: new Date()
}, options);
ctx.args = {absenceId: createdAbsence.id};
let error; let error;
await app.models.Worker.deleteAbsence(ctx, 18, createdAbsence.id).catch(e => { await app.models.Worker.deleteAbsence(ctx, workerId).catch(e => {
error = e; error = e;
}).finally(() => { }).finally(() => {
expect(error.message).toEqual(`You don't have enough privileges`); expect(error.message).toEqual(`You don't have enough privileges`);
}); });
expect(error).toBeDefined(); expect(error).toBeDefined();
await tx.rollback();
} catch (e) {
await tx.rollback();
throw e;
}
}); });
it('should create a new absence', async() => { it('should successfully delete an absence', async() => {
activeCtx.accessToken.userId = 19; activeCtx.accessToken.userId = 19;
expect(createdAbsence.businessFk).toEqual(businessId); const tx = await app.models.Calendar.beginTransaction({});
await app.models.Worker.deleteAbsence(ctx, workerId, createdAbsence.id); try {
const options = {transaction: tx};
const createdAbsence = await app.models.Calendar.create({
businessFk: businessId,
dayOffTypeFk: 1,
dated: new Date()
}, options);
const deletedAbsence = await app.models.Calendar.findById(createdAbsence.id); ctx.args = {absenceId: createdAbsence.id};
await app.models.Worker.deleteAbsence(ctx, workerId, options);
const deletedAbsence = await app.models.Calendar.findById(createdAbsence.id, null, options);
expect(deletedAbsence).toBeNull(); expect(deletedAbsence).toBeNull();
await tx.rollback();
} catch (e) {
await tx.rollback();
throw e;
}
}); });
}); });

View File

@ -0,0 +1,30 @@
const app = require('vn-loopback/server/server');
const LoopBackContext = require('loopback-context');
describe('Worker holidays()', () => {
const businessId = 106;
const workerId = 106;
const activeCtx = {
accessToken: {userId: workerId},
headers: {origin: 'http://localhost'}
};
const ctx = {req: activeCtx};
beforeEach(async() => {
spyOn(LoopBackContext, 'getCurrentContext').and.returnValue({
active: activeCtx
});
});
it('should get the absence calendar for a full year contract', async() => {
const now = new Date();
const year = now.getFullYear();
ctx.args = {businessFk: businessId, year: year};
const result = await app.models.Worker.holidays(ctx, workerId);
expect(result.totalHolidays).toEqual(27.5);
expect(result.holidaysEnjoyed).toEqual(5);
});
});