Add updateMe endpoint and user fiels
This commit is contained in:
@@ -106,6 +106,63 @@
|
||||
"relation": "manyToMany",
|
||||
"target": "api::choral.choral",
|
||||
"mappedBy": "users"
|
||||
},
|
||||
"board": {
|
||||
"type": "relation",
|
||||
"relation": "oneToOne",
|
||||
"target": "api::board.board"
|
||||
},
|
||||
"background": {
|
||||
"type": "media",
|
||||
"multiple": false,
|
||||
"required": false,
|
||||
"allowedTypes": [
|
||||
"images",
|
||||
"files",
|
||||
"videos",
|
||||
"audios"
|
||||
]
|
||||
},
|
||||
"name": {
|
||||
"type": "string"
|
||||
},
|
||||
"surname": {
|
||||
"type": "string"
|
||||
},
|
||||
"address": {
|
||||
"type": "text"
|
||||
},
|
||||
"gender": {
|
||||
"type": "enumeration",
|
||||
"enum": [
|
||||
"men",
|
||||
"women",
|
||||
"none"
|
||||
]
|
||||
},
|
||||
"job": {
|
||||
"type": "enumeration",
|
||||
"enum": [
|
||||
"choir_director",
|
||||
"choir_addict",
|
||||
"choir_master",
|
||||
"choir_singer",
|
||||
"none"
|
||||
]
|
||||
},
|
||||
"voice": {
|
||||
"type": "enumeration",
|
||||
"enum": [
|
||||
"soprano",
|
||||
"mezzo-soprano",
|
||||
"alto",
|
||||
"tenor",
|
||||
"baryton",
|
||||
"basse"
|
||||
]
|
||||
},
|
||||
"dob": {
|
||||
"type": "date"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -93,6 +93,93 @@ module.exports = (plugin) => {
|
||||
};
|
||||
};
|
||||
|
||||
const uploadImage = async (ctx, label: string, username: string) => {
|
||||
const key = `${label}Image`;
|
||||
if (ctx.request.files[key]) {
|
||||
const files = Array.isArray(ctx.request.files[key])
|
||||
? ctx.request.files[key][0]
|
||||
: ctx.request.files[key];
|
||||
const extension = files.originalFilename.match(/\.[0-9a-z]+$/i);
|
||||
const payload = {
|
||||
fileInfo: {
|
||||
caption: "undefined",
|
||||
alternativeText: username || "",
|
||||
name: `${username}_avatar${extension}`,
|
||||
},
|
||||
};
|
||||
const asset = await strapi.services["plugin::upload.upload"].upload({
|
||||
data: payload,
|
||||
files,
|
||||
});
|
||||
return asset[0].id;
|
||||
}
|
||||
|
||||
return 0;
|
||||
};
|
||||
|
||||
plugin.services.providers = providers;
|
||||
|
||||
plugin.controllers.user.updateMe = async (ctx) => {
|
||||
const user = ctx.state.user;
|
||||
|
||||
// User has to be logged in to update themselves
|
||||
if (!user) {
|
||||
return ctx.unauthorized();
|
||||
}
|
||||
|
||||
const data = JSON.parse(ctx.request.body.data);
|
||||
|
||||
const newData = lod.pick(data, [
|
||||
"email",
|
||||
"username",
|
||||
"name",
|
||||
"surname",
|
||||
"address",
|
||||
"gender",
|
||||
"voice",
|
||||
"job",
|
||||
"dob",
|
||||
]);
|
||||
|
||||
// Make sure there is no duplicate user with the same username
|
||||
if (newData.username) {
|
||||
const userWithSameUsername = await strapi
|
||||
.query("plugin::users-permissions.user")
|
||||
.findOne({ where: { username: newData.username } });
|
||||
|
||||
if (userWithSameUsername && userWithSameUsername.id != user.id) {
|
||||
return ctx.badRequest("Username already taken");
|
||||
}
|
||||
}
|
||||
|
||||
// Make sure there is no duplicate user with the same email
|
||||
if (newData.email) {
|
||||
const userWithSameEmail = await strapi
|
||||
.query("plugin::users-permissions.user")
|
||||
.findOne({ where: { email: newData.email.toLowerCase() } });
|
||||
|
||||
if (userWithSameEmail && userWithSameEmail.id != user.id) {
|
||||
return ctx.badRequest("Email already taken");
|
||||
}
|
||||
newData.email = newData.email.toLowerCase();
|
||||
}
|
||||
|
||||
ctx.request.body = newData;
|
||||
ctx.params = { id: user.id };
|
||||
|
||||
const avatarId = await uploadImage(ctx, "avatar", newData.username);
|
||||
if (avatarId != 0) ctx.request.body.avatar = avatarId;
|
||||
const backgroundId = await uploadImage(ctx, "background", newData.username);
|
||||
if (backgroundId != 0) ctx.request.body.background = backgroundId;
|
||||
|
||||
return plugin.controllers.user.update(ctx);
|
||||
};
|
||||
|
||||
plugin.routes["content-api"].routes.push({
|
||||
method: "PUT",
|
||||
path: "/users/me",
|
||||
handler: "user.updateMe",
|
||||
});
|
||||
|
||||
return plugin;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user