2024-04-10 13:06:03 +00:00
|
|
|
const UserError = require('vn-loopback/util/user-error');
|
2019-03-11 07:00:49 +00:00
|
|
|
module.exports = Self => {
|
2023-12-13 12:55:31 +00:00
|
|
|
const validateTin = require('vn-loopback/util/validateTin');
|
2019-03-11 07:00:49 +00:00
|
|
|
require('../methods/worker/filter')(Self);
|
2019-05-17 11:27:51 +00:00
|
|
|
require('../methods/worker/mySubordinates')(Self);
|
|
|
|
require('../methods/worker/isSubordinate')(Self);
|
2019-11-05 07:59:48 +00:00
|
|
|
require('../methods/worker/getWorkedHours')(Self);
|
2019-11-22 09:42:05 +00:00
|
|
|
require('../methods/worker/uploadFile')(Self);
|
2020-07-08 13:05:56 +00:00
|
|
|
require('../methods/worker/createAbsence')(Self);
|
|
|
|
require('../methods/worker/deleteAbsence')(Self);
|
|
|
|
require('../methods/worker/updateAbsence')(Self);
|
2021-04-13 12:08:10 +00:00
|
|
|
require('../methods/worker/active')(Self);
|
|
|
|
require('../methods/worker/activeWithRole')(Self);
|
|
|
|
require('../methods/worker/activeWithInheritedRole')(Self);
|
2021-06-03 09:00:40 +00:00
|
|
|
require('../methods/worker/contracts')(Self);
|
2021-06-04 13:49:48 +00:00
|
|
|
require('../methods/worker/holidays')(Self);
|
2021-06-03 09:00:40 +00:00
|
|
|
require('../methods/worker/activeContract')(Self);
|
2022-11-02 12:24:35 +00:00
|
|
|
require('../methods/worker/new')(Self);
|
2023-02-28 06:49:34 +00:00
|
|
|
require('../methods/worker/deallocatePDA')(Self);
|
|
|
|
require('../methods/worker/allocatePDA')(Self);
|
2023-05-31 08:15:16 +00:00
|
|
|
require('../methods/worker/search')(Self);
|
2023-04-26 07:48:02 +00:00
|
|
|
require('../methods/worker/isAuthorized')(Self);
|
2023-10-16 13:58:25 +00:00
|
|
|
require('../methods/worker/setPassword')(Self);
|
2023-02-27 07:39:46 +00:00
|
|
|
|
|
|
|
Self.validatesUniquenessOf('locker', {
|
|
|
|
message: 'This locker has already been assigned'
|
|
|
|
});
|
2023-12-13 12:55:31 +00:00
|
|
|
|
2024-04-10 12:32:26 +00:00
|
|
|
// Self.validateAsync('fi', tinIsValid, {
|
|
|
|
// message: 'Invalid TIN'
|
|
|
|
// });
|
2023-12-13 12:55:31 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
2024-01-03 12:44:20 +00:00
|
|
|
if (!this.fi || !validateTin(this.fi, code))
|
2023-12-26 14:03:10 +00:00
|
|
|
err();
|
2023-12-13 12:55:31 +00:00
|
|
|
done();
|
|
|
|
}
|
2024-04-10 12:32:26 +00:00
|
|
|
|
2024-04-10 13:06:03 +00:00
|
|
|
Self.observe('before save', async function(ctx, next) {
|
|
|
|
if (ctx.isNewInstance) return;
|
|
|
|
const isOwner = await checkModifyPermission(ctx);
|
|
|
|
const phoneHasChanged = !!ctx.data.user?.recoveryPhone;
|
2024-04-10 12:32:26 +00:00
|
|
|
|
2024-04-10 13:06:03 +00:00
|
|
|
const {models} = Self.app;
|
|
|
|
if (!isOwner) {
|
|
|
|
if (phoneHasChanged)
|
|
|
|
throw new UserError('Phone can\'t be updated');
|
|
|
|
else {
|
|
|
|
const {recoveryPhone} = ctx.data.user;
|
|
|
|
const {id} = ctx.currentInstance;
|
|
|
|
await models.VnUser.updateAll({id}, {recoveryPhone});
|
|
|
|
ctx.data.code = ctx.currentInstance.code;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
delete ctx.data.user;
|
2024-04-10 12:32:26 +00:00
|
|
|
});
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2019-03-11 07:00:49 +00:00
|
|
|
};
|