Skip to content

Commit e940674

Browse files
authored
Merge pull request #116 from CS3219-AY2425S1/delete-history-entries-on-missing-question-and-categories
Add History Deletion on Question Deletion
2 parents 4d6f6ec + 5fa2271 commit e940674

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

backend/question-service/controllers/historyController.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,15 @@ export const getUserHistoryEntries = async (req: any, res: Response) => {
1717
model: "category",
1818
},
1919
});
20-
const historyViewModels = historyEntries.map((entry) => {
20+
21+
historyEntries.forEach(async (entry) => {
22+
if (entry.question === null) {
23+
await historyEntryModel.findByIdAndDelete({_id: entry._id});
24+
}
25+
})
26+
const historyViewModels = historyEntries
27+
.filter((entry) => !(entry.question === null))
28+
.map((entry) => {
2129
return {
2230
id: entry._id,
2331
key: entry._id,

backend/question-service/controllers/questionsController.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { Request, Response, NextFunction } from "express";
44
import { EachMessagePayload } from "kafkajs";
55
import { producer, QUESTION_TOPIC } from "../utils/kafkaClient";
66
import { Types } from "mongoose";
7+
import historyEntryModel from "../models/HistoryEntry";
78

89
const DIFFICULTIES = ["easy", "medium", "hard"];
910

@@ -143,6 +144,7 @@ export async function deleteQuestion(
143144
const { id } = request.params;
144145

145146
try {
147+
await historyEntryModel.deleteMany({ question: id });
146148
const deletedQuestion = await questionModel.findOneAndDelete({
147149
_id: id,
148150
});

0 commit comments

Comments
 (0)