feat(notifications): bell alerts for assigned leads and web sign-ups
InvitationWiringTest registers StaffNotifier and asserts it is wired. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EFUzfiCVCgCVJZq4NDEqZs
This commit is contained in:
1 parent
66e2e488d9
commit
e3f08bc6cf
12 files changed
+495
-8
No files matched your search
+2
@@ -1,6 +1,8 @@
|
||||
package com.solvway.services.counter.constant;
|
||||
|
||||
public class RestApiSystemServiceConstant {
|
||||
// add-lead-notifications: the staff bell
|
||||
public static final String API_REST_SYSTEM_NOTIFICATION_SEND = "/notification/send";
|
||||
//Message
|
||||
public static final String API_REST_SYSTEM_MESSAGE_FIND_BY_CODE = "/message/getMessagesByCode/{code}";
|
||||
public static final String API_REST_SYSTEM_MESSAGE_FIND_BY_CODE_AND_AREA = "/message/getMessagesByCodeAndArea/{code}/{Area}";
|
||||
|
||||
+11
-2
@@ -19,5 +19,14 @@ public record EventLeadCaptureRequest(
|
||||
List<Long> interestIds,
|
||||
LocalDate nextFollowUpDate,
|
||||
String notes,
|
||||
String doneBy
|
||||
) {}
|
||||
String doneBy,
|
||||
/* add-lead-notifications: the acting staff user's id, so assigning a lead to yourself is silent. */
|
||||
Long doneByUserId
|
||||
) {
|
||||
public EventLeadCaptureRequest(Long companyId, long eventUuid, Long leadUuid, String name, String mobileE164, String email,
|
||||
String companyName, String jobTitle, Long salespersonUserId, String temperature, String status,
|
||||
List<Long> interestIds, LocalDate nextFollowUpDate, String notes, String doneBy) {
|
||||
this(companyId, eventUuid, leadUuid, name, mobileE164, email, companyName, jobTitle, salespersonUserId, temperature, status,
|
||||
interestIds, nextFollowUpDate, notes, doneBy, null);
|
||||
}
|
||||
}
|
||||
+8
-2
@@ -6,9 +6,15 @@ import java.util.List;
|
||||
/** Replaces an event lead's working fields and its interest set. Values arrive validated by mobile-api. attended: true stamps now if empty, false clears, null leaves it. */
|
||||
public record EventLeadUpdateRequest(Long companyId, long eventLeadUuid, Long salespersonUserId, String temperature,
|
||||
String status, List<Long> interestIds, LocalDate nextFollowUpDate, String notes,
|
||||
String doneBy, Boolean attended) {
|
||||
String doneBy, Boolean attended, Long doneByUserId) {
|
||||
/** doneByUserId (add-lead-notifications): the acting staff user's id, so assigning a lead to yourself is silent. */
|
||||
public EventLeadUpdateRequest(Long companyId, long eventLeadUuid, Long salespersonUserId, String temperature,
|
||||
String status, List<Long> interestIds, LocalDate nextFollowUpDate, String notes,
|
||||
String doneBy, Boolean attended) {
|
||||
this(companyId, eventLeadUuid, salespersonUserId, temperature, status, interestIds, nextFollowUpDate, notes, doneBy, attended, null);
|
||||
}
|
||||
public EventLeadUpdateRequest(Long companyId, long eventLeadUuid, Long salespersonUserId, String temperature,
|
||||
String status, List<Long> interestIds, LocalDate nextFollowUpDate, String notes, String doneBy) {
|
||||
this(companyId, eventLeadUuid, salespersonUserId, temperature, status, interestIds, nextFollowUpDate, notes, doneBy, null);
|
||||
this(companyId, eventLeadUuid, salespersonUserId, temperature, status, interestIds, nextFollowUpDate, notes, doneBy, null, null);
|
||||
}
|
||||
}
|
||||
+24
-1
@@ -54,6 +54,7 @@ public class EventLeadService {
|
||||
private final EventRepository eventRepository;
|
||||
private final GuestLinker guestLinker;
|
||||
private final WhatsappNotifier whatsappNotifier;
|
||||
private final StaffNotifier staffNotifier;
|
||||
|
||||
// ponytail: null event repository, linker and notifier keep older wiring working; drop once callers pass all.
|
||||
public EventLeadService(LeadRepository leadRepository, EventLeadRepository eventLeadRepository,
|
||||
@@ -70,6 +71,24 @@ public class EventLeadService {
|
||||
eventRepository, guestLinker, null);
|
||||
}
|
||||
|
||||
public EventLeadService(LeadRepository leadRepository, EventLeadRepository eventLeadRepository,
|
||||
EventLeadInterestRepository eventLeadInterestRepository,
|
||||
EventLeadStatusChangeRepository eventLeadStatusChangeRepository,
|
||||
EventRepository eventRepository, GuestLinker guestLinker, WhatsappNotifier whatsappNotifier) {
|
||||
this(leadRepository, eventLeadRepository, eventLeadInterestRepository, eventLeadStatusChangeRepository,
|
||||
eventRepository, guestLinker, whatsappNotifier, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* add-lead-notifications: the staff bell for a capture or a registration. A landing-page sign-up is a sign-up;
|
||||
* anything else tells the salesperson the lead is theirs (silent when they assigned it to themselves).
|
||||
*/
|
||||
public void notifyStaff(EventLead e, Lead lead, String source, Long actorUserId) {
|
||||
if (staffNotifier == null) return;
|
||||
if ("WEB".equals(source)) staffNotifier.webSignup(e, lead);
|
||||
else staffNotifier.leadAssigned(e, lead, actorUserId);
|
||||
}
|
||||
|
||||
/** The one place every capture or salesperson change tells the WhatsApp service; skipped for a lead with no mobile. */
|
||||
public void notifyLeadChanged(Lead lead) {
|
||||
if (whatsappNotifier != null && lead.getMobileE164() != null) {
|
||||
@@ -161,6 +180,7 @@ public class EventLeadService {
|
||||
linkGuest(req.eventUuid(), eventLead, lead);
|
||||
List<Long> interestIds = saveInterests(eventLead.getId(), req.companyId(), req.interestIds());
|
||||
notifyLeadChanged(lead);
|
||||
notifyStaff(eventLead, lead, null, req.doneByUserId());
|
||||
return detail(eventLead, lead, interestIds);
|
||||
}
|
||||
|
||||
@@ -219,7 +239,10 @@ public class EventLeadService {
|
||||
List<Long> interestIds = saveInterests(eventLead.getId(), req.companyId(), req.interestIds());
|
||||
Lead lead = leadRepository.findByUuidAndCompanyId(eventLead.getLeadUuid(), req.companyId())
|
||||
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND));
|
||||
if (salespersonChanged) notifyLeadChanged(lead);
|
||||
if (salespersonChanged) {
|
||||
notifyLeadChanged(lead);
|
||||
notifyStaff(eventLead, lead, null, req.doneByUserId());
|
||||
}
|
||||
return detail(eventLead, lead, interestIds);
|
||||
}
|
||||
|
||||
|
||||
+3
@@ -134,6 +134,7 @@ public class RegistrationService {
|
||||
eventLeadService.saveInterests(e.getId(), req.companyId(), req.interestIds());
|
||||
link(req, e, lead);
|
||||
eventLeadService.notifyLeadChanged(lead);
|
||||
eventLeadService.notifyStaff(e, lead, req.source(), null);
|
||||
return new RegisterResult(match.isEmpty() ? "CREATED" : "LINKED", String.valueOf(e.getUuid()));
|
||||
}
|
||||
|
||||
@@ -149,6 +150,8 @@ public class RegistrationService {
|
||||
}
|
||||
link(req, e, lead);
|
||||
if (assign) eventLeadService.notifyLeadChanged(lead);
|
||||
// add-lead-notifications: a repeat landing-page sign-up is news to the team; a QR scan only when it assigns.
|
||||
if (WEB.equals(req.source()) || assign) eventLeadService.notifyStaff(e, lead, req.source(), null);
|
||||
return new RegisterResult(assign ? "ASSIGNED" : "ALREADY_CAPTURED", String.valueOf(e.getUuid()));
|
||||
}
|
||||
|
||||
|
||||
+128
@@ -0,0 +1,128 @@
|
||||
package com.solvway.services.counter.service;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.solvway.core.constant.SystemConstants;
|
||||
import com.solvway.services.counter.constant.RestApiSystemServiceConstant;
|
||||
import com.solvway.services.counter.model.Event;
|
||||
import com.solvway.services.counter.model.EventLead;
|
||||
import com.solvway.services.counter.model.EventSalesperson;
|
||||
import com.solvway.services.counter.model.Lead;
|
||||
import com.solvway.services.counter.repo.EventRepository;
|
||||
import com.solvway.services.counter.repo.EventSalespersonRepository;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.client.SimpleClientHttpRequestFactory;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.transaction.support.TransactionSynchronization;
|
||||
import org.springframework.transaction.support.TransactionSynchronizationManager;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* add-lead-notifications: tells staff about their leads through the bell in service-system. Resolves who hears
|
||||
* about it, then sends after the surrounding transaction commits. Best effort: never throws, no retry, and logs ids
|
||||
* only. Parameters carry the lead's and event's names, never a phone number or email address.
|
||||
*/
|
||||
@Component
|
||||
@Slf4j
|
||||
public class StaffNotifier {
|
||||
private static final int TIMEOUT_MS = 2000;
|
||||
private static final String URL = "http://" + SystemConstants.SERVICE_ID_SYSTEM
|
||||
+ RestApiSystemServiceConstant.API_REST_SYSTEM_NOTIFICATION_SEND;
|
||||
private static final ObjectMapper JSON = new ObjectMapper();
|
||||
|
||||
private final RestTemplate restTemplate;
|
||||
private final EventRepository events;
|
||||
private final EventSalespersonRepository team;
|
||||
|
||||
@Autowired
|
||||
public StaffNotifier(EventRepository events, EventSalespersonRepository team) {
|
||||
// The shared RestTemplate bean has 5 s timeouts; this call must never hold a request thread that long.
|
||||
SimpleClientHttpRequestFactory f = new SimpleClientHttpRequestFactory();
|
||||
f.setConnectTimeout(TIMEOUT_MS);
|
||||
f.setReadTimeout(TIMEOUT_MS);
|
||||
this.restTemplate = new RestTemplate(f);
|
||||
this.events = events;
|
||||
this.team = team;
|
||||
}
|
||||
|
||||
StaffNotifier(RestTemplate restTemplate, EventRepository events, EventSalespersonRepository team) {
|
||||
this.restTemplate = restTemplate;
|
||||
this.events = events;
|
||||
this.team = team;
|
||||
}
|
||||
|
||||
/** The event lead's salesperson hears about it, unless nobody is assigned or they assigned it to themselves. */
|
||||
public void leadAssigned(EventLead e, Lead lead, Long actorUserId) {
|
||||
Long to = e.getSalespersonUserId();
|
||||
if (to == null || to.equals(actorUserId)) return;
|
||||
send(List.of(row(to, e, lead, "LEAD_ASSIGNED", "NoticeLeadAssigned")));
|
||||
}
|
||||
|
||||
/** A landing-page sign-up: its salesperson, else everyone on the event's sales team, else nobody. */
|
||||
public void webSignup(EventLead e, Lead lead) {
|
||||
Set<Long> to = new LinkedHashSet<>();
|
||||
if (e.getSalespersonUserId() != null) {
|
||||
to.add(e.getSalespersonUserId());
|
||||
} else {
|
||||
team.findByEventUuid(e.getEventUuid()).stream().map(EventSalesperson::getUserId).forEach(to::add);
|
||||
}
|
||||
if (to.isEmpty()) return;
|
||||
List<Map<String, Object>> rows = new ArrayList<>();
|
||||
for (Long userId : to) rows.add(row(userId, e, lead, "WEB_SIGNUP", "NoticeWebSignup"));
|
||||
send(rows);
|
||||
}
|
||||
|
||||
private Map<String, Object> row(long userId, EventLead e, Lead lead, String type, String title) {
|
||||
Map<String, Object> params = new LinkedHashMap<>();
|
||||
params.put("lead", Objects.toString(lead.getName(), ""));
|
||||
params.put("event", events.findByUuid(e.getEventUuid()).map(Event::getName).orElse(""));
|
||||
return row(userId, e.getCompanyId(), type, title, Map.of("code", "NoticeLeadBody", "params", params),
|
||||
"/leads/" + lead.getUuid(), null);
|
||||
}
|
||||
|
||||
/** One bell row; {@code message} is a code and its parameters, rendered in the reader's language. */
|
||||
static Map<String, Object> row(long userId, Long companyId, String type, String title, Map<String, Object> message,
|
||||
String link, String collapseKey) {
|
||||
Map<String, Object> r = new LinkedHashMap<>();
|
||||
r.put("receiver", String.valueOf(userId));
|
||||
r.put("companyId", companyId);
|
||||
r.put("type", type);
|
||||
r.put("title", title);
|
||||
try {
|
||||
r.put("message", JSON.writeValueAsString(message));
|
||||
} catch (JsonProcessingException ex) {
|
||||
throw new IllegalStateException(ex);
|
||||
}
|
||||
r.put("link", link);
|
||||
r.put("collapseKey", collapseKey);
|
||||
return r;
|
||||
}
|
||||
|
||||
void send(List<Map<String, Object>> rows) {
|
||||
if (!TransactionSynchronizationManager.isSynchronizationActive()) {
|
||||
post(rows);
|
||||
return;
|
||||
}
|
||||
TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronization() {
|
||||
@Override public void afterCommit() { post(rows); }
|
||||
});
|
||||
}
|
||||
|
||||
private void post(List<Map<String, Object>> rows) {
|
||||
try {
|
||||
restTemplate.postForLocation(URL, rows);
|
||||
} catch (Exception e) {
|
||||
// ponytail: no retry; the lead, chat and follow-up date are still there. Pub/Sub if losses show up.
|
||||
log.warn("Staff notification ({} rows, type {}) failed: {}", rows.size(), rows.get(0).get("type"), e.getClass().getSimpleName());
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user