salix/modules/worker/back/methods/worker-time-control/addTimeEntry.js

57 lines
1.7 KiB
JavaScript
Raw Normal View History

2019-05-17 11:27:51 +00:00
const UserError = require('vn-loopback/util/user-error');
module.exports = Self => {
2019-11-05 07:59:48 +00:00
Self.remoteMethodCtx('addTimeEntry', {
2019-05-17 11:27:51 +00:00
description: 'Adds a new hour registry',
accessType: 'WRITE',
accepts: [{
2021-06-17 13:09:04 +00:00
arg: 'id',
type: 'number',
description: 'The worker id',
http: {source: 'path'}
},
{
arg: 'timed',
type: 'date',
required: true
},
{
arg: 'direction',
type: 'string',
required: true
2019-05-17 11:27:51 +00:00
}],
returns: [{
type: 'Object',
root: true
}],
http: {
2021-06-17 13:09:04 +00:00
path: `/:id/addTimeEntry`,
2019-05-17 11:27:51 +00:00
verb: 'POST'
}
});
2021-06-17 13:09:04 +00:00
Self.addTimeEntry = async(ctx, workerId, options) => {
const models = Self.app.models;
const args = ctx.args;
const currentUserId = ctx.req.accessToken.userId;
const myOptions = {};
2019-05-17 11:27:51 +00:00
2021-06-17 13:09:04 +00:00
if (typeof options == 'object')
Object.assign(myOptions, options);
const isSubordinate = await models.Worker.isSubordinate(ctx, workerId, myOptions);
const isTeamBoss = await models.Account.hasRole(currentUserId, 'teamBoss', myOptions);
const isHimself = currentUserId == workerId;
2019-05-17 11:27:51 +00:00
2021-06-17 13:09:04 +00:00
if (isSubordinate === false || (isSubordinate && isHimself && !isTeamBoss))
throw new UserError(`You don't have enough privileges`);
2019-05-17 11:27:51 +00:00
query = `CALL vn.workerTimeControl_clockIn(?,?,?)`;
const [response] = await Self.rawSql(query, [workerId, args.timed, args.direction], myOptions);
if (response[0] && response[0].error)
throw new UserError(response[0].error);
return response;
2019-05-17 11:27:51 +00:00
};
};