@@ -170,7 +170,7 @@ export const postFileAnswer = async (
170170
171171export type CorrectAnswers = { questionId : string , answer : string } [ ] ;
172172
173- export const getAnswers = async ( { accessToken, bookId, group } : {
173+ export const getLastAnswers = async ( { accessToken, bookId, group } : {
174174 accessToken : string ;
175175 bookId : number ;
176176 group : string | undefined | null ;
@@ -179,37 +179,46 @@ export const getAnswers = async ({ accessToken, bookId, group }: {
179179 const answers = [
180180 ...(
181181 await db . all ( `
182- SELECT answers.answer, q.questionId, isCorrect, points
183- FROM answers
184- JOIN questions q ON answers.questionId = q.id
185- LEFT JOIN groups g ON answers.groupId = g.id
186- WHERE userId = ? AND bookId = ? AND g.name IS ? AND q.type NOT LIKE 'upload%'
187- ORDER BY answers.createdAt` ,
182+ SELECT answer, questionId, isCorrect, points, nAttempts FROM (
183+ SELECT a.answer, q.questionId, a.isCorrect, a.points,
184+ ROW_NUMBER() OVER (PARTITION BY a.questionId ORDER BY a.createdAt DESC) AS rn,
185+ COUNT(*) OVER (PARTITION BY a.questionId) AS nAttempts
186+ FROM answers a
187+ JOIN questions q ON a.questionId = q.id
188+ LEFT JOIN groups g ON a.groupId = g.id
189+ WHERE a.userId = ? AND a.bookId = ? AND g.name IS ? AND q.type NOT LIKE 'upload%'
190+ ) t
191+ WHERE rn = 1;` ,
188192 [ userId , bookId , group ?? null ]
189- ) ) . map ( ( { answer, questionId, isCorrect, points} ) => ( {
193+ ) ) . map ( ( { answer, questionId, isCorrect, points, nAttempts } ) => ( {
190194 type : "value" ,
191195 answer,
192196 questionId,
193197 points,
198+ nAttempts,
194199 // DB stores 0 and 1 even if the column is declared as BOOLEAN
195200 isCorrect : isCorrect === null ? undefined : ! ! isCorrect ,
196- } ) as AnswerValue & { questionId : string } ) ,
201+ } ) as AnswerValue & { questionId : string , nAttempts : number } ) ,
197202 ...(
198203 await db . all ( `
199- SELECT q.questionId, group_concat(u.filename, "\n") as files
200- FROM answers
201- JOIN questions q ON answers.questionId = q.id
202- LEFT JOIN groups g ON answers.groupId = g.id
203- LEFT JOIN uploads u ON u.answerId = answers.id
204- WHERE userId = ? AND bookId = ? AND g.name IS ? AND q.type LIKE 'upload%'
205- GROUP BY answers.id
206- ORDER BY answers.createdAt` ,
204+ SELECT questionId, group_concat(u.filename, "\n") as files, 1 as nAttempts FROM (
205+ SELECT q.questionId, a.id as answerId,
206+ ROW_NUMBER() OVER (PARTITION BY a.questionId ORDER BY a.createdAt DESC) AS rn
207+ FROM answers a
208+ JOIN questions q ON a.questionId = q.id
209+ LEFT JOIN groups g ON a.groupId = g.id
210+ WHERE userId = ? AND bookId = ? AND g.name IS ? AND q.type LIKE 'upload%'
211+ ) t
212+ JOIN uploads u ON u.answerId = t.answerId
213+ WHERE rn = 1
214+ GROUP BY t.answerId;` ,
207215 [ userId , bookId , group ?? null ]
208- ) ) . map ( ( { files, questionId} ) => ( {
216+ ) ) . map ( ( { files, questionId, nAttempts } ) => ( {
209217 type : "files" ,
210218 questionId,
219+ nAttempts,
211220 files : files ? files . split ( "\n" ) : [ ] ,
212- } ) as AnswerFile & { questionId : string } )
221+ } ) as AnswerFile & { questionId : string , nAttempts : number } )
213222 ] ;
214223
215224 const correctAnswers =
0 commit comments