refs #5878 feat: validate params in supplier model
gitea/salix/pipeline/head There was a failure building this commit Details

This commit is contained in:
Javier Segarra 2023-11-27 10:47:49 +01:00
parent 3822183095
commit 334c5f03a5
2 changed files with 76 additions and 65 deletions

View File

@ -128,9 +128,7 @@ module.exports = Self => {
let tx; let tx;
const myOptions = {}; const myOptions = {};
const models = Self.app.models; const models = Self.app.models;
const args = ctx.args; const {args, req: {__: $t}} = ctx;
const userId = ctx.req.accessToken.userId;
const $t = ctx.req.__;
if (typeof options == 'object') if (typeof options == 'object')
Object.assign(myOptions, options); Object.assign(myOptions, options);

View File

@ -1,3 +1,65 @@
const validatorBodyModel = require('../../../../client/back/methods/client/body_model_validator');
const BODY_MODEL = [{
arg: 'name',
type: 'string'
},
{
arg: 'nif',
type: 'string'
},
{
arg: 'account',
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: 'string'
},
{
arg: 'healthRegister',
type: 'string'
},
{
arg: 'isVies',
type: 'boolean'
},
{
arg: 'isTrucker',
type: 'boolean'
}];
module.exports = Self => { module.exports = Self => {
Self.remoteMethod('updateFiscalData', { Self.remoteMethod('updateFiscalData', {
description: 'Updates fiscal data of a supplier', description: 'Updates fiscal data of a supplier',
@ -14,65 +76,11 @@ module.exports = Self => {
http: {source: 'path'} http: {source: 'path'}
}, },
{ {
arg: 'name', arg: 'data',
type: 'string' type: 'object',
}, http: {source: 'body'}
{ }
arg: 'nif', ],
type: 'string'
},
{
arg: 'account',
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: 'string'
},
{
arg: 'healthRegister',
type: 'string'
},
{
arg: 'isVies',
type: 'boolean'
},
{
arg: 'isTrucker',
type: 'boolean'
}],
returns: { returns: {
arg: 'res', arg: 'res',
type: 'string', type: 'string',
@ -86,13 +94,18 @@ module.exports = Self => {
Self.updateFiscalData = async(ctx, supplierId) => { Self.updateFiscalData = async(ctx, supplierId) => {
const models = Self.app.models; const models = Self.app.models;
const args = ctx.args; const {args} = ctx;
const supplier = await models.Supplier.findById(supplierId); const supplier = await models.Supplier.findById(supplierId);
// Remove unwanted properties // Remove unwanted properties
delete args.ctx; delete args.ctx;
delete args.id; delete args.id;
const {isValid, tag} = validatorBodyModel(BODY_MODEL, args.data);
return supplier.updateAttributes(args); if (!isValid) {
const key = $t(`${tag}`);
throw new Error($t('Model is not valid', {key})
);
}
return supplier.updateAttributes(args.data);
}; };
}; };