@@ -23,11 +23,11 @@ export const getUserHistoryEntries = async (req: any, res: Response) => {
23
23
key : entry . _id ,
24
24
roomId : entry . roomId ,
25
25
attemptStartedAt : entry . attemptStartedAt . getTime ( ) ,
26
- attemptCompletedAt : entry . attemptCompletedAt . getTime ( ) ,
26
+ lastAttemptSubmittedAt : entry . lastAttemptSubmittedAt . getTime ( ) ,
27
27
title : entry . question . title ,
28
28
difficulty : entry . question . difficulty ,
29
29
topics : entry . question . categories . map ( ( cat : any ) => cat . name ) ,
30
- attemptCodes : entry . attemptCodes ,
30
+ attemptCodes : entry . attemptCodes . filter ( ( attemptCode ) => attemptCode && attemptCode !== "" ) ,
31
31
} ;
32
32
} ) ;
33
33
res . status ( 200 ) . json ( historyViewModels ) ;
@@ -40,38 +40,45 @@ export const createOrUpdateUserHistoryEntry = async (req: any, res: Response) =>
40
40
try {
41
41
const userId = req . userId ;
42
42
43
- const { questionId, roomId, attemptStartedAt, attemptCompletedAt, collaboratorId, attemptCode } = req . body ;
43
+ const { questionId, roomId, attemptStartedAt, attemptCompletedAt, collaboratorId, attemptCode, isInitial } = req . body ;
44
44
45
45
if ( ! roomId ) {
46
46
return res . status ( 400 ) . json ( { error : "roomId is required" } ) ;
47
47
}
48
48
49
49
const existingEntry = await historyEntryModel . findOne ( { userId, roomId } ) ;
50
50
51
- if ( existingEntry ) {
51
+ if ( ! isInitial && existingEntry && attemptCode && attemptCode !== "" ) {
52
52
existingEntry . question = questionId ;
53
53
existingEntry . attemptStartedAt = attemptStartedAt ;
54
- existingEntry . attemptCompletedAt = attemptCompletedAt ;
54
+ existingEntry . lastAttemptSubmittedAt = attemptCompletedAt ;
55
55
existingEntry . collaboratorId = collaboratorId ;
56
56
existingEntry . attemptCodes . push ( attemptCode ) ;
57
57
58
58
const updatedEntry = await existingEntry . save ( ) ;
59
59
60
60
return res . status ( 200 ) . json ( updatedEntry ) ;
61
+ } else if ( ! existingEntry ) {
62
+ try {
63
+ const newHistoryEntry = new historyEntryModel ( {
64
+ userId,
65
+ question : questionId ,
66
+ roomId,
67
+ attemptStartedAt,
68
+ attemptCompletedAt,
69
+ collaboratorId,
70
+ attemptCodes : isInitial ? [ attemptCode ] : [ ] ,
71
+ } ) ;
72
+
73
+ const savedEntry = await newHistoryEntry . save ( ) ;
74
+
75
+ return res . status ( 201 ) . json ( savedEntry ) ;
76
+ } catch {
77
+ return res . status ( 200 ) . json ( "Attempt already exists." ) ;
78
+ }
61
79
} else {
62
- const newHistoryEntry = new historyEntryModel ( {
63
- userId,
64
- question : questionId ,
65
- roomId,
66
- attemptStartedAt,
67
- attemptCompletedAt,
68
- collaboratorId,
69
- attemptCodes : [ attemptCode ] ,
70
- } ) ;
71
-
72
- const savedEntry = await newHistoryEntry . save ( ) ;
73
-
74
- return res . status ( 201 ) . json ( savedEntry ) ;
80
+ return res . status ( 200 ) . json ( "Attempt already exists." ) ;
81
+ // To reach here, this must be initial creation and there's an existing entry. No need to create new attempt.
75
82
}
76
83
} catch ( error ) {
77
84
return res . status ( 500 ) . json ( { error : getErrorMessage ( error ) } ) ;
0 commit comments