Skip to content

Commit

Permalink
Implement support for keeping multiple sponsor scans of the same atte…
Browse files Browse the repository at this point in the history
…ndee coming from different operators (#1205) - cherry-picked from 6d7e3cc
  • Loading branch information
cbellone committed Mar 30, 2023
1 parent 27fc690 commit 0580cab
Show file tree
Hide file tree
Showing 9 changed files with 2,626 additions and 2,539 deletions.
Expand Up @@ -460,6 +460,7 @@ public void downloadSponsorScanExport(@PathVariable("eventName") String eventNam
header.addAll(fields.stream().map(TicketFieldConfiguration::getName).toList());
header.add("Sponsor notes");
header.add("Lead Status");
header.add("Operator");

Stream<String[]> sponsorScans = userManager.findAllEnabledUsers(principal.getName()).stream()
.map(u -> Pair.of(u, userManager.getUserRole(u)))
Expand All @@ -486,6 +487,7 @@ public void downloadSponsorScanExport(@PathVariable("eventName") String eventNam

line.add(sponsorScan.getNotes());
line.add(sponsorScan.getLeadStatus().name());
line.add(sponsorScan.getOperator());
return line.toArray(new String[0]);
});

Expand Down
13 changes: 9 additions & 4 deletions src/main/java/alfio/controller/api/v1/AttendeeApiController.java
Expand Up @@ -50,6 +50,7 @@
@RequestMapping("/api/attendees")
public class AttendeeApiController {

public static final String ALFIO_OPERATOR_HEADER = "Alfio-Operator";
private static final Logger log = LoggerFactory.getLogger(AttendeeApiController.class);

private final AttendeeManager attendeeManager;
Expand All @@ -73,15 +74,19 @@ public ResponseEntity<String> handleGenericException(RuntimeException e) {


@PostMapping("/sponsor-scan")
public ResponseEntity<TicketAndCheckInResult> scanBadge(@RequestBody SponsorScanRequest request, Principal principal) {
return ResponseEntity.ok(attendeeManager.registerSponsorScan(request.eventName, request.ticketIdentifier, request.notes, request.leadStatus, principal.getName()));
public ResponseEntity<TicketAndCheckInResult> scanBadge(@RequestBody SponsorScanRequest request,
Principal principal,
@RequestHeader(name = ALFIO_OPERATOR_HEADER, required = false) String operator) {
return ResponseEntity.ok(attendeeManager.registerSponsorScan(request.eventName, request.ticketIdentifier, request.notes, request.leadStatus, principal.getName(), operator));
}

@PostMapping("/sponsor-scan/bulk")
public ResponseEntity<List<TicketAndCheckInResult>> scanBadges(@RequestBody List<SponsorScanRequest> requests, Principal principal) {
public ResponseEntity<List<TicketAndCheckInResult>> scanBadges(@RequestBody List<SponsorScanRequest> requests,
Principal principal,
@RequestHeader(name = ALFIO_OPERATOR_HEADER, required = false) String operator) {
String username = principal.getName();
return ResponseEntity.ok(requests.stream()
.map(request -> attendeeManager.registerSponsorScan(request.eventName, request.ticketIdentifier, request.notes, request.leadStatus, username))
.map(request -> attendeeManager.registerSponsorScan(request.eventName, request.ticketIdentifier, request.notes, request.leadStatus, username, operator))
.collect(Collectors.toList()));
}

Expand Down
21 changes: 15 additions & 6 deletions src/main/java/alfio/manager/AttendeeManager.java
Expand Up @@ -34,12 +34,13 @@
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;

@Component
public class AttendeeManager {

public static final String DEFAULT_OPERATOR_ID = "__DEFAULT__";
private final SponsorScanRepository sponsorScanRepository;
private final EventRepository eventRepository;
private final TicketRepository ticketRepository;
Expand Down Expand Up @@ -67,7 +68,12 @@ public AttendeeManager(SponsorScanRepository sponsorScanRepository,
this.clockProvider = clockProvider;
}

public TicketAndCheckInResult registerSponsorScan(String eventShortName, String ticketUid, String notes, SponsorScan.LeadStatus leadStatus, String username) {
public TicketAndCheckInResult registerSponsorScan(String eventShortName,
String ticketUid,
String notes,
SponsorScan.LeadStatus leadStatus,
String username,
String operatorId) {
int userId = userRepository.getByUsername(username).getId();
Optional<EventAndOrganizationId> maybeEvent = eventRepository.findOptionalEventAndOrganizationIdByShortName(eventShortName);
if(maybeEvent.isEmpty()) {
Expand All @@ -82,12 +88,13 @@ public TicketAndCheckInResult registerSponsorScan(String eventShortName, String
if(ticket.getStatus() != Ticket.TicketStatus.CHECKED_IN) {
return new TicketAndCheckInResult(new TicketWithCategory(ticket, null), new DefaultCheckInResult(CheckInStatus.INVALID_TICKET_STATE, "not checked-in"));
}
Optional<ZonedDateTime> existingRegistration = sponsorScanRepository.getRegistrationTimestamp(userId, event.getId(), ticket.getId());
var operator = Objects.requireNonNullElse(operatorId, DEFAULT_OPERATOR_ID);
Optional<ZonedDateTime> existingRegistration = sponsorScanRepository.getRegistrationTimestamp(userId, event.getId(), ticket.getId(), operator);
if(existingRegistration.isEmpty()) {
ZoneId eventZoneId = eventRepository.getZoneIdByEventId(event.getId());
sponsorScanRepository.insert(userId, ZonedDateTime.now(clockProvider.withZone(eventZoneId)), event.getId(), ticket.getId(), notes, leadStatus);
sponsorScanRepository.insert(userId, ZonedDateTime.now(clockProvider.withZone(eventZoneId)), event.getId(), ticket.getId(), notes, leadStatus, operator);
} else {
sponsorScanRepository.updateNotesAndLeadStatus(userId, event.getId(), ticket.getId(), notes, leadStatus);
sponsorScanRepository.updateNotesAndLeadStatus(userId, event.getId(), ticket.getId(), notes, leadStatus, operator);
}
return new TicketAndCheckInResult(new TicketWithCategory(ticket, null), new DefaultCheckInResult(CheckInStatus.SUCCESS, "success"));
}
Expand Down Expand Up @@ -123,7 +130,9 @@ private List<SponsorAttendeeData> loadAttendeesData(EventAndOrganizationId event
.map(scan -> {
Ticket ticket = scan.getTicket();
return new SponsorAttendeeData(ticket.getUuid(), scan.getSponsorScan().getTimestamp().format(EventUtil.JSON_DATETIME_FORMATTER), ticket.getFullName(), ticket.getEmail());
}).collect(Collectors.toList());
})
.distinct()
.toList();
}

}
5 changes: 3 additions & 2 deletions src/main/java/alfio/model/DetailedScanData.java
Expand Up @@ -60,12 +60,13 @@ public DetailedScanData(@Column("t_id") int ticketId,
@Column("s_event_id") int scanEventId,
@Column("s_ticket_id") int scanTicketId,
@Column("s_notes") String notes,
@Column("s_lead_status") SponsorScan.LeadStatus leadStatus) {
@Column("s_lead_status") SponsorScan.LeadStatus leadStatus,
@Column("s_operator") String operator) {
this.ticket = new Ticket(ticketId, ticketUuid, ticketCreation, ticketCategoryId,
ticketStatus, ticketEventId, ticketsReservationId, ticketFullName, ticketFirstName,
ticketLastName, ticketEmail, ticketLockedAssignment, ticketUserLanguage, ticketSrcPriceCts,
ticketFinalPriceCts, ticketVatCts, ticketDiscountCts, extReference, currencyCode, ticketTags,
ticketSubscriptionId, ticketVatStatus);
this.sponsorScan = new SponsorScan(scanUserId, scanTimestamp, scanEventId, scanTicketId, notes, leadStatus);
this.sponsorScan = new SponsorScan(scanUserId, scanTimestamp, scanEventId, scanTicketId, notes, leadStatus, operator);
}
}
5 changes: 4 additions & 1 deletion src/main/java/alfio/model/SponsorScan.java
Expand Up @@ -34,19 +34,22 @@ public enum LeadStatus {
private final int ticketId;
private final String notes;
private final LeadStatus leadStatus;
private final String operator;


public SponsorScan(@Column("user_id") int userId,
@Column("creation") ZonedDateTime timestamp,
@Column("event_id") int eventId,
@Column("ticket_id") int ticketId,
@Column("notes") String notes,
@Column("lead_status") LeadStatus leadStatus) {
@Column("lead_status") LeadStatus leadStatus,
@Column("operator") String operator) {
this.userId = userId;
this.timestamp = timestamp;
this.eventId = eventId;
this.ticketId = ticketId;
this.notes = notes;
this.leadStatus = leadStatus;
this.operator = operator;
}
}
16 changes: 9 additions & 7 deletions src/main/java/alfio/repository/SponsorScanRepository.java
Expand Up @@ -33,29 +33,31 @@ public interface SponsorScanRepository {

ZonedDateTime DEFAULT_TIMESTAMP = ZonedDateTime.ofInstant(Instant.EPOCH, ZoneOffset.UTC);

@Query("select creation from sponsor_scan where user_id = :userId and event_id = :eventId and ticket_id = :ticketId")
Optional<ZonedDateTime> getRegistrationTimestamp(@Bind("userId") int userId, @Bind("eventId") int eventId, @Bind("ticketId") int ticketId);
@Query("select creation from sponsor_scan where user_id = :userId and event_id = :eventId and ticket_id = :ticketId and operator = :operator")
Optional<ZonedDateTime> getRegistrationTimestamp(@Bind("userId") int userId, @Bind("eventId") int eventId, @Bind("ticketId") int ticketId, @Bind("operator") String operator);

@Query("insert into sponsor_scan (user_id, creation, event_id, ticket_id, notes, lead_status) values(:userId, :creation, :eventId, :ticketId, :notes, :leadStatus)")
@Query("insert into sponsor_scan (user_id, creation, event_id, ticket_id, notes, lead_status, operator) values(:userId, :creation, :eventId, :ticketId, :notes, :leadStatus, :operator)")
int insert(@Bind("userId") int userId,
@Bind("creation") ZonedDateTime creation,
@Bind("eventId") int eventId,
@Bind("ticketId") int ticketId,
@Bind("notes") String notes,
@Bind("leadStatus") SponsorScan.LeadStatus leadStatus);
@Bind("leadStatus") SponsorScan.LeadStatus leadStatus,
@Bind("operator") String operator);

@Query("update sponsor_scan set notes = :notes, lead_status = :leadStatus where user_id = :userId and event_id = :eventId and ticket_id = :ticketId")
@Query("update sponsor_scan set notes = :notes, lead_status = :leadStatus where user_id = :userId and event_id = :eventId and ticket_id = :ticketId and operator = :operator")
int updateNotesAndLeadStatus(@Bind("userId") int userId,
@Bind("eventId") int eventId,
@Bind("ticketId") int ticketId,
@Bind("notes") String notes,
@Bind("leadStatus") SponsorScan.LeadStatus leadStatus);
@Bind("leadStatus") SponsorScan.LeadStatus leadStatus,
@Bind("operator") String operator);

@Query("select t.id t_id, t.uuid t_uuid, t.creation t_creation, t.category_id t_category_id, t.status t_status, t.event_id t_event_id," +
" t.src_price_cts t_src_price_cts, t.final_price_cts t_final_price_cts, t.vat_cts t_vat_cts, t.discount_cts t_discount_cts, t.tickets_reservation_id t_tickets_reservation_id," +
" t.full_name t_full_name, t.first_name t_first_name, t.last_name t_last_name, t.email_address t_email_address, t.locked_assignment t_locked_assignment," +
" t.user_language t_user_language, t.ext_reference t_ext_reference, t.currency_code t_currency_code, t.tags t_tags, t.subscription_id_fk t_subscription_id, t.vat_status t_vat_status," +
" s.user_id s_user_id, s.creation s_creation, s.event_id s_event_id, s.ticket_id s_ticket_id, s.notes s_notes, s.lead_status s_lead_status, " +
" s.user_id s_user_id, s.creation s_creation, s.event_id s_event_id, s.ticket_id s_ticket_id, s.notes s_notes, s.lead_status s_lead_status, s.operator s_operator, " +
" (case when s.lead_status = 'HOT' then 2 when s.lead_status = 'WARM' then 1 else 0 end) as priority"+
" from sponsor_scan s, ticket t where s.event_id = :eventId and s.user_id = :userId and s.creation > :start and s.ticket_id = t.id order by priority desc, s.creation")
List<DetailedScanData> loadSponsorData(@Bind("eventId") int eventId,
Expand Down
@@ -0,0 +1,21 @@
--
-- This file is part of alf.io.
--
-- alf.io is free software: you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as published by
-- the Free Software Foundation, either version 3 of the License, or
-- (at your option) any later version.
--
-- alf.io is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-- GNU General Public License for more details.
--
-- You should have received a copy of the GNU General Public License
-- along with alf.io. If not, see <http://www.gnu.org/licenses/>.
--

alter table sponsor_scan add column operator text not null default '__DEFAULT__';
alter table sponsor_scan drop constraint "spsc_unique_ticket";
alter table sponsor_scan add constraint "spsc_unique_ticket" unique(event_id, ticket_id, user_id, operator);

0 comments on commit 0580cab

Please sign in to comment.