feat(backend): extend conversation endpoint for optional receiverId
When receiverId is omitted, returns all documents where the person is sender or receiver (single-person mode). Bilateral mode is unchanged. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -212,7 +212,7 @@ public class DocumentController {
|
|||||||
@GetMapping("/conversation")
|
@GetMapping("/conversation")
|
||||||
public List<Document> getConversation(
|
public List<Document> getConversation(
|
||||||
@RequestParam UUID senderId,
|
@RequestParam UUID senderId,
|
||||||
@RequestParam UUID receiverId,
|
@RequestParam(required = false) UUID receiverId,
|
||||||
@RequestParam(required = false) LocalDate from,
|
@RequestParam(required = false) LocalDate from,
|
||||||
@RequestParam(required = false) LocalDate to,
|
@RequestParam(required = false) LocalDate to,
|
||||||
@RequestParam(defaultValue = "DESC") String dir) {
|
@RequestParam(defaultValue = "DESC") String dir) {
|
||||||
|
|||||||
@@ -60,11 +60,9 @@ public interface DocumentRepository extends JpaRepository<Document, UUID>, JpaSp
|
|||||||
@Query("SELECT DISTINCT d FROM Document d " +
|
@Query("SELECT DISTINCT d FROM Document d " +
|
||||||
"JOIN d.receivers r " +
|
"JOIN d.receivers r " +
|
||||||
"WHERE " +
|
"WHERE " +
|
||||||
// Logik: (Sender A an Empfänger B) ODER (Sender B an Empfänger A)
|
|
||||||
"((d.sender.id = :person1 AND r.id = :person2) " +
|
"((d.sender.id = :person1 AND r.id = :person2) " +
|
||||||
" OR " +
|
" OR " +
|
||||||
" (d.sender.id = :person2 AND r.id = :person1)) " +
|
" (d.sender.id = :person2 AND r.id = :person1)) " +
|
||||||
// UND das Datum stimmt
|
|
||||||
"AND d.documentDate BETWEEN :from AND :to")
|
"AND d.documentDate BETWEEN :from AND :to")
|
||||||
List<Document> findConversation(
|
List<Document> findConversation(
|
||||||
@Param("person1") UUID person1,
|
@Param("person1") UUID person1,
|
||||||
@@ -73,4 +71,14 @@ public interface DocumentRepository extends JpaRepository<Document, UUID>, JpaSp
|
|||||||
@Param("to") LocalDate to,
|
@Param("to") LocalDate to,
|
||||||
Sort sort);
|
Sort sort);
|
||||||
|
|
||||||
|
@Query("SELECT DISTINCT d FROM Document d " +
|
||||||
|
"LEFT JOIN d.receivers r " +
|
||||||
|
"WHERE (d.sender.id = :personId OR r.id = :personId) " +
|
||||||
|
"AND d.documentDate BETWEEN :from AND :to")
|
||||||
|
List<Document> findSinglePersonCorrespondence(
|
||||||
|
@Param("personId") UUID personId,
|
||||||
|
@Param("from") LocalDate from,
|
||||||
|
@Param("to") LocalDate to,
|
||||||
|
Sort sort);
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -328,6 +328,9 @@ public class DocumentService {
|
|||||||
public List<Document> getConversationFiltered(UUID senderId, UUID receiverId, LocalDate from, LocalDate to, Sort sort) {
|
public List<Document> getConversationFiltered(UUID senderId, UUID receiverId, LocalDate from, LocalDate to, Sort sort) {
|
||||||
LocalDate dateFrom = (from != null) ? from : LocalDate.parse("0000-01-01");
|
LocalDate dateFrom = (from != null) ? from : LocalDate.parse("0000-01-01");
|
||||||
LocalDate dateTo = (to != null) ? to : LocalDate.now();
|
LocalDate dateTo = (to != null) ? to : LocalDate.now();
|
||||||
|
if (receiverId == null) {
|
||||||
|
return documentRepository.findSinglePersonCorrespondence(senderId, dateFrom, dateTo, sort);
|
||||||
|
}
|
||||||
return documentRepository.findConversation(senderId, receiverId, dateFrom, dateTo, sort);
|
return documentRepository.findConversation(senderId, receiverId, dateFrom, dateTo, sort);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1244,4 +1244,33 @@ class DocumentServiceTest {
|
|||||||
assertThat(captor.getValue().getSort())
|
assertThat(captor.getValue().getSort())
|
||||||
.isEqualTo(Sort.by(Sort.Direction.DESC, "updatedAt"));
|
.isEqualTo(Sort.by(Sort.Direction.DESC, "updatedAt"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ─── getConversationFiltered (single-person mode) ─────────────────────────
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void getConversationFiltered_callsSinglePersonQuery_whenReceiverIdIsNull() {
|
||||||
|
UUID personId = UUID.randomUUID();
|
||||||
|
Sort sort = Sort.by(Sort.Direction.DESC, "documentDate");
|
||||||
|
when(documentRepository.findSinglePersonCorrespondence(eq(personId), any(), any(), eq(sort)))
|
||||||
|
.thenReturn(List.of());
|
||||||
|
|
||||||
|
documentService.getConversationFiltered(personId, null, null, null, sort);
|
||||||
|
|
||||||
|
verify(documentRepository).findSinglePersonCorrespondence(eq(personId), any(), any(), eq(sort));
|
||||||
|
verify(documentRepository, never()).findConversation(any(), any(), any(), any(), any());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void getConversationFiltered_callsBilateralQuery_whenReceiverIdIsSet() {
|
||||||
|
UUID senderId = UUID.randomUUID();
|
||||||
|
UUID receiverId = UUID.randomUUID();
|
||||||
|
Sort sort = Sort.by(Sort.Direction.DESC, "documentDate");
|
||||||
|
when(documentRepository.findConversation(eq(senderId), eq(receiverId), any(), any(), eq(sort)))
|
||||||
|
.thenReturn(List.of());
|
||||||
|
|
||||||
|
documentService.getConversationFiltered(senderId, receiverId, null, null, sort);
|
||||||
|
|
||||||
|
verify(documentRepository).findConversation(eq(senderId), eq(receiverId), any(), any(), eq(sort));
|
||||||
|
verify(documentRepository, never()).findSinglePersonCorrespondence(any(), any(), any(), any());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user