Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,32 @@ public static <T> Predicate<T> distinctByKey(final Function<? super T, ?> keyExt
return t -> seen.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null;
}

private QuestionBank refetchAndBackfillQuestion(
final String slug, final QuestionBank existingBankQuestion, final boolean fast) {
LeetcodeQuestion question =
fast ? leetcodeClient.findQuestionBySlugFast(slug) : leetcodeClient.findQuestionBySlug(slug);

QuestionBank refetchedQuestion = QuestionBank.builder()
.questionSlug(question.getTitleSlug())
.questionDifficulty(QuestionDifficulty.valueOf(question.getDifficulty()))
.questionTitle(question.getQuestionTitle())
.questionNumber(question.getQuestionId())
.questionLink("https://leetcode.com/problems/" + question.getTitleSlug())
.description(Optional.ofNullable(question.getQuestion()))
.acceptanceRate(question.getAcceptanceRate())
.topics(question.getTopics().stream()
.map(SubmissionsHandler::topicTagToQuestionTopic)
.toList())
.build();

if (existingBankQuestion != null) {
refetchedQuestion.setId(existingBankQuestion.getId());
questionBankRepository.updateQuestion(refetchedQuestion);
}

return refetchedQuestion;
}

public ArrayList<AcceptedSubmission> handleSubmissions(
final List<LeetcodeSubmission> leetcodeSubmissions, final User user, boolean fast) {
ArrayList<AcceptedSubmission> acceptedSubmissions = new ArrayList<>();
Expand All @@ -116,26 +142,16 @@ public ArrayList<AcceptedSubmission> handleSubmissions(
.map(s -> {
String slug = s.getTitleSlug();

QuestionBank bankQuestion = questionBankRepository
.getQuestionBySlug(slug)
.orElseGet(() -> {
LeetcodeQuestion question = fast
? leetcodeClient.findQuestionBySlugFast(slug)
: leetcodeClient.findQuestionBySlug(slug);

return QuestionBank.builder()
.questionSlug(question.getTitleSlug())
.questionDifficulty(QuestionDifficulty.valueOf(question.getDifficulty()))
.questionTitle(question.getQuestionTitle())
.questionNumber(question.getQuestionId())
.questionLink("https://leetcode.com/problems/" + question.getTitleSlug())
.description(Optional.ofNullable(question.getQuestion()))
.acceptanceRate(question.getAcceptanceRate())
.topics(question.getTopics().stream()
.map(SubmissionsHandler::topicTagToQuestionTopic)
.toList())
.build();
});
Optional<QuestionBank> existingBankQuestion = questionBankRepository.getQuestionBySlug(slug);

boolean hasDescription = existingBankQuestion
.flatMap(QuestionBank::getDescription)
.filter(description -> !description.isBlank())
.isPresent();

QuestionBank bankQuestion = hasDescription
? existingBankQuestion.get()
: refetchAndBackfillQuestion(slug, existingBankQuestion.orElse(null), fast);

return Pair.of(slug, bankQuestion);
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,43 @@ void testNoBank() {
verify(leetcodeClient, never()).findQuestionBySlug(anyString());
}

@Test
@DisplayName("re-fetches and backfills the question bank entry when its description is missing")
void backfillsMissingDescription() {
LeetcodeSubmission sub = acceptedSubmission(603, "two-sum", LocalDateTime.of(2025, 6, 1, 12, 0));
when(questionRepository.questionExistsBySubmissionId("603")).thenReturn(false);
when(questionRepository.getQuestionBySlugAndUserId("two-sum", USER_ID)).thenReturn(Optional.empty());

QuestionBank staleBankQuestion = QuestionBank.builder()
.id("bank-1")
.questionSlug("two-sum")
.questionDifficulty(QuestionDifficulty.Easy)
.questionTitle("two-sum")
.questionNumber(1)
.questionLink("https://leetcode.com/problems/two-sum")
.description(Optional.empty())
.acceptanceRate(50f)
.topics(List.of())
.build();

when(questionBankRepository.getQuestionBySlug("two-sum")).thenReturn(Optional.of(staleBankQuestion));
when(leetcodeClient.findQuestionBySlug("two-sum")).thenReturn(leetcodeQuestion("two-sum", "Easy", 50f));

handler.handleSubmissions(List.of(sub), user, false);

verify(leetcodeClient).findQuestionBySlug("two-sum");

ArgumentCaptor<QuestionBank> bankCaptor = ArgumentCaptor.forClass(QuestionBank.class);
verify(questionBankRepository).updateQuestion(bankCaptor.capture());
assertEquals("bank-1", bankCaptor.getValue().getId());
assertEquals(
Optional.of("Description of two-sum"), bankCaptor.getValue().getDescription());

ArgumentCaptor<Question> qCaptor = ArgumentCaptor.forClass(Question.class);
verify(questionRepository).createQuestion(qCaptor.capture());
assertEquals(Optional.of("Description of two-sum"), qCaptor.getValue().getDescription());
}

@Test
@DisplayName("distinctByKey filters duplicates by the given key")
void distinctByKeyWorks() {
Expand Down
Loading