salix/modules/worker/back/methods/worker/updateAbsence.js

43 lines
1.3 KiB
JavaScript

const UserError = require('vn-loopback/util/user-error');
module.exports = Self => {
Self.remoteMethodCtx('updateAbsence', {
description: 'Updates a worker absence',
accepts: [{
arg: 'id',
type: 'Number',
description: 'The worker id',
http: {source: 'path'}
},
{
arg: 'absenceId',
type: 'Number',
required: true
},
{
arg: 'absenceTypeId',
type: 'Number',
required: true
}],
returns: 'Object',
http: {
path: `/:id/updateAbsence`,
verb: 'PATCH'
}
});
Self.updateAbsence = async(ctx, id, absenceId, absenceTypeId) => {
const models = Self.app.models;
const userId = ctx.req.accessToken.userId;
const isSubordinate = await models.Worker.isSubordinate(ctx, id);
const isTeamBoss = await models.Account.hasRole(userId, 'teamBoss');
if (!isSubordinate || (isSubordinate && userId == id && !isTeamBoss))
throw new UserError(`You don't have enough privileges`);
const absence = await models.Calendar.findById(absenceId);
return absence.updateAttribute('dayOffTypeFk', absenceTypeId);
};
};