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:
ngqileandClaude Opus 5.5 committed 2026-12-02 11:03:56 +08:00
1 parent 627e0134e2
commit e6c3a488cf
15 files changed
+1148 -6

No files matched your search

@@ -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());
}
}