salix/modules/client/back/methods/client/updateFiscalData.js

131 lines
3.1 KiB
JavaScript

let UserError = require('vn-loopback/util/user-error');
module.exports = Self => {
Self.remoteMethod('updateFiscalData', {
description: 'Updates fiscal data of a client',
accessType: 'WRITE',
accepts: [{
arg: 'ctx',
type: 'Object',
http: {source: 'context'}
},
{
arg: 'id',
type: 'Number',
description: 'The client id',
http: {source: 'path'}
},
{
arg: 'socialName',
type: 'string'
},
{
arg: 'fi',
type: 'string'
},
{
arg: 'street',
type: 'string'
},
{
arg: 'postcode',
type: 'string'
},
{
arg: 'city',
type: 'string'
},
{
arg: 'countryFk',
type: 'number'
},
{
arg: 'provinceFk',
type: 'number'
},
{
arg: 'hasToInvoiceByAddress',
type: 'boolean'
},
{
arg: 'hasToInvoice',
type: 'boolean'
},
{
arg: 'isActive',
type: 'boolean'
},
{
arg: 'isFreezed',
type: 'boolean'
},
{
arg: 'isVies',
type: 'boolean'
},
{
arg: 'isToBeMailed',
type: 'boolean'
},
{
arg: 'isEqualizated',
type: 'boolean'
},
{
arg: 'isTaxDataVerified',
type: 'boolean'
},
{
arg: 'isTaxDataChecked',
type: 'boolean'
},
{
arg: 'despiteOfClient',
type: 'number'
}],
returns: {
arg: 'res',
type: 'string',
root: true
},
http: {
path: `/:id/updateFiscalData`,
verb: 'PATCH'
}
});
Self.updateFiscalData = async(ctx, clientId) => {
const models = Self.app.models;
const args = ctx.args;
const userId = ctx.req.accessToken.userId;
const isSalesAssistant = await models.Account.hasRole(userId, 'salesAssistant');
const $t = ctx.req.__;
const client = await models.Client.findById(clientId);
if (!isSalesAssistant && client.isTaxDataChecked)
throw new UserError(`You can't make changes on a client with verified data`);
if (args.despiteOfClient) {
const logRecord = {
originFk: clientId,
userFk: userId,
action: 'update',
changedModel: 'Client',
changedModelId: clientId,
description: $t(`Client checked as validated despite of duplication`, {
clientId: args.despiteOfClient
})
};
await models.ClientLog.create(logRecord);
}
// Remove unwanted properties
delete args.ctx;
delete args.id;
return client.updateAttributes(args);
};
};