salix/modules/worker/back/models/worker.js

74 lines
2.8 KiB
JavaScript

module.exports = Self => {
const validateTin = require('vn-loopback/util/validateTin');
require('../methods/worker/filter')(Self);
require('../methods/worker/mySubordinates')(Self);
require('../methods/worker/isSubordinate')(Self);
require('../methods/worker/getWorkedHours')(Self);
require('../methods/worker/uploadFile')(Self);
require('../methods/worker/createAbsence')(Self);
require('../methods/worker/deleteAbsence')(Self);
require('../methods/worker/updateAbsence')(Self);
require('../methods/worker/active')(Self);
require('../methods/worker/activeWithRole')(Self);
require('../methods/worker/activeWithInheritedRole')(Self);
require('../methods/worker/contracts')(Self);
require('../methods/worker/holidays')(Self);
require('../methods/worker/activeContract')(Self);
require('../methods/worker/new')(Self);
require('../methods/worker/deallocatePDA')(Self);
require('../methods/worker/allocatePDA')(Self);
require('../methods/worker/search')(Self);
require('../methods/worker/isAuthorized')(Self);
require('../methods/worker/setPassword')(Self);
Self.validatesUniquenessOf('locker', {
message: 'This locker has already been assigned'
});
// Self.validateAsync('fi', tinIsValid, {
// message: 'Invalid TIN'
// });
async function tinIsValid(err, done) {
const filter = {
fields: ['code'],
where: {id: this.countryFk}
};
const country = await Self.app.models.Country.findOne(filter);
const code = country ? country.code.toLowerCase() : null;
if (!this.fi || !validateTin(this.fi, code))
err();
done();
}
Self.observe('before save', async function(ctx) {
// if (ctx.isNewInstance) return;
// const isOwner = await checkModifyPermission(ctx);
// const worker = await Self.findById(ctx.currentInstance.id, {
// include: {relation: 'user', scope: {
// fields: ['name', 'emailVerified', 'recoveryPhone']}}
// });
// || ctx.data.user.recoveryPhone !== worker.user().recoveryPhone;
// const phoneHasChanged = !!ctx.data.user?.recoveryPhone;
// if (!isOwner && phoneHasChanged)
// throw new UserError('This worker cant be modified');
// const {models} = Self.app;
// if (phoneHasChanged)
// await models.VnUser.updateAll({id: ctx.currentInstance.id}, {recoveryPhone: ctx.data.user.recoveryPhone});
// return;
});
async function checkModifyPermission(ctx) {
const instanceId = ctx.currentInstance.id;
const userId = ctx.options.accessToken.userId;
try {
return (instanceId == userId);
} catch (error) {
throw new UserError(error);
}
}
};