#1852 worker.time-control
This commit is contained in:
parent
d1b114de59
commit
7c199b5d74
|
@ -62,7 +62,7 @@ module.exports = Self => {
|
|||
*/
|
||||
Self.hasRole = async function(userId, name) {
|
||||
let roles = await Self.getRoles(userId);
|
||||
return roles.find(role => role == name);
|
||||
return roles.some(role => role == name);
|
||||
};
|
||||
|
||||
/**
|
||||
|
|
|
@ -26,7 +26,7 @@ module.exports = Self => {
|
|||
const myUserId = ctx.req.accessToken.userId;
|
||||
const myWorker = await Worker.findOne({where: {userFk: myUserId}});
|
||||
const isSubordinate = await Worker.isSubordinate(ctx, data.workerFk);
|
||||
const isTeamBoss = await Self.app.models.Account.hasRole(myUserId, 'teamBoss') == 'teamBoss';
|
||||
const isTeamBoss = await Self.app.models.Account.hasRole(myUserId, 'teamBoss');
|
||||
|
||||
if (isSubordinate === false || (isSubordinate && myWorker.id == data.workerFk && !isTeamBoss))
|
||||
throw new UserError(`You don't have enough privileges`);
|
||||
|
|
|
@ -28,9 +28,9 @@ module.exports = Self => {
|
|||
const targetTimeEntry = await Self.findById(id);
|
||||
|
||||
const isSubordinate = await workerModel.isSubordinate(ctx, targetTimeEntry.userFk);
|
||||
const isTeamBoss = await Self.app.models.Account.hasRole(currentUserId, 'teamBoss');
|
||||
const isHHRR = await Self.app.models.Account.hasRole(currentUserId, 'hr');
|
||||
|
||||
const notAllowed = isSubordinate === false || (isSubordinate && currentUserId == targetTimeEntry.userFk && !isTeamBoss);
|
||||
const notAllowed = isSubordinate === false || (isSubordinate && currentUserId == targetTimeEntry.userFk && !isHHRR);
|
||||
|
||||
if (notAllowed)
|
||||
throw new UserError(`You don't have enough privileges`);
|
||||
|
|
|
@ -1,53 +0,0 @@
|
|||
const app = require('vn-loopback/server/server');
|
||||
|
||||
fdescribe('workerTimeControl addTimeEntry()', () => {
|
||||
it('should fail to add a time entry if the target user is not a subordinate', async() => {
|
||||
let error;
|
||||
let ctx = {req: {accessToken: {userId: 1}}};
|
||||
let data = {
|
||||
workerFk: 2,
|
||||
timed: new Date()
|
||||
};
|
||||
|
||||
try {
|
||||
await app.models.WorkerTimeControl.addTimeEntry(ctx, data);
|
||||
} 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() => {
|
||||
let error;
|
||||
let ctx = {req: {accessToken: {userId: 1}}};
|
||||
let data = {
|
||||
workerFk: 1,
|
||||
timed: new Date()
|
||||
};
|
||||
|
||||
try {
|
||||
await app.models.WorkerTimeControl.addTimeEntry(ctx, data);
|
||||
} catch (e) {
|
||||
error = e;
|
||||
}
|
||||
|
||||
expect(error).toBeDefined();
|
||||
expect(error.statusCode).toBe(400);
|
||||
expect(error.message).toBe(`You don't have enough privileges`);
|
||||
});
|
||||
|
||||
fit('should add if the current user is team boss and the target user is a subordinate', async() => {
|
||||
let ctx = {req: {accessToken: {userId: 13}}};
|
||||
let data = {
|
||||
workerFk: 13,
|
||||
timed: new Date()
|
||||
};
|
||||
|
||||
let result = await app.models.WorkerTimeControl.addTimeEntry(ctx, data);
|
||||
|
||||
expect(result).toBe(``);
|
||||
});
|
||||
});
|
|
@ -1,45 +0,0 @@
|
|||
// const app = require('vn-loopback/server/server');
|
||||
|
||||
// describe('workerTimeControl filter()', () => {
|
||||
// it('should return 1 result filtering by id', async() => {
|
||||
// let ctx = {req: {accessToken: {userId: 106}}, args: {workerFk: 106}};
|
||||
// const firstHour = new Date();
|
||||
// firstHour.setHours(7, 0, 0, 0);
|
||||
// const lastHour = new Date();
|
||||
// lastHour.setDate(lastHour.getDate() + 1);
|
||||
// lastHour.setHours(15, 0, 0, 0);
|
||||
|
||||
// const filter = {
|
||||
// where: {
|
||||
// timed: {between: [firstHour, lastHour]}
|
||||
// }
|
||||
// };
|
||||
// let result = await app.models.WorkerTimeControl.filter(ctx, filter);
|
||||
|
||||
// expect(result.length).toEqual(4);
|
||||
// });
|
||||
|
||||
// it('should return a privilege error for a non subordinate worker', async() => {
|
||||
// let ctx = {req: {accessToken: {userId: 107}}, args: {workerFk: 106}};
|
||||
// const firstHour = new Date();
|
||||
// firstHour.setHours(7, 0, 0, 0);
|
||||
// const lastHour = new Date();
|
||||
// lastHour.setDate(lastHour.getDate() + 1);
|
||||
// lastHour.setHours(15, 0, 0, 0);
|
||||
|
||||
// const filter = {
|
||||
// where: {
|
||||
// timed: {between: [firstHour, lastHour]}
|
||||
// }
|
||||
// };
|
||||
|
||||
// let error;
|
||||
// await app.models.WorkerTimeControl.filter(ctx, filter).catch(e => {
|
||||
// error = e;
|
||||
// }).finally(() => {
|
||||
// expect(error.message).toEqual(`You don't have enough privileges`);
|
||||
// });
|
||||
|
||||
// expect(error).toBeDefined();
|
||||
// });
|
||||
// });
|
|
@ -0,0 +1,99 @@
|
|||
const app = require('vn-loopback/server/server');
|
||||
|
||||
describe('workerTimeControl addTimeEntry()', () => {
|
||||
let insertedTime;
|
||||
|
||||
it('should fail to add a time entry if the target user is not a subordinate', async() => {
|
||||
let error;
|
||||
let ctx = {req: {accessToken: {userId: 1}}};
|
||||
let data = {
|
||||
workerFk: 2,
|
||||
timed: new Date()
|
||||
};
|
||||
|
||||
try {
|
||||
await app.models.WorkerTimeControl.addTimeEntry(ctx, data);
|
||||
} 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() => {
|
||||
let error;
|
||||
let ctx = {req: {accessToken: {userId: 1}}};
|
||||
let data = {
|
||||
workerFk: 1,
|
||||
timed: new Date()
|
||||
};
|
||||
|
||||
try {
|
||||
await app.models.WorkerTimeControl.addTimeEntry(ctx, data);
|
||||
} catch (e) {
|
||||
error = e;
|
||||
}
|
||||
|
||||
expect(error).toBeDefined();
|
||||
expect(error.statusCode).toBe(400);
|
||||
expect(error.message).toBe(`You don't have enough privileges`);
|
||||
});
|
||||
|
||||
it('should add if the current user is team boss and the target user is a subordinate', async() => {
|
||||
todayAtSix = new Date();
|
||||
todayAtSix.setHours(18, 30, 0, 0);
|
||||
|
||||
let teamBossId = 13;
|
||||
let ctx = {req: {accessToken: {userId: teamBossId}}};
|
||||
let data = {
|
||||
workerFk: teamBossId,
|
||||
timed: todayAtSix
|
||||
};
|
||||
|
||||
await app.models.WorkerTimeControl.addTimeEntry(ctx, data);
|
||||
|
||||
insertedTime = await app.models.WorkerTimeControl.findOne({where: {timed: data.timed}});
|
||||
|
||||
let createdTimeEntry = await app.models.WorkerTimeControl.findById(insertedTime.id);
|
||||
|
||||
expect(createdTimeEntry).toBeDefined();
|
||||
});
|
||||
|
||||
it('should try but fail to delete the created time entry for the team boss as team boss', async() => {
|
||||
let error;
|
||||
let teamBossId = 13;
|
||||
let ctx = {req: {accessToken: {userId: teamBossId}}};
|
||||
|
||||
let createdTimeEntry = await app.models.WorkerTimeControl.findById(insertedTime.id);
|
||||
|
||||
expect(createdTimeEntry).toBeDefined();
|
||||
|
||||
try {
|
||||
await app.models.WorkerTimeControl.deleteTimeEntry(ctx, createdTimeEntry.id);
|
||||
} catch (e) {
|
||||
error = e;
|
||||
}
|
||||
|
||||
expect(error).toBeDefined();
|
||||
expect(error.statusCode).toBe(400);
|
||||
expect(error.message).toBe(`You don't have enough privileges`);
|
||||
});
|
||||
|
||||
it('should delete the created time entry for the team boss as HHRR', async() => {
|
||||
let HHRRId = 37;
|
||||
let ctx = {req: {accessToken: {userId: HHRRId}}};
|
||||
|
||||
let createdTimeEntry = await app.models.WorkerTimeControl.findById(insertedTime.id);
|
||||
|
||||
expect(createdTimeEntry).toBeDefined();
|
||||
|
||||
ctx.req.accessToken.userId = HHRRId;
|
||||
await app.models.WorkerTimeControl.deleteTimeEntry(ctx, insertedTime.id);
|
||||
|
||||
createdTimeEntry = await app.models.WorkerTimeControl.findById(insertedTime.id);
|
||||
|
||||
expect(createdTimeEntry).toBeNull();
|
||||
});
|
||||
});
|
Loading…
Reference in New Issue