2019-11-14 13:20:05 +00:00
|
|
|
const UserError = require('vn-loopback/util/user-error');
|
|
|
|
|
2019-11-05 07:59:48 +00:00
|
|
|
module.exports = Self => {
|
|
|
|
Self.remoteMethodCtx('deleteTimeEntry', {
|
|
|
|
description: 'Deletes a manual time entry for a worker if the user role is above the worker',
|
|
|
|
accessType: 'READ',
|
|
|
|
accepts: [{
|
|
|
|
arg: 'id',
|
|
|
|
type: 'number',
|
|
|
|
required: true,
|
|
|
|
description: 'The time entry id',
|
|
|
|
http: {source: 'path'}
|
|
|
|
}],
|
|
|
|
returns: {
|
|
|
|
type: 'boolean',
|
|
|
|
root: true
|
|
|
|
},
|
|
|
|
http: {
|
|
|
|
path: `/:id/deleteTimeEntry`,
|
|
|
|
verb: 'POST'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
Self.deleteTimeEntry = async(ctx, id) => {
|
2019-11-14 13:20:05 +00:00
|
|
|
const currentUserId = ctx.req.accessToken.userId;
|
2019-11-05 07:59:48 +00:00
|
|
|
const workerModel = Self.app.models.Worker;
|
|
|
|
|
|
|
|
const targetTimeEntry = await Self.findById(id);
|
2019-11-14 13:20:05 +00:00
|
|
|
const isSubordinate = await workerModel.isSubordinate(ctx, targetTimeEntry.userFk);
|
2021-02-02 09:07:13 +00:00
|
|
|
const isTeamBoss = await Self.app.models.Account.hasRole(currentUserId, 'teamBoss');
|
|
|
|
const isHimself = currentUserId == targetTimeEntry.userFk;
|
2019-11-14 13:20:05 +00:00
|
|
|
|
2021-02-02 09:07:13 +00:00
|
|
|
const notAllowed = isSubordinate === false || (isSubordinate && isHimself && !isTeamBoss);
|
2019-11-05 07:59:48 +00:00
|
|
|
|
2019-11-14 13:20:05 +00:00
|
|
|
if (notAllowed)
|
2019-11-05 07:59:48 +00:00
|
|
|
throw new UserError(`You don't have enough privileges`);
|
|
|
|
|
|
|
|
return Self.rawSql('CALL vn.workerTimeControl_remove(?, ?)', [
|
|
|
|
targetTimeEntry.userFk, targetTimeEntry.timed]);
|
|
|
|
};
|
|
|
|
};
|