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

75 lines
2.4 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
2021-11-30 11:31:02 +00:00
const minTime = new Date(args.timed);
minTime.setHours(0, 0, 0, 0);
query = `SELECT * FROM vn.workerLabour WHERE workerFk = ? AND (ended >= ? OR ended IS NULL);`;
2021-11-30 11:31:02 +00:00
const [workerLabour] = await Self.rawSql(query, [workerId, minTime]);
const absence = await models.Calendar.findOne({
where: {
businessFk: workerLabour.businessFk,
2021-11-30 11:31:02 +00:00
dated: minTime
}
});
2021-11-30 11:31:02 +00:00
if (absence) {
const absenceType = await models.AbsenceType.findById(absence.dayOffTypeFk, null, myOptions);
const isNotHalfAbsence = absenceType.id != 6 && absenceType.id != 15 && absenceType.id != 21;
2021-11-30 11:31:02 +00:00
if (isNotHalfAbsence)
throw new UserError(`The worker has a marked absence that day`);
}
2021-06-17 13:09:04 +00:00
return models.WorkerTimeControl.create({
userFk: workerId,
direction: args.direction,
timed: args.timed,
2021-06-17 13:09:04 +00:00
manual: true
}, myOptions);
2019-05-17 11:27:51 +00:00
};
};