File: /var/dev/nowruzgan/rest/api/controllers/user/update.js
module.exports = {
friendlyName: 'UpdateUser',
inputs: {
id: {
type: 'number',
},
firstName: {
type: 'string',
allowNull: true
},
lastName: {
type: 'string',
allowNull: true
},
roles: {
type: 'ref',
required: false
},
},
exits: {
notFound: { statusCode: 404 },
forbidden: { statusCode: 403 },
dbError: { statusCode: 500 },
},
getActions: () => ({'user.update': 'تغییر مشخصات کاربران'}),
fn: async function (inputs, exits) {
let session = this.req.sessionData;
let action = 'user.update';
await sails.helpers.permit(action, session).intercept('reject', 'forbidden');
sails.log.info({session, action, message: `updating user info`});
let user = await User.findOne({id: inputs.id})
.intercept(sails.log.interceptError(session, action, `on finding the user`, 'dbError'));
if(!user) return exits.notFound();
await sails.getDatastore('mysql').transaction(async db => {
await User.update({id: user.id}).set({
firstName: inputs.firstName || '',
lastName: inputs.lastName || '',
}).usingConnection(db).catch(sails.log.relay(session, action, `on updating the user`, 'dbError'));
if(!inputs.roles) return;
await UserRole.destroy({user: user.id})
.usingConnection(db).catch(sails.log.relay(session, action, `on deleting roles`, 'dbError'));
await UserRole.createEach(inputs.roles.map(role => ({role, user: user.id})))
.usingConnection(db).catch(sails.log.relay(session, action, `on deleting roles`, 'dbError'));
}).intercept('transaction', sails.log.interceptError(session, action, 'relay', 'dbError'));
return exits.success(user);
}
};