Implement API get user id by username

This commit is contained in:
lethemanh
2025-03-25 13:42:07 +07:00
committed by Anton Shepilov
parent bebf337d28
commit 5e60517c16
2 changed files with 22 additions and 1 deletions
@@ -48,7 +48,7 @@ export class AdminDeleteUserController {
} }
} }
} catch (err) { } catch (err) {
adminLogger.error("[DELETE USER] ", JSON.stringify({ err, userId }), "User deletion error"); adminLogger.error({ err, userId }, "[DELETE USER] User deletion error");
return { return {
status: "failed", status: "failed",
userId, userId,
@@ -67,4 +67,10 @@ export class AdminDeleteUserController {
).getEntities(); ).getEntities();
return users.map(({ id, delete_process_started_epoch }) => [id, delete_process_started_epoch]); return users.map(({ id, delete_process_started_epoch }) => [id, delete_process_started_epoch]);
} }
async getUserIdByUsername(username: string) {
const user = await gr.services.users.get({ username_canonical: username });
return user?.id;
}
} }
@@ -29,6 +29,8 @@ function getUserIfValidQuery(request: FastifyRequest, reply: FastifyReply) {
}; };
} }
type TUserGetIdByUsernameBody = TQueryBody & { username: string };
const routes: FastifyPluginCallback = async (fastify: FastifyInstance, _opts, next) => { const routes: FastifyPluginCallback = async (fastify: FastifyInstance, _opts, next) => {
const urlRoot = "/api/user/delete"; const urlRoot = "/api/user/delete";
const config = getConfig(); const config = getConfig();
@@ -48,6 +50,19 @@ const routes: FastifyPluginCallback = async (fastify: FastifyInstance, _opts, ne
if (!authenticateAdminQuery(request, reply)) return false; if (!authenticateAdminQuery(request, reply)) return false;
return await controller.listUsersPendingDeletion(); return await controller.listUsersPendingDeletion();
}); });
fastify.post("/api/user-id", async (request, reply) => {
if (!authenticateAdminQuery(request, reply)) return false;
const { username } = request.body as TUserGetIdByUsernameBody;
const userId = await controller.getUserIdByUsername(username);
if (!userId) {
return reply.status(404).send({ message: "User not found" });
}
return { id: userId };
});
} }
next(); next();
}; };