1
- import { Response } from 'express' ;
2
- import historyEntryModel from '../models/HistoryEntry' ;
3
- import { AuthenticatedRequest } from 'middlewares/auth' ;
1
+ import { Response , Request } from "express" ;
2
+ import historyEntryModel from "../models/HistoryEntry" ;
4
3
5
4
const getErrorMessage = ( error : unknown ) : string => {
6
5
if ( error instanceof Error ) return error . message ;
7
- return ' An unexpected error occurred' ;
6
+ return " An unexpected error occurred" ;
8
7
} ;
9
8
10
- const extractUserIdFromToken = ( req : AuthenticatedRequest ) : string | null => {
11
- const userId = req . userId ;
12
- if ( ! userId ) {
13
- console . error ( 'userId missing - Token is likely invalid' ) ;
14
- return null ;
15
- }
16
- return userId
17
- } ;
18
-
19
- export const getUserHistoryEntries = async ( req : AuthenticatedRequest , res : Response ) => {
9
+ export const getUserHistoryEntries = async ( req : any , res : Response ) => {
20
10
try {
21
- const userId = extractUserIdFromToken ( req ) ;
22
-
23
- if ( ! userId ) {
24
- return res . status ( 401 ) . json ( { error : 'Invalid or missing token' } ) ;
25
- }
11
+ const userId = req . userId ;
26
12
27
- const historyEntries = await historyEntryModel . find ( { userId } )
28
- . populate ( {
29
- path : 'question' ,
13
+ const historyEntries = await historyEntryModel . find ( { userId } ) . populate ( {
14
+ path : "question" ,
30
15
populate : {
31
- path : ' categories' ,
32
- model : ' category' ,
16
+ path : " categories" ,
17
+ model : " category" ,
33
18
} ,
34
19
} ) ;
35
20
const historyViewModels = historyEntries . map ( ( entry ) => {
@@ -43,25 +28,22 @@ export const getUserHistoryEntries = async (req: AuthenticatedRequest, res: Resp
43
28
difficulty : entry . question . difficulty ,
44
29
topics : entry . question . categories . map ( ( cat : any ) => cat . name ) ,
45
30
attemptCodes : entry . attemptCodes ,
46
- } } ) ;
31
+ } ;
32
+ } ) ;
47
33
res . status ( 200 ) . json ( historyViewModels ) ;
48
34
} catch ( error ) {
49
35
res . status ( 500 ) . json ( { error : getErrorMessage ( error ) } ) ;
50
36
}
51
37
} ;
52
38
53
- export const createOrUpdateUserHistoryEntry = async ( req : AuthenticatedRequest , res : Response ) => {
39
+ export const createOrUpdateUserHistoryEntry = async ( req : any , res : Response ) => {
54
40
try {
55
- const userId = extractUserIdFromToken ( req ) ;
56
-
57
- if ( ! userId ) {
58
- return res . status ( 401 ) . json ( { error : 'Invalid or missing token' } ) ;
59
- }
41
+ const userId = req . userId ;
60
42
61
43
const { questionId, roomId, attemptStartedAt, attemptCompletedAt, collaboratorId, attemptCode } = req . body ;
62
44
63
45
if ( ! roomId ) {
64
- return res . status ( 400 ) . json ( { error : ' roomId is required' } ) ;
46
+ return res . status ( 400 ) . json ( { error : " roomId is required" } ) ;
65
47
}
66
48
67
49
const existingEntry = await historyEntryModel . findOne ( { userId, roomId } ) ;
@@ -96,13 +78,9 @@ export const createOrUpdateUserHistoryEntry = async (req: AuthenticatedRequest,
96
78
}
97
79
} ;
98
80
99
- export const removeRoomIdPresence = async ( req : AuthenticatedRequest , res : Response ) => {
81
+ export const removeRoomIdPresence = async ( req : any , res : Response ) => {
100
82
try {
101
- const userId = extractUserIdFromToken ( req ) ;
102
-
103
- if ( ! userId ) {
104
- return res . status ( 401 ) . json ( { error : 'Invalid or missing token' } ) ;
105
- }
83
+ const userId = req . userId ;
106
84
const { roomId } = req . params ;
107
85
108
86
const existingEntries = await historyEntryModel . find ( { roomId } ) ;
@@ -114,41 +92,32 @@ export const removeRoomIdPresence = async (req: AuthenticatedRequest, res: Respo
114
92
updatedEntries . push ( entry . _id . toString ( ) ) ;
115
93
} ) ;
116
94
117
- return res . status ( 200 ) . json ( { updatedEntries } )
95
+ return res . status ( 200 ) . json ( { updatedEntries } ) ;
118
96
} catch ( error ) {
119
97
return res . status ( 500 ) . json ( { error : getErrorMessage ( error ) } ) ;
120
98
}
121
- }
99
+ } ;
122
100
123
- export const deleteUserHistoryEntry = async ( req : AuthenticatedRequest , res : Response ) => {
101
+ export const deleteUserHistoryEntry = async ( req : any , res : Response ) => {
124
102
try {
125
- const userId = extractUserIdFromToken ( req ) ;
126
-
127
- if ( ! userId ) {
128
- return res . status ( 401 ) . json ( { error : 'Invalid or missing token' } ) ;
129
- }
130
-
103
+ const userId = req . userId ;
131
104
const { id } = req . params ;
132
105
133
106
const deletedEntry = await historyEntryModel . findOneAndDelete ( { _id : id , userId } ) ;
134
107
135
108
if ( ! deletedEntry ) {
136
- return res . status ( 404 ) . json ( { message : ' History entry not found' } ) ;
109
+ return res . status ( 404 ) . json ( { message : " History entry not found" } ) ;
137
110
}
138
111
139
- res . status ( 200 ) . json ( { message : ' History entry deleted successfully' } ) ;
112
+ res . status ( 200 ) . json ( { message : " History entry deleted successfully" } ) ;
140
113
} catch ( error ) {
141
114
res . status ( 500 ) . json ( { error : getErrorMessage ( error ) } ) ;
142
115
}
143
116
} ;
144
117
145
- export const deleteUserHistoryEntries = async ( req : AuthenticatedRequest , res : Response ) => {
118
+ export const deleteUserHistoryEntries = async ( req : any , res : Response ) => {
146
119
try {
147
- const userId = extractUserIdFromToken ( req ) ;
148
-
149
- if ( ! userId ) {
150
- return res . status ( 401 ) . json ( { error : 'Invalid or missing token' } ) ;
151
- }
120
+ const userId = req . userId ;
152
121
153
122
const { ids } = req . body ;
154
123
if ( ! Array . isArray ( ids ) ) {
@@ -162,13 +131,9 @@ export const deleteUserHistoryEntries = async (req: AuthenticatedRequest, res: R
162
131
}
163
132
} ;
164
133
165
- export const deleteAllUserHistoryEntries = async ( req : AuthenticatedRequest , res : Response ) => {
134
+ export const deleteAllUserHistoryEntries = async ( req : any , res : Response ) => {
166
135
try {
167
- const userId = extractUserIdFromToken ( req ) ;
168
-
169
- if ( ! userId ) {
170
- return res . status ( 401 ) . json ( { error : 'Invalid or missing token' } ) ;
171
- }
136
+ const userId = req . userId ;
172
137
173
138
const result = await historyEntryModel . deleteMany ( { userId } ) ;
174
139
res . status ( 200 ) . json ( {
0 commit comments