Get user id by email; Revert changes of API delete user

This commit is contained in:
lethemanh
2025-03-27 14:03:01 +07:00
committed by Anton Shepilov
parent 5e60517c16
commit eb46da342c
3 changed files with 13 additions and 20 deletions
@@ -17,16 +17,10 @@ export class AdminDeleteUserController {
async deleteUser(
userId: string,
deleteData: boolean,
username?: string,
): Promise<{ status: "failed" | "deleting" | "done"; userId?: string }> {
try {
let pk: UserPrimaryKey = { id: userId };
if (username) {
pk = { username_canonical: username };
}
const data = await gr.services.users.anonymizeAndDelete(
pk,
{ id: userId },
{
user: { server_request: true },
} as unknown as ExecutionContext,
@@ -68,8 +62,8 @@ export class AdminDeleteUserController {
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 });
async getUserIdByEmail(email: string) {
const user = await gr.services.users.get({ email_canonical: email });
return user?.id;
}
@@ -14,22 +14,21 @@ function authenticateAdminQuery(request: FastifyRequest, reply: FastifyReply) {
return true;
}
type TUserDeleteQueryBody = TQueryBody & { userId: string; deleteData: boolean; username?: string };
type TUserDeleteQueryBody = TQueryBody & { userId: string; deleteData: boolean };
function getUserIfValidQuery(request: FastifyRequest, reply: FastifyReply) {
if (!authenticateAdminQuery(request, reply)) return { userId: "", deleteData: false };
const body = request.body as TUserDeleteQueryBody;
if (!body.userId?.length && !body.username) {
if (!body.userId?.length) {
reply.status(400).send();
return { userId: "", username: "", deleteData: false };
return { userId: "", deleteData: false };
}
return {
userId: body.userId || "",
username: body.username,
deleteData: body.deleteData,
};
}
type TUserGetIdByUsernameBody = TQueryBody & { username: string };
type TUserGetIdByEmailBody = TQueryBody & { email: string };
const routes: FastifyPluginCallback = async (fastify: FastifyInstance, _opts, next) => {
const urlRoot = "/api/user/delete";
@@ -37,13 +36,13 @@ const routes: FastifyPluginCallback = async (fastify: FastifyInstance, _opts, ne
const controller = new AdminDeleteUserController();
if (config?.endpointSecret?.length) {
fastify.post(urlRoot, async (request, reply) => {
const { userId, deleteData, username } = getUserIfValidQuery(request, reply);
if (!userId && !username) return false;
const { userId, deleteData } = getUserIfValidQuery(request, reply);
if (!userId) return false;
if (userId == "e2e_simulate_timeout")
return new Promise(() => {
return;
});
return await controller.deleteUser(userId, deleteData, username);
return await controller.deleteUser(userId, deleteData);
});
fastify.post(`${urlRoot}/pending`, async (request, reply) => {
@@ -54,8 +53,8 @@ const routes: FastifyPluginCallback = async (fastify: FastifyInstance, _opts, ne
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);
const { email } = request.body as TUserGetIdByEmailBody;
const userId = await controller.getUserIdByEmail(email);
if (!userId) {
return reply.status(404).send({ message: "User not found" });
@@ -151,7 +151,7 @@ export type UserNotificationPreferences = {
export type UserPrimaryKey = {
id?: uuid;
username_canonical?: string;
email_canonical?: string;
};
export function getInstance(user: Partial<User>): User {