#5878 - FiscalData validations #1851

Merged
carlossa merged 58 commits from 5858-fiscalData-validations into dev 2024-02-28 13:53:34 +00:00
2 changed files with 76 additions and 65 deletions
Showing only changes of commit 334c5f03a5 - Show all commits

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,19 +1,6 @@
module.exports = Self => {
Self.remoteMethod('updateFiscalData', {
description: 'Updates fiscal data of a supplier',
accessType: 'WRITE',
accepts: [{
arg: 'ctx',
type: 'Object',
http: {source: 'context'}
},
{
arg: 'id',
type: 'Number',
description: 'The supplier id',
http: {source: 'path'}
},
{
const validatorBodyModel = require('../../../../client/back/methods/client/body_model_validator');
const BODY_MODEL = [{
arg: 'name',
type: 'string'
},
@ -72,7 +59,28 @@ module.exports = Self => {
{
arg: 'isTrucker',
type: 'boolean'
}],
}];
module.exports = Self => {
Self.remoteMethod('updateFiscalData', {
description: 'Updates fiscal data of a supplier',
accessType: 'WRITE',
accepts: [{
arg: 'ctx',
type: 'Object',
http: {source: 'context'}
},
{
arg: 'id',
type: 'Number',
description: 'The supplier id',
http: {source: 'path'}
},
{
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);
};
};