Skip to content

Commit cf24c32

Browse files
committed
Add verifytoken check for qn history
-Add verifytoken check before creating and updating question history -Removing unnecessary API -fix bug with hasSubmitted
1 parent af2dfe3 commit cf24c32

File tree

13 files changed

+77
-211
lines changed

13 files changed

+77
-211
lines changed

backend/collab-service/src/api/questionHistoryService.ts

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,26 @@ export const createQuestionHistory = (
1717
title: string,
1818
submissionStatus: string,
1919
code: string,
20-
language: string
20+
language: string,
21+
authToken: string
2122
) => {
2223
const dateAttempted = new Date();
23-
return qnHistoryService.post("/", {
24-
userIds,
25-
questionId,
26-
title,
27-
submissionStatus,
28-
dateAttempted,
29-
timeTaken: 0,
30-
code,
31-
language,
32-
});
24+
return qnHistoryService.post(
25+
"/",
26+
{
27+
userIds,
28+
questionId,
29+
title,
30+
submissionStatus,
31+
dateAttempted,
32+
timeTaken: 0,
33+
code,
34+
language,
35+
},
36+
{
37+
headers: {
38+
Authorization: authToken,
39+
},
40+
}
41+
);
3342
};

backend/collab-service/src/handlers/websocketHandler.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,16 @@ export const handleWebsocketCollabEvents = (socket: Socket) => {
7171
const isPartnerReady = partnerReadiness.get(roomId);
7272

7373
if (isPartnerReady && doc.getText().length === 0) {
74+
const token =
75+
socket.handshake.headers.authorization || socket.handshake.auth.token;
7476
createQuestionHistory(
7577
[uid1, uid2],
7678
qnId,
7779
qnTitle,
7880
"Attempted",
7981
template,
80-
language
82+
language,
83+
token
8184
)
8285
.then((res) => {
8386
doc.transact(() => {

backend/matching-service/src/api/questionHistoryService.ts

Lines changed: 0 additions & 31 deletions
This file was deleted.

backend/qn-history-service/.env.sample

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ SERVICE_PORT=3006
44
# Origins for cors
55
ORIGINS=http://localhost:5173,http://127.0.0.1:5173
66

7+
# Other services
8+
USER_SERVICE_URL=http://user-service:3001/api
9+
710
# Tests
811
MONGO_URI_TEST=mongodb://mongo:mongo@test-mongo:27017/
912

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import axios from "axios";
2+
import dotenv from "dotenv";
3+
4+
dotenv.config();
5+
6+
const USER_SERVICE_URL =
7+
process.env.USER_SERVICE_URL || "http://user-service:3001/api";
8+
9+
export const userClient = axios.create({
10+
baseURL: USER_SERVICE_URL,
11+
withCredentials: true,
12+
});

backend/qn-history-service/src/controllers/questionHistoryController.ts

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -83,31 +83,6 @@ export const updateQnHistory = async (
8383
}
8484
};
8585

86-
export const deleteQnHistory = async (
87-
req: Request,
88-
res: Response
89-
): Promise<void> => {
90-
try {
91-
const { id } = req.params;
92-
93-
if (!id.match(MONGO_OBJ_ID_FORMAT)) {
94-
res.status(400).json({ message: MONGO_OBJ_ID_MALFORMED_MESSAGE });
95-
return;
96-
}
97-
98-
const currQnHistory = await QnHistory.findById(id);
99-
if (!currQnHistory) {
100-
res.status(404).json({ message: QN_HIST_NOT_FOUND_MESSAGE });
101-
return;
102-
}
103-
104-
await QnHistory.findByIdAndDelete(id);
105-
res.status(200).json({ message: QN_HIST_DELETED_MESSAGE });
106-
} catch (error) {
107-
res.status(500).json({ message: SERVER_ERROR_MESSAGE, error });
108-
}
109-
};
110-
11186
type QnHistListParams = {
11287
page: string;
11388
qnHistLimit: string;
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { NextFunction, Response, Request } from "express";
2+
import { userClient } from "../api/userService";
3+
4+
export const verifyToken = (
5+
req: Request,
6+
res: Response,
7+
next: NextFunction
8+
) => {
9+
const authHeader = req.headers.authorization;
10+
userClient
11+
.get("/auth/verify-token", { headers: { Authorization: authHeader } })
12+
.then(() => next())
13+
.catch((err) => {
14+
console.log(err.response);
15+
return res.status(err.response.status).json(err.response.data);
16+
});
17+
};
Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,20 @@
11
import express from "express";
22
import {
33
createQnHistory,
4-
deleteQnHistory,
54
readQnHistIndiv,
65
readQnHistoryList,
76
updateQnHistory,
87
} from "../controllers/questionHistoryController";
8+
import { verifyToken } from "../middlewares/basicAccessControl";
99

1010
const router = express.Router();
1111

12-
router.post("/", createQnHistory);
12+
router.post("/", verifyToken, createQnHistory);
1313

14-
router.put("/:id", updateQnHistory);
14+
router.put("/:id", verifyToken, updateQnHistory);
1515

1616
router.get("/", readQnHistoryList);
1717

1818
router.get("/:id", readQnHistIndiv);
1919

20-
router.delete("/:id", deleteQnHistory);
21-
2220
export default router;

backend/qn-history-service/src/utils/constants.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ export const QN_HIST_CREATED_MESSAGE = "Question history created successfully.";
22

33
export const QN_HIST_NOT_FOUND_MESSAGE = "Question history not found.";
44

5-
export const QN_HIST_DELETED_MESSAGE = "Question history deleted successfully.";
6-
75
export const SERVER_ERROR_MESSAGE = "Server error.";
86

97
export const QN_HIST_RETRIEVED_MESSAGE =

backend/qn-history-service/swagger.yml

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -255,40 +255,6 @@ paths:
255255
application/json:
256256
schema:
257257
$ref: "#/definitions/ServerError"
258-
delete:
259-
tags:
260-
- qnhistories
261-
summary: Deletes a question history
262-
description: Deletes a question history
263-
parameters:
264-
- in: path
265-
name: id
266-
type: string
267-
required: true
268-
description: Question history id
269-
responses:
270-
200:
271-
description: Successful Response
272-
content:
273-
application/json:
274-
schema:
275-
type: object
276-
properties:
277-
message:
278-
type: string
279-
description: Message
280-
404:
281-
description: Question History Not Found
282-
content:
283-
application/json:
284-
schema:
285-
$ref: "#/definitions/Error"
286-
500:
287-
description: Internal Server Error
288-
content:
289-
application/json:
290-
schema:
291-
$ref: "#/definitions/ServerError"
292258
get:
293259
tags:
294260
- qnhistories

0 commit comments

Comments
 (0)