salix/modules/worker/back/methods/worker-time-control/specs/timeEntry.spec.js

216 lines
7.1 KiB
JavaScript
Raw Normal View History

2019-11-18 10:00:09 +00:00
const app = require('vn-loopback/server/server');
2020-10-08 14:00:19 +00:00
const LoopBackContext = require('loopback-context');
2021-06-17 13:09:04 +00:00
const models = app.models;
2020-10-08 14:00:19 +00:00
2021-02-02 09:12:37 +00:00
describe('workerTimeControl add/delete timeEntry()', () => {
2020-10-08 14:00:19 +00:00
const HHRRId = 37;
const teamBossId = 13;
const employeeId = 1;
const salesPersonId = 1106;
2021-02-02 09:07:13 +00:00
const salesBossId = 19;
2020-10-08 14:00:19 +00:00
let activeCtx = {
accessToken: {userId: 50},
};
let ctx = {req: activeCtx};
2019-11-18 10:00:09 +00:00
2022-04-01 11:55:45 +00:00
afterAll(() => {
jasmine.clock().install();
const baseTime = new Date(2022, 0, 19);
jasmine.clock().mockDate(baseTime);
});
2020-10-08 14:00:19 +00:00
beforeAll(() => {
spyOn(LoopBackContext, 'getCurrentContext').and.returnValue({
active: activeCtx
});
2022-04-01 11:55:45 +00:00
jasmine.clock().uninstall();
2020-10-08 14:00:19 +00:00
});
2019-11-18 10:00:09 +00:00
it('should fail to add a time entry if the target user is not a subordinate', async() => {
2020-10-08 14:00:19 +00:00
activeCtx.accessToken.userId = employeeId;
2021-06-17 13:09:04 +00:00
const workerId = 2;
2019-11-18 10:00:09 +00:00
let error;
try {
2022-04-01 11:55:45 +00:00
ctx.args = {timed: new Date(), direction: 'in'};
2021-06-17 13:09:04 +00:00
await models.WorkerTimeControl.addTimeEntry(ctx, workerId);
2019-11-18 10:00:09 +00:00
} catch (e) {
error = e;
}
expect(error).toBeDefined();
expect(error.statusCode).toBe(400);
expect(error.message).toBe(`You don't have enough privileges`);
});
it('should fail to add if the current and the target user are the same and is not team boss', async() => {
2020-10-08 14:00:19 +00:00
activeCtx.accessToken.userId = employeeId;
2021-06-17 13:09:04 +00:00
const workerId = employeeId;
2019-11-18 10:00:09 +00:00
let error;
try {
2022-04-01 11:55:45 +00:00
ctx.args = {timed: new Date(), direction: 'in'};
2021-06-17 13:09:04 +00:00
await models.WorkerTimeControl.addTimeEntry(ctx, workerId);
2019-11-18 10:00:09 +00:00
} catch (e) {
error = e;
}
expect(error).toBeDefined();
expect(error.statusCode).toBe(400);
expect(error.message).toBe(`You don't have enough privileges`);
});
2020-10-08 14:00:19 +00:00
it('should add if the current user is team boss and the target user is a himself', async() => {
activeCtx.accessToken.userId = teamBossId;
2021-06-17 13:09:04 +00:00
const workerId = teamBossId;
2019-11-18 10:00:09 +00:00
2021-06-17 13:09:04 +00:00
const tx = await models.WorkerTimeControl.beginTransaction({});
try {
const options = {transaction: tx};
2022-04-01 11:55:45 +00:00
const todayAtSix = new Date();
2021-06-17 13:09:04 +00:00
todayAtSix.setHours(18, 30, 0, 0);
2019-11-18 10:00:09 +00:00
2021-06-17 13:09:04 +00:00
ctx.args = {timed: todayAtSix, direction: 'in'};
const createdTimeEntry = await models.WorkerTimeControl.addTimeEntry(ctx, workerId, options);
2019-11-18 10:00:09 +00:00
2021-06-17 13:09:04 +00:00
expect(createdTimeEntry.id).toBeDefined();
2019-11-18 10:00:09 +00:00
2021-06-17 13:09:04 +00:00
await tx.rollback();
} catch (e) {
await tx.rollback();
throw e;
}
2019-11-18 10:00:09 +00:00
});
it('should fail to add a time entry if the target user has absent that day', async() => {
activeCtx.accessToken.userId = salesBossId;
const workerId = salesPersonId;
let error;
let calendar = await app.models.Calendar.findById(3);
try {
ctx.args = {timed: new Date(calendar.dated), direction: 'in'};
await models.WorkerTimeControl.addTimeEntry(ctx, workerId);
} catch (e) {
error = e;
}
expect(error.message).toBe(`The worker has a marked absence that day`);
});
2020-10-08 14:00:19 +00:00
it('should try but fail to delete his own time entry', async() => {
2021-02-02 09:07:13 +00:00
activeCtx.accessToken.userId = salesBossId;
2021-06-17 13:09:04 +00:00
const workerId = salesBossId;
2020-10-08 14:00:19 +00:00
2021-06-17 13:09:04 +00:00
let error;
const tx = await models.WorkerTimeControl.beginTransaction({});
try {
const options = {transaction: tx};
2020-10-08 14:00:19 +00:00
2022-04-01 11:55:45 +00:00
const todayAtSeven = new Date();
2021-06-17 13:09:04 +00:00
todayAtSeven.setHours(19, 30, 0, 0);
2020-10-08 14:00:19 +00:00
2021-06-17 13:09:04 +00:00
ctx.args = {timed: todayAtSeven, direction: 'in'};
const createdTimeEntry = await models.WorkerTimeControl.addTimeEntry(ctx, workerId, options);
2019-11-18 10:00:09 +00:00
2021-02-02 09:07:13 +00:00
activeCtx.accessToken.userId = salesPersonId;
2021-06-17 13:09:04 +00:00
await models.WorkerTimeControl.deleteTimeEntry(ctx, createdTimeEntry.id, options);
await tx.rollback();
2019-11-18 10:00:09 +00:00
} catch (e) {
error = e;
2021-06-17 13:09:04 +00:00
await tx.rollback();
2019-11-18 10:00:09 +00:00
}
expect(error).toBeDefined();
expect(error.statusCode).toBe(400);
expect(error.message).toBe(`You don't have enough privileges`);
});
2021-02-02 09:07:13 +00:00
it('should delete the created time entry for the team boss as himself', async() => {
activeCtx.accessToken.userId = teamBossId;
2021-06-17 13:09:04 +00:00
const workerId = teamBossId;
2021-02-02 09:07:13 +00:00
2021-06-17 13:09:04 +00:00
const tx = await models.WorkerTimeControl.beginTransaction({});
try {
const options = {transaction: tx};
2021-02-02 09:07:13 +00:00
2022-04-01 11:55:45 +00:00
const todayAtFive = new Date();
2021-06-17 13:09:04 +00:00
todayAtFive.setHours(17, 30, 0, 0);
2021-02-02 09:07:13 +00:00
2021-06-17 13:09:04 +00:00
ctx.args = {timed: todayAtFive, direction: 'in'};
const createdTimeEntry = await models.WorkerTimeControl.addTimeEntry(ctx, workerId, options);
2021-02-02 09:07:13 +00:00
2021-06-17 13:09:04 +00:00
expect(createdTimeEntry.id).toBeDefined();
2021-02-02 09:07:13 +00:00
2021-06-17 13:09:04 +00:00
await models.WorkerTimeControl.deleteTimeEntry(ctx, createdTimeEntry.id, options);
2021-02-02 09:07:13 +00:00
2021-06-17 13:09:04 +00:00
const deletedTimeEntry = await models.WorkerTimeControl.findById(createdTimeEntry.id, null, options);
2021-02-02 09:07:13 +00:00
2021-06-17 13:09:04 +00:00
expect(deletedTimeEntry).toBeNull();
await tx.rollback();
} catch (e) {
await tx.rollback();
throw e;
}
2021-02-02 09:07:13 +00:00
});
2019-11-18 10:00:09 +00:00
it('should delete the created time entry for the team boss as HHRR', async() => {
2020-10-08 14:00:19 +00:00
activeCtx.accessToken.userId = HHRRId;
2021-06-17 13:09:04 +00:00
const workerId = teamBossId;
const tx = await models.WorkerTimeControl.beginTransaction({});
try {
const options = {transaction: tx};
2022-04-01 11:55:45 +00:00
const todayAtFive = new Date();
2021-06-17 13:09:04 +00:00
todayAtFive.setHours(17, 30, 0, 0);
ctx.args = {timed: todayAtFive, direction: 'in'};
const createdTimeEntry = await models.WorkerTimeControl.addTimeEntry(ctx, workerId, options);
expect(createdTimeEntry.id).toBeDefined();
await models.WorkerTimeControl.deleteTimeEntry(ctx, createdTimeEntry.id, options);
const deletedTimeEntry = await models.WorkerTimeControl.findById(createdTimeEntry.id, null, options);
2020-10-08 14:00:19 +00:00
2021-06-17 13:09:04 +00:00
expect(deletedTimeEntry).toBeNull();
await tx.rollback();
} catch (e) {
await tx.rollback();
throw e;
}
});
it('should edit the created time entry for the team boss as HHRR', async() => {
activeCtx.accessToken.userId = HHRRId;
const workerId = teamBossId;
2020-10-08 14:00:19 +00:00
2021-06-17 13:09:04 +00:00
const tx = await models.WorkerTimeControl.beginTransaction({});
try {
const options = {transaction: tx};
2020-10-08 14:00:19 +00:00
2022-04-01 11:55:45 +00:00
const todayAtFive = new Date();
2021-06-17 13:09:04 +00:00
todayAtFive.setHours(17, 30, 0, 0);
2020-10-08 14:00:19 +00:00
2021-06-17 13:09:04 +00:00
ctx.args = {timed: todayAtFive, direction: 'in'};
const createdTimeEntry = await models.WorkerTimeControl.addTimeEntry(ctx, workerId, options);
2019-11-18 10:00:09 +00:00
2021-06-17 13:09:04 +00:00
expect(createdTimeEntry.id).toBeDefined();
2019-11-18 10:00:09 +00:00
2021-06-17 13:09:04 +00:00
ctx.args = {direction: 'out'};
const updatedTimeEntry = await models.WorkerTimeControl.updateTimeEntry(ctx, createdTimeEntry.id, options);
2019-11-18 10:00:09 +00:00
2021-06-17 13:09:04 +00:00
expect(updatedTimeEntry.direction).toEqual('out');
await tx.rollback();
} catch (e) {
await tx.rollback();
throw e;
}
2019-11-18 10:00:09 +00:00
});
});