feat: counter WhatsApp contact lookup and lead-changed notify
GET /counter/lead/whatsappContact returns the company's newest lead for a mobile with its latest salesperson (the Leads list's definition). Event lead capture, registration and salesperson changes notify the WhatsApp service after commit, best effort, never logging the mobile. 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
b13e39ac75
commit
ae96e316ec
11 files changed
+434
-5
No files matched your search
+1
-1
@@ -29,7 +29,7 @@ class InvitationWiringTest {
|
||||
RedisUtil.class, TransactionTemplate.class}) {
|
||||
mockBean(ctx, c);
|
||||
}
|
||||
ctx.register(GuestLinker.class, EventLeadService.class, RegistrationService.class, EventService.class,
|
||||
ctx.register(GuestLinker.class, WhatsappNotifier.class, EventLeadService.class, RegistrationService.class, EventService.class,
|
||||
EventHeadcountService.class);
|
||||
ctx.refresh();
|
||||
|
||||
|
||||
+94
@@ -0,0 +1,94 @@
|
||||
package com.solvway.services.counter.service;
|
||||
|
||||
import com.solvway.services.counter.controller.LeadController;
|
||||
import com.solvway.services.counter.dto.WhatsappContact;
|
||||
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.Test;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
||||
import org.springframework.web.server.ResponseStatusException;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
|
||||
/** GET /counter/lead/whatsappContact: newest lead for the mobile in one company, with its latest salesperson. */
|
||||
class WhatsappLookupTest {
|
||||
private static final long COMPANY = 42L;
|
||||
private static final String MOBILE = "+60123456789";
|
||||
|
||||
private final LeadRepository leads = mock(LeadRepository.class);
|
||||
private final LeadService service = new LeadService(leads, mock(EventLeadRepository.class));
|
||||
|
||||
private static Lead lead(String name) {
|
||||
Lead l = new Lead();
|
||||
l.setCompanyId(COMPANY);
|
||||
l.setName(name);
|
||||
l.setMobileE164(MOBILE);
|
||||
return l;
|
||||
}
|
||||
|
||||
@Test
|
||||
void newestLeadOfTheCompanyWinsAndCarriesTheLatestSalesperson() {
|
||||
Lead newest = lead("Newest");
|
||||
when(leads.findFirstByCompanyIdAndMobileE164OrderByIdDesc(COMPANY, MOBILE)).thenReturn(Optional.of(newest));
|
||||
when(leads.latestSalespersonUserId(COMPANY, newest.getUuid())).thenReturn(7L);
|
||||
|
||||
assertThat(service.whatsappContact(COMPANY, MOBILE))
|
||||
.isEqualTo(new WhatsappContact(newest.getUuid(), "Newest", 7L));
|
||||
}
|
||||
|
||||
@Test
|
||||
void noSalespersonYieldsNullNotAnError() {
|
||||
Lead l = lead("A");
|
||||
when(leads.findFirstByCompanyIdAndMobileE164OrderByIdDesc(COMPANY, MOBILE)).thenReturn(Optional.of(l));
|
||||
when(leads.latestSalespersonUserId(COMPANY, l.getUuid())).thenReturn(null);
|
||||
|
||||
assertThat(service.whatsappContact(COMPANY, MOBILE).latestSalespersonUserId()).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
void otherCompanyOrUnknownMobileIs404() {
|
||||
when(leads.findFirstByCompanyIdAndMobileE164OrderByIdDesc(COMPANY, MOBILE)).thenReturn(Optional.empty());
|
||||
|
||||
assertThatThrownBy(() -> service.whatsappContact(COMPANY, MOBILE))
|
||||
.isInstanceOfSatisfying(ResponseStatusException.class,
|
||||
e -> assertThat(e.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND));
|
||||
}
|
||||
|
||||
@Test
|
||||
void latestSalespersonQueryReusesTheSearchWindowAndSkipsEventLeadsWithoutOne() throws Exception {
|
||||
String sql = LeadRepository.class.getMethod("latestSalespersonUserId", long.class, long.class)
|
||||
.getAnnotation(Query.class).value();
|
||||
|
||||
assertThat(sql).contains(LeadRepository.LATEST_SALESPERSON_JOIN)
|
||||
.contains("salesperson_user_id IS NOT NULL").contains("ORDER BY captured_at DESC, id DESC");
|
||||
assertThat(LeadRepository.SEARCH_FROM).contains(LeadRepository.LATEST_SALESPERSON_JOIN);
|
||||
}
|
||||
|
||||
@Test
|
||||
void httpMapsFoundNotFoundAndMissingCompany() throws Exception {
|
||||
LeadService svc = mock(LeadService.class);
|
||||
when(svc.whatsappContact(COMPANY, MOBILE)).thenReturn(new WhatsappContact(9L, "A", 7L));
|
||||
when(svc.whatsappContact(1L, MOBILE)).thenThrow(new ResponseStatusException(HttpStatus.NOT_FOUND));
|
||||
MockMvc mvc = MockMvcBuilders.standaloneSetup(new LeadController(svc)).build();
|
||||
|
||||
mvc.perform(get("/counter/lead/whatsappContact").param("companyId", "42").param("mobileE164", MOBILE))
|
||||
.andExpect(status().isOk()).andExpect(jsonPath("$.leadUuid").value(9))
|
||||
.andExpect(jsonPath("$.latestSalespersonUserId").value(7));
|
||||
mvc.perform(get("/counter/lead/whatsappContact").param("companyId", "1").param("mobileE164", MOBILE))
|
||||
.andExpect(status().isNotFound());
|
||||
mvc.perform(get("/counter/lead/whatsappContact").param("mobileE164", MOBILE))
|
||||
.andExpect(status().isBadRequest());
|
||||
}
|
||||
}
|
||||
+80
@@ -0,0 +1,80 @@
|
||||
package com.solvway.services.counter.service;
|
||||
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.test.web.client.MockRestServiceServer;
|
||||
import org.springframework.transaction.support.TransactionSynchronization;
|
||||
import org.springframework.transaction.support.TransactionSynchronizationManager;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
import static org.springframework.test.web.client.match.MockRestRequestMatchers.content;
|
||||
import static org.springframework.test.web.client.match.MockRestRequestMatchers.method;
|
||||
import static org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo;
|
||||
import static org.springframework.test.web.client.response.MockRestResponseCreators.withStatus;
|
||||
import static org.springframework.test.web.client.response.MockRestResponseCreators.withSuccess;
|
||||
import static org.assertj.core.api.Assertions.assertThatCode;
|
||||
|
||||
class WhatsappNotifierTest {
|
||||
private static final String URL = "http://service-whatsapp/whatsapp/ownership/leadChanged";
|
||||
|
||||
private final RestTemplate rest = new RestTemplate();
|
||||
private final MockRestServiceServer server = MockRestServiceServer.bindTo(rest).build();
|
||||
private final WhatsappNotifier notifier = new WhatsappNotifier(rest);
|
||||
|
||||
@AfterEach
|
||||
void clear() {
|
||||
if (TransactionSynchronizationManager.isSynchronizationActive()) TransactionSynchronizationManager.clearSynchronization();
|
||||
}
|
||||
|
||||
@Test
|
||||
void postsCompanyAndMobile() {
|
||||
server.expect(requestTo(URL)).andExpect(method(HttpMethod.POST))
|
||||
.andExpect(content().json("{\"companyId\":42,\"mobileE164\":\"+60123456789\"}"))
|
||||
.andRespond(withSuccess());
|
||||
|
||||
notifier.leadChanged(42L, "+60123456789", 9L);
|
||||
|
||||
server.verify();
|
||||
}
|
||||
|
||||
@Test
|
||||
void noMobileMeansNoCall() {
|
||||
notifier.leadChanged(42L, null, 9L);
|
||||
notifier.leadChanged(42L, " ", 9L);
|
||||
|
||||
server.verify(); // no expectation set: any call would have failed
|
||||
}
|
||||
|
||||
@Test
|
||||
void serverErrorAndConnectionFailureAreSwallowed() {
|
||||
server.expect(requestTo(URL)).andRespond(withStatus(HttpStatus.INTERNAL_SERVER_ERROR));
|
||||
server.expect(requestTo(URL)).andRespond(r -> { throw new java.net.ConnectException("refused"); });
|
||||
|
||||
assertThatCode(() -> notifier.leadChanged(42L, "+60123456789", 9L)).doesNotThrowAnyException();
|
||||
assertThatCode(() -> notifier.leadChanged(42L, "+60123456789", 9L)).doesNotThrowAnyException();
|
||||
}
|
||||
|
||||
@Test
|
||||
void insideATransactionTheCallWaitsForCommit() {
|
||||
server.expect(requestTo(URL)).andRespond(withSuccess());
|
||||
TransactionSynchronizationManager.initSynchronization();
|
||||
|
||||
notifier.leadChanged(42L, "+60123456789", 9L);
|
||||
TransactionSynchronizationManager.getSynchronizations().forEach(TransactionSynchronization::afterCommit);
|
||||
|
||||
server.verify();
|
||||
}
|
||||
|
||||
@Test
|
||||
void aRolledBackTransactionNotifiesNobody() {
|
||||
TransactionSynchronizationManager.initSynchronization();
|
||||
|
||||
notifier.leadChanged(42L, "+60123456789", 9L);
|
||||
TransactionSynchronizationManager.getSynchronizations()
|
||||
.forEach(s -> s.afterCompletion(TransactionSynchronization.STATUS_ROLLED_BACK));
|
||||
|
||||
server.verify(); // no expectation set: any call would have failed
|
||||
}
|
||||
}
|
||||
+148
@@ -0,0 +1,148 @@
|
||||
package com.solvway.services.counter.service;
|
||||
|
||||
import com.solvway.services.counter.dto.EventLeadCaptureRequest;
|
||||
import com.solvway.services.counter.dto.EventLeadUpdateRequest;
|
||||
import com.solvway.services.counter.model.EventLead;
|
||||
import com.solvway.services.counter.model.Lead;
|
||||
import com.solvway.services.counter.repo.EventLeadInterestRepository;
|
||||
import com.solvway.services.counter.repo.EventLeadRepository;
|
||||
import com.solvway.services.counter.repo.EventLeadStatusChangeRepository;
|
||||
import com.solvway.services.counter.repo.LeadRepository;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.transaction.PlatformTransactionManager;
|
||||
import org.springframework.transaction.support.TransactionTemplate;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
import static org.mockito.AdditionalAnswers.returnsFirstArg;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyLong;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/** Every save path that creates an event lead or changes its salesperson tells the WhatsApp service, best effort. */
|
||||
class WhatsappNotifyOnSaveTest {
|
||||
private static final long COMPANY = 42L;
|
||||
private static final long EVENT = 1000L;
|
||||
private static final String MOBILE = "+60123456789";
|
||||
|
||||
private final LeadRepository leads = mock(LeadRepository.class);
|
||||
private final EventLeadRepository eventLeads = mock(EventLeadRepository.class);
|
||||
private final WhatsappNotifier notifier = mock(WhatsappNotifier.class);
|
||||
private EventLeadService service;
|
||||
private RegistrationService registration;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
service = new EventLeadService(leads, eventLeads, mock(EventLeadInterestRepository.class),
|
||||
mock(EventLeadStatusChangeRepository.class), null, null, notifier);
|
||||
registration = new RegistrationService(new TransactionTemplate(mock(PlatformTransactionManager.class)),
|
||||
leads, eventLeads, service);
|
||||
when(leads.saveAndFlush(any(Lead.class))).then(returnsFirstArg());
|
||||
when(eventLeads.saveAndFlush(any(EventLead.class))).then(i -> {
|
||||
EventLead e = i.getArgument(0);
|
||||
e.setId(1L);
|
||||
return e;
|
||||
});
|
||||
}
|
||||
|
||||
private static Lead lead(String mobile) {
|
||||
Lead l = new Lead();
|
||||
l.setCompanyId(COMPANY);
|
||||
l.setMobileE164(mobile);
|
||||
return l;
|
||||
}
|
||||
|
||||
private static EventLeadCaptureRequest capture(String mobile) {
|
||||
return new EventLeadCaptureRequest(COMPANY, EVENT, null, "Aisyah", mobile, null, null, null, 7L, null, null, null, null, null, "staff");
|
||||
}
|
||||
|
||||
@Test
|
||||
void staffCaptureNotifiesWithTheLeadMobile() {
|
||||
service.capture(capture(MOBILE));
|
||||
|
||||
verify(notifier).leadChanged(eq(COMPANY), eq(MOBILE), anyLong());
|
||||
}
|
||||
|
||||
@Test
|
||||
void captureWithoutMobileDoesNotNotify() {
|
||||
service.capture(capture(null));
|
||||
|
||||
verify(notifier, never()).leadChanged(anyLong(), any(), anyLong());
|
||||
}
|
||||
|
||||
@Test
|
||||
void qrRegistrationNotifies() {
|
||||
registration.register(new RegistrationService.RegisterRequest(COMPANY, EVENT, 7L, "A", MOBILE, null, null, null,
|
||||
null, null, null));
|
||||
|
||||
verify(notifier).leadChanged(eq(COMPANY), eq(MOBILE), anyLong());
|
||||
}
|
||||
|
||||
@Test
|
||||
void qrRegistrationAssigningASalespersonToAnExistingCaptureNotifies() {
|
||||
Lead l = lead(MOBILE);
|
||||
EventLead existing = new EventLead();
|
||||
existing.setLeadUuid(l.getUuid());
|
||||
when(leads.findFirstByCompanyIdAndMobileE164OrderByIdDesc(COMPANY, MOBILE)).thenReturn(Optional.of(l));
|
||||
when(eventLeads.findByEventUuidAndLeadUuid(EVENT, l.getUuid())).thenReturn(Optional.of(existing));
|
||||
|
||||
registration.register(new RegistrationService.RegisterRequest(COMPANY, EVENT, 7L, "A", MOBILE, null, null, null,
|
||||
null, null, null));
|
||||
|
||||
verify(notifier).leadChanged(COMPANY, MOBILE, l.getUuid());
|
||||
}
|
||||
|
||||
@Test
|
||||
void qrRegistrationOfAnAlreadyOwnedCaptureDoesNotNotify() {
|
||||
Lead l = lead(MOBILE);
|
||||
EventLead existing = new EventLead();
|
||||
existing.setSalespersonUserId(5L);
|
||||
when(leads.findFirstByCompanyIdAndMobileE164OrderByIdDesc(COMPANY, MOBILE)).thenReturn(Optional.of(l));
|
||||
when(eventLeads.findByEventUuidAndLeadUuid(EVENT, l.getUuid())).thenReturn(Optional.of(existing));
|
||||
|
||||
registration.register(new RegistrationService.RegisterRequest(COMPANY, EVENT, 7L, "A", MOBILE, null, null, null,
|
||||
null, null, null));
|
||||
|
||||
verify(notifier, never()).leadChanged(anyLong(), any(), anyLong());
|
||||
}
|
||||
|
||||
private EventLeadUpdateRequest update(Long salesperson) {
|
||||
return new EventLeadUpdateRequest(COMPANY, 555L, salesperson, null, null, null, null, null, "bo");
|
||||
}
|
||||
|
||||
private EventLead stubUpdate(Long currentSalesperson) {
|
||||
Lead l = lead(MOBILE);
|
||||
EventLead e = new EventLead();
|
||||
e.setCompanyId(COMPANY);
|
||||
e.setLeadUuid(l.getUuid());
|
||||
e.setId(1L);
|
||||
e.setSalespersonUserId(currentSalesperson);
|
||||
when(eventLeads.findByUuidAndCompanyId(555L, COMPANY)).thenReturn(Optional.of(e));
|
||||
when(eventLeads.saveAndFlush(any(EventLead.class))).then(returnsFirstArg());
|
||||
when(leads.findByUuidAndCompanyId(l.getUuid(), COMPANY)).thenReturn(Optional.of(l));
|
||||
return e;
|
||||
}
|
||||
|
||||
@Test
|
||||
void salespersonChangeNotifies() {
|
||||
stubUpdate(5L);
|
||||
|
||||
service.update(update(7L));
|
||||
|
||||
verify(notifier).leadChanged(eq(COMPANY), eq(MOBILE), anyLong());
|
||||
}
|
||||
|
||||
@Test
|
||||
void updateKeepingTheSameSalespersonDoesNotNotify() {
|
||||
stubUpdate(5L);
|
||||
|
||||
service.update(update(5L));
|
||||
|
||||
verify(notifier, never()).leadChanged(anyLong(), any(), anyLong());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user