feat: company-scoped events, leads, event leads and exports

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-11-30 20:30:46 +08:00
1 parent de2ae63cea
commit ec504fd529
31 files changed
+1096 -190

No files matched your search

@@ -4,4 +4,13 @@ import java.time.LocalDateTime;
public record LeadSummary(long leadUuid, String name, String mobileE164, String email, String companyName,
String jobTitle, long eventCount, Long lastEventUuid, String lastEventName,
LocalDateTime lastCapturedAt, Long latestSalespersonUserId) {}
LocalDateTime lastCapturedAt, Long latestSalespersonUserId, Long companyId) {
/** Without the lead's own company id (single-company callers already know it). */
public LeadSummary(long leadUuid, String name, String mobileE164, String email, String companyName,
String jobTitle, long eventCount, Long lastEventUuid, String lastEventName,
LocalDateTime lastCapturedAt, Long latestSalespersonUserId) {
this(leadUuid, name, mobileE164, email, companyName, jobTitle, eventCount, lastEventUuid, lastEventName,
lastCapturedAt, latestSalespersonUserId, null);
}
}
@@ -35,6 +35,7 @@ public interface LeadRepository extends JpaRepository<Lead, Long> {
String getLastEventName();
java.time.LocalDateTime getLastCapturedAt();
Long getLatestSalespersonUserId();
Long getCompanyId();
}
/** The search condition uses only `lead` columns, so the count skips the aggregate joins. */
@@ -53,13 +54,13 @@ public interface LeadRepository extends JpaRepository<Lead, Long> {
/**
* Returns one row per lead with columns lead_uuid AS leadUuid, name, mobile_e164 AS mobileE164, email_normalised AS email,
* company_name AS companyName, job_title AS jobTitle, eventCount, lastEventUuid, lastEventName, lastCapturedAt and
* latestSalespersonUserId. Native because JPQL has no window functions. Never-captured leads sort last.
* latestSalespersonUserId and companyId. Native because JPQL has no window functions. Never-captured leads sort last.
*/
@Query(nativeQuery = true,
value = "SELECT l.uuid AS leadUuid, l.name AS name, l.mobile_e164 AS mobileE164, l.email_normalised AS email,"
+ " l.company_name AS companyName, l.job_title AS jobTitle, COALESCE(c.cnt, 0) AS eventCount,"
+ " last.event_uuid AS lastEventUuid, ev.name AS lastEventName, last.captured_at AS lastCapturedAt,"
+ " sp.salesperson_user_id AS latestSalespersonUserId" + SEARCH_FROM
+ " sp.salesperson_user_id AS latestSalespersonUserId, l.company_id AS companyId" + SEARCH_FROM
+ " ORDER BY last.captured_at IS NULL, last.captured_at DESC, l.id DESC",
countQuery = "SELECT COUNT(*) FROM `lead` l" + SEARCH_WHERE)
Page<SummaryRow> search(@Param("companyId") Long companyId, @Param("q") String q,
@@ -41,7 +41,7 @@ public class LeadService {
return leadRepository.search(req.companyId(), q, digits, PageRequest.of(Math.max(req.page(), 0), size))
.map(r -> new LeadSummary(r.getLeadUuid(), r.getName(), r.getMobileE164(), r.getEmail(), r.getCompanyName(),
r.getJobTitle(), r.getEventCount(), r.getLastEventUuid(), r.getLastEventName(),
r.getLastCapturedAt(), r.getLatestSalespersonUserId()));
r.getLastCapturedAt(), r.getLatestSalespersonUserId(), r.getCompanyId()));
}
/** A null company looks the lead up across all companies; the detail reports the lead's own company. */