1
0
Fork 0
mirror of https://github.com/plankanban/planka.git synced 2025-07-18 20:59:44 +02:00
planka/server/api/controllers/users/update-avatar.js

71 lines
1.4 KiB
JavaScript
Raw Normal View History

2020-04-21 05:04:34 +05:00
const Errors = {
USER_NOT_FOUND: {
userNotFound: 'User not found',
},
};
module.exports = {
inputs: {
id: {
type: 'string',
regex: /^[0-9]+$/,
required: true,
},
},
exits: {
userNotFound: {
responseType: 'notFound',
},
uploadError: {
responseType: 'unprocessableEntity',
},
},
async fn(inputs, exits) {
const { currentUser } = this.req;
let user;
if (currentUser.isAdmin) {
user = await sails.helpers.users.getOne(inputs.id);
2020-04-21 05:04:34 +05:00
if (!user) {
throw Errors.USER_NOT_FOUND;
}
} else if (inputs.id !== currentUser.id) {
throw Errors.USER_NOT_FOUND; // Forbidden
} else {
user = currentUser;
}
this.req
.file('file')
.upload(sails.helpers.utils.createUserAvatarReceiver(), async (error, files) => {
if (error) {
return exits.uploadError(error.message);
}
2020-04-21 05:04:34 +05:00
if (files.length === 0) {
return exits.uploadError('No file was uploaded');
}
2020-04-21 05:04:34 +05:00
user = await sails.helpers.users.updateOne(
user,
{
avatarDirname: files[0].extra.dirname,
},
currentUser,
this.req,
);
2020-04-21 05:04:34 +05:00
if (!user) {
throw Errors.USER_NOT_FOUND;
}
2020-04-21 05:04:34 +05:00
return exits.success({
item: user.toJSON(),
});
2020-04-21 05:04:34 +05:00
});
},
};