2021-03-12 16:15:26 +00:00
|
|
|
const got = require('got');
|
2020-11-02 13:30:33 +00:00
|
|
|
const UserError = require('vn-loopback/util/user-error');
|
|
|
|
const getFinalState = require('vn-loopback/util/hook').getFinalState;
|
|
|
|
const isMultiple = require('vn-loopback/util/hook').isMultiple;
|
|
|
|
const validateTin = require('vn-loopback/util/validateTin');
|
|
|
|
const validateIban = require('vn-loopback/util/validateIban');
|
2019-11-06 07:20:45 +00:00
|
|
|
const LoopBackContext = require('loopback-context');
|
2018-01-29 11:37:54 +00:00
|
|
|
|
2018-05-04 09:46:03 +00:00
|
|
|
module.exports = Self => {
|
2018-01-29 11:37:54 +00:00
|
|
|
// Methods
|
2018-09-14 11:43:51 +00:00
|
|
|
require('../methods/client/getCard')(Self);
|
2018-01-29 11:37:54 +00:00
|
|
|
require('../methods/client/createWithUser')(Self);
|
|
|
|
require('../methods/client/hasCustomerRole')(Self);
|
2020-07-01 12:45:48 +00:00
|
|
|
require('../methods/client/canCreateTicket')(Self);
|
2018-03-07 11:24:47 +00:00
|
|
|
require('../methods/client/isValidClient')(Self);
|
2018-01-29 11:37:54 +00:00
|
|
|
require('../methods/client/addressesPropagateRe')(Self);
|
2018-03-09 15:44:18 +00:00
|
|
|
require('../methods/client/getDebt')(Self);
|
2018-03-27 13:06:22 +00:00
|
|
|
require('../methods/client/getMana')(Self);
|
|
|
|
require('../methods/client/getAverageInvoiced')(Self);
|
|
|
|
require('../methods/client/summary')(Self);
|
2018-07-13 10:37:58 +00:00
|
|
|
require('../methods/client/updateFiscalData')(Self);
|
2018-09-28 13:00:12 +00:00
|
|
|
require('../methods/client/getTransactions')(Self);
|
|
|
|
require('../methods/client/confirmTransaction')(Self);
|
2019-04-05 10:24:39 +00:00
|
|
|
require('../methods/client/canBeInvoiced')(Self);
|
2019-06-06 11:59:11 +00:00
|
|
|
require('../methods/client/uploadFile')(Self);
|
2019-08-09 06:04:44 +00:00
|
|
|
require('../methods/client/lastActiveTickets')(Self);
|
2020-01-15 12:27:14 +00:00
|
|
|
require('../methods/client/sendSms')(Self);
|
2020-01-28 08:03:20 +00:00
|
|
|
require('../methods/client/createAddress')(Self);
|
|
|
|
require('../methods/client/updateAddress')(Self);
|
2020-06-12 07:18:19 +00:00
|
|
|
require('../methods/client/consumption')(Self);
|
2021-01-22 14:46:03 +00:00
|
|
|
require('../methods/client/createReceipt')(Self);
|
2018-01-29 11:37:54 +00:00
|
|
|
|
|
|
|
// Validations
|
|
|
|
|
2019-02-28 07:55:34 +00:00
|
|
|
Self.validatesPresenceOf('street', {
|
|
|
|
message: 'Street cannot be empty'
|
|
|
|
});
|
|
|
|
|
|
|
|
Self.validatesPresenceOf('city', {
|
|
|
|
message: 'City cannot be empty'
|
|
|
|
});
|
|
|
|
|
2018-01-29 11:37:54 +00:00
|
|
|
Self.validatesUniquenessOf('fi', {
|
2018-04-05 11:08:10 +00:00
|
|
|
message: 'TIN must be unique'
|
2018-01-29 11:37:54 +00:00
|
|
|
});
|
2018-10-23 12:56:38 +00:00
|
|
|
|
2018-01-29 11:37:54 +00:00
|
|
|
Self.validatesUniquenessOf('socialName', {
|
2018-04-05 11:20:27 +00:00
|
|
|
message: 'The company name must be unique'
|
2018-01-29 11:37:54 +00:00
|
|
|
});
|
2018-10-23 12:56:38 +00:00
|
|
|
|
2018-01-29 11:37:54 +00:00
|
|
|
Self.validatesFormatOf('email', {
|
2018-04-05 11:20:27 +00:00
|
|
|
message: 'Invalid email',
|
2018-01-29 11:37:54 +00:00
|
|
|
allowNull: true,
|
|
|
|
allowBlank: true,
|
2018-03-12 11:08:34 +00:00
|
|
|
with: /^[\w|.|-]+@[\w|-]+(\.[\w|-]+)*(,[\w|.|-]+@[\w|-]+(\.[\w|-]+)*)*$/
|
2018-01-29 11:37:54 +00:00
|
|
|
});
|
2018-10-23 12:56:38 +00:00
|
|
|
|
2018-01-29 11:37:54 +00:00
|
|
|
Self.validatesLengthOf('postcode', {
|
|
|
|
allowNull: true,
|
|
|
|
allowBlank: true,
|
|
|
|
min: 3, max: 10
|
|
|
|
});
|
|
|
|
|
2018-07-27 13:13:01 +00:00
|
|
|
Self.validateAsync('iban', ibanNeedsValidation, {
|
|
|
|
message: 'The IBAN does not have the correct format'
|
2018-01-29 11:37:54 +00:00
|
|
|
});
|
2020-11-02 13:30:33 +00:00
|
|
|
|
2018-07-27 13:13:01 +00:00
|
|
|
async function ibanNeedsValidation(err, done) {
|
|
|
|
let filter = {
|
|
|
|
fields: ['code'],
|
|
|
|
where: {id: this.countryFk}
|
|
|
|
};
|
|
|
|
let country = await Self.app.models.Country.findOne(filter);
|
|
|
|
let code = country ? country.code.toLowerCase() : null;
|
2018-11-23 07:21:21 +00:00
|
|
|
if (code != 'es')
|
2018-07-27 13:13:01 +00:00
|
|
|
return done();
|
|
|
|
|
|
|
|
if (!validateIban(this.iban))
|
|
|
|
err();
|
|
|
|
done();
|
|
|
|
}
|
2018-01-29 11:37:54 +00:00
|
|
|
|
2018-03-21 11:57:23 +00:00
|
|
|
Self.validateAsync('fi', tinIsValid, {
|
2018-04-05 11:08:10 +00:00
|
|
|
message: 'Invalid TIN'
|
2018-01-29 11:37:54 +00:00
|
|
|
});
|
2019-03-13 07:35:25 +00:00
|
|
|
|
2018-03-21 11:57:23 +00:00
|
|
|
async function tinIsValid(err, done) {
|
2018-05-22 10:09:53 +00:00
|
|
|
if (!this.isTaxDataChecked)
|
|
|
|
return done();
|
|
|
|
|
2018-03-12 13:13:36 +00:00
|
|
|
let filter = {
|
|
|
|
fields: ['code'],
|
|
|
|
where: {id: this.countryFk}
|
|
|
|
};
|
|
|
|
let country = await Self.app.models.Country.findOne(filter);
|
|
|
|
let code = country ? country.code.toLowerCase() : null;
|
|
|
|
|
2019-03-13 07:35:25 +00:00
|
|
|
if (!this.fi || !validateTin(this.fi, code))
|
2018-03-12 13:13:36 +00:00
|
|
|
err();
|
|
|
|
done();
|
|
|
|
}
|
2018-01-29 11:37:54 +00:00
|
|
|
|
|
|
|
Self.validate('payMethod', hasSalesMan, {
|
2018-04-05 11:20:27 +00:00
|
|
|
message: 'Cannot change the payment method if no salesperson'
|
2018-01-29 11:37:54 +00:00
|
|
|
});
|
2018-10-16 11:06:58 +00:00
|
|
|
|
2018-01-29 11:37:54 +00:00
|
|
|
function hasSalesMan(err) {
|
2020-09-15 17:43:07 +00:00
|
|
|
if (this.payMethod && !this.salesPersonUser)
|
2018-01-29 11:37:54 +00:00
|
|
|
err();
|
|
|
|
}
|
|
|
|
|
2018-11-20 10:02:57 +00:00
|
|
|
Self.validate('isEqualizated', cannotHaveET, {
|
|
|
|
message: 'Cannot check Equalization Tax in this NIF/CIF'
|
|
|
|
});
|
|
|
|
|
|
|
|
function cannotHaveET(err) {
|
2019-09-19 06:58:42 +00:00
|
|
|
if (!this.fi) return;
|
|
|
|
|
2018-11-20 10:02:57 +00:00
|
|
|
let tin = this.fi.toUpperCase();
|
|
|
|
let cannotHaveET = /^[A-B]/.test(tin);
|
|
|
|
|
|
|
|
if (cannotHaveET && this.isEqualizated)
|
|
|
|
err();
|
|
|
|
}
|
|
|
|
|
2018-11-07 08:17:07 +00:00
|
|
|
Self.validateAsync('payMethodFk', hasIban, {
|
|
|
|
message: 'That payment method requires an IBAN'
|
|
|
|
});
|
|
|
|
|
|
|
|
function hasIban(err, done) {
|
|
|
|
Self.app.models.PayMethod.findById(this.payMethodFk, (_, instance) => {
|
2021-11-22 12:44:22 +00:00
|
|
|
const isMissingIban = instance && instance.isIbanRequiredForClients && !this.iban;
|
|
|
|
if (isMissingIban)
|
2018-11-07 08:17:07 +00:00
|
|
|
err();
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
Self.validateAsync('bankEntityFk', hasBic, {
|
|
|
|
message: 'That payment method requires a BIC'
|
|
|
|
});
|
|
|
|
|
|
|
|
function hasBic(err, done) {
|
2018-11-22 14:17:35 +00:00
|
|
|
if (this.iban && !this.bankEntityFk)
|
|
|
|
err();
|
|
|
|
done();
|
2018-11-07 08:17:07 +00:00
|
|
|
}
|
|
|
|
|
2019-02-28 07:55:34 +00:00
|
|
|
Self.validateAsync('defaultAddressFk', isActive,
|
|
|
|
{message: 'Unable to default a disabled consignee'}
|
|
|
|
);
|
|
|
|
|
|
|
|
async function isActive(err, done) {
|
|
|
|
if (!this.defaultAddressFk)
|
|
|
|
return done();
|
|
|
|
|
|
|
|
const address = await Self.app.models.Address.findById(this.defaultAddressFk);
|
|
|
|
if (address && !address.isActive) err();
|
|
|
|
done();
|
|
|
|
}
|
|
|
|
|
2019-09-18 08:35:46 +00:00
|
|
|
Self.validateAsync('postCode', hasValidPostcode, {
|
2020-01-29 13:13:25 +00:00
|
|
|
message: `The postcode doesn't exist. Please enter a correct one`
|
2019-09-18 08:35:46 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
async function hasValidPostcode(err, done) {
|
|
|
|
if (!this.postcode)
|
|
|
|
return done();
|
|
|
|
|
|
|
|
const models = Self.app.models;
|
|
|
|
const postcode = await models.Postcode.findById(this.postcode);
|
|
|
|
|
|
|
|
if (!postcode) err();
|
|
|
|
done();
|
|
|
|
}
|
|
|
|
|
2019-09-23 07:27:58 +00:00
|
|
|
function isAlpha(value) {
|
2019-10-01 13:12:02 +00:00
|
|
|
const regexp = new RegExp(/^[ñça-zA-Z0-9\s]*$/i);
|
2019-09-23 07:27:58 +00:00
|
|
|
|
|
|
|
return regexp.test(value);
|
|
|
|
}
|
|
|
|
|
2022-01-05 13:29:03 +00:00
|
|
|
Self.observe('before save', async ctx => {
|
|
|
|
const changes = ctx.data || ctx.instance;
|
|
|
|
const orgData = ctx.currentInstance;
|
|
|
|
|
2022-01-05 14:41:26 +00:00
|
|
|
const businessTypeFk = changes && changes.businessTypeFk || orgData && orgData.businessTypeFk;
|
|
|
|
const isTaxDataChecked = changes && changes.isTaxDataChecked || orgData && orgData.isTaxDataChecked;
|
|
|
|
|
|
|
|
let invalidBusinessType = false;
|
2022-01-05 14:17:21 +00:00
|
|
|
if (!ctx.isNewInstance) {
|
2022-01-05 14:41:26 +00:00
|
|
|
const isWorker = await Self.app.models.UserAccount.findById(orgData.id);
|
2022-01-05 14:17:21 +00:00
|
|
|
const changedFields = Object.keys(changes);
|
|
|
|
const hasChangedOtherFields = changedFields.some(key => key !== 'businessTypeFk');
|
2022-01-05 13:29:03 +00:00
|
|
|
|
2022-01-05 14:17:21 +00:00
|
|
|
if (!businessTypeFk && !isTaxDataChecked && !isWorker && !hasChangedOtherFields)
|
2022-01-05 14:41:26 +00:00
|
|
|
invalidBusinessType = true;
|
2022-01-05 14:17:21 +00:00
|
|
|
}
|
2022-01-05 14:41:26 +00:00
|
|
|
|
|
|
|
if (ctx.isNewInstance) {
|
|
|
|
if (!businessTypeFk && !isTaxDataChecked)
|
|
|
|
invalidBusinessType = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (invalidBusinessType)
|
|
|
|
throw new UserError(`The type of business must be filled in basic data`);
|
2022-01-05 13:29:03 +00:00
|
|
|
});
|
|
|
|
|
2018-01-29 11:37:54 +00:00
|
|
|
Self.observe('before save', async function(ctx) {
|
2021-09-07 14:07:46 +00:00
|
|
|
const changes = ctx.data || ctx.instance;
|
|
|
|
const orgData = ctx.currentInstance;
|
|
|
|
const finalState = getFinalState(ctx);
|
|
|
|
const payMethodWithIban = 4;
|
2018-01-29 11:37:54 +00:00
|
|
|
|
2019-09-23 07:27:58 +00:00
|
|
|
// Validate socialName format
|
2020-09-30 07:31:01 +00:00
|
|
|
const hasChanges = orgData && changes;
|
2020-10-28 13:12:57 +00:00
|
|
|
const socialName = changes && changes.socialName || orgData && orgData.socialName;
|
2020-09-30 09:19:52 +00:00
|
|
|
const isTaxDataChecked = hasChanges && (changes.isTaxDataChecked || orgData.isTaxDataChecked);
|
2019-09-23 07:27:58 +00:00
|
|
|
|
2020-09-30 07:31:01 +00:00
|
|
|
const socialNameChanged = hasChanges
|
|
|
|
&& orgData.socialName != socialName;
|
|
|
|
const dataCheckedChanged = hasChanges
|
|
|
|
&& orgData.isTaxDataChecked != isTaxDataChecked;
|
|
|
|
|
|
|
|
if ((socialNameChanged || dataCheckedChanged) && !isAlpha(socialName))
|
2022-01-05 15:23:58 +00:00
|
|
|
throw new UserError(`The social name has an invalid format`);
|
2019-09-23 07:27:58 +00:00
|
|
|
|
2018-01-29 11:37:54 +00:00
|
|
|
if (changes.salesPerson === null) {
|
|
|
|
changes.credit = 0;
|
|
|
|
changes.discount = 0;
|
|
|
|
changes.payMethodFk = 5; // Credit card
|
|
|
|
}
|
|
|
|
|
2021-09-07 14:07:46 +00:00
|
|
|
const payMethodFk = changes.payMethodFk || (orgData && orgData.payMethodFk);
|
|
|
|
const dueDay = changes.dueDay || (orgData && orgData.dueDay);
|
2018-07-10 12:13:47 +00:00
|
|
|
|
|
|
|
if (payMethodFk == payMethodWithIban && dueDay == 0)
|
2018-01-29 11:37:54 +00:00
|
|
|
changes.dueDay = 5;
|
|
|
|
|
|
|
|
if (isMultiple(ctx)) return;
|
|
|
|
|
2021-09-07 14:07:46 +00:00
|
|
|
if (!ctx.isNewInstance) {
|
|
|
|
const isTaxDataCheckedChanged = !orgData.isTaxDataChecked && changes.isTaxDataChecked;
|
|
|
|
if (isTaxDataCheckedChanged && !orgData.businessTypeFk)
|
|
|
|
throw new UserError(`Can't verify data unless the client has a business type`);
|
|
|
|
}
|
2021-09-09 12:43:18 +00:00
|
|
|
// Credit changes
|
|
|
|
if (changes.credit !== undefined)
|
|
|
|
await Self.changeCredit(ctx, finalState, changes);
|
2020-06-10 10:09:27 +00:00
|
|
|
});
|
2018-01-29 11:37:54 +00:00
|
|
|
|
2019-02-28 13:30:29 +00:00
|
|
|
Self.observe('after save', async ctx => {
|
|
|
|
if (ctx.isNewInstance) return;
|
|
|
|
|
|
|
|
const hookState = ctx.hookState;
|
|
|
|
const newInstance = hookState.newInstance;
|
|
|
|
const oldInstance = hookState.oldInstance;
|
|
|
|
const instance = ctx.instance;
|
2020-01-23 12:31:07 +00:00
|
|
|
const models = Self.app.models;
|
2019-02-28 13:30:29 +00:00
|
|
|
|
|
|
|
const payMethodChanged = oldInstance.payMethodFk != newInstance.payMethodFk;
|
|
|
|
const ibanChanged = oldInstance.iban != newInstance.iban;
|
|
|
|
const dueDayChanged = oldInstance.dueDay != newInstance.dueDay;
|
|
|
|
|
|
|
|
if (payMethodChanged || ibanChanged || dueDayChanged) {
|
2020-01-23 12:31:07 +00:00
|
|
|
const loopBackContext = LoopBackContext.getCurrentContext();
|
|
|
|
const httpCtx = {req: loopBackContext.active};
|
|
|
|
const httpRequest = httpCtx.req.http.req;
|
|
|
|
const $t = httpRequest.__;
|
2020-03-05 12:56:45 +00:00
|
|
|
const headers = httpRequest.headers;
|
|
|
|
const origin = headers.origin;
|
|
|
|
const authorization = headers.authorization;
|
2020-01-23 12:31:07 +00:00
|
|
|
|
|
|
|
const salesPersonId = instance.salesPersonFk;
|
|
|
|
|
|
|
|
if (salesPersonId) {
|
2021-04-07 11:09:53 +00:00
|
|
|
// Send email to client
|
|
|
|
if (instance.email) {
|
|
|
|
const worker = await models.EmailUser.findById(salesPersonId);
|
|
|
|
const params = {
|
|
|
|
authorization: authorization,
|
|
|
|
recipientId: instance.id,
|
|
|
|
recipient: instance.email,
|
|
|
|
replyTo: worker.email
|
|
|
|
};
|
|
|
|
await got.get(`${origin}/api/email/payment-update`, {
|
2022-01-11 12:52:01 +00:00
|
|
|
searchParams: params
|
2021-04-07 11:09:53 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-01-23 12:31:07 +00:00
|
|
|
const fullUrl = `${origin}/#!/client/${instance.id}/billing-data`;
|
2021-04-07 11:09:53 +00:00
|
|
|
const message = $t('Changed client paymethod', {
|
2020-01-23 12:31:07 +00:00
|
|
|
clientId: instance.id,
|
|
|
|
clientName: instance.name,
|
|
|
|
url: fullUrl
|
2019-02-28 13:30:29 +00:00
|
|
|
});
|
2020-01-23 12:31:07 +00:00
|
|
|
await models.Chat.sendCheckingPresence(httpCtx, salesPersonId, message);
|
2019-02-28 13:30:29 +00:00
|
|
|
}
|
|
|
|
}
|
2021-03-23 15:19:45 +00:00
|
|
|
|
|
|
|
const workerIdBefore = oldInstance.salesPersonFk;
|
|
|
|
const workerIdAfter = newInstance.salesPersonFk;
|
|
|
|
const assignmentChanged = workerIdBefore != workerIdAfter;
|
|
|
|
if (assignmentChanged)
|
2021-03-24 11:45:26 +00:00
|
|
|
await Self.notifyAssignment(instance, workerIdBefore, workerIdAfter);
|
2019-02-28 13:30:29 +00:00
|
|
|
});
|
|
|
|
|
2021-03-23 15:19:45 +00:00
|
|
|
// Send notification on client worker assignment
|
2021-03-24 11:45:26 +00:00
|
|
|
Self.notifyAssignment = async function notifyAssignment(client, previousWorkerId, currentWorkerId) {
|
2021-03-23 15:19:45 +00:00
|
|
|
const loopBackContext = LoopBackContext.getCurrentContext();
|
|
|
|
const httpCtx = {req: loopBackContext.active};
|
|
|
|
const httpRequest = httpCtx.req.http.req;
|
|
|
|
const $t = httpRequest.__;
|
|
|
|
const headers = httpRequest.headers;
|
|
|
|
const origin = headers.origin;
|
|
|
|
const models = Self.app.models;
|
|
|
|
|
2021-03-24 11:45:26 +00:00
|
|
|
let previousWorker = {name: $t('None')};
|
|
|
|
let currentWorker = {name: $t('None')};
|
2021-03-23 15:19:45 +00:00
|
|
|
if (previousWorkerId) {
|
|
|
|
const worker = await models.Worker.findById(previousWorkerId, {
|
|
|
|
include: {relation: 'user'}
|
|
|
|
});
|
2021-03-24 11:45:26 +00:00
|
|
|
previousWorker.user = worker && worker.user().name;
|
|
|
|
previousWorker.name = worker && worker.user().nickname;
|
2021-03-23 15:19:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (currentWorkerId) {
|
|
|
|
const worker = await models.Worker.findById(currentWorkerId, {
|
|
|
|
include: {relation: 'user'}
|
|
|
|
});
|
2021-03-24 11:45:26 +00:00
|
|
|
currentWorker.user = worker && worker.user().name;
|
|
|
|
currentWorker.name = worker && worker.user().nickname;
|
2021-03-23 15:19:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const fullUrl = `${origin}/#!/client/${client.id}/basic-data`;
|
|
|
|
const message = $t('Client assignment has changed', {
|
|
|
|
clientId: client.id,
|
|
|
|
clientName: client.name,
|
|
|
|
url: fullUrl,
|
2021-03-24 11:45:26 +00:00
|
|
|
previousWorkerName: previousWorker.name,
|
|
|
|
currentWorkerName: currentWorker.name
|
2021-03-23 15:19:45 +00:00
|
|
|
});
|
|
|
|
|
2021-03-24 11:45:26 +00:00
|
|
|
if (previousWorkerId)
|
|
|
|
await models.Chat.send(httpCtx, `@${previousWorker.user}`, message);
|
2021-03-23 15:19:45 +00:00
|
|
|
|
2021-03-24 11:45:26 +00:00
|
|
|
if (currentWorkerId)
|
|
|
|
await models.Chat.send(httpCtx, `@${currentWorker.user}`, message);
|
|
|
|
};
|
2021-03-23 15:19:45 +00:00
|
|
|
|
2021-09-09 12:43:18 +00:00
|
|
|
// Credit change validations
|
|
|
|
Self.changeCredit = async function changeCredit(ctx, finalState, changes) {
|
|
|
|
const models = Self.app.models;
|
|
|
|
const userId = ctx.options.accessToken.userId;
|
|
|
|
|
|
|
|
const isManager = await models.Account.hasRole(userId, 'manager', ctx.options);
|
|
|
|
if (!isManager) {
|
|
|
|
const lastCredit = await models.ClientCredit.findOne({
|
|
|
|
where: {
|
|
|
|
clientFk: finalState.id
|
|
|
|
},
|
|
|
|
order: 'id DESC'
|
|
|
|
}, ctx.options);
|
|
|
|
|
2021-09-09 14:44:15 +00:00
|
|
|
const lastAmount = lastCredit && lastCredit.amount;
|
|
|
|
const lastWorkerId = lastCredit && lastCredit.workerFk;
|
2021-09-09 12:43:18 +00:00
|
|
|
const lastWorkerIsManager = await models.Account.hasRole(lastWorkerId, 'manager', ctx.options);
|
|
|
|
|
|
|
|
if (lastAmount == 0 && lastWorkerIsManager)
|
|
|
|
throw new UserError(`You can't change the credit set to zero from a manager`);
|
|
|
|
|
|
|
|
const creditLimits = await models.ClientCreditLimit.find({
|
|
|
|
fields: ['roleFk'],
|
|
|
|
where: {
|
|
|
|
maxAmount: {gte: changes.credit}
|
|
|
|
}
|
|
|
|
}, ctx.options);
|
2018-01-29 11:37:54 +00:00
|
|
|
|
2021-09-09 12:43:18 +00:00
|
|
|
const requiredRoles = [];
|
|
|
|
for (limit of creditLimits)
|
|
|
|
requiredRoles.push(limit.roleFk);
|
2018-01-29 11:37:54 +00:00
|
|
|
|
2021-09-09 12:43:18 +00:00
|
|
|
const userRequiredRoles = await models.RoleMapping.count({
|
|
|
|
roleId: {inq: requiredRoles},
|
|
|
|
principalType: 'USER',
|
|
|
|
principalId: userId
|
|
|
|
}, ctx.options);
|
2018-01-29 11:37:54 +00:00
|
|
|
|
2021-09-09 12:43:18 +00:00
|
|
|
if (userRequiredRoles <= 0)
|
|
|
|
throw new UserError(`You don't have enough privileges to set this credit amount`);
|
|
|
|
}
|
2018-01-29 11:37:54 +00:00
|
|
|
|
2022-01-05 13:29:03 +00:00
|
|
|
await models.ClientCredit.create({
|
|
|
|
amount: changes.credit,
|
|
|
|
clientFk: finalState.id,
|
|
|
|
workerFk: userId
|
|
|
|
}, ctx.options);
|
2021-09-09 12:43:18 +00:00
|
|
|
};
|
2021-03-23 15:19:45 +00:00
|
|
|
|
|
|
|
const app = require('vn-loopback/server/server');
|
|
|
|
app.on('started', function() {
|
|
|
|
let account = app.models.Account;
|
|
|
|
|
|
|
|
account.observe('before save', async ctx => {
|
|
|
|
if (ctx.isNewInstance) return;
|
|
|
|
ctx.hookState.oldInstance = JSON.parse(JSON.stringify(ctx.currentInstance));
|
|
|
|
});
|
|
|
|
|
|
|
|
account.observe('after save', async ctx => {
|
|
|
|
let changes = ctx.data || ctx.instance;
|
|
|
|
if (!ctx.isNewInstance && changes) {
|
|
|
|
let oldData = ctx.hookState.oldInstance;
|
|
|
|
let hasChanges = oldData.name != changes.name || oldData.active != changes.active;
|
|
|
|
if (!hasChanges) return;
|
|
|
|
|
|
|
|
let userId = ctx.options.accessToken.userId;
|
|
|
|
let logRecord = {
|
|
|
|
originFk: oldData.id,
|
|
|
|
userFk: userId,
|
|
|
|
action: 'update',
|
|
|
|
changedModel: 'Account',
|
|
|
|
oldInstance: {name: oldData.name, active: oldData.active},
|
|
|
|
newInstance: {name: changes.name, active: changes.active}
|
|
|
|
};
|
|
|
|
await Self.app.models.ClientLog.create(logRecord);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2018-01-29 11:37:54 +00:00
|
|
|
};
|