2022-10-05 10:36:36 +00:00
|
|
|
const UserError = require('vn-loopback/util/user-error');
|
2022-06-16 09:32:42 +00:00
|
|
|
module.exports = Self => {
|
|
|
|
Self.remoteMethodCtx('updateUser', {
|
|
|
|
description: 'Updates the user information',
|
2023-04-24 09:07:04 +00:00
|
|
|
accessType: 'WRITE',
|
2022-06-16 09:32:42 +00:00
|
|
|
accepts: [
|
|
|
|
{
|
|
|
|
arg: 'id',
|
|
|
|
type: 'number',
|
2022-10-05 10:36:36 +00:00
|
|
|
description: 'The user id'
|
2022-06-16 09:32:42 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
arg: 'name',
|
|
|
|
type: 'string',
|
|
|
|
description: 'the user name'
|
|
|
|
},
|
2022-06-20 05:31:10 +00:00
|
|
|
{
|
|
|
|
arg: 'email',
|
2022-10-05 10:36:36 +00:00
|
|
|
type: 'any',
|
2022-06-20 05:31:10 +00:00
|
|
|
description: 'the user email'
|
|
|
|
},
|
2022-06-16 09:32:42 +00:00
|
|
|
{
|
2022-06-16 14:25:21 +00:00
|
|
|
arg: 'active',
|
2022-06-16 09:32:42 +00:00
|
|
|
type: 'boolean',
|
|
|
|
description: 'whether the user is active or not'
|
|
|
|
},
|
|
|
|
],
|
|
|
|
http: {
|
|
|
|
path: '/:id/updateUser',
|
|
|
|
verb: 'PATCH'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-06-16 14:25:21 +00:00
|
|
|
Self.updateUser = async function(ctx, id, options) {
|
2022-06-16 09:32:42 +00:00
|
|
|
const models = Self.app.models;
|
|
|
|
let tx;
|
|
|
|
const myOptions = {};
|
|
|
|
|
|
|
|
if (typeof options == 'object')
|
|
|
|
Object.assign(myOptions, options);
|
|
|
|
|
|
|
|
if (!myOptions.transaction) {
|
2023-01-23 14:24:00 +00:00
|
|
|
tx = await models.VnUser.beginTransaction({});
|
2022-06-16 09:32:42 +00:00
|
|
|
myOptions.transaction = tx;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2023-04-24 09:07:04 +00:00
|
|
|
const canEdit = await models.ACL.checkAccessAcl(ctx, 'Client', 'updateUser', 'WRITE');
|
|
|
|
if (!canEdit)
|
2022-10-05 10:36:36 +00:00
|
|
|
throw new UserError(`Not enough privileges to edit a client`);
|
2022-06-16 09:32:42 +00:00
|
|
|
|
2022-10-05 10:36:36 +00:00
|
|
|
const isClient = await models.Client.findById(id, null, myOptions);
|
2023-01-31 13:57:24 +00:00
|
|
|
const isAccount = await models.Account.findById(id, null, myOptions);
|
2022-06-16 09:32:42 +00:00
|
|
|
|
2023-01-31 13:57:24 +00:00
|
|
|
if (isClient && !isAccount) {
|
2023-01-23 14:24:00 +00:00
|
|
|
const user = await models.VnUser.findById(id, null, myOptions);
|
2022-10-05 10:36:36 +00:00
|
|
|
await user.updateAttributes(ctx.args, myOptions);
|
|
|
|
} else
|
|
|
|
throw new UserError(`Modifiable user details only by an administrator`);
|
2022-06-16 09:32:42 +00:00
|
|
|
|
|
|
|
if (tx) await tx.commit();
|
|
|
|
} catch (e) {
|
|
|
|
if (tx) await tx.rollback();
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|