Skip to content

Commit

Permalink
#81 Add new quiz service function to get recent quiz completions
Browse files Browse the repository at this point in the history
  • Loading branch information
danielemery committed Nov 26, 2023
1 parent a6291c9 commit 40931a1
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/quiz/quiz.persistence.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,4 +205,31 @@ export class QuizPersistence {
});
return slicePagedResults(result, limit, afterId !== undefined);
}

async getRecentQuizCompletions({ limit }: { limit: number }) {
return this.#prisma.client().quizCompletion.findMany({
take: limit,
orderBy: {
completedAt: 'desc',
},
include: {
completedBy: {
select: {
user: {
select: {
email: true,
name: true,
},
},
},
},
quiz: {
select: {
date: true,
type: true,
},
},
},
});
}
}
38 changes: 38 additions & 0 deletions src/quiz/quiz.service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const mockPersistence = {
getCompletionScoreWithQuizTypesForUser: jest.fn(),
getQuizzesWithUserResults: jest.fn(),
getQuizByIdWithResults: jest.fn(),
getRecentQuizCompletions: jest.fn(),
};
const mockFileService = {
createKey: jest.fn(),
Expand Down Expand Up @@ -315,5 +316,42 @@ describe('quiz', () => {
});
});
});
describe('getRecentQuizCompletions', () => {
it('must call getRecentQuizCompletions on persistence with correct arguments and transform the result', async () => {
mockPersistence.getRecentQuizCompletions.mockResolvedValueOnce([
{
completedAt: new Date('2023-01-01'),
score: new Decimal(12),
completedBy: [
{
user: {
email: '[email protected]',
name: 'Quiz Master',
},
},
],
quiz: {
type: 'SHARK',
date: new Date('2022-12-12'),
},
},
]);

const actual = await sut.getRecentQuizCompletions();

expect(mockPersistence.getRecentQuizCompletions).toHaveBeenCalledTimes(1);
expect(mockPersistence.getRecentQuizCompletions).toHaveBeenCalledWith({ limit: 20 });

expect(actual).toEqual([
{
completionDate: new Date('2023-01-01'),
score: 12,
participants: [{ email: '[email protected]', name: 'Quiz Master' }],
quizDate: new Date('2022-12-12'),
quizType: 'SHARK',
},
]);
});
});
});
});
19 changes: 19 additions & 0 deletions src/quiz/quiz.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,25 @@ export class QuizService {
};
}

/**
* Get the most recent `first` quiz completions along with their participants.
* @param first The number of completions to get. Defaults to 20.
* @returns The most recent `first` quiz completions along with their participants.
*/
async getRecentQuizCompletions(first = 20) {
const recent = await this.#persistence.getRecentQuizCompletions({ limit: first });
return recent.map((completion) => ({
quizType: completion.quiz.type,
quizDate: completion.quiz.date,
completionDate: completion.completedAt,
score: completion.score.toNumber(),
participants: completion.completedBy.map((user) => ({
name: user.user.name,
email: user.user.email,
})),
}));
}

async #populateFileWithUploadLink(file: { fileName: string; type: QuizImageType; imageKey: string }) {
const uploadLink = await this.#fileService.generateSignedUploadUrl(file.imageKey);
return {
Expand Down

0 comments on commit 40931a1

Please sign in to comment.