salix/services/loopback/common/methods/client/updateBillingData.js

64 lines
1.8 KiB
JavaScript
Raw Normal View History

let UserError = require('../../helpers').UserError;
2018-07-13 10:37:58 +00:00
module.exports = Self => {
Self.remoteMethodCtx('updateBillingData', {
description: 'Updates billing data of a client',
accessType: 'WRITE',
accepts: [{
arg: 'data',
type: 'Object',
required: true,
description: 'Params to update',
http: {source: 'body'}
}, {
arg: 'id',
type: 'string',
required: true,
description: 'Model id',
http: {source: 'path'}
}],
returns: {
arg: 'data',
type: 'Worker',
root: true
},
http: {
path: `/:id/updateBillingData`,
2018-09-18 12:36:41 +00:00
verb: 'POST'
2018-07-13 10:37:58 +00:00
}
});
Self.updateBillingData = async(ctx, params, id) => {
let userId = ctx.req.accessToken.userId;
let isAdministrative = await Self.app.models.Account.hasRole(userId, 'administrative');
2018-09-18 12:36:41 +00:00
let client = await Self.app.models.Client.findOne({where: {id: id}});
if (!isAdministrative && client.isTaxDataChecked)
throw new UserError(`You don't have enough privileges to do that`);
2018-07-13 10:37:58 +00:00
let validUpdateParams = [
'payMethodFk',
'dueDay',
'iban',
'hasLcr',
'hasCoreVnl',
'hasSepaVnl'
];
for (const key in params) {
if (validUpdateParams.indexOf(key) === -1)
throw new UserError(`You don't have enough privileges to do that`);
2018-07-13 10:37:58 +00:00
}
2018-09-18 12:36:41 +00:00
return client.updateAttributes({
payMethodFk: params.payMethodFk,
dueDay: params.dueDay,
iban: params.iban,
hasLcr: params.hasLcr,
hasCoreVnl: params.hasCoreVnl,
hasSepaVnl: params.hasSepaVnl
});
2018-07-13 10:37:58 +00:00
};
};