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

110 lines
3.0 KiB
JavaScript

const UserError = require('vn-loopback/util/user-error');
module.exports = Self => {
Self.remoteMethodCtx('updateFiscalData', {
description: 'Updates fiscal data of a supplier',
accessType: 'WRITE',
accepts: [{
arg: 'id',
type: 'Number',
description: 'The supplier id',
http: {source: 'path'}
}, {
arg: 'name',
type: 'string'
}, {
arg: 'nif',
type: 'string'
}, {
arg: 'account',
type: 'any'
}, {
arg: 'phone',
type: 'any'
}, {
arg: 'sageTaxTypeFk',
type: 'any'
}, {
arg: 'sageWithholdingFk',
type: 'any'
}, {
arg: 'sageTransactionTypeFk',
type: 'any'
}, {
arg: 'postCode',
type: 'any'
}, {
arg: 'street',
type: 'any'
}, {
arg: 'city',
type: 'string'
}, {
arg: 'provinceFk',
type: 'any'
}, {
arg: 'countryFk',
type: 'any'
}, {
arg: 'supplierActivityFk',
type: 'any'
}, {
arg: 'healthRegister',
type: 'any'
}, {
arg: 'isVies',
type: 'boolean'
}, {
arg: 'isTrucker',
type: 'boolean'
}],
returns: {
arg: 'res',
type: 'string',
root: true
},
http: {
path: `/:id/updateFiscalData`,
verb: 'PATCH'
}
});
Self.updateFiscalData = async(ctx, supplierId, name, nif, account, phone, sageTaxTypeFk, sageWithholdingFk, sageTransactionTypeFk, postCode, street, city, provinceFk, countryFk, supplierActivityFk, healthRegister, isVies, isTrucker, options) => {
const models = Self.app.models;
const {args} = ctx;
const myOptions = {};
const supplier = await models.Supplier.findById(supplierId);
if (typeof options == 'object') Object.assign(myOptions, options);
delete args.ctx;
delete args.id;
const updateAllFiscalData = await models.ACL.checkAccessAcl(ctx, 'Supplier', 'updateAllFiscalData', 'WRITE');
if (!updateAllFiscalData) {
for (const arg in args) {
if (args[arg] && !['street', 'postCode', 'city', 'provinceFk', 'phone'].includes(arg))
throw new UserError('You cannot update these fields');
}
}
return supplier.updateAttributes({
name,
nif,
account,
phone,
sageTaxTypeFk,
sageWithholdingFk,
sageTransactionTypeFk,
postCode,
street,
city,
provinceFk,
countryFk,
supplierActivityFk,
healthRegister,
isVies,
isTrucker
}, myOptions);
};
};