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