feat: Kinematic Instant Messaging #801
23
src/controllers/api/clearDialogueHistoryController.ts
Normal file
23
src/controllers/api/clearDialogueHistoryController.ts
Normal file
@ -0,0 +1,23 @@
|
||||
import { getInventory } from "@/src/services/inventoryService";
|
||||
import { getAccountIdForRequest } from "@/src/services/loginService";
|
||||
import { RequestHandler } from "express";
|
||||
|
||||
export const clearDialogueHistoryController: RequestHandler = async (req, res) => {
|
||||
const accountId = await getAccountIdForRequest(req);
|
||||
const inventory = await getInventory(accountId);
|
||||
const request = JSON.parse(String(req.body)) as IClearDialogueRequest;
|
||||
|
||||
if (inventory.DialogueHistory && inventory.DialogueHistory.Dialogues) {
|
||||
for (const dialogueName of request.Dialogues) {
|
||||
const index = inventory.DialogueHistory.Dialogues.findIndex(x => x.DialogueName == dialogueName);
|
||||
if (index != -1) {
|
||||
inventory.DialogueHistory.Dialogues.splice(index, 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
![]() ⚠️ Potential issue Add request validation and error handling. The function should validate the request structure and handle potential errors during inventory operations. Apply this diff to add validation and error handling:
📝 Committable suggestion
🧰 Tools🪛 Biome (1.9.4)[error] 9-9: Change to an optional chain. Unsafe fix: Change to an optional chain. (lint/complexity/useOptionalChain) _:warning: Potential issue_
**Add request validation and error handling.**
The function should validate the request structure and handle potential errors during inventory operations.
Apply this diff to add validation and error handling:
```diff
+ if (!request.Dialogues || !Array.isArray(request.Dialogues)) {
+ res.status(400).json({ error: 'Invalid request: Dialogues array is required' });
+ return;
+ }
+
if (inventory.DialogueHistory && inventory.DialogueHistory.Dialogues) {
for (const dialogueName of request.Dialogues) {
+ if (typeof dialogueName !== 'string') {
+ res.status(400).json({ error: 'Invalid request: Dialogue names must be strings' });
+ return;
+ }
+
const index = inventory.DialogueHistory.Dialogues.findIndex(x => x.DialogueName == dialogueName);
if (index != -1) {
inventory.DialogueHistory.Dialogues.splice(index, 1);
}
}
}
- await inventory.save();
- res.end();
+ try {
+ await inventory.save();
+ res.status(200).json({
+ success: true,
+ message: 'Dialogue history cleared successfully'
+ });
+ } catch (error) {
+ res.status(500).json({ error: 'Failed to save inventory' });
+ }
```
<!-- suggestion_start -->
<details>
<summary>📝 Committable suggestion</summary>
> ‼️ **IMPORTANT**
> Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
`````suggestion
if (!request.Dialogues || !Array.isArray(request.Dialogues)) {
res.status(400).json({ error: 'Invalid request: Dialogues array is required' });
return;
}
if (inventory.DialogueHistory && inventory.DialogueHistory.Dialogues) {
for (const dialogueName of request.Dialogues) {
if (typeof dialogueName !== 'string') {
res.status(400).json({ error: 'Invalid request: Dialogue names must be strings' });
return;
}
const index = inventory.DialogueHistory.Dialogues.findIndex(x => x.DialogueName == dialogueName);
if (index != -1) {
inventory.DialogueHistory.Dialogues.splice(index, 1);
}
}
}
try {
await inventory.save();
res.status(200).json({
success: true,
message: 'Dialogue history cleared successfully'
});
} catch (error) {
res.status(500).json({ error: 'Failed to save inventory' });
}
`````
</details>
<!-- suggestion_end -->
<details>
<summary>🧰 Tools</summary>
<details>
<summary>🪛 Biome (1.9.4)</summary>
[error] 9-9: Change to an optional chain.
Unsafe fix: Change to an optional chain.
(lint/complexity/useOptionalChain)
</details>
</details>
<!-- This is an auto-generated comment by CodeRabbit -->
|
||||
await inventory.save();
|
||||
res.end();
|
||||
};
|
||||
|
||||
interface IClearDialogueRequest {
|
||||
Dialogues: string[];
|
||||
}
|
@ -6,6 +6,7 @@ import { archonFusionController } from "@/src/controllers/api/archonFusionContro
|
||||
import { artifactsController } from "../controllers/api/artifactsController";
|
||||
import { checkDailyMissionBonusController } from "@/src/controllers/api/checkDailyMissionBonusController";
|
||||
import { claimCompletedRecipeController } from "@/src/controllers/api/claimCompletedRecipeController";
|
||||
import { clearDialogueHistoryController } from "@/src/controllers/api/clearDialogueHistoryController";
|
||||
import { createGuildController } from "@/src/controllers/api/createGuildController";
|
||||
import { creditsController } from "@/src/controllers/api/creditsController";
|
||||
import { deleteSessionController } from "@/src/controllers/api/deleteSessionController";
|
||||
@ -119,6 +120,7 @@ apiRouter.post("/arcaneCommon.php", arcaneCommonController);
|
||||
apiRouter.post("/archonFusion.php", archonFusionController);
|
||||
apiRouter.post("/artifacts.php", artifactsController);
|
||||
apiRouter.post("/claimCompletedRecipe.php", claimCompletedRecipeController);
|
||||
apiRouter.post("/clearDialogueHistory.php", clearDialogueHistoryController);
|
||||
apiRouter.post("/createGuild.php", createGuildController);
|
||||
apiRouter.post("/endlessXp.php", endlessXpController);
|
||||
apiRouter.post("/evolveWeapon.php", evolveWeaponController);
|
||||
|
Loading…
x
Reference in New Issue
Block a user
⚠️ Potential issue
Add error handling for JSON parsing.
The JSON parsing operation could throw an exception if the request body is malformed.
Apply this diff to add error handling:
📝 Committable suggestion