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

65 lines
1.7 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;
2018-09-18 12:36:41 +00:00
2018-09-27 06:46:58 +00:00
let data = filterAttributes(params, [
2018-07-13 10:37:58 +00:00
'payMethodFk',
2018-10-16 11:06:58 +00:00
'bankEntityFk',
2018-07-13 10:37:58 +00:00
'dueDay',
'iban',
'hasLcr',
'hasCoreVnl',
2018-09-27 06:46:58 +00:00
'hasSepaVnl']);
2018-07-13 10:37:58 +00:00
2018-09-27 06:46:58 +00:00
if (!Object.keys(data).length) return;
2018-07-13 10:37:58 +00:00
2018-09-27 10:58:19 +00:00
let isSalesAssistant = await Self.app.models.Account.hasRole(userId, 'salesAssistant');
2018-09-27 06:46:58 +00:00
let client = await Self.app.models.Client.findOne({where: {id: id}});
2018-09-27 10:58:19 +00:00
if (!isSalesAssistant)
2018-09-27 06:46:58 +00:00
throw new UserError(`You don't have enough privileges to do that`);
return client.updateAttributes(data);
2018-07-13 10:37:58 +00:00
};
2018-09-27 06:46:58 +00:00
function filterAttributes(params, allowed) {
let newParams = {};
Object.keys(params).forEach(attribute => {
if (allowed.indexOf(attribute) > -1)
newParams[attribute] = params[attribute];
});
return newParams;
}
2018-07-13 10:37:58 +00:00
};