@@ -27,7 +27,7 @@ export const getUserHistoryEntries = async (req: any, res: Response) => {
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 ) ;
@@ -59,19 +59,23 @@ export const createOrUpdateUserHistoryEntry = async (req: any, res: Response) =>
59
59
60
60
return res . status ( 200 ) . json ( updatedEntry ) ;
61
61
} else if ( ! existingEntry ) {
62
- const newHistoryEntry = new historyEntryModel ( {
63
- userId,
64
- question : questionId ,
65
- roomId,
66
- attemptStartedAt,
67
- attemptCompletedAt,
68
- collaboratorId,
69
- attemptCodes : isInitial ? [ attemptCode ] : [ ] ,
70
- } ) ;
71
-
72
- const savedEntry = await newHistoryEntry . save ( ) ;
73
-
74
- return res . status ( 201 ) . json ( savedEntry ) ;
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
+ }
75
79
} else {
76
80
return res . status ( 200 ) . json ( "Attempt already exists." ) ;
77
81
// To reach here, this must be initial creation and there's an existing entry. No need to create new attempt.
0 commit comments