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;
const myOptions = {};
const models = Self.app.models;
const args = ctx.args;
const userId = ctx.req.accessToken.userId;
const $t = ctx.req.__;
const {args, req: {__: $t}} = ctx;
if (typeof options == 'object')
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 => {
Self.remoteMethod('updateFiscalData', {
description: 'Updates fiscal data of a supplier',
@ -14,65 +76,11 @@ module.exports = Self => {
http: {source: 'path'}
},
{
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'
}],
arg: 'data',
type: 'object',
http: {source: 'body'}
}
],
returns: {
arg: 'res',
type: 'string',
@ -86,13 +94,18 @@ module.exports = Self => {
Self.updateFiscalData = async(ctx, supplierId) => {
const models = Self.app.models;
const args = ctx.args;
const {args} = ctx;
const supplier = await models.Supplier.findById(supplierId);
// Remove unwanted properties
delete args.ctx;
delete args.id;
return supplier.updateAttributes(args);
const {isValid, tag} = validatorBodyModel(BODY_MODEL, args.data);
if (!isValid) {
const key = $t(`${tag}`);
throw new Error($t('Model is not valid', {key})
);
}
return supplier.updateAttributes(args.data);
};
};