feat: event invitations, guest list and door check-in #37
No files matched your search
+7
@@ -1,6 +1,7 @@
|
||||
package com.solvway.services.counter.controller;
|
||||
|
||||
import com.solvway.services.counter.model.Event;
|
||||
import com.solvway.services.counter.service.EventHeadcountService;
|
||||
import com.solvway.services.counter.service.EventService;
|
||||
import com.solvway.services.counter.util.paging.PageableRest;
|
||||
import lombok.AllArgsConstructor;
|
||||
@@ -22,6 +23,7 @@ import java.util.Optional;
|
||||
public class EventController {
|
||||
private final EventService eventService;
|
||||
private final ModelMapper modelMapper;
|
||||
private final EventHeadcountService headcountService;
|
||||
@GetMapping("/findAll")
|
||||
public List<Event> findAll(){
|
||||
return eventService.findAll();
|
||||
@@ -35,6 +37,11 @@ public class EventController {
|
||||
return eventService.findByUuid(uuid);
|
||||
}
|
||||
|
||||
@PostMapping("/attendance")
|
||||
public EventHeadcountService.Headcount attendance(@RequestBody EventHeadcountService.Request req) {
|
||||
return headcountService.headcount(req);
|
||||
}
|
||||
|
||||
@PostMapping("/save")
|
||||
public Event save(@RequestBody Event event) {
|
||||
return eventService.save(event);
|
||||
|
||||
+7
-2
@@ -3,7 +3,12 @@ package com.solvway.services.counter.dto;
|
||||
import java.time.LocalDate;
|
||||
import java.util.List;
|
||||
|
||||
/** Replaces an event lead's working fields and its interest set. Values arrive validated by mobile-api. */
|
||||
/** 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) {}
|
||||
String doneBy, Boolean attended) {
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -24,6 +24,8 @@ public class Event implements Serializable {
|
||||
private LocalDateTime dateEnd;
|
||||
@Column(name = "company_id", updatable = false)
|
||||
private Long companyId;
|
||||
private Integer capacity;
|
||||
private Integer invitationCap;
|
||||
private String uploadFloorPlan;
|
||||
private String uploadStageActivities;
|
||||
private String uploadLuckyDraw;
|
||||
|
||||
+68
@@ -0,0 +1,68 @@
|
||||
package com.solvway.services.counter.model;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
/** A person invited to an event; linked to an event lead once they register or are captured. */
|
||||
@Getter
|
||||
@Setter
|
||||
@Entity
|
||||
@Table(name = "event_guest", uniqueConstraints = {
|
||||
@UniqueConstraint(columnNames = {"token"}),
|
||||
@UniqueConstraint(columnNames = {"event_uuid", "mobile_e164"}),
|
||||
@UniqueConstraint(columnNames = {"event_uuid", "email"})
|
||||
})
|
||||
public class EventGuest {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
|
||||
@Column(name = "company_id")
|
||||
private long companyId;
|
||||
|
||||
@Column(name = "event_uuid")
|
||||
private long eventUuid;
|
||||
|
||||
private String name;
|
||||
|
||||
@Column(name = "mobile_e164")
|
||||
private String mobileE164;
|
||||
|
||||
private String email;
|
||||
|
||||
@Column(name = "company_name")
|
||||
private String companyName;
|
||||
|
||||
@Column(name = "salesperson_user_id")
|
||||
private Long salespersonUserId;
|
||||
|
||||
@Column(length = 32)
|
||||
private String token;
|
||||
|
||||
@Column(name = "invited_at")
|
||||
private LocalDateTime invitedAt;
|
||||
|
||||
@Column(name = "invite_sent_count")
|
||||
private int inviteSentCount = 0;
|
||||
|
||||
@Column(name = "registered_at")
|
||||
private LocalDateTime registeredAt;
|
||||
|
||||
@Column(name = "event_lead_id")
|
||||
private Long eventLeadId;
|
||||
|
||||
@Column(name = "created_by", updatable = false)
|
||||
private String createdBy;
|
||||
|
||||
@Column(name = "created_at", updatable = false)
|
||||
private LocalDateTime createdAt = LocalDateTime.now();
|
||||
|
||||
@Column(name = "updated_by")
|
||||
private String updatedBy;
|
||||
|
||||
@Column(name = "updated_at")
|
||||
private LocalDateTime updatedAt;
|
||||
}
|
||||
+3
@@ -55,6 +55,9 @@ public class EventLead {
|
||||
|
||||
private Long consentNoticeId;
|
||||
|
||||
@Column(name = "attended_at")
|
||||
private LocalDateTime attendedAt;
|
||||
|
||||
@Column(updatable = false)
|
||||
private LocalDateTime capturedAt = LocalDateTime.now();
|
||||
|
||||
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
package com.solvway.services.counter.repo;
|
||||
|
||||
import com.solvway.services.counter.model.EventGuest;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
@Repository
|
||||
public interface EventGuestRepository extends JpaRepository<EventGuest, Long> {
|
||||
Optional<EventGuest> findFirstByEventUuidAndEventLeadIdIsNullAndMobileE164(long eventUuid, String mobileE164);
|
||||
|
||||
Optional<EventGuest> findFirstByEventUuidAndEventLeadIdIsNullAndEmail(long eventUuid, String email);
|
||||
|
||||
boolean existsByEventLeadId(Long eventLeadId);
|
||||
|
||||
long countByEventUuid(long eventUuid);
|
||||
|
||||
long countByEventUuidAndEventLeadIdIsNotNull(long eventUuid);
|
||||
}
|
||||
+7
@@ -40,6 +40,13 @@ public interface EventLeadRepository extends JpaRepository<EventLead, Long> {
|
||||
@Param("status") String status, @Param("interestId") Long interestId,
|
||||
@Param("leadType") String leadType, Pageable pageable);
|
||||
|
||||
long countByEventUuidAndAttendedAtIsNotNull(long eventUuid);
|
||||
|
||||
/** Event leads of the event that no guest row links to. */
|
||||
@Query("SELECT COUNT(e) FROM EventLead e WHERE e.eventUuid = :eventUuid"
|
||||
+ " AND NOT EXISTS (SELECT 1 FROM EventGuest g WHERE g.eventLeadId = e.id)")
|
||||
long countWalkIns(@Param("eventUuid") long eventUuid);
|
||||
|
||||
/** Rows are [EventLead, Event]; Event is null when the event row is gone. Newest capture first. */
|
||||
@Query("SELECT e, ev FROM EventLead e LEFT JOIN Event ev ON ev.uuid = e.eventUuid"
|
||||
+ " WHERE e.companyId = :companyId AND e.leadUuid = :leadUuid ORDER BY e.capturedAt DESC, e.id DESC")
|
||||
|
||||
+33
@@ -0,0 +1,33 @@
|
||||
package com.solvway.services.counter.service;
|
||||
|
||||
import com.solvway.services.counter.repo.EventGuestRepository;
|
||||
import com.solvway.services.counter.repo.EventLeadRepository;
|
||||
import com.solvway.services.counter.repo.EventRepository;
|
||||
import lombok.AllArgsConstructor;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.server.ResponseStatusException;
|
||||
|
||||
/** Invited / registered / attended numbers of one event. */
|
||||
@Service
|
||||
@AllArgsConstructor
|
||||
public class EventHeadcountService {
|
||||
public record Request(Long companyId, long eventUuid) {}
|
||||
/** total = registered guests + walk-ins (event leads with no guest row). */
|
||||
public record Headcount(long invited, Integer cap, long registered, long attended, long walkIns, long total, Integer capacity) {}
|
||||
|
||||
private final EventRepository eventRepository;
|
||||
private final EventGuestRepository guestRepository;
|
||||
private final EventLeadRepository eventLeadRepository;
|
||||
|
||||
public Headcount headcount(Request req) {
|
||||
var event = eventRepository.findByUuid(req.eventUuid())
|
||||
.filter(e -> req.companyId() != null && req.companyId().equals(e.getCompanyId()))
|
||||
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "EVENT_NOT_FOUND"));
|
||||
long registered = guestRepository.countByEventUuidAndEventLeadIdIsNotNull(req.eventUuid());
|
||||
long walkIns = eventLeadRepository.countWalkIns(req.eventUuid());
|
||||
return new Headcount(guestRepository.countByEventUuid(req.eventUuid()), event.getInvitationCap(), registered,
|
||||
eventLeadRepository.countByEventUuidAndAttendedAtIsNotNull(req.eventUuid()), walkIns, registered + walkIns,
|
||||
event.getCapacity());
|
||||
}
|
||||
}
|
||||
+30
-1
@@ -14,8 +14,11 @@ import com.solvway.services.counter.model.Lead;
|
||||
import com.solvway.services.counter.repo.EventLeadInterestRepository;
|
||||
import com.solvway.services.counter.repo.EventLeadRepository;
|
||||
import com.solvway.services.counter.repo.EventLeadStatusChangeRepository;
|
||||
import com.solvway.services.counter.repo.EventRepository;
|
||||
import com.solvway.services.counter.repo.LeadRepository;
|
||||
import com.solvway.services.counter.util.EventDays;
|
||||
import lombok.AllArgsConstructor;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.dao.DataIntegrityViolationException;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
@@ -39,7 +42,7 @@ import java.util.stream.Collectors;
|
||||
* Never log a lead's name, mobile or email.
|
||||
*/
|
||||
@Service
|
||||
@AllArgsConstructor
|
||||
@AllArgsConstructor(onConstructor_ = @Autowired)
|
||||
public class EventLeadService {
|
||||
private static final int MAX_MATCHES = 20;
|
||||
private static final int MAX_PAGE_SIZE = 100;
|
||||
@@ -48,6 +51,25 @@ public class EventLeadService {
|
||||
private final EventLeadRepository eventLeadRepository;
|
||||
private final EventLeadInterestRepository eventLeadInterestRepository;
|
||||
private final EventLeadStatusChangeRepository eventLeadStatusChangeRepository;
|
||||
private final EventRepository eventRepository;
|
||||
private final GuestLinker guestLinker;
|
||||
|
||||
// ponytail: null event repository and linker keep the pre-invitation wiring working; drop once callers pass both.
|
||||
public EventLeadService(LeadRepository leadRepository, EventLeadRepository eventLeadRepository,
|
||||
EventLeadInterestRepository eventLeadInterestRepository,
|
||||
EventLeadStatusChangeRepository eventLeadStatusChangeRepository) {
|
||||
this(leadRepository, eventLeadRepository, eventLeadInterestRepository, eventLeadStatusChangeRepository, null, null);
|
||||
}
|
||||
|
||||
/** True when today is one of the event's days. */
|
||||
public boolean isDuringEvent(long eventUuid) {
|
||||
return eventRepository != null && eventRepository.findByUuid(eventUuid)
|
||||
.map(e -> EventDays.isDuring(e, LocalDateTime.now())).orElse(false);
|
||||
}
|
||||
|
||||
public void linkGuest(long eventUuid, EventLead eventLead, Lead lead) {
|
||||
if (guestLinker != null) guestLinker.link(eventUuid, eventLead, lead);
|
||||
}
|
||||
|
||||
public List<LeadMatch> findMatches(LeadMatchRequest req) {
|
||||
requireCompany(req.companyId());
|
||||
@@ -111,6 +133,7 @@ public class EventLeadService {
|
||||
eventLead.setNextFollowUpDate(req.nextFollowUpDate());
|
||||
eventLead.setNotes(req.notes());
|
||||
eventLead.setCapturedBy(req.doneBy());
|
||||
if (isDuringEvent(req.eventUuid())) eventLead.setAttendedAt(LocalDateTime.now());
|
||||
try {
|
||||
eventLead = eventLeadRepository.saveAndFlush(eventLead);
|
||||
} catch (DataIntegrityViolationException e) {
|
||||
@@ -119,6 +142,7 @@ public class EventLeadService {
|
||||
}
|
||||
|
||||
recordStatusChange(eventLead, null, eventLead.getStatus(), eventLead.getCapturedAt(), req.doneBy());
|
||||
linkGuest(req.eventUuid(), eventLead, lead);
|
||||
List<Long> interestIds = saveInterests(eventLead.getId(), req.companyId(), req.interestIds());
|
||||
return detail(eventLead, lead, interestIds);
|
||||
}
|
||||
@@ -162,6 +186,11 @@ public class EventLeadService {
|
||||
if (req.status() != null) eventLead.setStatus(req.status());
|
||||
eventLead.setNextFollowUpDate(req.nextFollowUpDate());
|
||||
eventLead.setNotes(blankToNull(req.notes()));
|
||||
if (Boolean.TRUE.equals(req.attended())) {
|
||||
if (eventLead.getAttendedAt() == null) eventLead.setAttendedAt(LocalDateTime.now());
|
||||
} else if (Boolean.FALSE.equals(req.attended())) {
|
||||
eventLead.setAttendedAt(null);
|
||||
}
|
||||
eventLead.setUpdatedBy(req.doneBy());
|
||||
eventLead.setUpdatedAt(LocalDateTime.now());
|
||||
eventLead = eventLeadRepository.saveAndFlush(eventLead);
|
||||
|
||||
+18
-2
@@ -2,12 +2,14 @@ package com.solvway.services.counter.service;
|
||||
|
||||
import com.solvway.core.enums.GeneralEnum;
|
||||
import com.solvway.services.counter.model.Event;
|
||||
import com.solvway.services.counter.repo.EventGuestRepository;
|
||||
import com.solvway.services.counter.repo.EventRepository;
|
||||
import com.solvway.services.counter.util.paging.PageableRest;
|
||||
import com.solvway.services.counter.util.redis.RedisUtil;
|
||||
import jakarta.annotation.PostConstruct;
|
||||
import jakarta.transaction.Transactional;
|
||||
import lombok.AllArgsConstructor;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.scheduling.annotation.EnableScheduling;
|
||||
@@ -24,11 +26,17 @@ import java.util.concurrent.TimeUnit;
|
||||
|
||||
@Service
|
||||
@Slf4j
|
||||
@AllArgsConstructor
|
||||
@AllArgsConstructor(onConstructor_ = @Autowired)
|
||||
@EnableScheduling
|
||||
public class EventService {
|
||||
private final EventRepository eventRepository;
|
||||
private final RedisUtil<Event> redisUtil;
|
||||
private final EventGuestRepository guestRepository;
|
||||
|
||||
// ponytail: null guest repository skips the cap-below-guests check; drop once callers pass one.
|
||||
public EventService(EventRepository eventRepository, RedisUtil<Event> redisUtil) {
|
||||
this(eventRepository, redisUtil, null);
|
||||
}
|
||||
private static final String EVENT_CACHE_PREFIX = "service:counter:event:";
|
||||
private static final String EVENT_CACHE_CURRENT_PREFIX="service:counter:event:current:";
|
||||
@PostConstruct
|
||||
@@ -38,12 +46,20 @@ public class EventService {
|
||||
}
|
||||
@Transactional
|
||||
public Event save(Event event) {
|
||||
if (event.getCapacity() != null && event.getCapacity() < 1) throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "CAPACITY_INVALID");
|
||||
if (event.getInvitationCap() != null && event.getInvitationCap() < 1) throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "INVITATION_CAP_INVALID");
|
||||
if (event.getId() == null) {
|
||||
if (event.getCompanyId() == null) throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "COMPANY_REQUIRED");
|
||||
} else {
|
||||
// company is immutable: always carry the stored one so entity and cache match the DB
|
||||
Optional<Event> stored = eventRepository.findById(event.getId());
|
||||
if (stored.isPresent()) event.setCompanyId(stored.get().getCompanyId());
|
||||
if (stored.isPresent()) {
|
||||
event.setCompanyId(stored.get().getCompanyId());
|
||||
if (event.getInvitationCap() != null && guestRepository != null
|
||||
&& guestRepository.countByEventUuid(stored.get().getUuid()) > event.getInvitationCap()) {
|
||||
throw new ResponseStatusException(HttpStatus.CONFLICT, "CAP_BELOW_GUESTS");
|
||||
}
|
||||
}
|
||||
}
|
||||
event = eventRepository.save(event);
|
||||
updateEventCache(event);
|
||||
|
||||
+33
@@ -0,0 +1,33 @@
|
||||
package com.solvway.services.counter.service;
|
||||
|
||||
import com.solvway.services.counter.model.EventGuest;
|
||||
import com.solvway.services.counter.model.EventLead;
|
||||
import com.solvway.services.counter.model.Lead;
|
||||
import com.solvway.services.counter.repo.EventGuestRepository;
|
||||
import lombok.AllArgsConstructor;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.Optional;
|
||||
|
||||
/** Links an unlinked guest of the event to the event lead that matches by mobile, else email. Never logs contact data. */
|
||||
@Service
|
||||
@AllArgsConstructor
|
||||
public class GuestLinker {
|
||||
private final EventGuestRepository guests;
|
||||
|
||||
// ponytail: unlocked read-modify-write; two concurrent captures could link the same guest (last write wins). Upgrade: SELECT ... FOR UPDATE on the guest row.
|
||||
public void link(long eventUuid, EventLead eventLead, Lead lead) {
|
||||
if (guests.existsByEventLeadId(eventLead.getId())) return;
|
||||
Optional<EventGuest> guest = lead.getMobileE164() == null ? Optional.empty()
|
||||
: guests.findFirstByEventUuidAndEventLeadIdIsNullAndMobileE164(eventUuid, lead.getMobileE164());
|
||||
if (guest.isEmpty() && lead.getEmailNormalised() != null) {
|
||||
guest = guests.findFirstByEventUuidAndEventLeadIdIsNullAndEmail(eventUuid, lead.getEmailNormalised());
|
||||
}
|
||||
guest.ifPresent(g -> {
|
||||
g.setEventLeadId(eventLead.getId());
|
||||
if (g.getRegisteredAt() == null) g.setRegisteredAt(LocalDateTime.now());
|
||||
guests.save(g);
|
||||
});
|
||||
}
|
||||
}
|
||||
+10
-6
@@ -19,7 +19,9 @@ import java.time.LocalDateTime;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Base64;
|
||||
import java.util.Comparator;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
|
||||
/** QR registration codes per event: one event code and one personal code per salesperson. */
|
||||
@@ -34,7 +36,7 @@ public class RegistrationLinkService {
|
||||
public record LinkView(long eventUuid, Long salespersonUserId, String token, boolean enabled) {}
|
||||
public record LinkLookup(long companyId, long eventUuid, Long salespersonUserId, String token, boolean enabled) {}
|
||||
public record MineRow(String eventUuid, String eventName, String startDate, String endDate, String location,
|
||||
String token, boolean enabled) {}
|
||||
String token, boolean enabled, String addedAt) {}
|
||||
|
||||
private final RegistrationLinkRepository linkRepository;
|
||||
private final EventRepository eventRepository;
|
||||
@@ -73,16 +75,18 @@ public class RegistrationLinkService {
|
||||
@Transactional
|
||||
public List<MineRow> mine(MineRequest req) {
|
||||
LocalDate today = LocalDate.parse(req.today());
|
||||
List<Long> uuids = eventSalespersonRepository.findByCompanyIdAndUserId(req.companyId(), req.userId()).stream()
|
||||
.map(EventSalesperson::getEventUuid).toList();
|
||||
if (uuids.isEmpty()) return List.of();
|
||||
return eventRepository.findByCompanyIdAndUuidIn(req.companyId(), uuids).stream()
|
||||
Map<Long, LocalDateTime> addedAt = new HashMap<>();
|
||||
eventSalespersonRepository.findByCompanyIdAndUserId(req.companyId(), req.userId())
|
||||
.forEach(r -> addedAt.putIfAbsent(r.getEventUuid(), r.getCreateDateTime()));
|
||||
if (addedAt.isEmpty()) return List.of();
|
||||
return eventRepository.findByCompanyIdAndUuidIn(req.companyId(), addedAt.keySet()).stream()
|
||||
.filter(e -> e.getEnabled() == GeneralEnum.FlagYesNo.Y && e.getDateEnd() != null && !e.getDateEnd().toLocalDate().isBefore(today))
|
||||
.sorted(Comparator.comparing(Event::getDateStart, Comparator.nullsLast(Comparator.naturalOrder())))
|
||||
.map(e -> {
|
||||
RegistrationLink l = personal(req.companyId(), e.getUuid(), req.userId());
|
||||
return new MineRow(String.valueOf(e.getUuid()), e.getName(), e.getDateStart() == null ? null : e.getDateStart().toLocalDate().toString(),
|
||||
e.getDateEnd().toLocalDate().toString(), e.getLocation(), l.getToken(), l.isEnabled());
|
||||
e.getDateEnd().toLocalDate().toString(), e.getLocation(), l.getToken(), l.isEnabled(),
|
||||
addedAt.get(e.getUuid()) == null ? null : addedAt.get(e.getUuid()).toString());
|
||||
}).toList();
|
||||
}
|
||||
|
||||
|
||||
+32
-8
@@ -5,6 +5,7 @@ import com.solvway.services.counter.model.Lead;
|
||||
import com.solvway.services.counter.repo.EventLeadRepository;
|
||||
import com.solvway.services.counter.repo.LeadRepository;
|
||||
import lombok.AllArgsConstructor;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.dao.DataIntegrityViolationException;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.support.TransactionTemplate;
|
||||
@@ -19,11 +20,18 @@ import java.util.Optional;
|
||||
* Never log a lead's name, mobile or email.
|
||||
*/
|
||||
@Service
|
||||
@AllArgsConstructor
|
||||
@AllArgsConstructor(onConstructor_ = @Autowired)
|
||||
public class RegistrationService {
|
||||
public record RegisterRequest(Long companyId, long eventUuid, Long salespersonUserId, String name, String mobileE164,
|
||||
String email, String companyName, String jobTitle, List<Long> interestIds,
|
||||
Long consentNoticeId, LocalDateTime consentAt) {}
|
||||
Long consentNoticeId, LocalDateTime consentAt, boolean attend) {
|
||||
public RegisterRequest(Long companyId, long eventUuid, Long salespersonUserId, String name, String mobileE164,
|
||||
String email, String companyName, String jobTitle, List<Long> interestIds,
|
||||
Long consentNoticeId, LocalDateTime consentAt) {
|
||||
this(companyId, eventUuid, salespersonUserId, name, mobileE164, email, companyName, jobTitle, interestIds,
|
||||
consentNoticeId, consentAt, false);
|
||||
}
|
||||
}
|
||||
public record RegisterResult(String outcome, String eventLeadUuid) {}
|
||||
|
||||
private static final String QR = "qr";
|
||||
@@ -32,6 +40,13 @@ public class RegistrationService {
|
||||
private final LeadRepository leadRepository;
|
||||
private final EventLeadRepository eventLeadRepository;
|
||||
private final EventLeadService eventLeadService;
|
||||
private final GuestLinker guestLinker;
|
||||
|
||||
// ponytail: null linker keeps the pre-invitation wiring working; drop once callers pass one.
|
||||
public RegistrationService(TransactionTemplate transactionTemplate, LeadRepository leadRepository,
|
||||
EventLeadRepository eventLeadRepository, EventLeadService eventLeadService) {
|
||||
this(transactionTemplate, leadRepository, eventLeadRepository, eventLeadService, null);
|
||||
}
|
||||
|
||||
public RegisterResult register(RegisterRequest req) {
|
||||
try {
|
||||
@@ -72,7 +87,7 @@ public class RegistrationService {
|
||||
} else {
|
||||
lead = match.get();
|
||||
Optional<EventLead> captured = eventLeadRepository.findByEventUuidAndLeadUuid(req.eventUuid(), lead.getUuid());
|
||||
if (captured.isPresent()) return alreadyCaptured(captured.get(), req.salespersonUserId());
|
||||
if (captured.isPresent()) return alreadyCaptured(captured.get(), lead, req);
|
||||
notes = differences(lead, req);
|
||||
leadType = "EXISTING";
|
||||
}
|
||||
@@ -89,21 +104,30 @@ public class RegistrationService {
|
||||
e.setCapturedBy(QR);
|
||||
e.setConsentAt(req.consentAt());
|
||||
e.setConsentNoticeId(req.consentNoticeId());
|
||||
if (req.attend() || eventLeadService.isDuringEvent(req.eventUuid())) e.setAttendedAt(LocalDateTime.now());
|
||||
e = eventLeadRepository.saveAndFlush(e);
|
||||
eventLeadService.recordStatusChange(e, null, "OPEN", e.getCapturedAt(), QR);
|
||||
eventLeadService.saveInterests(e.getId(), req.companyId(), req.interestIds());
|
||||
link(req, e, lead);
|
||||
return new RegisterResult(match.isEmpty() ? "CREATED" : "LINKED", String.valueOf(e.getUuid()));
|
||||
}
|
||||
|
||||
private RegisterResult alreadyCaptured(EventLead e, Long salespersonUserId) {
|
||||
if (e.getSalespersonUserId() == null && salespersonUserId != null) {
|
||||
e.setSalespersonUserId(salespersonUserId);
|
||||
private RegisterResult alreadyCaptured(EventLead e, Lead lead, RegisterRequest req) {
|
||||
boolean assign = e.getSalespersonUserId() == null && req.salespersonUserId() != null;
|
||||
boolean attend = req.attend() && e.getAttendedAt() == null;
|
||||
if (assign) e.setSalespersonUserId(req.salespersonUserId());
|
||||
if (attend) e.setAttendedAt(LocalDateTime.now());
|
||||
if (assign || attend) {
|
||||
e.setUpdatedBy(QR);
|
||||
e.setUpdatedAt(LocalDateTime.now());
|
||||
eventLeadRepository.save(e);
|
||||
return new RegisterResult("ASSIGNED", String.valueOf(e.getUuid()));
|
||||
}
|
||||
return new RegisterResult("ALREADY_CAPTURED", String.valueOf(e.getUuid()));
|
||||
link(req, e, lead);
|
||||
return new RegisterResult(assign ? "ASSIGNED" : "ALREADY_CAPTURED", String.valueOf(e.getUuid()));
|
||||
}
|
||||
|
||||
private void link(RegisterRequest req, EventLead e, Lead lead) {
|
||||
if (guestLinker != null) guestLinker.link(req.eventUuid(), e, lead);
|
||||
}
|
||||
|
||||
/** Submitted non-blank values that differ from the lead's, as an event-lead note; null when nothing differs. */
|
||||
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
package com.solvway.services.counter.util;
|
||||
|
||||
import com.solvway.services.counter.model.Event;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
/** Event days are calendar days in the server zone: the whole first and last day count. */
|
||||
public final class EventDays {
|
||||
private EventDays() {}
|
||||
|
||||
public static boolean isDuring(Event event, LocalDateTime now) {
|
||||
if (event.getDateEnd() == null) return false;
|
||||
LocalDate day = now.toLocalDate();
|
||||
boolean started = event.getDateStart() == null || !day.isBefore(event.getDateStart().toLocalDate());
|
||||
return started && !day.isAfter(event.getDateEnd().toLocalDate());
|
||||
}
|
||||
}
|
||||
+41
@@ -0,0 +1,41 @@
|
||||
package com.solvway.services.counter.model;
|
||||
|
||||
import jakarta.persistence.Column;
|
||||
import jakarta.persistence.Table;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/** EventGuest maps the event_guest table of 01-event-guests.sql. */
|
||||
class EventGuestMappingTest {
|
||||
@Test
|
||||
void tableAndUniqueKeys() {
|
||||
Table t = EventGuest.class.getAnnotation(Table.class);
|
||||
assertThat(t.name()).isEqualTo("event_guest");
|
||||
List<List<String>> uniques = Arrays.stream(t.uniqueConstraints()).map(u -> List.of(u.columnNames())).toList();
|
||||
assertThat(uniques).containsExactlyInAnyOrder(List.of("token"), List.of("event_uuid", "mobile_e164"), List.of("event_uuid", "email"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void columnNames() throws Exception {
|
||||
assertThat(columnOf("eventUuid")).isEqualTo("event_uuid");
|
||||
assertThat(columnOf("mobileE164")).isEqualTo("mobile_e164");
|
||||
assertThat(columnOf("eventLeadId")).isEqualTo("event_lead_id");
|
||||
assertThat(columnOf("salespersonUserId")).isEqualTo("salesperson_user_id");
|
||||
assertThat(EventGuest.class.getDeclaredField("token").getAnnotation(Column.class).length()).isEqualTo(32);
|
||||
for (String f : List.of("companyId", "name", "companyName", "token", "invitedAt", "inviteSentCount", "registeredAt",
|
||||
"createdBy", "createdAt", "updatedBy", "updatedAt")) {
|
||||
assertThat(EventGuest.class.getDeclaredField(f)).isNotNull();
|
||||
}
|
||||
assertThat(new EventGuest().getInviteSentCount()).isZero();
|
||||
}
|
||||
|
||||
private static String columnOf(String field) throws Exception {
|
||||
Field f = EventGuest.class.getDeclaredField(field);
|
||||
return f.getAnnotation(Column.class).name();
|
||||
}
|
||||
}
|
||||
+265
@@ -0,0 +1,265 @@
|
||||
package com.solvway.services.counter.service;
|
||||
|
||||
import com.solvway.services.counter.dto.EventLeadCaptureRequest;
|
||||
import com.solvway.services.counter.dto.EventLeadUpdateRequest;
|
||||
import com.solvway.services.counter.model.Event;
|
||||
import com.solvway.services.counter.model.EventLead;
|
||||
import com.solvway.services.counter.model.Lead;
|
||||
import com.solvway.services.counter.repo.EventLeadInterestRepository;
|
||||
import com.solvway.services.counter.repo.EventLeadRepository;
|
||||
import com.solvway.services.counter.repo.EventLeadStatusChangeRepository;
|
||||
import com.solvway.services.counter.repo.EventRepository;
|
||||
import com.solvway.services.counter.repo.LeadRepository;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
import org.springframework.transaction.PlatformTransactionManager;
|
||||
import org.springframework.transaction.support.TransactionTemplate;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.AdditionalAnswers.returnsFirstArg;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.inOrder;
|
||||
import static org.mockito.Mockito.lenient;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/** Attendance stamped at capture (staff and QR), the attend flag, the staff edit toggle, and the guest link call. */
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class AttendanceAtCaptureTest {
|
||||
private static final long COMPANY = 42L;
|
||||
private static final long EVENT = 1000L;
|
||||
|
||||
@Mock private LeadRepository leadRepository;
|
||||
@Mock private EventLeadRepository eventLeadRepository;
|
||||
@Mock private EventLeadInterestRepository interestRepository;
|
||||
@Mock private EventLeadStatusChangeRepository statusChangeRepository;
|
||||
@Mock private EventRepository eventRepository;
|
||||
@Mock private GuestLinker guestLinker;
|
||||
|
||||
private EventLeadService eventLeadService;
|
||||
private RegistrationService registration;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
eventLeadService = new EventLeadService(leadRepository, eventLeadRepository, interestRepository, statusChangeRepository,
|
||||
eventRepository, guestLinker);
|
||||
registration = new RegistrationService(new TransactionTemplate(mock(PlatformTransactionManager.class)),
|
||||
leadRepository, eventLeadRepository, eventLeadService, guestLinker);
|
||||
lenient().when(leadRepository.saveAndFlush(any(Lead.class))).then(returnsFirstArg());
|
||||
lenient().when(eventLeadRepository.saveAndFlush(any(EventLead.class))).then(i -> {
|
||||
EventLead e = i.getArgument(0);
|
||||
e.setId(1L);
|
||||
return e;
|
||||
});
|
||||
}
|
||||
|
||||
private void eventDays(int startOffset, int endOffset) {
|
||||
Event ev = new Event();
|
||||
ev.setDateStart(LocalDateTime.now().plusDays(startOffset));
|
||||
ev.setDateEnd(LocalDateTime.now().plusDays(endOffset));
|
||||
lenient().when(eventRepository.findByUuid(EVENT)).thenReturn(Optional.of(ev));
|
||||
}
|
||||
|
||||
private EventLead staffCapture() {
|
||||
eventLeadService.capture(new EventLeadCaptureRequest(COMPANY, EVENT, null, "Aisyah", "+60123456789", null, null, null,
|
||||
7L, null, null, List.of(), null, null, "sales1"));
|
||||
ArgumentCaptor<EventLead> c = ArgumentCaptor.forClass(EventLead.class);
|
||||
verify(eventLeadRepository).saveAndFlush(c.capture());
|
||||
return c.getValue();
|
||||
}
|
||||
|
||||
private static RegistrationService.RegisterRequest qr(boolean attend) {
|
||||
return new RegistrationService.RegisterRequest(COMPANY, EVENT, 7L, "Aisyah", "+60123456789", null, null, null,
|
||||
List.of(), null, null, attend);
|
||||
}
|
||||
|
||||
private EventLead qrNewEventLead(boolean attend) {
|
||||
lenient().when(leadRepository.findFirstByCompanyIdAndMobileE164OrderByIdDesc(COMPANY, "+60123456789")).thenReturn(Optional.empty());
|
||||
registration.register(qr(attend));
|
||||
ArgumentCaptor<EventLead> c = ArgumentCaptor.forClass(EventLead.class);
|
||||
verify(eventLeadRepository).saveAndFlush(c.capture());
|
||||
return c.getValue();
|
||||
}
|
||||
|
||||
private EventLead existingCaptured(LocalDateTime attendedAt) {
|
||||
Lead lead = new Lead();
|
||||
lead.setCompanyId(COMPANY);
|
||||
EventLead existing = new EventLead();
|
||||
existing.setId(9L);
|
||||
existing.setLeadUuid(lead.getUuid());
|
||||
existing.setSalespersonUserId(7L);
|
||||
existing.setAttendedAt(attendedAt);
|
||||
when(leadRepository.findFirstByCompanyIdAndMobileE164OrderByIdDesc(COMPANY, "+60123456789")).thenReturn(Optional.of(lead));
|
||||
when(eventLeadRepository.findByEventUuidAndLeadUuid(EVENT, lead.getUuid())).thenReturn(Optional.of(existing));
|
||||
return existing;
|
||||
}
|
||||
|
||||
// --- staff capture ---
|
||||
|
||||
@Test
|
||||
void staffCaptureDuringEventDaysSetsAttendedAt() {
|
||||
eventDays(-1, 1);
|
||||
assertThat(staffCapture().getAttendedAt()).isNotNull();
|
||||
}
|
||||
|
||||
private EventLead staffCaptureOfExistingLead() {
|
||||
Lead crm = new Lead();
|
||||
crm.setName("Aisyah");
|
||||
lenient().when(leadRepository.findByUuidAndCompanyId(crm.getUuid(), COMPANY)).thenReturn(Optional.of(crm));
|
||||
eventLeadService.capture(new EventLeadCaptureRequest(COMPANY, EVENT, crm.getUuid(), "Aisyah", null, null, null, null,
|
||||
7L, null, null, List.of(), null, null, "sales1"));
|
||||
ArgumentCaptor<EventLead> c = ArgumentCaptor.forClass(EventLead.class);
|
||||
verify(eventLeadRepository).saveAndFlush(c.capture());
|
||||
return c.getValue();
|
||||
}
|
||||
|
||||
@Test
|
||||
void staffCaptureOfExistingLeadDuringEventDaysSetsAttendedAt() {
|
||||
eventDays(-1, 1);
|
||||
assertThat(staffCaptureOfExistingLead().getAttendedAt()).isNotNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
void staffCaptureOfExistingLeadBeforeEventDaysLeavesAttendedAtEmpty() {
|
||||
eventDays(2, 3);
|
||||
assertThat(staffCaptureOfExistingLead().getAttendedAt()).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
void staffCaptureBeforeEventDaysLeavesAttendedAtEmpty() {
|
||||
eventDays(2, 3);
|
||||
assertThat(staffCapture().getAttendedAt()).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
void staffCaptureLinksGuestAfterTheEventLeadIsSaved() {
|
||||
eventDays(-1, 1);
|
||||
EventLead e = staffCapture();
|
||||
var order = inOrder(eventLeadRepository, guestLinker);
|
||||
order.verify(eventLeadRepository).saveAndFlush(e);
|
||||
order.verify(guestLinker).link(org.mockito.ArgumentMatchers.eq(EVENT), org.mockito.ArgumentMatchers.same(e), any(Lead.class));
|
||||
}
|
||||
|
||||
// --- QR register ---
|
||||
|
||||
@Test
|
||||
void qrNewDuringEventDaysSetsAttendedAt() {
|
||||
eventDays(-1, 1);
|
||||
assertThat(qrNewEventLead(false).getAttendedAt()).isNotNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
void qrNewBeforeEventDaysLeavesAttendedAtEmpty() {
|
||||
eventDays(2, 3);
|
||||
assertThat(qrNewEventLead(false).getAttendedAt()).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
void attendTrueSetsAttendedAtOnNewEvenOutsideEventDays() {
|
||||
eventDays(2, 3);
|
||||
assertThat(qrNewEventLead(true).getAttendedAt()).isNotNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
void attendTrueSetsAttendedAtOnAlreadyCapturedAndSaves() {
|
||||
EventLead existing = existingCaptured(null);
|
||||
|
||||
RegistrationService.RegisterResult r = registration.register(qr(true));
|
||||
|
||||
assertThat(r.outcome()).isEqualTo("ALREADY_CAPTURED");
|
||||
assertThat(existing.getAttendedAt()).isNotNull();
|
||||
verify(eventLeadRepository).save(existing);
|
||||
}
|
||||
|
||||
@Test
|
||||
void repeatRegistrationDuringEventDaysDoesNotSetAttendedAt() {
|
||||
eventDays(-1, 1);
|
||||
EventLead existing = existingCaptured(null);
|
||||
|
||||
registration.register(qr(false));
|
||||
|
||||
assertThat(existing.getAttendedAt()).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
void attendTrueKeepsAnExistingAttendedAt() {
|
||||
LocalDateTime earlier = LocalDateTime.of(2026, 12, 1, 9, 0);
|
||||
EventLead existing = existingCaptured(earlier);
|
||||
|
||||
registration.register(qr(true));
|
||||
|
||||
assertThat(existing.getAttendedAt()).isEqualTo(earlier);
|
||||
}
|
||||
|
||||
@Test
|
||||
void qrRegisterLinksGuestForNewAndForAlreadyCaptured() {
|
||||
eventDays(-1, 1);
|
||||
EventLead existing = existingCaptured(null);
|
||||
|
||||
registration.register(qr(false));
|
||||
|
||||
verify(guestLinker).link(org.mockito.ArgumentMatchers.eq(EVENT), org.mockito.ArgumentMatchers.same(existing), any(Lead.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void qrRegisterNewLinksGuest() {
|
||||
eventDays(-1, 1);
|
||||
EventLead e = qrNewEventLead(false);
|
||||
verify(guestLinker).link(org.mockito.ArgumentMatchers.eq(EVENT), org.mockito.ArgumentMatchers.same(e), any(Lead.class));
|
||||
}
|
||||
|
||||
// --- staff edit ---
|
||||
|
||||
private EventLeadUpdateRequest update(Boolean attended) {
|
||||
return new EventLeadUpdateRequest(COMPANY, 3L, null, null, null, List.of(), null, null, "sales2", attended);
|
||||
}
|
||||
|
||||
private EventLead editable(LocalDateTime attendedAt) {
|
||||
EventLead el = new EventLead();
|
||||
el.setId(5L);
|
||||
el.setAttendedAt(attendedAt);
|
||||
lenient().when(eventLeadRepository.findByUuidAndCompanyId(3L, COMPANY)).thenReturn(Optional.of(el));
|
||||
lenient().when(eventLeadRepository.saveAndFlush(el)).thenReturn(el);
|
||||
lenient().when(leadRepository.findByUuidAndCompanyId(el.getLeadUuid(), COMPANY)).thenReturn(Optional.of(new Lead()));
|
||||
return el;
|
||||
}
|
||||
|
||||
@Test
|
||||
void updateAttendedTrueSetsTimeWhenEmpty() {
|
||||
EventLead el = editable(null);
|
||||
eventLeadService.update(update(true));
|
||||
assertThat(el.getAttendedAt()).isNotNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
void updateAttendedTrueKeepsExistingTime() {
|
||||
LocalDateTime earlier = LocalDateTime.of(2026, 12, 1, 9, 0);
|
||||
EventLead el = editable(earlier);
|
||||
eventLeadService.update(update(true));
|
||||
assertThat(el.getAttendedAt()).isEqualTo(earlier);
|
||||
}
|
||||
|
||||
@Test
|
||||
void updateAttendedFalseClears() {
|
||||
EventLead el = editable(LocalDateTime.of(2026, 12, 1, 9, 0));
|
||||
eventLeadService.update(update(false));
|
||||
assertThat(el.getAttendedAt()).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
void updateAttendedNullLeavesUnchanged() {
|
||||
LocalDateTime earlier = LocalDateTime.of(2026, 12, 1, 9, 0);
|
||||
EventLead el = editable(earlier);
|
||||
eventLeadService.update(update(null));
|
||||
assertThat(el.getAttendedAt()).isEqualTo(earlier);
|
||||
}
|
||||
}
|
||||
+79
@@ -0,0 +1,79 @@
|
||||
package com.solvway.services.counter.service;
|
||||
|
||||
import com.solvway.services.counter.model.Event;
|
||||
import com.solvway.services.counter.repo.EventGuestRepository;
|
||||
import com.solvway.services.counter.repo.EventLeadRepository;
|
||||
import com.solvway.services.counter.repo.EventRepository;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
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.when;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class EventHeadcountServiceTest {
|
||||
private static final long COMPANY = 42L;
|
||||
private static final long EVENT = 1000L;
|
||||
|
||||
@Mock private EventRepository eventRepository;
|
||||
@Mock private EventGuestRepository guestRepository;
|
||||
@Mock private EventLeadRepository eventLeadRepository;
|
||||
|
||||
private EventHeadcountService service() {
|
||||
return new EventHeadcountService(eventRepository, guestRepository, eventLeadRepository);
|
||||
}
|
||||
|
||||
@Test
|
||||
void reportsTheNumbers() {
|
||||
Event e = new Event();
|
||||
e.setCompanyId(COMPANY);
|
||||
e.setCapacity(100);
|
||||
e.setInvitationCap(50);
|
||||
when(eventRepository.findByUuid(EVENT)).thenReturn(Optional.of(e));
|
||||
when(guestRepository.countByEventUuid(EVENT)).thenReturn(40L);
|
||||
when(guestRepository.countByEventUuidAndEventLeadIdIsNotNull(EVENT)).thenReturn(30L);
|
||||
when(eventLeadRepository.countByEventUuidAndAttendedAtIsNotNull(EVENT)).thenReturn(35L);
|
||||
when(eventLeadRepository.countWalkIns(EVENT)).thenReturn(15L);
|
||||
|
||||
EventHeadcountService.Headcount h = service().headcount(new EventHeadcountService.Request(COMPANY, EVENT));
|
||||
|
||||
assertThat(h).isEqualTo(new EventHeadcountService.Headcount(40, 50, 30, 35, 15, 45, 100));
|
||||
}
|
||||
|
||||
@Test
|
||||
void nullCapAndCapacityStayNull() {
|
||||
Event e = new Event();
|
||||
e.setCompanyId(COMPANY);
|
||||
when(eventRepository.findByUuid(EVENT)).thenReturn(Optional.of(e));
|
||||
|
||||
EventHeadcountService.Headcount h = service().headcount(new EventHeadcountService.Request(COMPANY, EVENT));
|
||||
|
||||
assertThat(h.cap()).isNull();
|
||||
assertThat(h.capacity()).isNull();
|
||||
assertThat(h.total()).isZero();
|
||||
}
|
||||
|
||||
@Test
|
||||
void eventOfAnotherCompanyIs404() {
|
||||
Event e = new Event();
|
||||
e.setCompanyId(99L);
|
||||
when(eventRepository.findByUuid(EVENT)).thenReturn(Optional.of(e));
|
||||
|
||||
assertThatThrownBy(() -> service().headcount(new EventHeadcountService.Request(COMPANY, EVENT)))
|
||||
.isInstanceOfSatisfying(ResponseStatusException.class, ex -> assertThat(ex.getStatusCode().value()).isEqualTo(404));
|
||||
}
|
||||
|
||||
@Test
|
||||
void unknownEventIs404() {
|
||||
when(eventRepository.findByUuid(EVENT)).thenReturn(Optional.empty());
|
||||
|
||||
assertThatThrownBy(() -> service().headcount(new EventHeadcountService.Request(COMPANY, EVENT)))
|
||||
.isInstanceOfSatisfying(ResponseStatusException.class, ex -> assertThat(ex.getStatusCode().value()).isEqualTo(404));
|
||||
}
|
||||
}
|
||||
+99
@@ -0,0 +1,99 @@
|
||||
package com.solvway.services.counter.service;
|
||||
|
||||
import com.solvway.services.counter.model.Event;
|
||||
import com.solvway.services.counter.repo.EventGuestRepository;
|
||||
import com.solvway.services.counter.repo.EventRepository;
|
||||
import com.solvway.services.counter.util.redis.RedisUtil;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
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.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.lenient;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/** Event capacity and invitation cap on save. */
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class EventSettingsTest {
|
||||
@Mock private EventRepository eventRepository;
|
||||
@Mock private RedisUtil<Event> redisUtil;
|
||||
@Mock private EventGuestRepository guestRepository;
|
||||
|
||||
private EventService service;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
service = new EventService(eventRepository, redisUtil, guestRepository);
|
||||
lenient().when(eventRepository.save(any(Event.class))).thenAnswer(i -> i.getArgument(0));
|
||||
}
|
||||
|
||||
private static Event event(Long id, Integer capacity, Integer cap) {
|
||||
Event e = new Event();
|
||||
e.setId(id);
|
||||
e.setCompanyId(7L);
|
||||
e.setCapacity(capacity);
|
||||
e.setInvitationCap(cap);
|
||||
return e;
|
||||
}
|
||||
|
||||
private void assertRejected(Event e, int status, String reason) {
|
||||
assertThatThrownBy(() -> service.save(e)).isInstanceOfSatisfying(ResponseStatusException.class, ex -> {
|
||||
assertThat(ex.getStatusCode().value()).isEqualTo(status);
|
||||
assertThat(ex.getReason()).isEqualTo(reason);
|
||||
});
|
||||
verify(eventRepository, never()).save(any(Event.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void nullAndPositiveValuesAreAccepted() {
|
||||
assertThat(service.save(event(null, null, null)).getCapacity()).isNull();
|
||||
Event saved = service.save(event(null, 100, 1));
|
||||
assertThat(saved.getCapacity()).isEqualTo(100);
|
||||
assertThat(saved.getInvitationCap()).isEqualTo(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
void capacityBelowOneIsRefused() {
|
||||
assertRejected(event(null, 0, null), 400, "CAPACITY_INVALID");
|
||||
}
|
||||
|
||||
@Test
|
||||
void invitationCapBelowOneIsRefused() {
|
||||
assertRejected(event(null, null, -3), 400, "INVITATION_CAP_INVALID");
|
||||
}
|
||||
|
||||
@Test
|
||||
void capBelowCurrentGuestCountIsConflict() {
|
||||
Event stored = event(5L, null, null);
|
||||
when(eventRepository.findById(5L)).thenReturn(Optional.of(stored));
|
||||
when(guestRepository.countByEventUuid(stored.getUuid())).thenReturn(40L);
|
||||
|
||||
assertRejected(event(5L, null, 39), 409, "CAP_BELOW_GUESTS");
|
||||
}
|
||||
|
||||
@Test
|
||||
void capEqualToGuestCountIsAccepted() {
|
||||
Event stored = event(5L, null, null);
|
||||
when(eventRepository.findById(5L)).thenReturn(Optional.of(stored));
|
||||
when(guestRepository.countByEventUuid(stored.getUuid())).thenReturn(40L);
|
||||
|
||||
assertThat(service.save(event(5L, null, 40)).getInvitationCap()).isEqualTo(40);
|
||||
}
|
||||
|
||||
@Test
|
||||
void eventExposesTheTwoFieldsAsJsonProperties() throws Exception {
|
||||
Event e = event(1L, 100, 50);
|
||||
var json = new com.fasterxml.jackson.databind.ObjectMapper().findAndRegisterModules().valueToTree(e);
|
||||
assertThat(json.get("capacity").asInt()).isEqualTo(100);
|
||||
assertThat(json.get("invitationCap").asInt()).isEqualTo(50);
|
||||
}
|
||||
}
|
||||
+103
@@ -0,0 +1,103 @@
|
||||
package com.solvway.services.counter.service;
|
||||
|
||||
import com.solvway.services.counter.model.EventGuest;
|
||||
import com.solvway.services.counter.model.EventLead;
|
||||
import com.solvway.services.counter.model.Lead;
|
||||
import com.solvway.services.counter.repo.EventGuestRepository;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.Optional;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.lenient;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class GuestLinkerTest {
|
||||
private static final long EVENT = 1000L;
|
||||
private static final String MOBILE = "+60123456789";
|
||||
private static final String EMAIL = "aisyah@example.com";
|
||||
|
||||
@Mock private EventGuestRepository guests;
|
||||
private GuestLinker linker;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
linker = new GuestLinker(guests);
|
||||
lenient().when(guests.findFirstByEventUuidAndEventLeadIdIsNullAndMobileE164(any(Long.class), any())).thenReturn(Optional.empty());
|
||||
lenient().when(guests.findFirstByEventUuidAndEventLeadIdIsNullAndEmail(any(Long.class), any())).thenReturn(Optional.empty());
|
||||
}
|
||||
|
||||
private static EventLead eventLead() {
|
||||
EventLead e = new EventLead();
|
||||
e.setId(55L);
|
||||
return e;
|
||||
}
|
||||
|
||||
private static Lead lead(String mobile, String email) {
|
||||
Lead l = new Lead();
|
||||
l.setMobileE164(mobile);
|
||||
l.setEmailNormalised(email);
|
||||
return l;
|
||||
}
|
||||
|
||||
@Test
|
||||
void linksByMobileAndStampsRegisteredAt() {
|
||||
EventGuest g = new EventGuest();
|
||||
when(guests.findFirstByEventUuidAndEventLeadIdIsNullAndMobileE164(EVENT, MOBILE)).thenReturn(Optional.of(g));
|
||||
|
||||
linker.link(EVENT, eventLead(), lead(MOBILE, EMAIL));
|
||||
|
||||
assertThat(g.getEventLeadId()).isEqualTo(55L);
|
||||
assertThat(g.getRegisteredAt()).isNotNull();
|
||||
verify(guests).save(g);
|
||||
verify(guests, never()).findFirstByEventUuidAndEventLeadIdIsNullAndEmail(any(Long.class), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void fallsBackToEmailWhenNoMobileMatch() {
|
||||
EventGuest g = new EventGuest();
|
||||
when(guests.findFirstByEventUuidAndEventLeadIdIsNullAndEmail(EVENT, EMAIL)).thenReturn(Optional.of(g));
|
||||
|
||||
linker.link(EVENT, eventLead(), lead(MOBILE, EMAIL));
|
||||
|
||||
assertThat(g.getEventLeadId()).isEqualTo(55L);
|
||||
verify(guests).save(g);
|
||||
}
|
||||
|
||||
@Test
|
||||
void keepsExistingRegisteredAt() {
|
||||
EventGuest g = new EventGuest();
|
||||
LocalDateTime earlier = LocalDateTime.of(2026, 12, 1, 9, 0);
|
||||
g.setRegisteredAt(earlier);
|
||||
when(guests.findFirstByEventUuidAndEventLeadIdIsNullAndEmail(EVENT, EMAIL)).thenReturn(Optional.of(g));
|
||||
|
||||
linker.link(EVENT, eventLead(), lead(null, EMAIL));
|
||||
|
||||
assertThat(g.getRegisteredAt()).isEqualTo(earlier);
|
||||
}
|
||||
|
||||
@Test
|
||||
void noOpWhenEventLeadAlreadyLinked() {
|
||||
when(guests.existsByEventLeadId(55L)).thenReturn(true);
|
||||
|
||||
linker.link(EVENT, eventLead(), lead(MOBILE, EMAIL));
|
||||
|
||||
verify(guests, never()).save(any());
|
||||
verify(guests, never()).findFirstByEventUuidAndEventLeadIdIsNullAndMobileE164(any(Long.class), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void noGuestNothingSaved() {
|
||||
linker.link(EVENT, eventLead(), lead(MOBILE, EMAIL));
|
||||
verify(guests, never()).save(any());
|
||||
}
|
||||
}
|
||||
+43
@@ -0,0 +1,43 @@
|
||||
package com.solvway.services.counter.service;
|
||||
|
||||
import com.solvway.services.counter.repo.EventGuestRepository;
|
||||
import com.solvway.services.counter.repo.EventLeadInterestRepository;
|
||||
import com.solvway.services.counter.repo.EventLeadRepository;
|
||||
import com.solvway.services.counter.repo.EventLeadStatusChangeRepository;
|
||||
import com.solvway.services.counter.repo.EventRepository;
|
||||
import com.solvway.services.counter.repo.LeadRepository;
|
||||
import com.solvway.services.counter.util.redis.RedisUtil;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.test.util.ReflectionTestUtils;
|
||||
import org.springframework.transaction.support.TransactionTemplate;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
/** Spring must pick the full constructors, so production always gets the linker and the extra repositories. */
|
||||
class InvitationWiringTest {
|
||||
private static <T> void mockBean(AnnotationConfigApplicationContext ctx, Class<T> c) {
|
||||
ctx.registerBean(c, () -> mock(c));
|
||||
}
|
||||
|
||||
@Test
|
||||
void servicesAreWiredWithTheirFullDependencies() {
|
||||
try (var ctx = new AnnotationConfigApplicationContext()) {
|
||||
for (Class<?> c : new Class<?>[]{LeadRepository.class, EventLeadRepository.class, EventLeadInterestRepository.class,
|
||||
EventLeadStatusChangeRepository.class, EventRepository.class, EventGuestRepository.class,
|
||||
RedisUtil.class, TransactionTemplate.class}) {
|
||||
mockBean(ctx, c);
|
||||
}
|
||||
ctx.register(GuestLinker.class, EventLeadService.class, RegistrationService.class, EventService.class,
|
||||
EventHeadcountService.class);
|
||||
ctx.refresh();
|
||||
|
||||
assertThat(ReflectionTestUtils.getField(ctx.getBean(EventLeadService.class), "guestLinker")).isNotNull();
|
||||
assertThat(ReflectionTestUtils.getField(ctx.getBean(EventLeadService.class), "eventRepository")).isNotNull();
|
||||
assertThat(ReflectionTestUtils.getField(ctx.getBean(RegistrationService.class), "guestLinker")).isNotNull();
|
||||
assertThat(ReflectionTestUtils.getField(ctx.getBean(EventService.class), "guestRepository")).isNotNull();
|
||||
assertThat(ctx.getBean(EventHeadcountService.class)).isNotNull();
|
||||
}
|
||||
}
|
||||
}
|
||||
+67
@@ -0,0 +1,67 @@
|
||||
package com.solvway.services.counter.service;
|
||||
|
||||
import com.solvway.core.enums.GeneralEnum;
|
||||
import com.solvway.services.counter.model.Event;
|
||||
import com.solvway.services.counter.model.EventSalesperson;
|
||||
import com.solvway.services.counter.model.RegistrationLink;
|
||||
import com.solvway.services.counter.repo.EventRepository;
|
||||
import com.solvway.services.counter.repo.EventSalespersonRepository;
|
||||
import com.solvway.services.counter.repo.RegistrationLinkRepository;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.ArgumentMatchers.anyCollection;
|
||||
import static org.mockito.ArgumentMatchers.anyLong;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class RegistrationLinkMineAddedAtTest {
|
||||
@Mock private RegistrationLinkRepository linkRepository;
|
||||
@Mock private EventRepository eventRepository;
|
||||
@Mock private EventSalespersonRepository eventSalespersonRepository;
|
||||
|
||||
@Test
|
||||
void mineReturnsTheTeamRowCreateTimeAsAddedAt() throws Exception {
|
||||
LocalDateTime added = LocalDateTime.of(2026, 11, 20, 14, 30);
|
||||
assertThat(mineAddedAt(added)).isEqualTo(added.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void mineReturnsNullAddedAtWhenCreateTimeIsMissing() throws Exception {
|
||||
assertThat(mineAddedAt(null)).isNull();
|
||||
}
|
||||
|
||||
private String mineAddedAt(LocalDateTime added) throws Exception {
|
||||
EventSalesperson row = new EventSalesperson();
|
||||
row.setEventUuid(1L);
|
||||
row.setCreateDateTime(added);
|
||||
when(eventSalespersonRepository.findByCompanyIdAndUserId(42L, 7L)).thenReturn(List.of(row));
|
||||
Event ev = new Event();
|
||||
Field uuid = Event.class.getDeclaredField("uuid");
|
||||
uuid.setAccessible(true);
|
||||
uuid.set(ev, 1L);
|
||||
ev.setCompanyId(42L);
|
||||
ev.setName("Expo");
|
||||
ev.setEnabled(GeneralEnum.FlagYesNo.Y);
|
||||
ev.setDateEnd(LocalDateTime.of(2026, 12, 12, 18, 0));
|
||||
when(eventRepository.findByCompanyIdAndUuidIn(eq(42L), anyCollection())).thenReturn(List.of(ev));
|
||||
RegistrationLink link = new RegistrationLink();
|
||||
link.setToken("T");
|
||||
when(linkRepository.findFirstByEventUuidAndSalespersonUserIdOrderByIdAsc(anyLong(), eq(7L))).thenReturn(Optional.of(link));
|
||||
|
||||
var out = new RegistrationLinkService(linkRepository, eventRepository, eventSalespersonRepository)
|
||||
.mine(new RegistrationLinkService.MineRequest(42L, 7L, "2026-12-01"));
|
||||
|
||||
assertThat(out).hasSize(1);
|
||||
return out.get(0).addedAt();
|
||||
}
|
||||
}
|
||||
+51
@@ -0,0 +1,51 @@
|
||||
package com.solvway.services.counter.util;
|
||||
|
||||
import com.solvway.services.counter.model.Event;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
class EventDaysTest {
|
||||
private static Event event(LocalDateTime start, LocalDateTime end) {
|
||||
Event e = new Event();
|
||||
e.setDateStart(start);
|
||||
e.setDateEnd(end);
|
||||
return e;
|
||||
}
|
||||
|
||||
private static final Event EVENT = event(LocalDateTime.of(2026, 12, 5, 18, 0), LocalDateTime.of(2026, 12, 7, 9, 0));
|
||||
|
||||
@Test
|
||||
void firstDayCountsEvenBeforeTheStartTime() {
|
||||
assertThat(EventDays.isDuring(EVENT, LocalDateTime.of(2026, 12, 5, 0, 1))).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
void lastDayCountsEvenAfterTheEndTime() {
|
||||
assertThat(EventDays.isDuring(EVENT, LocalDateTime.of(2026, 12, 7, 23, 59))).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
void dayBeforeDoesNotCount() {
|
||||
assertThat(EventDays.isDuring(EVENT, LocalDateTime.of(2026, 12, 4, 23, 59))).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
void dayAfterDoesNotCount() {
|
||||
assertThat(EventDays.isDuring(EVENT, LocalDateTime.of(2026, 12, 8, 0, 0))).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
void nullStartOnlyTheEndCounts() {
|
||||
Event e = event(null, LocalDateTime.of(2026, 12, 7, 9, 0));
|
||||
assertThat(EventDays.isDuring(e, LocalDateTime.of(2020, 1, 1, 0, 0))).isTrue();
|
||||
assertThat(EventDays.isDuring(e, LocalDateTime.of(2026, 12, 8, 0, 0))).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
void nullEndIsNeverDuring() {
|
||||
assertThat(EventDays.isDuring(event(LocalDateTime.of(2026, 12, 5, 18, 0), null), LocalDateTime.of(2026, 12, 5, 19, 0))).isFalse();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user