From 3af5cccd5904be190e9f89f156e658766b8217d2 Mon Sep 17 00:00:00 2001 From: ngqile Date: Tue, 1 Dec 2026 21:31:03 +0800 Subject: [PATCH] feat: event lead rows carry attendance and source, with an attended filter Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM --- .../changes/add-event-invitations/tasks.md | 2 +- .../admin/AdminEventLeadController.java | 3 +- .../admin/AdminEventLeadExportController.java | 3 +- .../api/service/AdminEventLeadService.java | 27 ++++++-- .../admin/AdminEventLeadControllerTest.java | 10 +-- .../admin/AdminLeadExportControllerTest.java | 6 +- .../api/service/AdminEventLeadExportTest.java | 14 ++-- ...AdminEventLeadServiceCompanyScopeTest.java | 14 ++-- .../service/AdminEventLeadServiceTest.java | 36 +++++++++- .../services/counter/dto/EventLeadDetail.java | 4 +- .../counter/dto/EventLeadSearchRequest.java | 4 +- .../counter/repo/EventLeadRepository.java | 4 +- .../counter/service/EventLeadService.java | 5 +- .../controller/EventLeadControllerTest.java | 2 +- .../service/EventLeadListAndUpdateTest.java | 69 ++++++++++++++++--- .../counter/service/LeadCompanyScopeTest.java | 12 ++-- 16 files changed, 160 insertions(+), 55 deletions(-) diff --git a/openspec/changes/add-event-invitations/tasks.md b/openspec/changes/add-event-invitations/tasks.md index bc1ba26..9edf4d4 100644 --- a/openspec/changes/add-event-invitations/tasks.md +++ b/openspec/changes/add-event-invitations/tasks.md @@ -110,7 +110,7 @@ Waves: - Scope: `solvway-mobile-api/src/**` - Verify: `mvn -T 1C -q -pl solvway-mobile-api verify` -- [ ] 3.3 Event lead reads carry attendance and source (gap found in 5.3: 3.1 delivered only the write path). +- [x] 3.3 Event lead reads carry attendance and source (gap found in 5.3: 3.1 delivered only the write path). - Depends on: 3.1 - Tests first: - **Counter:** the event lead detail and search rows include `attendedAt` and `source`; `EventLeadSearchRequest.attended` (true, false or absent) filters on `attended_at` set or empty. diff --git a/solvway-mobile-api/src/main/java/com/solvway/mobile/api/controller/admin/AdminEventLeadController.java b/solvway-mobile-api/src/main/java/com/solvway/mobile/api/controller/admin/AdminEventLeadController.java index 36f1e42..2cf6f7d 100644 --- a/solvway-mobile-api/src/main/java/com/solvway/mobile/api/controller/admin/AdminEventLeadController.java +++ b/solvway-mobile-api/src/main/java/com/solvway/mobile/api/controller/admin/AdminEventLeadController.java @@ -73,10 +73,11 @@ public class AdminEventLeadController { @RequestParam(required = false) String status, @RequestParam(required = false) Long interestId, @RequestParam(required = false) String leadType, + @RequestParam(required = false) Boolean attended, @RequestParam(defaultValue = "0") int page, @RequestParam(defaultValue = "20") int size, @RequestParam(required = false) Long companyId) { - return ResponseEntity.ok(adminEventLeadService.list(eventUuid, salespersonId, temperature, status, interestId, leadType, page, size, companyId)); + return ResponseEntity.ok(adminEventLeadService.list(eventUuid, salespersonId, temperature, status, interestId, leadType, attended, page, size, companyId)); } @AppAuth diff --git a/solvway-mobile-api/src/main/java/com/solvway/mobile/api/controller/admin/AdminEventLeadExportController.java b/solvway-mobile-api/src/main/java/com/solvway/mobile/api/controller/admin/AdminEventLeadExportController.java index 1a96863..3d6a059 100644 --- a/solvway-mobile-api/src/main/java/com/solvway/mobile/api/controller/admin/AdminEventLeadExportController.java +++ b/solvway-mobile-api/src/main/java/com/solvway/mobile/api/controller/admin/AdminEventLeadExportController.java @@ -31,8 +31,9 @@ public class AdminEventLeadExportController { @RequestParam(required = false) String status, @RequestParam(required = false) Long interestId, @RequestParam(required = false) String leadType, + @RequestParam(required = false) Boolean attended, @RequestParam(required = false) Long companyId) { - byte[] file = adminEventLeadService.export(eventUuid, salespersonId, temperature, status, interestId, leadType, companyId); + byte[] file = adminEventLeadService.export(eventUuid, salespersonId, temperature, status, interestId, leadType, attended, companyId); // The service has already parsed the uuid as a number, so its digits are the canonical uuid; nothing else reaches the header. return xlsx("event-leads-" + eventUuid.replaceAll("\\D", ""), file); } diff --git a/solvway-mobile-api/src/main/java/com/solvway/mobile/api/service/AdminEventLeadService.java b/solvway-mobile-api/src/main/java/com/solvway/mobile/api/service/AdminEventLeadService.java index fcd6a11..6fdb75b 100644 --- a/solvway-mobile-api/src/main/java/com/solvway/mobile/api/service/AdminEventLeadService.java +++ b/solvway-mobile-api/src/main/java/com/solvway/mobile/api/service/AdminEventLeadService.java @@ -183,9 +183,9 @@ public class AdminEventLeadService { // --- listing and edit --- public Map list(String eventUuid, Long salespersonId, String temperature, String status, Long interestId, - String leadType, int page, int size, Long companyId) { + String leadType, Boolean attended, int page, int size, Long companyId) { CompanyEvent event = loadEvent(eventUuid, companyId); - Map p = search(event, salespersonId, temperature, status, interestId, leadType, page, size); + Map p = search(event, salespersonId, temperature, status, interestId, leadType, attended, page, size); List> rows = p == null || p.get("content") == null ? List.of() : (List>) p.get("content"); Map> interests = rows.isEmpty() ? Map.of() : interests(event.getCompanyId()); Map> users = new HashMap<>(); @@ -199,14 +199,14 @@ public class AdminEventLeadService { } /** The whole filtered listing as xlsx, paged 100 at a time. ponytail: buffers up to 20000 rows in memory; stream to the response if that grows. */ - public byte[] export(String eventUuid, Long salespersonId, String temperature, String status, Long interestId, String leadType, Long companyId) { + public byte[] export(String eventUuid, Long salespersonId, String temperature, String status, Long interestId, String leadType, Boolean attended, Long companyId) { User caller = caller(); CompanyEvent event = loadEvent(eventUuid, companyId); Map> users = new HashMap<>(); Map>[] interests = new Map[1]; return LeadExport.build(caller.getUsername(), "event-leads:" + event.getUuid(), "Event leads", - List.of("Name", "Mobile", "Email", "Lead type", "Salesperson", "Temperature", "Status", "Interests", "Next follow-up"), - page -> search(event, salespersonId, temperature, status, interestId, leadType, page, LeadExport.PAGE), + List.of("Name", "Mobile", "Email", "Lead type", "Salesperson", "Temperature", "Status", "Interests", "Next follow-up", "Attended", "Source"), + page -> search(event, salespersonId, temperature, status, interestId, leadType, attended, page, LeadExport.PAGE), r -> { if (interests[0] == null) interests[0] = interests(event.getCompanyId()); Map m = view(r, interests[0], users); @@ -215,10 +215,20 @@ public class AdminEventLeadService { return Arrays.asList(m.get("name"), m.get("mobile"), m.get("email"), label(m.get("leadType")), sp == null ? null : sp.get("name"), label(m.get("temperature")), label(m.get("status")), String.join(", ", ((List>) m.get("interests")).stream().map(i -> String.valueOf(i.get("name"))).toList()), - follow == null ? null : LocalDate.parse(follow)); + follow == null ? null : LocalDate.parse(follow), + (boolean) m.get("attended") ? "Yes" : "No", sourceLabel(m.get("source"))); }); } + private static String sourceLabel(Object source) { + return switch (String.valueOf(source)) { + case "STAFF" -> "Staff"; + case "QR" -> "QR"; + case "INVITE" -> "Invite"; + default -> source == null ? null : source.toString(); + }; + } + /** HOT becomes Hot. */ private static String label(Object code) { String s = code == null ? null : code.toString(); @@ -226,7 +236,7 @@ public class AdminEventLeadService { } private Map search(CompanyEvent event, Long salespersonId, String temperature, String status, Long interestId, - String leadType, int page, int size) { + String leadType, Boolean attended, int page, int size) { Map body = new LinkedHashMap<>(); body.put("companyId", event.getCompanyId()); body.put("eventUuid", event.getUuid()); @@ -235,6 +245,7 @@ public class AdminEventLeadService { body.put("status", oneOf("status", status, STATUSES)); body.put("interestId", interestId); body.put("leadType", oneOf("leadType", leadType, LEAD_TYPES)); + body.put("attended", attended); body.put("page", Math.max(page, 0)); body.put("size", size); return restTemplateUtil.postForEntity(url(RestApiCounterServiceConstants.API_REST_COUNTER_EVENT_LEAD_FIND_BY_EVENT), MAP, body).getBody(); @@ -351,6 +362,8 @@ public class AdminEventLeadService { m.put("nextFollowUpDate", d.get("nextFollowUpDate")); m.put("notes", d.get("notes")); m.put("capturedAt", d.get("capturedAt")); + m.put("attended", d.get("attendedAt") != null); + m.put("source", d.get("source")); return m; } diff --git a/solvway-mobile-api/src/test/java/com/solvway/mobile/api/controller/admin/AdminEventLeadControllerTest.java b/solvway-mobile-api/src/test/java/com/solvway/mobile/api/controller/admin/AdminEventLeadControllerTest.java index 7be5554..1d41f93 100644 --- a/solvway-mobile-api/src/test/java/com/solvway/mobile/api/controller/admin/AdminEventLeadControllerTest.java +++ b/solvway-mobile-api/src/test/java/com/solvway/mobile/api/controller/admin/AdminEventLeadControllerTest.java @@ -85,15 +85,15 @@ class AdminEventLeadControllerTest { @Test void listPassesFiltersAndPaging() throws Exception { - when(eventLeads.list(EV, 21L, "HOT", "OPEN", 3L, "NEW", 1, 25, null)).thenReturn(Map.of("content", List.of(), "totalElements", 0)); + when(eventLeads.list(EV, 21L, "HOT", "OPEN", 3L, "NEW", false, 1, 25, null)).thenReturn(Map.of("content", List.of(), "totalElements", 0)); mvc.perform(get("/api/admin/event-leads/" + EV + "/leads").param("salespersonId", "21").param("temperature", "HOT") - .param("status", "OPEN").param("interestId", "3").param("leadType", "NEW").param("page", "1").param("size", "25")) + .param("status", "OPEN").param("interestId", "3").param("leadType", "NEW").param("attended", "false").param("page", "1").param("size", "25")) .andExpect(status().isOk()); - verify(eventLeads).list(EV, 21L, "HOT", "OPEN", 3L, "NEW", 1, 25, null); + verify(eventLeads).list(EV, 21L, "HOT", "OPEN", 3L, "NEW", false, 1, 25, null); - when(eventLeads.list(EV, null, null, null, null, null, 0, 20, null)).thenReturn(Map.of("content", List.of())); + when(eventLeads.list(EV, null, null, null, null, null, null, 0, 20, null)).thenReturn(Map.of("content", List.of())); mvc.perform(get("/api/admin/event-leads/" + EV + "/leads")).andExpect(status().isOk()); - verify(eventLeads).list(EV, null, null, null, null, null, 0, 20, null); + verify(eventLeads).list(EV, null, null, null, null, null, null, 0, 20, null); } @Test diff --git a/solvway-mobile-api/src/test/java/com/solvway/mobile/api/controller/admin/AdminLeadExportControllerTest.java b/solvway-mobile-api/src/test/java/com/solvway/mobile/api/controller/admin/AdminLeadExportControllerTest.java index bee6175..ce233c4 100644 --- a/solvway-mobile-api/src/test/java/com/solvway/mobile/api/controller/admin/AdminLeadExportControllerTest.java +++ b/solvway-mobile-api/src/test/java/com/solvway/mobile/api/controller/admin/AdminLeadExportControllerTest.java @@ -56,10 +56,10 @@ class AdminLeadExportControllerTest { @Test void eventLeadExportPassesFiltersAndNamesTheFile() throws Exception { AdminEventLeadService svc = mock(AdminEventLeadService.class); - when(svc.export("77", 22L, "HOT", "OPEN", 3L, "NEW", null)).thenReturn(new byte[]{9}); + when(svc.export("77", 22L, "HOT", "OPEN", 3L, "NEW", true, null)).thenReturn(new byte[]{9}); MockMvc mvc = MockMvcBuilders.standaloneSetup(new AdminEventLeadExportController(svc)).build(); mvc.perform(get("/api/admin/event-leads/77/export").param("salespersonId", "22").param("temperature", "HOT") - .param("status", "OPEN").param("interestId", "3").param("leadType", "NEW")).andExpect(status().isOk()) + .param("status", "OPEN").param("interestId", "3").param("leadType", "NEW").param("attended", "true")).andExpect(status().isOk()) .andExpect(header().string("Content-Type", XLSX)) .andExpect(header().string("Content-Disposition", org.hamcrest.Matchers.matchesPattern("attachment; filename=\"event-leads-77-\\d{8}-\\d{4}\\.xlsx\""))) .andExpect(content().bytes(new byte[]{9})); @@ -70,7 +70,7 @@ class AdminLeadExportControllerTest { // The service accepts " 77\r\n" (it trims before parsing); none of those characters may reach the header. AdminEventLeadService svc = mock(AdminEventLeadService.class); when(svc.export(org.mockito.ArgumentMatchers.anyString(), org.mockito.ArgumentMatchers.any(), org.mockito.ArgumentMatchers.any(), - org.mockito.ArgumentMatchers.any(), org.mockito.ArgumentMatchers.any(), org.mockito.ArgumentMatchers.any(), org.mockito.ArgumentMatchers.any())).thenReturn(new byte[]{9}); + org.mockito.ArgumentMatchers.any(), org.mockito.ArgumentMatchers.any(), org.mockito.ArgumentMatchers.any(), org.mockito.ArgumentMatchers.any(), org.mockito.ArgumentMatchers.any())).thenReturn(new byte[]{9}); MockMvc mvc = MockMvcBuilders.standaloneSetup(new AdminEventLeadExportController(svc)).build(); mvc.perform(get("/api/admin/event-leads/{uuid}/export", " 77\r\n")).andExpect(status().isOk()) .andExpect(header().string("Content-Disposition", org.hamcrest.Matchers.matchesPattern("attachment; filename=\"event-leads-77-\\d{8}-\\d{4}\\.xlsx\""))); diff --git a/solvway-mobile-api/src/test/java/com/solvway/mobile/api/service/AdminEventLeadExportTest.java b/solvway-mobile-api/src/test/java/com/solvway/mobile/api/service/AdminEventLeadExportTest.java index c0a99db..3f72bce 100644 --- a/solvway-mobile-api/src/test/java/com/solvway/mobile/api/service/AdminEventLeadExportTest.java +++ b/solvway-mobile-api/src/test/java/com/solvway/mobile/api/service/AdminEventLeadExportTest.java @@ -113,6 +113,8 @@ class AdminEventLeadExportTest { r.put("status", "QUALIFIED"); r.put("nextFollowUpDate", "2027-01-20"); r.put("interestIds", List.of(3, 5)); + r.put("attendedAt", i % 2 == 0 ? "2027-01-20T09:30:00" : null); + r.put("source", i % 2 == 0 ? "QR" : "INVITE"); return r; } @@ -127,7 +129,7 @@ class AdminEventLeadExportTest { } private byte[] export() { - return service.export(String.valueOf(EV), 22L, "HOT", null, 3L, null, null); + return service.export(String.valueOf(EV), 22L, "HOT", null, 3L, null, true, null); } private static Sheet sheet(byte[] bytes) throws Exception { @@ -141,7 +143,7 @@ class AdminEventLeadExportTest { assertThat(s.getSheetName()).isEqualTo("Event leads"); List header = new ArrayList<>(); s.getRow(0).forEach(c -> header.add(c.getStringCellValue())); - assertThat(header).containsExactly("Name", "Mobile", "Email", "Lead type", "Salesperson", "Temperature", "Status", "Interests", "Next follow-up"); + assertThat(header).containsExactly("Name", "Mobile", "Email", "Lead type", "Salesperson", "Temperature", "Status", "Interests", "Next follow-up", "Attended", "Source"); assertThat(s.getLastRowNum()).isEqualTo(2); } @@ -163,6 +165,10 @@ class AdminEventLeadExportTest { assertThat(DateUtil.isCellDateFormatted(d)).isTrue(); assertThat(d.getCellStyle().getDataFormatString()).isEqualTo("dd/mm/yyyy"); assertThat(d.getDateCellValue().toInstant().atZone(java.time.ZoneId.systemDefault()).toLocalDate()).isEqualTo(LocalDate.of(2027, 1, 20)); + assertThat(r.getCell(9).getStringCellValue()).isEqualTo("Yes"); + assertThat(r.getCell(10).getStringCellValue()).isEqualTo("QR"); + assertThat(s.getRow(2).getCell(9).getStringCellValue()).isEqualTo("No"); + assertThat(s.getRow(2).getCell(10).getStringCellValue()).isEqualTo("Invite"); } @Test @@ -187,7 +193,7 @@ class AdminEventLeadExportTest { verify(rest, times(3)).postForEntity(eq(SEARCH), any(ParameterizedTypeReference.class), bodies.capture()); for (Object b : bodies.getAllValues()) { assertThat((Map) b).containsEntry("size", 100).containsEntry("eventUuid", EV).containsEntry("salespersonUserId", 22L) - .containsEntry("temperature", "HOT").containsEntry("interestId", 3L); + .containsEntry("temperature", "HOT").containsEntry("interestId", 3L).containsEntry("attended", true); } verify(rest, times(1)).getForEntity(eq(EVENT), any(ParameterizedTypeReference.class), eq(EV)); verify(rest, times(1)).getForEntity(eq(INTERESTS), any(ParameterizedTypeReference.class), eq(COMPANY)); @@ -209,7 +215,7 @@ class AdminEventLeadExportTest { @Test void invalidFilterIsStillRejectedBeforeAnything() { - assertThatThrownBy(() -> service.export(String.valueOf(EV), null, "BOILING", null, null, null, null)) + assertThatThrownBy(() -> service.export(String.valueOf(EV), null, "BOILING", null, null, null, null, null)) .isInstanceOfSatisfying(CodedResponseStatusException.class, e -> assertThat(e.getCode()).isEqualTo("INVALID_VALUE")); } diff --git a/solvway-mobile-api/src/test/java/com/solvway/mobile/api/service/AdminEventLeadServiceCompanyScopeTest.java b/solvway-mobile-api/src/test/java/com/solvway/mobile/api/service/AdminEventLeadServiceCompanyScopeTest.java index be794f7..2099b74 100644 --- a/solvway-mobile-api/src/test/java/com/solvway/mobile/api/service/AdminEventLeadServiceCompanyScopeTest.java +++ b/solvway-mobile-api/src/test/java/com/solvway/mobile/api/service/AdminEventLeadServiceCompanyScopeTest.java @@ -193,8 +193,8 @@ class AdminEventLeadServiceCompanyScopeTest { void list_andExport_otherCompanysEventIsNotFoundForStaff() { caller(5L, false); stubEvent(9L); - assertEventNotFound(() -> service.list(String.valueOf(EV), null, null, null, null, null, 0, 20, null)); - assertEventNotFound(() -> service.export(String.valueOf(EV), null, null, null, null, null, 9L)); + assertEventNotFound(() -> service.list(String.valueOf(EV), null, null, null, null, null, null, 0, 20, null)); + assertEventNotFound(() -> service.export(String.valueOf(EV), null, null, null, null, null, null, 9L)); verify(rest, never()).postForEntity(eq(SEARCH), any(ParameterizedTypeReference.class), any()); } @@ -202,7 +202,7 @@ class AdminEventLeadServiceCompanyScopeTest { void list_staffIgnoresRequestedCompany() { caller(5L, false); stubEvent(5L); - service.list(String.valueOf(EV), null, null, null, null, null, 0, 20, 9L); + service.list(String.valueOf(EV), null, null, null, null, null, null, 0, 20, 9L); assertThat(sentOnce(SEARCH)).containsEntry("companyId", 5L).containsEntry("eventUuid", EV); } @@ -210,15 +210,15 @@ class AdminEventLeadServiceCompanyScopeTest { void list_andExport_superRequestingAnotherCompanyThanTheEventsIsNotFound() { caller(null, true); stubEvent(9L); - assertEventNotFound(() -> service.list(String.valueOf(EV), null, null, null, null, null, 0, 20, 7L)); - assertEventNotFound(() -> service.export(String.valueOf(EV), null, null, null, null, null, 7L)); + assertEventNotFound(() -> service.list(String.valueOf(EV), null, null, null, null, null, null, 0, 20, 7L)); + assertEventNotFound(() -> service.export(String.valueOf(EV), null, null, null, null, null, null, 7L)); } @Test void list_andExport_superWithoutCompanyUsesTheEventsCompany() { caller(null, true); stubEvent(9L); - service.list(String.valueOf(EV), null, null, null, null, null, 0, 20, null); + service.list(String.valueOf(EV), null, null, null, null, null, null, 0, 20, null); assertThat(sentOnce(SEARCH)).containsEntry("companyId", 9L); } @@ -226,7 +226,7 @@ class AdminEventLeadServiceCompanyScopeTest { void export_staffGetsOwnEventsRowsOnly() { caller(5L, false); stubEvent(5L); - service.export(String.valueOf(EV), null, null, null, null, null, 9L); + service.export(String.valueOf(EV), null, null, null, null, null, null, 9L); assertThat(sentOnce(SEARCH)).containsEntry("companyId", 5L); } diff --git a/solvway-mobile-api/src/test/java/com/solvway/mobile/api/service/AdminEventLeadServiceTest.java b/solvway-mobile-api/src/test/java/com/solvway/mobile/api/service/AdminEventLeadServiceTest.java index 472d482..cb5f1fd 100644 --- a/solvway-mobile-api/src/test/java/com/solvway/mobile/api/service/AdminEventLeadServiceTest.java +++ b/solvway-mobile-api/src/test/java/com/solvway/mobile/api/service/AdminEventLeadServiceTest.java @@ -159,6 +159,7 @@ class AdminEventLeadServiceTest { d.put("leadType", "NEW"); d.put("salespersonUserId", salespersonUserId); d.put("status", "OPEN"); + d.put("source", "STAFF"); d.put("interestIds", interestIds); when(rest.getForEntity(eq(FIND), any(ParameterizedTypeReference.class), eq(EL))).thenReturn(ResponseEntity.ok(d)); } @@ -369,7 +370,7 @@ class AdminEventLeadServiceTest { when(rest.getForEntity(eq(EVENT), any(ParameterizedTypeReference.class), eq(1L))).thenReturn(ResponseEntity.ok().build()); assertCoded(() -> service.capture("1", capture(null, "Aisyah", "0123456789", null)), HttpStatus.NOT_FOUND, "EVENT_NOT_FOUND", null); - assertCoded(() -> service.list("not-a-number", null, null, null, null, null, 0, 20, null), HttpStatus.NOT_FOUND, "EVENT_NOT_FOUND", null); + assertCoded(() -> service.list("not-a-number", null, null, null, null, null, null, 0, 20, null), HttpStatus.NOT_FOUND, "EVENT_NOT_FOUND", null); } // --- salesperson from the team --- @@ -485,7 +486,7 @@ class AdminEventLeadServiceTest { when(rest.postForEntity(eq(SEARCH), any(ParameterizedTypeReference.class), any())) .thenReturn(ResponseEntity.ok(Map.of("content", List.of(row), "totalElements", 1, "totalPages", 1, "number", 0, "size", 20))); - Map out = service.list(String.valueOf(EV), 22L, "HOT", "OPEN", 3L, "EXISTING", 0, 20, null); + Map out = service.list(String.valueOf(EV), 22L, "HOT", "OPEN", 3L, "EXISTING", null, 0, 20, null); assertThat(sent(SEARCH)).containsEntry("companyId", COMPANY).containsEntry("eventUuid", EV).containsEntry("salespersonUserId", 22L) .containsEntry("temperature", "HOT").containsEntry("status", "OPEN").containsEntry("interestId", 3L) @@ -516,6 +517,37 @@ class AdminEventLeadServiceTest { assertCoded(() -> service.get(String.valueOf(EV), String.valueOf(EL)), HttpStatus.NOT_FOUND, "EVENT_LEAD_NOT_FOUND", null); } + @Test + void listingSendsAttendedFilterAndRowsShowAttendedAndSource() { + Map yes = new LinkedHashMap<>(); + yes.put("eventLeadUuid", EL); + yes.put("eventUuid", EV); + yes.put("name", "Aisyah"); + yes.put("attendedAt", "2027-01-20T09:30:00"); + yes.put("source", "QR"); + Map no = new LinkedHashMap<>(yes); + no.put("attendedAt", null); + no.put("source", "INVITE"); + when(rest.postForEntity(eq(SEARCH), any(ParameterizedTypeReference.class), any())) + .thenReturn(ResponseEntity.ok(Map.of("content", List.of(yes, no), "totalElements", 2, "totalPages", 1, "number", 0, "size", 20))); + + Map out = service.list(String.valueOf(EV), null, null, null, null, null, true, 0, 20, null); + + assertThat(sent(SEARCH)).containsEntry("attended", true); + List> rows = (List>) out.get("content"); + assertThat(rows.get(0)).containsEntry("attended", true).containsEntry("source", "QR"); + assertThat(rows.get(1)).containsEntry("attended", false).containsEntry("source", "INVITE"); + } + + @Test + void getShowsAttendedAndSource() { + stubStored(EV, 21L, List.of(4L)); + + Map out = service.get(String.valueOf(EV), String.valueOf(EL)); + + assertThat(out).containsEntry("attended", false).containsEntry("source", "STAFF"); + } + @Test void getShowsSalespersonAndInterestsByName() { stubStored(EV, 21L, List.of(4L)); diff --git a/solvway-services-counter/src/main/java/com/solvway/services/counter/dto/EventLeadDetail.java b/solvway-services-counter/src/main/java/com/solvway/services/counter/dto/EventLeadDetail.java index 97ee41d..da5f4fe 100644 --- a/solvway-services-counter/src/main/java/com/solvway/services/counter/dto/EventLeadDetail.java +++ b/solvway-services-counter/src/main/java/com/solvway/services/counter/dto/EventLeadDetail.java @@ -22,5 +22,7 @@ public record EventLeadDetail( String notes, LocalDateTime capturedAt, List interestIds, - Long companyId + Long companyId, + LocalDateTime attendedAt, + String source ) {} diff --git a/solvway-services-counter/src/main/java/com/solvway/services/counter/dto/EventLeadSearchRequest.java b/solvway-services-counter/src/main/java/com/solvway/services/counter/dto/EventLeadSearchRequest.java index 58028ae..e5fff3b 100644 --- a/solvway-services-counter/src/main/java/com/solvway/services/counter/dto/EventLeadSearchRequest.java +++ b/solvway-services-counter/src/main/java/com/solvway/services/counter/dto/EventLeadSearchRequest.java @@ -1,5 +1,5 @@ package com.solvway.services.counter.dto; -/** One event's leads, filtered; null or blank filters are ignored. page is 0-based. companyId null means all companies. */ +/** One event's leads, filtered; null or blank filters are ignored; attended true or false keeps only attended or not attended. page is 0-based. companyId null means all companies. */ public record EventLeadSearchRequest(Long companyId, long eventUuid, Long salespersonUserId, String temperature, - String status, Long interestId, String leadType, int page, int size) {} + String status, Long interestId, String leadType, Boolean attended, int page, int size) {} diff --git a/solvway-services-counter/src/main/java/com/solvway/services/counter/repo/EventLeadRepository.java b/solvway-services-counter/src/main/java/com/solvway/services/counter/repo/EventLeadRepository.java index 3a215e5..e655f1b 100644 --- a/solvway-services-counter/src/main/java/com/solvway/services/counter/repo/EventLeadRepository.java +++ b/solvway-services-counter/src/main/java/com/solvway/services/counter/repo/EventLeadRepository.java @@ -29,6 +29,8 @@ public interface EventLeadRepository extends JpaRepository { + " AND (:temperature IS NULL OR e.temperature = :temperature)" + " AND (:status IS NULL OR e.status = :status)" + " AND (:leadType IS NULL OR e.leadType = :leadType)" + + " AND (:attended IS NULL OR (:attended = true AND e.attendedAt IS NOT NULL)" + + " OR (:attended = false AND e.attendedAt IS NULL))" + " AND (:interestId IS NULL OR EXISTS (SELECT 1 FROM EventLeadInterest i" + " WHERE i.id.eventLeadId = e.id AND i.id.interestId = :interestId))"; @@ -38,7 +40,7 @@ public interface EventLeadRepository extends JpaRepository { Page search(@Param("companyId") Long companyId, @Param("eventUuid") long eventUuid, @Param("salespersonUserId") Long salespersonUserId, @Param("temperature") String temperature, @Param("status") String status, @Param("interestId") Long interestId, - @Param("leadType") String leadType, Pageable pageable); + @Param("leadType") String leadType, @Param("attended") Boolean attended, Pageable pageable); long countByEventUuidAndAttendedAtIsNotNull(long eventUuid); diff --git a/solvway-services-counter/src/main/java/com/solvway/services/counter/service/EventLeadService.java b/solvway-services-counter/src/main/java/com/solvway/services/counter/service/EventLeadService.java index b6e06e7..13a5739 100644 --- a/solvway-services-counter/src/main/java/com/solvway/services/counter/service/EventLeadService.java +++ b/solvway-services-counter/src/main/java/com/solvway/services/counter/service/EventLeadService.java @@ -163,7 +163,7 @@ public class EventLeadService { Pageable pageable = PageRequest.of(Math.max(req.page(), 0), Math.min(Math.max(req.size(), 1), MAX_PAGE_SIZE), Sort.by(Sort.Order.desc("capturedAt"), Sort.Order.desc("id"))); Page rows = eventLeadRepository.search(req.companyId(), req.eventUuid(), req.salespersonUserId(), - blankToNull(req.temperature()), blankToNull(req.status()), req.interestId(), blankToNull(req.leadType()), pageable); + blankToNull(req.temperature()), blankToNull(req.status()), req.interestId(), blankToNull(req.leadType()), req.attended(), pageable); if (rows.isEmpty()) return rows.map(r -> null); Map> interests = eventLeadInterestRepository .findByIdEventLeadIdIn(rows.map(r -> ((EventLead) r[0]).getId()).getContent()).stream() @@ -235,7 +235,8 @@ public class EventLeadService { private static EventLeadDetail detail(EventLead e, Lead l, List interestIds) { return new EventLeadDetail(e.getUuid(), e.getEventUuid(), l.getUuid(), l.getName(), l.getMobileE164(), l.getEmailNormalised(), l.getCompanyName(), l.getJobTitle(), e.getLeadType(), e.getSalespersonUserId(), - e.getTemperature(), e.getStatus(), e.getNextFollowUpDate(), e.getNotes(), e.getCapturedAt(), interestIds, e.getCompanyId()); + e.getTemperature(), e.getStatus(), e.getNextFollowUpDate(), e.getNotes(), e.getCapturedAt(), interestIds, e.getCompanyId(), + e.getAttendedAt(), e.getSource()); } private static void fillBlank(String current, String offered, Consumer setter) { diff --git a/solvway-services-counter/src/test/java/com/solvway/services/counter/controller/EventLeadControllerTest.java b/solvway-services-counter/src/test/java/com/solvway/services/counter/controller/EventLeadControllerTest.java index 841add4..249f3f3 100644 --- a/solvway-services-counter/src/test/java/com/solvway/services/counter/controller/EventLeadControllerTest.java +++ b/solvway-services-counter/src/test/java/com/solvway/services/counter/controller/EventLeadControllerTest.java @@ -53,7 +53,7 @@ class EventLeadControllerTest { @Test void findByEventDelegates() { - EventLeadSearchRequest req = new EventLeadSearchRequest(42L, 1L, null, null, null, null, null, 0, 20); + EventLeadSearchRequest req = new EventLeadSearchRequest(42L, 1L, null, null, null, null, null, null, 0, 20); Page out = Page.empty(); when(service.search(req)).thenReturn(out); diff --git a/solvway-services-counter/src/test/java/com/solvway/services/counter/service/EventLeadListAndUpdateTest.java b/solvway-services-counter/src/test/java/com/solvway/services/counter/service/EventLeadListAndUpdateTest.java index e278ea7..b480840 100644 --- a/solvway-services-counter/src/test/java/com/solvway/services/counter/service/EventLeadListAndUpdateTest.java +++ b/solvway-services-counter/src/test/java/com/solvway/services/counter/service/EventLeadListAndUpdateTest.java @@ -34,6 +34,7 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyCollection; +import static org.mockito.ArgumentMatchers.anyLong; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.inOrder; import static org.mockito.Mockito.never; @@ -61,13 +62,13 @@ class EventLeadListAndUpdateTest { @Test void listingPassesEveryFilterAndPagesNewestCaptureFirstWithIdTieBreak() { - when(eventLeadRepository.search(eq(COMPANY), eq(EVENT), eq(11L), eq("HOT"), eq("OPEN"), eq(3L), eq("NEW"), any())) + when(eventLeadRepository.search(eq(COMPANY), eq(EVENT), eq(11L), eq("HOT"), eq("OPEN"), eq(3L), eq("NEW"), eq(null), any())) .thenReturn(Page.empty()); - service.search(new EventLeadSearchRequest(COMPANY, EVENT, 11L, "HOT", "OPEN", 3L, "NEW", 2, 25)); + service.search(new EventLeadSearchRequest(COMPANY, EVENT, 11L, "HOT", "OPEN", 3L, "NEW", null, 2, 25)); ArgumentCaptor pageable = ArgumentCaptor.forClass(Pageable.class); - verify(eventLeadRepository).search(eq(COMPANY), eq(EVENT), eq(11L), eq("HOT"), eq("OPEN"), eq(3L), eq("NEW"), pageable.capture()); + verify(eventLeadRepository).search(eq(COMPANY), eq(EVENT), eq(11L), eq("HOT"), eq("OPEN"), eq(3L), eq("NEW"), eq(null), pageable.capture()); assertThat(pageable.getValue().getPageNumber()).isEqualTo(2); assertThat(pageable.getValue().getPageSize()).isEqualTo(25); assertThat(pageable.getValue().getSort().toString()).isEqualTo("capturedAt: DESC,id: DESC"); @@ -75,13 +76,13 @@ class EventLeadListAndUpdateTest { @Test void blankFiltersAreIgnoredAndPageSizeIsBounded() { - when(eventLeadRepository.search(eq(COMPANY), eq(EVENT), eq(null), eq(null), eq(null), eq(null), eq(null), any())) + when(eventLeadRepository.search(eq(COMPANY), eq(EVENT), eq(null), eq(null), eq(null), eq(null), eq(null), eq(null), any())) .thenReturn(Page.empty()); - service.search(new EventLeadSearchRequest(COMPANY, EVENT, null, " ", "", null, null, -1, 1000)); + service.search(new EventLeadSearchRequest(COMPANY, EVENT, null, " ", "", null, null, null, -1, 1000)); ArgumentCaptor pageable = ArgumentCaptor.forClass(Pageable.class); - verify(eventLeadRepository).search(eq(COMPANY), eq(EVENT), eq(null), eq(null), eq(null), eq(null), eq(null), pageable.capture()); + verify(eventLeadRepository).search(eq(COMPANY), eq(EVENT), eq(null), eq(null), eq(null), eq(null), eq(null), eq(null), pageable.capture()); assertThat(pageable.getValue().getPageNumber()).isZero(); assertThat(pageable.getValue().getPageSize()).isEqualTo(100); } @@ -92,12 +93,12 @@ class EventLeadListAndUpdateTest { EventLead b = eventLead(2L, 502L); Lead la = lead(501L, "Aisyah"); Lead lb = lead(502L, "Ahmad"); - when(eventLeadRepository.search(eq(COMPANY), eq(EVENT), any(), any(), any(), any(), any(), any())) + when(eventLeadRepository.search(eq(COMPANY), eq(EVENT), any(), any(), any(), any(), any(), any(), any())) .thenReturn(new PageImpl<>(List.of(new Object[]{a, la}, new Object[]{b, lb}), PageRequest.of(0, 20), 2)); when(eventLeadInterestRepository.findByIdEventLeadIdIn(anyCollection())) .thenReturn(List.of(row(1L, 3L), row(1L, 4L), row(2L, 3L))); - Page page = service.search(new EventLeadSearchRequest(COMPANY, EVENT, null, null, null, null, null, 0, 20)); + Page page = service.search(new EventLeadSearchRequest(COMPANY, EVENT, null, null, null, null, null, null, 0, 20)); assertThat(page.getTotalElements()).isEqualTo(2); assertThat(page.getContent()).extracting(EventLeadDetail::name).containsExactly("Aisyah", "Ahmad"); @@ -107,10 +108,56 @@ class EventLeadListAndUpdateTest { } @Test - void emptyPageSkipsTheInterestQuery() { - when(eventLeadRepository.search(eq(COMPANY), eq(EVENT), any(), any(), any(), any(), any(), any())).thenReturn(Page.empty()); + void attendedFilterIsPassedToTheQuery() { + when(eventLeadRepository.search(any(), anyLong(), any(), any(), any(), any(), any(), any(), any())).thenReturn(Page.empty()); - assertThat(service.search(new EventLeadSearchRequest(COMPANY, EVENT, null, null, null, null, null, 0, 20)).getContent()).isEmpty(); + service.search(new EventLeadSearchRequest(COMPANY, EVENT, null, null, null, null, null, true, 0, 20)); + + verify(eventLeadRepository).search(eq(COMPANY), eq(EVENT), eq(null), eq(null), eq(null), eq(null), eq(null), eq(true), any()); + } + + @Test + void notAttendedFilterIsPassedToTheQuery() { + when(eventLeadRepository.search(any(), anyLong(), any(), any(), any(), any(), any(), any(), any())).thenReturn(Page.empty()); + + service.search(new EventLeadSearchRequest(COMPANY, EVENT, null, null, null, null, null, false, 0, 20)); + + verify(eventLeadRepository).search(eq(COMPANY), eq(EVENT), eq(null), eq(null), eq(null), eq(null), eq(null), eq(false), any()); + } + + @Test + void listingRowsReportAttendanceAndSource() { + EventLead a = eventLead(1L, 501L); + a.setAttendedAt(java.time.LocalDateTime.of(2027, 1, 20, 9, 30)); + a.setSource("INVITE"); + when(eventLeadRepository.search(any(), anyLong(), any(), any(), any(), any(), any(), any(), any())) + .thenReturn(new PageImpl<>(List.of(new Object[]{a, lead(501L, "Aisyah")}), PageRequest.of(0, 20), 1)); + when(eventLeadInterestRepository.findByIdEventLeadIdIn(anyCollection())).thenReturn(List.of()); + + EventLeadDetail d = service.search(new EventLeadSearchRequest(COMPANY, EVENT, null, null, null, null, null, null, 0, 20)).getContent().get(0); + + assertThat(d.attendedAt()).isEqualTo(java.time.LocalDateTime.of(2027, 1, 20, 9, 30)); + assertThat(d.source()).isEqualTo("INVITE"); + } + + @Test + void detailReportsNoAttendanceAndDefaultSource() { + EventLead e = eventLead(1L, 501L); + when(eventLeadRepository.findByUuidAndCompanyId(7L, COMPANY)).thenReturn(java.util.Optional.of(e)); + when(leadRepository.findByUuidAndCompanyId(501L, COMPANY)).thenReturn(java.util.Optional.of(lead(501L, "Aisyah"))); + when(eventLeadInterestRepository.findByIdEventLeadId(1L)).thenReturn(List.of()); + + EventLeadDetail d = service.findByUuid(COMPANY, 7L); + + assertThat(d.attendedAt()).isNull(); + assertThat(d.source()).isEqualTo("STAFF"); + } + + @Test + void emptyPageSkipsTheInterestQuery() { + when(eventLeadRepository.search(eq(COMPANY), eq(EVENT), any(), any(), any(), any(), any(), any(), any())).thenReturn(Page.empty()); + + assertThat(service.search(new EventLeadSearchRequest(COMPANY, EVENT, null, null, null, null, null, null, 0, 20)).getContent()).isEmpty(); verify(eventLeadInterestRepository, never()).findByIdEventLeadIdIn(anyCollection()); } diff --git a/solvway-services-counter/src/test/java/com/solvway/services/counter/service/LeadCompanyScopeTest.java b/solvway-services-counter/src/test/java/com/solvway/services/counter/service/LeadCompanyScopeTest.java index dcd72cd..6f34afe 100644 --- a/solvway-services-counter/src/test/java/com/solvway/services/counter/service/LeadCompanyScopeTest.java +++ b/solvway-services-counter/src/test/java/com/solvway/services/counter/service/LeadCompanyScopeTest.java @@ -96,18 +96,18 @@ class LeadCompanyScopeTest { @Test void eventLeadSearchWithoutCompanyPassesNullMeaningAll() { - when(eventLeadRepository.search(any(), anyLong(), any(), any(), any(), any(), any(), any(Pageable.class))) + when(eventLeadRepository.search(any(), anyLong(), any(), any(), any(), any(), any(), any(), any(Pageable.class))) .thenReturn(Page.empty()); - eventLeadService.search(new EventLeadSearchRequest(null, 9L, null, null, null, null, null, 0, 20)); - verify(eventLeadRepository).search(eq(null), eq(9L), any(), any(), any(), any(), any(), any(Pageable.class)); + eventLeadService.search(new EventLeadSearchRequest(null, 9L, null, null, null, null, null, null, 0, 20)); + verify(eventLeadRepository).search(eq(null), eq(9L), any(), any(), any(), any(), any(), any(), any(Pageable.class)); } @Test void eventLeadSearchWithCompanyStillFilters() { - when(eventLeadRepository.search(any(), anyLong(), any(), any(), any(), any(), any(), any(Pageable.class))) + when(eventLeadRepository.search(any(), anyLong(), any(), any(), any(), any(), any(), any(), any(Pageable.class))) .thenReturn(Page.empty()); - eventLeadService.search(new EventLeadSearchRequest(COMPANY, 9L, null, null, null, null, null, 0, 20)); - verify(eventLeadRepository).search(eq(COMPANY), eq(9L), any(), any(), any(), any(), any(), any(Pageable.class)); + eventLeadService.search(new EventLeadSearchRequest(COMPANY, 9L, null, null, null, null, null, null, 0, 20)); + verify(eventLeadRepository).search(eq(COMPANY), eq(9L), any(), any(), any(), any(), any(), any(), any(Pageable.class)); } // --- detail ---