feat(services-counter): lead search, detail and update
Search lists every lead in the company with its event count, last event and latest salesperson, worked out at read time. Detail adds the event history and other leads sharing the mobile or email. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM
This commit is contained in:
1 parent
a1d9a82d88
commit
e75ef80dc0
13 files changed
+462
-1
No files matched your search
+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