feat: lead listing, lead detail and xlsx export (change 4b) #9
No files matched your search
@@ -15,7 +15,7 @@ Design: `design.md` (decision numbers below refer to it). All tasks run serially
|
||||
|
||||
## 2. services-counter
|
||||
|
||||
- [ ] 2.1 Add lead search, detail and update (design decisions 2 and 3):
|
||||
- [x] 2.1 Add lead search, detail and update (design decisions 2 and 3):
|
||||
- `POST /counter/lead/search`: the native query with `cnt`, `last` and `sp`.
|
||||
- `GET /counter/lead/detail/{companyId}/{leadUuid}`: the lead with its history and shared contacts.
|
||||
- `POST /counter/lead/update`: `@Transactional`.
|
||||
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
package com.solvway.services.counter.controller;
|
||||
|
||||
import com.solvway.services.counter.dto.*;
|
||||
import com.solvway.services.counter.service.LeadService;
|
||||
import lombok.AllArgsConstructor;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/counter")
|
||||
@AllArgsConstructor
|
||||
public class LeadController {
|
||||
private final LeadService leadService;
|
||||
|
||||
@PostMapping("/lead/search")
|
||||
public Page<LeadSummary> search(@RequestBody LeadSearchRequest req) { return leadService.search(req); }
|
||||
|
||||
@GetMapping("/lead/detail/{companyId}/{leadUuid}")
|
||||
public LeadDetail detail(@PathVariable("companyId") long companyId, @PathVariable("leadUuid") long leadUuid) {
|
||||
return leadService.detail(companyId, leadUuid);
|
||||
}
|
||||
|
||||
@PostMapping("/lead/update")
|
||||
public LeadDetail update(@RequestBody LeadUpdateRequest req) { return leadService.update(req); }
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
package com.solvway.services.counter.dto;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
|
||||
public record LeadDetail(long leadUuid, String name, String mobileE164, String email, String companyName,
|
||||
String jobTitle, String notes, LocalDateTime createdAt, LocalDateTime updatedAt,
|
||||
String updatedBy, List<LeadHistoryEntry> history, List<SharedContact> sharedContacts) {}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
package com.solvway.services.counter.dto;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
/** One capture of the lead at an event; event fields are null when the event no longer exists. */
|
||||
public record LeadHistoryEntry(long eventLeadUuid, long eventUuid, String eventName, LocalDateTime eventStart,
|
||||
LocalDateTime eventEnd, String leadType, Long salespersonUserId, String temperature,
|
||||
String status, LocalDateTime capturedAt) {}
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
package com.solvway.services.counter.dto;
|
||||
|
||||
/** q is free text (name or email); mobileDigits is digits only. Both optional. */
|
||||
public record LeadSearchRequest(long companyId, String q, String mobileDigits, int page, int size) {}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
package com.solvway.services.counter.dto;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
public record LeadSummary(long leadUuid, String name, String mobileE164, String email, String companyName,
|
||||
String jobTitle, long eventCount, Long lastEventUuid, String lastEventName,
|
||||
LocalDateTime lastCapturedAt, Long latestSalespersonUserId) {}
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
package com.solvway.services.counter.dto;
|
||||
|
||||
public record LeadUpdateRequest(long companyId, long leadUuid, String name, String mobileE164, String email,
|
||||
String companyName, String jobTitle, String notes, String doneBy) {}
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
package com.solvway.services.counter.dto;
|
||||
|
||||
/** Another lead sharing this lead's mobile and/or email. */
|
||||
public record SharedContact(long leadUuid, String name, boolean sameMobile, boolean sameEmail) {}
|
||||
+5
@@ -37,4 +37,9 @@ public interface EventLeadRepository extends JpaRepository<EventLead, Long> {
|
||||
@Param("salespersonUserId") Long salespersonUserId, @Param("temperature") String temperature,
|
||||
@Param("status") String status, @Param("interestId") Long interestId,
|
||||
@Param("leadType") String leadType, Pageable pageable);
|
||||
|
||||
/** Rows are [EventLead, Event]; Event is null when the event row is gone. Newest capture first. */
|
||||
@Query("SELECT e, ev FROM EventLead e LEFT JOIN Event ev ON ev.uuid = e.eventUuid"
|
||||
+ " WHERE e.companyId = :companyId AND e.leadUuid = :leadUuid ORDER BY e.capturedAt DESC, e.id DESC")
|
||||
List<Object[]> history(@Param("companyId") long companyId, @Param("leadUuid") long leadUuid);
|
||||
}
|
||||
+43
@@ -1,6 +1,7 @@
|
||||
package com.solvway.services.counter.repo;
|
||||
|
||||
import com.solvway.services.counter.model.Lead;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
@@ -19,4 +20,46 @@ public interface LeadRepository extends JpaRepository<Lead, Long> {
|
||||
@Param("email") String email, Pageable pageable);
|
||||
|
||||
Optional<Lead> findByUuidAndCompanyId(long uuid, long companyId);
|
||||
|
||||
interface SummaryRow {
|
||||
long getLeadUuid();
|
||||
String getName();
|
||||
String getMobileE164();
|
||||
String getEmail();
|
||||
String getCompanyName();
|
||||
String getJobTitle();
|
||||
long getEventCount();
|
||||
Long getLastEventUuid();
|
||||
String getLastEventName();
|
||||
java.time.LocalDateTime getLastCapturedAt();
|
||||
Long getLatestSalespersonUserId();
|
||||
}
|
||||
|
||||
/** The search condition uses only `lead` columns, so the count skips the aggregate joins. */
|
||||
String SEARCH_WHERE = " WHERE l.company_id = :companyId AND ((:q IS NULL AND :digits IS NULL) OR LOWER(l.name) LIKE :q ESCAPE '\\\\'"
|
||||
+ " OR l.email_normalised LIKE :q ESCAPE '\\\\' OR (:digits IS NOT NULL AND l.mobile_e164 LIKE :digits))";
|
||||
|
||||
String SEARCH_FROM = " FROM `lead` l"
|
||||
+ " LEFT JOIN (SELECT lead_uuid, COUNT(*) cnt FROM event_lead WHERE company_id = :companyId GROUP BY lead_uuid) c ON c.lead_uuid = l.uuid"
|
||||
+ " LEFT JOIN (SELECT lead_uuid, event_uuid, captured_at, ROW_NUMBER() OVER (PARTITION BY lead_uuid ORDER BY captured_at DESC, id DESC) rn"
|
||||
+ " FROM event_lead WHERE company_id = :companyId) last ON last.lead_uuid = l.uuid AND last.rn = 1"
|
||||
+ " LEFT JOIN `event` ev ON ev.uuid = last.event_uuid"
|
||||
+ " LEFT JOIN (SELECT lead_uuid, salesperson_user_id, ROW_NUMBER() OVER (PARTITION BY lead_uuid ORDER BY captured_at DESC, id DESC) rn"
|
||||
+ " FROM event_lead WHERE company_id = :companyId AND salesperson_user_id IS NOT NULL) sp ON sp.lead_uuid = l.uuid AND sp.rn = 1"
|
||||
+ SEARCH_WHERE;
|
||||
|
||||
/**
|
||||
* Returns one row per lead with columns lead_uuid AS leadUuid, name, mobile_e164 AS mobileE164, email_normalised AS email,
|
||||
* company_name AS companyName, job_title AS jobTitle, eventCount, lastEventUuid, lastEventName, lastCapturedAt and
|
||||
* latestSalespersonUserId. Native because JPQL has no window functions. Never-captured leads sort last.
|
||||
*/
|
||||
@Query(nativeQuery = true,
|
||||
value = "SELECT l.uuid AS leadUuid, l.name AS name, l.mobile_e164 AS mobileE164, l.email_normalised AS email,"
|
||||
+ " l.company_name AS companyName, l.job_title AS jobTitle, COALESCE(c.cnt, 0) AS eventCount,"
|
||||
+ " last.event_uuid AS lastEventUuid, ev.name AS lastEventName, last.captured_at AS lastCapturedAt,"
|
||||
+ " sp.salesperson_user_id AS latestSalespersonUserId" + SEARCH_FROM
|
||||
+ " ORDER BY last.captured_at IS NULL, last.captured_at DESC, l.id DESC",
|
||||
countQuery = "SELECT COUNT(*) FROM `lead` l" + SEARCH_WHERE)
|
||||
Page<SummaryRow> search(@Param("companyId") long companyId, @Param("q") String q,
|
||||
@Param("digits") String digits, Pageable pageable);
|
||||
}
|
||||
+90
@@ -0,0 +1,90 @@
|
||||
package com.solvway.services.counter.service;
|
||||
|
||||
import com.solvway.services.counter.dto.LeadDetail;
|
||||
import com.solvway.services.counter.dto.LeadHistoryEntry;
|
||||
import com.solvway.services.counter.dto.LeadSearchRequest;
|
||||
import com.solvway.services.counter.dto.LeadSummary;
|
||||
import com.solvway.services.counter.dto.LeadUpdateRequest;
|
||||
import com.solvway.services.counter.dto.SharedContact;
|
||||
import com.solvway.services.counter.model.Event;
|
||||
import com.solvway.services.counter.model.EventLead;
|
||||
import com.solvway.services.counter.model.Lead;
|
||||
import com.solvway.services.counter.repo.EventLeadRepository;
|
||||
import com.solvway.services.counter.repo.LeadRepository;
|
||||
import lombok.AllArgsConstructor;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.web.server.ResponseStatusException;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
/** Lead directory: search, detail with event history and shared contacts, and edit. Never log lead PII. */
|
||||
@Service
|
||||
@AllArgsConstructor
|
||||
public class LeadService {
|
||||
private static final int MAX_SHARED = 20;
|
||||
private static final int MAX_PAGE_SIZE = 100;
|
||||
|
||||
private final LeadRepository leadRepository;
|
||||
private final EventLeadRepository eventLeadRepository;
|
||||
|
||||
public Page<LeadSummary> search(LeadSearchRequest req) {
|
||||
String q = req.q() == null || req.q().isBlank() ? null
|
||||
: "%" + req.q().trim().toLowerCase().replace("\\", "\\\\").replace("%", "\\%").replace("_", "\\_") + "%";
|
||||
String digits = req.mobileDigits() == null || req.mobileDigits().isBlank() ? null : "%" + req.mobileDigits().trim() + "%";
|
||||
int size = req.size() < 1 ? 20 : Math.min(req.size(), MAX_PAGE_SIZE);
|
||||
return leadRepository.search(req.companyId(), q, digits, PageRequest.of(Math.max(req.page(), 0), size))
|
||||
.map(r -> new LeadSummary(r.getLeadUuid(), r.getName(), r.getMobileE164(), r.getEmail(), r.getCompanyName(),
|
||||
r.getJobTitle(), r.getEventCount(), r.getLastEventUuid(), r.getLastEventName(),
|
||||
r.getLastCapturedAt(), r.getLatestSalespersonUserId()));
|
||||
}
|
||||
|
||||
public LeadDetail detail(long companyId, long leadUuid) {
|
||||
return detail(find(companyId, leadUuid));
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public LeadDetail update(LeadUpdateRequest req) {
|
||||
Lead lead = find(req.companyId(), req.leadUuid());
|
||||
lead.setName(req.name());
|
||||
lead.setMobileE164(req.mobileE164());
|
||||
lead.setEmailNormalised(req.email());
|
||||
lead.setCompanyName(req.companyName());
|
||||
lead.setJobTitle(req.jobTitle());
|
||||
lead.setNotes(req.notes());
|
||||
lead.setUpdatedBy(req.doneBy());
|
||||
lead.setUpdatedAt(LocalDateTime.now());
|
||||
return detail(leadRepository.saveAndFlush(lead));
|
||||
}
|
||||
|
||||
private Lead find(long companyId, long leadUuid) {
|
||||
return leadRepository.findByUuidAndCompanyId(leadUuid, companyId)
|
||||
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "LEAD_NOT_FOUND"));
|
||||
}
|
||||
|
||||
private LeadDetail detail(Lead l) {
|
||||
List<LeadHistoryEntry> history = eventLeadRepository.history(l.getCompanyId(), l.getUuid()).stream().map(r -> {
|
||||
EventLead e = (EventLead) r[0];
|
||||
Event ev = (Event) r[1];
|
||||
return new LeadHistoryEntry(e.getUuid(), e.getEventUuid(), ev == null ? null : ev.getName(),
|
||||
ev == null ? null : ev.getDateStart(), ev == null ? null : ev.getDateEnd(), e.getLeadType(),
|
||||
e.getSalespersonUserId(), e.getTemperature(), e.getStatus(), e.getCapturedAt());
|
||||
}).toList();
|
||||
// One extra row so the cap still holds after the lead itself is filtered out.
|
||||
List<SharedContact> shared = leadRepository.findMatches(l.getCompanyId(), l.getMobileE164(), l.getEmailNormalised(),
|
||||
PageRequest.of(0, MAX_SHARED + 1)).stream()
|
||||
.filter(o -> o.getUuid() != l.getUuid())
|
||||
.limit(MAX_SHARED)
|
||||
.map(o -> new SharedContact(o.getUuid(), o.getName(),
|
||||
l.getMobileE164() != null && Objects.equals(l.getMobileE164(), o.getMobileE164()),
|
||||
l.getEmailNormalised() != null && Objects.equals(l.getEmailNormalised(), o.getEmailNormalised())))
|
||||
.toList();
|
||||
return new LeadDetail(l.getUuid(), l.getName(), l.getMobileE164(), l.getEmailNormalised(), l.getCompanyName(),
|
||||
l.getJobTitle(), l.getNotes(), l.getCreatedAt(), l.getUpdatedAt(), l.getUpdatedBy(), history, shared);
|
||||
}
|
||||
}
|
||||
+49
@@ -0,0 +1,49 @@
|
||||
package com.solvway.services.counter.controller;
|
||||
|
||||
import com.solvway.services.counter.dto.LeadDetail;
|
||||
import com.solvway.services.counter.dto.LeadSearchRequest;
|
||||
import com.solvway.services.counter.dto.LeadSummary;
|
||||
import com.solvway.services.counter.dto.LeadUpdateRequest;
|
||||
import com.solvway.services.counter.service.LeadService;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
import org.springframework.data.domain.Page;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/** LeadController delegates to the service and returns its result. */
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class LeadControllerTest {
|
||||
|
||||
@Mock private LeadService service;
|
||||
|
||||
@Test
|
||||
void searchDelegates() {
|
||||
LeadSearchRequest req = new LeadSearchRequest(42L, "a", null, 0, 20);
|
||||
Page<LeadSummary> out = Page.empty();
|
||||
when(service.search(req)).thenReturn(out);
|
||||
|
||||
assertThat(new LeadController(service).search(req)).isSameAs(out);
|
||||
}
|
||||
|
||||
@Test
|
||||
void detailDelegates() {
|
||||
LeadDetail out = mock(LeadDetail.class);
|
||||
when(service.detail(42L, 7L)).thenReturn(out);
|
||||
|
||||
assertThat(new LeadController(service).detail(42L, 7L)).isSameAs(out);
|
||||
}
|
||||
|
||||
@Test
|
||||
void updateDelegates() {
|
||||
LeadUpdateRequest req = mock(LeadUpdateRequest.class);
|
||||
LeadDetail out = mock(LeadDetail.class);
|
||||
when(service.update(req)).thenReturn(out);
|
||||
|
||||
assertThat(new LeadController(service).update(req)).isSameAs(out);
|
||||
}
|
||||
}
|
||||
+214
@@ -0,0 +1,214 @@
|
||||
package com.solvway.services.counter.service;
|
||||
|
||||
import com.solvway.services.counter.dto.LeadDetail;
|
||||
import com.solvway.services.counter.dto.LeadSearchRequest;
|
||||
import com.solvway.services.counter.dto.LeadSummary;
|
||||
import com.solvway.services.counter.dto.LeadUpdateRequest;
|
||||
import com.solvway.services.counter.model.Event;
|
||||
import com.solvway.services.counter.model.EventLead;
|
||||
import com.solvway.services.counter.model.Lead;
|
||||
import com.solvway.services.counter.repo.EventLeadRepository;
|
||||
import com.solvway.services.counter.repo.LeadRepository;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.PageImpl;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.web.server.ResponseStatusException;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
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.eq;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/** LeadService: lead search, detail and update. */
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class LeadServiceTest {
|
||||
|
||||
private static final long COMPANY = 42L;
|
||||
|
||||
@Mock private LeadRepository leadRepository;
|
||||
@Mock private EventLeadRepository eventLeadRepository;
|
||||
|
||||
private LeadService service;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
service = new LeadService(leadRepository, eventLeadRepository);
|
||||
}
|
||||
|
||||
private Pageable search(String q, String digits, int page, int size) {
|
||||
when(leadRepository.search(eq(COMPANY), any(), any(), any(Pageable.class))).thenReturn(Page.empty());
|
||||
service.search(new LeadSearchRequest(COMPANY, q, digits, page, size));
|
||||
ArgumentCaptor<Pageable> p = ArgumentCaptor.forClass(Pageable.class);
|
||||
verify(leadRepository).search(eq(COMPANY), any(), any(), p.capture());
|
||||
return p.getValue();
|
||||
}
|
||||
|
||||
private String[] searchArgs(String q, String digits) {
|
||||
when(leadRepository.search(eq(COMPANY), any(), any(), any(Pageable.class))).thenReturn(Page.empty());
|
||||
service.search(new LeadSearchRequest(COMPANY, q, digits, 0, 20));
|
||||
ArgumentCaptor<String> qc = ArgumentCaptor.forClass(String.class);
|
||||
ArgumentCaptor<String> dc = ArgumentCaptor.forClass(String.class);
|
||||
verify(leadRepository).search(eq(COMPANY), qc.capture(), dc.capture(), any(Pageable.class));
|
||||
return new String[]{qc.getValue(), dc.getValue()};
|
||||
}
|
||||
|
||||
@Test
|
||||
void pageAndSizeAreClamped() {
|
||||
Pageable p = search("a", null, -3, 1000);
|
||||
assertThat(p.getPageNumber()).isZero();
|
||||
assertThat(p.getPageSize()).isEqualTo(100);
|
||||
}
|
||||
|
||||
@Test
|
||||
void sizeBelowOneDefaultsToTwenty() {
|
||||
assertThat(search("a", null, 2, 0).getPageSize()).isEqualTo(20);
|
||||
}
|
||||
|
||||
@Test
|
||||
void blankQueryAndDigitsBecomeNull() {
|
||||
assertThat(searchArgs(" ", " ")).containsExactly(null, null);
|
||||
}
|
||||
|
||||
@Test
|
||||
void queryIsTrimmedLowercasedAndWrapped() {
|
||||
assertThat(searchArgs(" Aisyah ", null)[0]).isEqualTo("%aisyah%");
|
||||
}
|
||||
|
||||
@Test
|
||||
void likeWildcardsAndBackslashAreEscaped() {
|
||||
assertThat(searchArgs("50%_a\\B", null)[0]).isEqualTo("%50\\%\\_a\\\\b%");
|
||||
}
|
||||
|
||||
@Test
|
||||
void digitsArePassedAsContains() {
|
||||
assertThat(searchArgs("a", "12345")[1]).isEqualTo("%12345%");
|
||||
}
|
||||
|
||||
@Test
|
||||
void searchMapsProjectionRows() {
|
||||
LeadRepository.SummaryRow row = mock(LeadRepository.SummaryRow.class);
|
||||
LocalDateTime at = LocalDateTime.of(2026, 1, 2, 3, 4);
|
||||
when(row.getLeadUuid()).thenReturn(7L);
|
||||
when(row.getName()).thenReturn("Aisyah");
|
||||
when(row.getEventCount()).thenReturn(3L);
|
||||
when(row.getLastEventUuid()).thenReturn(9L);
|
||||
when(row.getLastEventName()).thenReturn("Expo");
|
||||
when(row.getLastCapturedAt()).thenReturn(at);
|
||||
when(row.getLatestSalespersonUserId()).thenReturn(11L);
|
||||
when(leadRepository.search(eq(COMPANY), any(), any(), any(Pageable.class))).thenReturn(new PageImpl<>(List.of(row)));
|
||||
|
||||
LeadSummary s = service.search(new LeadSearchRequest(COMPANY, null, null, 0, 20)).getContent().get(0);
|
||||
|
||||
assertThat(s).isEqualTo(new LeadSummary(7L, "Aisyah", null, null, null, null, 3L, 9L, "Expo", at, 11L));
|
||||
}
|
||||
|
||||
@Test
|
||||
void detailIs404ForUnknownOrOtherCompanyLead() {
|
||||
when(leadRepository.findByUuidAndCompanyId(7L, COMPANY)).thenReturn(Optional.empty());
|
||||
|
||||
assertThatThrownBy(() -> service.detail(COMPANY, 7L))
|
||||
.isInstanceOfSatisfying(ResponseStatusException.class, e -> {
|
||||
assertThat(e.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND);
|
||||
assertThat(e.getReason()).isEqualTo("LEAD_NOT_FOUND");
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void detailListsHistoryWithOrWithoutEventAndSharedContactsExcludingSelf() {
|
||||
Lead me = lead(7L, "Me", "+601", "me@x.com");
|
||||
when(leadRepository.findByUuidAndCompanyId(7L, COMPANY)).thenReturn(Optional.of(me));
|
||||
EventLead el = new EventLead();
|
||||
el.setUuid(70L);
|
||||
el.setEventUuid(1000L);
|
||||
el.setLeadType("NEW");
|
||||
Event ev = new Event();
|
||||
ev.setName("Expo");
|
||||
EventLead orphan = new EventLead();
|
||||
orphan.setUuid(71L);
|
||||
orphan.setEventUuid(2000L);
|
||||
when(eventLeadRepository.history(COMPANY, 7L)).thenReturn(List.of(new Object[]{el, ev}, new Object[]{orphan, null}));
|
||||
when(leadRepository.findMatches(eq(COMPANY), eq("+601"), eq("me@x.com"), any(Pageable.class))).thenReturn(List.of(
|
||||
me, lead(8L, "SameMobile", "+601", "other@x.com"), lead(9L, "SameEmail", "+602", "me@x.com"),
|
||||
lead(10L, "Both", "+601", "me@x.com")));
|
||||
|
||||
LeadDetail d = service.detail(COMPANY, 7L);
|
||||
|
||||
assertThat(d.history()).extracting("eventLeadUuid", "eventName").containsExactly(
|
||||
org.assertj.core.groups.Tuple.tuple(70L, "Expo"), org.assertj.core.groups.Tuple.tuple(71L, null));
|
||||
assertThat(d.sharedContacts()).extracting("leadUuid", "sameMobile", "sameEmail").containsExactly(
|
||||
org.assertj.core.groups.Tuple.tuple(8L, true, false), org.assertj.core.groups.Tuple.tuple(9L, false, true),
|
||||
org.assertj.core.groups.Tuple.tuple(10L, true, true));
|
||||
}
|
||||
|
||||
@Test
|
||||
void sharedContactsAreCappedAtTwenty() {
|
||||
Lead me = lead(7L, "Me", "+601", null);
|
||||
when(leadRepository.findByUuidAndCompanyId(7L, COMPANY)).thenReturn(Optional.of(me));
|
||||
when(eventLeadRepository.history(COMPANY, 7L)).thenReturn(List.of());
|
||||
when(leadRepository.findMatches(eq(COMPANY), eq("+601"), eq(null), any(Pageable.class))).thenReturn(
|
||||
java.util.stream.LongStream.range(100, 125).mapToObj(i -> lead(i, "n", "+601", null)).toList());
|
||||
|
||||
assertThat(service.detail(COMPANY, 7L).sharedContacts()).hasSize(20);
|
||||
}
|
||||
|
||||
@Test
|
||||
void updateSetsSixFieldsAndAudit() {
|
||||
Lead me = lead(7L, "Old", "+601", "old@x.com");
|
||||
when(leadRepository.findByUuidAndCompanyId(7L, COMPANY)).thenReturn(Optional.of(me));
|
||||
when(leadRepository.saveAndFlush(me)).thenReturn(me);
|
||||
when(eventLeadRepository.history(COMPANY, 7L)).thenReturn(List.of());
|
||||
when(leadRepository.findMatches(eq(COMPANY), any(), any(), any(Pageable.class))).thenReturn(List.of());
|
||||
|
||||
LeadDetail d = service.update(new LeadUpdateRequest(COMPANY, 7L, "New", "+609", null, "Acme", "CEO", "note", "bob"));
|
||||
|
||||
assertThat(me.getName()).isEqualTo("New");
|
||||
assertThat(me.getMobileE164()).isEqualTo("+609");
|
||||
assertThat(me.getEmailNormalised()).isNull();
|
||||
assertThat(me.getCompanyName()).isEqualTo("Acme");
|
||||
assertThat(me.getJobTitle()).isEqualTo("CEO");
|
||||
assertThat(me.getNotes()).isEqualTo("note");
|
||||
assertThat(me.getUpdatedBy()).isEqualTo("bob");
|
||||
assertThat(me.getUpdatedAt()).isNotNull();
|
||||
assertThat(d.name()).isEqualTo("New");
|
||||
assertThat(d.updatedBy()).isEqualTo("bob");
|
||||
}
|
||||
|
||||
@Test
|
||||
void updateIs404ForUnknownLead() {
|
||||
when(leadRepository.findByUuidAndCompanyId(7L, COMPANY)).thenReturn(Optional.empty());
|
||||
|
||||
assertThatThrownBy(() -> service.update(new LeadUpdateRequest(COMPANY, 7L, "n", null, null, null, null, null, "bob")))
|
||||
.isInstanceOfSatisfying(ResponseStatusException.class, e -> assertThat(e.getReason()).isEqualTo("LEAD_NOT_FOUND"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void updateIsOneTransaction() throws Exception {
|
||||
assertThat(LeadService.class.getMethod("update", LeadUpdateRequest.class)
|
||||
.isAnnotationPresent(Transactional.class)).isTrue();
|
||||
}
|
||||
|
||||
private static Lead lead(long uuid, String name, String mobile, String email) {
|
||||
Lead l = new Lead();
|
||||
l.setUuid(uuid);
|
||||
l.setCompanyId(COMPANY);
|
||||
l.setName(name);
|
||||
l.setMobileE164(mobile);
|
||||
l.setEmailNormalised(email);
|
||||
return l;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user