76 lines
2.5 KiB
JavaScript
76 lines
2.5 KiB
JavaScript
const UserError = require('vn-loopback/util/user-error');
|
|
|
|
module.exports = Self => {
|
|
Self.remoteMethodCtx('addTimeEntry', {
|
|
description: 'Adds a new hour registry',
|
|
accessType: 'WRITE',
|
|
accepts: [{
|
|
arg: 'id',
|
|
type: 'number',
|
|
description: 'The worker id',
|
|
http: {source: 'path'}
|
|
},
|
|
{
|
|
arg: 'timed',
|
|
type: 'date',
|
|
required: true
|
|
},
|
|
{
|
|
arg: 'direction',
|
|
type: 'string',
|
|
required: true
|
|
}],
|
|
returns: [{
|
|
type: 'Object',
|
|
root: true
|
|
}],
|
|
http: {
|
|
path: `/:id/addTimeEntry`,
|
|
verb: 'POST'
|
|
}
|
|
});
|
|
|
|
Self.addTimeEntry = async(ctx, workerId, options) => {
|
|
const models = Self.app.models;
|
|
const args = ctx.args;
|
|
const currentUserId = ctx.req.accessToken.userId;
|
|
const myOptions = {};
|
|
|
|
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;
|
|
|
|
if (isSubordinate === false || (isSubordinate && isHimself && !isTeamBoss))
|
|
throw new UserError(`You don't have enough privileges`);
|
|
|
|
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);`;
|
|
const [workerLabour] = await Self.rawSql(query, [workerId, minTime]);
|
|
const absence = await models.Calendar.findOne({
|
|
where: {
|
|
businessFk: workerLabour.businessFk,
|
|
dated: minTime
|
|
}
|
|
});
|
|
if (absence) {
|
|
const absenceType = await models.AbsenceType.findById(absence.dayOffTypeFk, null, myOptions);
|
|
const isNotHalfAbsence = absenceType.code != 'halfHoliday'
|
|
&& absenceType.code != 'halfPaidLeave'
|
|
&& absenceType.code != 'halfFurlough';
|
|
if (isNotHalfAbsence)
|
|
throw new UserError(`The worker has a marked absence that day`);
|
|
}
|
|
return models.WorkerTimeControl.create({
|
|
userFk: workerId,
|
|
direction: args.direction,
|
|
timed: args.timed,
|
|
manual: true
|
|
}, myOptions);
|
|
};
|
|
};
|