Add updateMe endpoint and user fiels

This commit is contained in:
2025-04-06 22:30:47 +02:00
parent 4ae35e1185
commit 961b4aab93
3 changed files with 159 additions and 0 deletions

View File

@@ -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;
};