Skip to content

Commit

Permalink
#81 Add new quiz service function to get recently uploaded quizzes
Browse files Browse the repository at this point in the history
  • Loading branch information
danielemery committed Nov 26, 2023
1 parent 5cab74a commit 6cf45dd
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/quiz/quiz.persistence.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,4 +233,21 @@ export class QuizPersistence {
},
});
}

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

const actual = await sut.getRecentQuizUploads();

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

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

/**
* Get the most recent `first` quiz uploads.
* @param first The number of quizzes to get. Defaults to 20.
* @returns The most recent `first` quiz uploads.
*/
async getRecentQuizUploads(first = 20) {
const recent = await this.#persistence.getRecentQuizUploads({ limit: first });
return recent.map((quiz) => ({
id: quiz.id,
type: quiz.type,
date: quiz.date,
uploadedAt: quiz.uploadedAt,
uploadedBy: {
name: quiz.uploadedByUser.name,
email: quiz.uploadedByUser.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 6cf45dd

Please sign in to comment.