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