feat: WhatsApp inbox reads: list tabs, thread, read markers, unread, start chat
The service lists a user's conversations by tab (mine, unassigned, all) with per-user unread counts in one query, pages threads backwards, keeps read markers monotonic, counts unread conversations for the badge, and starts or claims a chat from a lead or event lead. A staff call without a company fails closed. The read-marker key is a class, not a record, because Hibernate 6.4 cannot bind a record as an @IdClass. 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
627e0134e2
commit
e6c3a488cf
15 files changed
+1148
-6
No files matched your search
+7
@@ -29,6 +29,13 @@ public class LeadController {
|
||||
return leadService.whatsappContact(companyId, mobileE164);
|
||||
}
|
||||
|
||||
@GetMapping("/lead/whatsappTarget")
|
||||
public WhatsappTarget whatsappTarget(@RequestParam(value = "companyId", required = false) Long companyId,
|
||||
@RequestParam(value = "leadUuid", required = false) Long leadUuid,
|
||||
@RequestParam(value = "eventLeadUuid", required = false) Long eventLeadUuid) {
|
||||
return leadService.whatsappTarget(companyId, leadUuid, eventLeadUuid);
|
||||
}
|
||||
|
||||
@PostMapping("/lead/update")
|
||||
public LeadDetail update(@RequestBody LeadUpdateRequest req) { return leadService.update(req); }
|
||||
}
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
package com.solvway.services.counter.dto;
|
||||
|
||||
/** Counter GET /counter/lead/whatsappTarget response; the lead's own company id lets a super admin start without one. */
|
||||
public record WhatsappTarget(Long leadUuid, String name, String mobileE164, Long companyId) {}
|
||||
+23
-3
@@ -7,6 +7,7 @@ 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.dto.WhatsappContact;
|
||||
import com.solvway.services.counter.dto.WhatsappTarget;
|
||||
import com.solvway.services.counter.model.Event;
|
||||
import com.solvway.services.counter.model.EventLead;
|
||||
import com.solvway.services.counter.model.Lead;
|
||||
@@ -47,9 +48,7 @@ public class LeadService {
|
||||
|
||||
/** A null company looks the lead up across all companies; the detail reports the lead's own company. */
|
||||
public LeadDetail detail(Long companyId, long leadUuid) {
|
||||
return detail(companyId == null
|
||||
? leadRepository.findByUuid(leadUuid).orElseThrow(LeadService::notFound)
|
||||
: find(companyId, leadUuid));
|
||||
return detail(anyCompanyLead(companyId, leadUuid));
|
||||
}
|
||||
|
||||
/** The newest lead of the company with this mobile and its latest salesperson; 404 when there is none. */
|
||||
@@ -59,6 +58,27 @@ public class LeadService {
|
||||
return new WhatsappContact(l.getUuid(), l.getName(), leadRepository.latestSalespersonUserId(companyId, l.getUuid()));
|
||||
}
|
||||
|
||||
/** The lead (or the event lead's lead) to start a WhatsApp chat with; a null company is any company. 404 when none or no mobile. */
|
||||
public WhatsappTarget whatsappTarget(Long companyId, Long leadUuid, Long eventLeadUuid) {
|
||||
Long uuid = leadUuid;
|
||||
if (uuid == null && eventLeadUuid != null) {
|
||||
uuid = (companyId == null ? eventLeadRepository.findByUuid(eventLeadUuid)
|
||||
: eventLeadRepository.findByUuidAndCompanyId(eventLeadUuid, companyId)).map(EventLead::getLeadUuid).orElse(null);
|
||||
}
|
||||
if (uuid == null) {
|
||||
throw notFound();
|
||||
}
|
||||
Lead l = anyCompanyLead(companyId, uuid);
|
||||
if (l.getMobileE164() == null || l.getMobileE164().isBlank()) {
|
||||
throw notFound();
|
||||
}
|
||||
return new WhatsappTarget(l.getUuid(), l.getName(), l.getMobileE164(), l.getCompanyId());
|
||||
}
|
||||
|
||||
private Lead anyCompanyLead(Long companyId, long leadUuid) {
|
||||
return companyId == null ? leadRepository.findByUuid(leadUuid).orElseThrow(LeadService::notFound) : find(companyId, leadUuid);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public LeadDetail update(LeadUpdateRequest req) {
|
||||
if (req.companyId() == null) throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "COMPANY_REQUIRED");
|
||||
|
||||
+102
@@ -0,0 +1,102 @@
|
||||
package com.solvway.services.counter.service;
|
||||
|
||||
import com.solvway.services.counter.controller.LeadController;
|
||||
import com.solvway.services.counter.dto.WhatsappTarget;
|
||||
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.Test;
|
||||
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/whatsappTarget: the mobile and company of a lead (or of an event lead's lead) to start a chat with. */
|
||||
class WhatsappTargetTest {
|
||||
private static final long COMPANY = 42L;
|
||||
|
||||
private final LeadRepository leads = mock(LeadRepository.class);
|
||||
private final EventLeadRepository eventLeads = mock(EventLeadRepository.class);
|
||||
private final LeadService service = new LeadService(leads, eventLeads);
|
||||
|
||||
private static Lead lead(long company, String mobile) {
|
||||
Lead l = new Lead();
|
||||
l.setCompanyId(company);
|
||||
l.setName("Aisyah");
|
||||
l.setMobileE164(mobile);
|
||||
return l;
|
||||
}
|
||||
|
||||
private static void assertNotFound(Runnable call) {
|
||||
assertThatThrownBy(call::run).isInstanceOfSatisfying(ResponseStatusException.class,
|
||||
e -> assertThat(e.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND));
|
||||
}
|
||||
|
||||
@Test
|
||||
void leadOfTheCompanyYieldsNameMobileAndCompany() {
|
||||
Lead l = lead(COMPANY, "+60111222333");
|
||||
when(leads.findByUuidAndCompanyId(l.getUuid(), COMPANY)).thenReturn(Optional.of(l));
|
||||
|
||||
assertThat(service.whatsappTarget(COMPANY, l.getUuid(), null))
|
||||
.isEqualTo(new WhatsappTarget(l.getUuid(), "Aisyah", "+60111222333", COMPANY));
|
||||
}
|
||||
|
||||
@Test
|
||||
void nullCompanyLooksTheLeadUpAcrossCompaniesAndReportsItsOwn() {
|
||||
Lead l = lead(7L, "+60111222333");
|
||||
when(leads.findByUuid(l.getUuid())).thenReturn(Optional.of(l));
|
||||
|
||||
assertThat(service.whatsappTarget(null, l.getUuid(), null).companyId()).isEqualTo(7L);
|
||||
}
|
||||
|
||||
@Test
|
||||
void eventLeadResolvesToItsLead() {
|
||||
Lead l = lead(COMPANY, "+60111222333");
|
||||
EventLead e = new EventLead();
|
||||
e.setCompanyId(COMPANY);
|
||||
e.setLeadUuid(l.getUuid());
|
||||
when(eventLeads.findByUuidAndCompanyId(e.getUuid(), COMPANY)).thenReturn(Optional.of(e));
|
||||
when(leads.findByUuidAndCompanyId(l.getUuid(), COMPANY)).thenReturn(Optional.of(l));
|
||||
|
||||
assertThat(service.whatsappTarget(COMPANY, null, e.getUuid()).leadUuid()).isEqualTo(l.getUuid());
|
||||
}
|
||||
|
||||
@Test
|
||||
void unknownLeadOtherCompanyOrNoMobileIs404() {
|
||||
Lead noMobile = lead(COMPANY, null);
|
||||
when(leads.findByUuidAndCompanyId(noMobile.getUuid(), COMPANY)).thenReturn(Optional.of(noMobile));
|
||||
Lead blank = lead(COMPANY, " ");
|
||||
when(leads.findByUuidAndCompanyId(blank.getUuid(), COMPANY)).thenReturn(Optional.of(blank));
|
||||
|
||||
assertNotFound(() -> service.whatsappTarget(COMPANY, noMobile.getUuid(), null));
|
||||
assertNotFound(() -> service.whatsappTarget(COMPANY, blank.getUuid(), null));
|
||||
assertNotFound(() -> service.whatsappTarget(COMPANY, 1L, null));
|
||||
assertNotFound(() -> service.whatsappTarget(COMPANY, null, 2L));
|
||||
assertNotFound(() -> service.whatsappTarget(COMPANY, null, null));
|
||||
}
|
||||
|
||||
@Test
|
||||
void httpMapsFoundAndNotFound() throws Exception {
|
||||
LeadService svc = mock(LeadService.class);
|
||||
when(svc.whatsappTarget(COMPANY, 9L, null)).thenReturn(new WhatsappTarget(9L, "A", "+60111222333", COMPANY));
|
||||
when(svc.whatsappTarget(COMPANY, 1L, null)).thenThrow(new ResponseStatusException(HttpStatus.NOT_FOUND));
|
||||
MockMvc mvc = MockMvcBuilders.standaloneSetup(new LeadController(svc)).build();
|
||||
|
||||
mvc.perform(get("/counter/lead/whatsappTarget").param("companyId", "42").param("leadUuid", "9"))
|
||||
.andExpect(status().isOk()).andExpect(jsonPath("$.mobileE164").value("+60111222333"))
|
||||
.andExpect(jsonPath("$.companyId").value(42));
|
||||
mvc.perform(get("/counter/lead/whatsappTarget").param("companyId", "42").param("leadUuid", "1"))
|
||||
.andExpect(status().isNotFound());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user