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

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