-
-
Notifications
You must be signed in to change notification settings - Fork 268
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Adds the ability to delete users through the admin panel, provi…
…ding more control over user accounts.
- Loading branch information
Showing
8 changed files
with
296 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
import { FastifyReply, FastifyRequest } from "fastify"; | ||
import { DeleteUserRequest } from "./type"; | ||
import TelegramBot from "../../../../integration/telegram"; | ||
|
||
export const adminDeleteUserHandler = async ( | ||
request: FastifyRequest<DeleteUserRequest>, | ||
reply: FastifyReply | ||
) => { | ||
try { | ||
const prisma = request.server.prisma; | ||
const user = request.user; | ||
|
||
if (!user || !user.user_id) { | ||
return reply.status(401).send({ | ||
message: "Unauthorized", | ||
}); | ||
} | ||
|
||
if (user.user_id === request.body.user_id) { | ||
return reply.status(400).send({ | ||
message: "You cannot delete yourself", | ||
}); | ||
} | ||
|
||
const userToDelete = await prisma.user.findUnique({ | ||
where: { | ||
user_id: request.body.user_id, | ||
}, | ||
select: { | ||
user_id: true, | ||
isAdministrator: true, | ||
}, | ||
}); | ||
|
||
if (!userToDelete) { | ||
return reply.status(404).send({ | ||
message: "User not found", | ||
}); | ||
} | ||
|
||
if (userToDelete.isAdministrator) { | ||
return reply.status(403).send({ | ||
message: "You cannot delete an admin", | ||
}); | ||
} | ||
|
||
await prisma.$transaction(async (tx) => { | ||
const bots = await tx.bot.findMany({ | ||
where: { | ||
user_id: request.body.user_id, | ||
}, | ||
select: { | ||
id: true, | ||
}, | ||
}); | ||
|
||
const botIds = bots.map((bot) => bot.id); | ||
|
||
if (botIds.length > 0) { | ||
const botIntegrations = await tx.botIntegration.findMany({ | ||
where: { | ||
bot_id: { | ||
in: botIds, | ||
}, | ||
}, | ||
}); | ||
|
||
for (const botIntegration of botIntegrations) { | ||
if (botIntegration.provider === "telegram") { | ||
await TelegramBot.disconnect(botIntegration.identifier); | ||
} | ||
} | ||
|
||
await tx.botIntegration.deleteMany({ | ||
where: { | ||
bot_id: { | ||
in: botIds, | ||
}, | ||
}, | ||
}); | ||
|
||
await tx.botDocument.deleteMany({ | ||
where: { | ||
botId: { | ||
in: botIds, | ||
}, | ||
}, | ||
}); | ||
|
||
await tx.botSource.deleteMany({ | ||
where: { | ||
botId: { | ||
in: botIds, | ||
}, | ||
}, | ||
}); | ||
|
||
const botPlaygrounds = await tx.botPlayground.findMany({ | ||
where: { | ||
botId: { | ||
in: botIds, | ||
}, | ||
}, | ||
select: { | ||
id: true, | ||
}, | ||
}); | ||
|
||
const playgroundIds = botPlaygrounds.map((bp) => bp.id); | ||
|
||
await tx.botPlaygroundMessage.deleteMany({ | ||
where: { | ||
botPlaygroundId: { | ||
in: playgroundIds, | ||
}, | ||
}, | ||
}); | ||
|
||
await tx.botPlayground.deleteMany({ | ||
where: { | ||
botId: { | ||
in: botIds, | ||
}, | ||
}, | ||
}); | ||
|
||
await tx.bot.deleteMany({ | ||
where: { | ||
id: { | ||
in: botIds, | ||
}, | ||
}, | ||
}); | ||
} | ||
await tx.user.delete({ | ||
where: { | ||
user_id: request.body.user_id, | ||
}, | ||
}); | ||
}); | ||
|
||
return reply.status(200).send({ | ||
message: "User deleted successfully", | ||
}); | ||
} catch (error) { | ||
console.error("Error in adminDeleteUserHandler:", error); | ||
return reply.status(500).send({ | ||
message: "Internal server error", | ||
}); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from "./get.handler"; | ||
export * from "./post.handler"; | ||
export * from "./model.handler"; | ||
export * from "./model.handler"; | ||
export * from "./delete.handler"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.