161 lines
4.5 KiB
JavaScript
161 lines
4.5 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: 'any'
|
|
},
|
|
{
|
|
arg: 'postcode',
|
|
type: 'any'
|
|
},
|
|
{
|
|
arg: 'city',
|
|
type: 'any'
|
|
},
|
|
{
|
|
arg: 'countryFk',
|
|
type: 'any'
|
|
},
|
|
{
|
|
arg: 'provinceFk',
|
|
type: 'any'
|
|
},
|
|
{
|
|
arg: 'sageTaxTypeFk',
|
|
type: 'any'
|
|
},
|
|
{
|
|
arg: 'sageTransactionTypeFk',
|
|
type: 'any'
|
|
},
|
|
{
|
|
arg: 'transferorFk',
|
|
type: 'any'
|
|
},
|
|
{
|
|
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: 'any'
|
|
},
|
|
{
|
|
arg: 'hasElectronicInvoice',
|
|
type: 'boolean'
|
|
},
|
|
{
|
|
arg: 'hasDailyInvoice',
|
|
type: 'boolean'
|
|
}
|
|
],
|
|
returns: {
|
|
arg: 'res',
|
|
type: 'string',
|
|
root: true
|
|
},
|
|
http: {
|
|
path: `/:id/updateFiscalData`,
|
|
verb: 'PATCH'
|
|
}
|
|
});
|
|
|
|
Self.updateFiscalData = async(ctx, clientId, options) => {
|
|
let tx;
|
|
const myOptions = {};
|
|
const models = Self.app.models;
|
|
const args = ctx.args;
|
|
|
|
if (typeof options == 'object')
|
|
Object.assign(myOptions, options);
|
|
if (!myOptions.transaction) {
|
|
tx = await Self.beginTransaction({});
|
|
myOptions.transaction = tx;
|
|
}
|
|
try {
|
|
const canEditNotTaxDataChecked =
|
|
await models.ACL.checkAccessAcl(ctx, 'Client', 'editFiscalDataWithoutTaxDataCheck', 'WRITE');
|
|
const client = await models.Client.findById(clientId, null, myOptions);
|
|
if (!canEditNotTaxDataChecked && client.isTaxDataChecked)
|
|
throw new UserError(`Not enough privileges to edit a client with verified data`);
|
|
// Sage data validation
|
|
const taxDataChecked = args.isTaxDataChecked;
|
|
const sageTaxChecked = client.sageTaxTypeFk || args.sageTaxTypeFk;
|
|
const sageTransactionChecked = client.sageTransactionTypeFk || args.sageTransactionTypeFk;
|
|
const hasSageData = sageTaxChecked && sageTransactionChecked;
|
|
|
|
if (taxDataChecked && !hasSageData)
|
|
throw new UserError(`You need to fill sage information before you check verified data`);
|
|
|
|
// Remove unwanted properties
|
|
delete args.ctx;
|
|
delete args.id;
|
|
|
|
const updatedClient = await client.updateAttributes(args, myOptions);
|
|
|
|
if (tx) await tx.commit();
|
|
|
|
return updatedClient;
|
|
} catch (e) {
|
|
if (tx) await tx.rollback();
|
|
throw e;
|
|
}
|
|
};
|
|
};
|