2018-08-02 07:49:00 +00:00
|
|
|
let UserError = require('../../helpers').UserError;
|
|
|
|
|
2018-07-13 10:37:58 +00:00
|
|
|
module.exports = Self => {
|
|
|
|
Self.remoteMethodCtx('updateFiscalData', {
|
|
|
|
description: 'Updates billing data of a client',
|
|
|
|
accessType: 'WRITE',
|
|
|
|
accepts: [{
|
|
|
|
arg: 'data',
|
|
|
|
type: 'Object',
|
|
|
|
required: true,
|
|
|
|
description: 'Params to update',
|
|
|
|
http: {source: 'body'}
|
|
|
|
}, {
|
|
|
|
arg: 'id',
|
|
|
|
type: 'string',
|
|
|
|
required: true,
|
|
|
|
description: 'Model id',
|
|
|
|
http: {source: 'path'}
|
|
|
|
}],
|
|
|
|
returns: {
|
|
|
|
arg: 'res',
|
|
|
|
type: 'String',
|
|
|
|
root: true
|
|
|
|
},
|
|
|
|
http: {
|
|
|
|
path: `/:id/updateFiscalData`,
|
|
|
|
verb: 'PATCH'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
Self.updateFiscalData = async(ctx, params, id) => {
|
|
|
|
let userId = ctx.req.accessToken.userId;
|
|
|
|
let isAdministrative = await Self.app.models.Account.hasRole(userId, 'administrative');
|
|
|
|
let [taxData] = await Self.app.models.Client.find({where: {id: id}, fields: ['isTaxDataChecked']});
|
2018-08-13 12:10:03 +00:00
|
|
|
|
2018-07-13 10:37:58 +00:00
|
|
|
if (!isAdministrative && taxData.isTaxDataChecked)
|
2018-08-02 07:49:00 +00:00
|
|
|
throw new UserError(`You don't have enough privileges to do that`);
|
2018-07-13 10:37:58 +00:00
|
|
|
|
|
|
|
let validUpdateParams = [
|
|
|
|
'socialName',
|
|
|
|
'fi',
|
|
|
|
'street',
|
|
|
|
'postcode',
|
|
|
|
'city',
|
|
|
|
'countryFk',
|
|
|
|
'provinceFk',
|
|
|
|
'isActive',
|
|
|
|
'isFreezed',
|
|
|
|
'hasToInvoice',
|
|
|
|
'isVies',
|
|
|
|
'isToBeMailed',
|
|
|
|
'hasToInvoiceByAddress',
|
|
|
|
'isEqualizated',
|
2018-07-16 07:19:38 +00:00
|
|
|
'isTaxDataVerified',
|
|
|
|
'isTaxDataChecked'
|
2018-07-13 10:37:58 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
for (const key in params) {
|
|
|
|
if (validUpdateParams.indexOf(key) === -1)
|
2018-08-02 07:49:00 +00:00
|
|
|
throw new UserError(`You don't have enough privileges to do that`);
|
2018-07-13 10:37:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
params.id = id;
|
2018-10-03 10:53:30 +00:00
|
|
|
|
|
|
|
let client = await Self.app.models.Client.findById(id);
|
|
|
|
return await client.updateAttributes(params);
|
2018-07-13 10:37:58 +00:00
|
|
|
};
|
|
|
|
};
|