Skip to content

Commit

Permalink
#414 - uniform search results: apply the same filters in both "ticket…
Browse files Browse the repository at this point in the history
…s" and "reservations" search
  • Loading branch information
cbellone committed Apr 26, 2018
1 parent 890dd33 commit 309461c
Show file tree
Hide file tree
Showing 14 changed files with 341 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@

import java.math.BigDecimal;
import java.security.Principal;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.Map;
Expand Down Expand Up @@ -66,10 +67,11 @@ public PageAndContent<List<TicketReservation>> findAll(@PathVariable("eventName"
@RequestParam(value = "search", required = false) String search,
@RequestParam(value = "status", required = false) List<TicketReservation.TicketReservationStatus> status,
Principal principal) {
Event event = eventRepository.findByShortName(eventName);
eventManager.checkOwnership(event, principal.getName(), event.getOrganizationId());
Pair<List<TicketReservation>, Integer> res = ticketReservationManager.findAllReservationsInEvent(event.getId(), page, search, status);
return new PageAndContent<>(res.getLeft(), res.getRight());
return eventManager.getOptionalByName(eventName, principal.getName())
.map(event -> {
Pair<List<TicketReservation>, Integer> res = ticketReservationManager.findAllReservationsInEvent(event.getId(), page, search, status);
return new PageAndContent<>(res.getLeft(), res.getRight());
}).orElseGet(() -> new PageAndContent<>(Collections.emptyList(), 0));
}

@RequestMapping(value = "/event/{eventName}/{reservationId}/confirm", method = RequestMethod.PUT)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -431,9 +431,9 @@ public List<SerializablePair<TicketReservation, OrderSummary>> getPendingPayment

@RequestMapping(value = "/events/{eventName}/pending-payments-count")
public Integer getPendingPaymentsCount(@PathVariable("eventName") String eventName, Principal principal) {
if(Optional.ofNullable(principal).map(Principal::getName).map(userManager::findUserByUsername).map(userManager::isOwner).orElse(false)) {
Event event = eventManager.getSingleEvent(eventName, principal.getName());
return ticketReservationManager.getPendingPaymentsCount(event.getId());
Optional<Event> maybeEvent = eventManager.getOptionalByName(eventName, principal.getName());
if(maybeEvent.isPresent()) {
return ticketReservationManager.getPendingPaymentsCount(maybeEvent.get().getId());
} else {
return 0;
}
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/alfio/manager/EventStatisticsManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public class EventStatisticsManager {
private final EventRepository eventRepository;
private final EventDescriptionRepository eventDescriptionRepository;
private final TicketRepository ticketRepository;
private final TicketSearchRepository ticketSearchRepository;
private final TicketCategoryRepository ticketCategoryRepository;
private final TicketCategoryDescriptionRepository ticketCategoryDescriptionRepository;
private final TicketReservationRepository ticketReservationRepository;
Expand Down Expand Up @@ -128,15 +129,15 @@ public List<TicketWithStatistic> loadModifiedTickets(int eventId, int categoryId
Event event = eventRepository.findById(eventId);
String toSearch = prepareSearchTerm(search);
final int pageSize = 30;
return ticketRepository.findAllModifiedTicketsWithReservationAndTransaction(eventId, categoryId, page * pageSize, pageSize, toSearch).stream()
return ticketSearchRepository.findAllModifiedTicketsWithReservationAndTransaction(eventId, categoryId, page * pageSize, pageSize, toSearch).stream()
.map(t -> new TicketWithStatistic(t.getTicket(), event, t.getTicketReservation(), event.getZoneId(), t.getTransaction()))
.sorted()
.collect(Collectors.toList());
}

public Integer countModifiedTicket(int eventId, int categoryId, String search) {
String toSearch = prepareSearchTerm(search);
return ticketRepository.countAllModifiedTicketsWithReservationAndTransaction(eventId, categoryId, toSearch);
return ticketSearchRepository.countAllModifiedTicketsWithReservationAndTransaction(eventId, categoryId, toSearch);
}

public Predicate<Event> noSeatsAvailable() {
Expand Down
7 changes: 5 additions & 2 deletions src/main/java/alfio/manager/TicketReservationManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ public class TicketReservationManager {
private final AuditingRepository auditingRepository;
private final UserRepository userRepository;
private final ExtensionManager extensionManager;
private final TicketSearchRepository ticketSearchRepository;

public static class NotEnoughTicketsException extends RuntimeException {

Expand Down Expand Up @@ -158,7 +159,7 @@ public TicketReservationManager(EventRepository eventRepository,
InvoiceSequencesRepository invoiceSequencesRepository,
AuditingRepository auditingRepository,
UserRepository userRepository,
ExtensionManager extensionManager) {
ExtensionManager extensionManager, TicketSearchRepository ticketSearchRepository) {
this.eventRepository = eventRepository;
this.organizationRepository = organizationRepository;
this.ticketRepository = ticketRepository;
Expand All @@ -183,6 +184,7 @@ public TicketReservationManager(EventRepository eventRepository,
this.auditingRepository = auditingRepository;
this.userRepository = userRepository;
this.extensionManager = extensionManager;
this.ticketSearchRepository = ticketSearchRepository;
}

/**
Expand Down Expand Up @@ -244,7 +246,8 @@ public Pair<List<TicketReservation>, Integer> findAllReservationsInEvent(int eve
String toSearch = StringUtils.trimToNull(search);
toSearch = toSearch == null ? null : ("%" + toSearch + "%");
List<String> toFilter = (status == null || status.isEmpty() ? Arrays.asList(TicketReservationStatus.values()) : status).stream().map(TicketReservationStatus::toString).collect(toList());
return Pair.of(ticketReservationRepository.findAllReservationsInEvent(eventId, offset, pageSize, toSearch, toFilter), ticketReservationRepository.countAllReservationsInEvent(eventId, toSearch, toFilter));
List<TicketReservation> reservationsForEvent = ticketSearchRepository.findReservationsForEvent(eventId, offset, pageSize, toSearch, toFilter);
return Pair.of(reservationsForEvent, ticketSearchRepository.countReservationsForEvent(eventId, toSearch, toFilter));
}

void reserveTicketsForCategory(Event event, Optional<String> specialPriceSessionId, String transactionId, TicketReservationWithOptionalCodeModification ticketReservation, Locale locale, boolean forWaitingQueue, PromoCodeDiscount discount) {
Expand Down
19 changes: 10 additions & 9 deletions src/main/java/alfio/model/TicketWithReservationAndTransaction.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,26 +35,26 @@ public class TicketWithReservationAndTransaction {
private final Optional<Transaction> transaction;


public TicketWithReservationAndTransaction(@Column("t_id") int id,
public TicketWithReservationAndTransaction(@Column("t_id") Integer id,
@Column("t_uuid") String uuid,
@Column("t_creation") ZonedDateTime creation,
@Column("t_category_id") Integer categoryId,
@Column("t_status") String status,
@Column("t_event_id") int eventId,
@Column("t_tickets_reservation_id") String ticketsReservationId,
@Column("t_full_name") String fullName,
@Column("t_first_name") String firstName,
@Column("t_last_name") String lastName,
@Column("t_email_address") String email,
@Column("t_locked_assignment") boolean lockedAssignment,
@Column("t_locked_assignment") Boolean lockedAssignment,
@Column("t_user_language") String userLanguage,
@Column("t_src_price_cts") int srcPriceCts,
@Column("t_final_price_cts") int finalPriceCts,
@Column("t_vat_cts") int vatCts,
@Column("t_discount_cts") int discountCts,
@Column("t_src_price_cts") Integer srcPriceCts,
@Column("t_final_price_cts") Integer finalPriceCts,
@Column("t_vat_cts") Integer vatCts,
@Column("t_discount_cts") Integer discountCts,
@Column("t_ext_reference") String extReference,
//
@Column("tr_id") String trId,
@Column("tr_event_id") int eventId,
@Column("tr_validity") Date validity,
@Column("tr_status") TicketReservation.TicketReservationStatus trStatus,
@Column("tr_full_name") String trFullName,
Expand Down Expand Up @@ -91,9 +91,10 @@ public TicketWithReservationAndTransaction(@Column("t_id") int id,
@Column("bt_plat_fee") Long platformFee,
@Column("bt_gtw_fee") Long gatewayFee
) {
this.ticket = new Ticket(id, uuid, creation, categoryId, status, eventId, ticketsReservationId,

this.ticket = id != null ? new Ticket(id, uuid, creation, categoryId, status, eventId, ticketsReservationId,
fullName, firstName, lastName, email, lockedAssignment, userLanguage,
srcPriceCts, finalPriceCts, vatCts, discountCts, extReference);
srcPriceCts, finalPriceCts, vatCts, discountCts, extReference) : null;


this.ticketReservation = new TicketReservation(trId, validity, trStatus,
Expand Down
18 changes: 0 additions & 18 deletions src/main/java/alfio/repository/TicketRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import alfio.model.FullTicketInfo;
import alfio.model.Ticket;
import alfio.model.TicketCSVInfo;
import alfio.model.TicketWithReservationAndTransaction;
import ch.digitalfondue.npjt.*;

import java.util.Date;
Expand Down Expand Up @@ -56,23 +55,6 @@ public interface TicketRepository {
Integer countAssignedTickets(@Bind("eventId") int eventId, @Bind("categoryId") int categoryId);


String FIND_ALL_MODIFIED_TICKETS_WITH_RESERVATION_AND_TRANSACTION = "select * from ticket_and_reservation_and_tx where t_status in ('PENDING', 'ACQUIRED', 'TO_BE_PAID', 'CANCELLED', 'CHECKED_IN') and t_category_id = :categoryId and t_event_id = :eventId and " +
" (:search is null or (lower(t_uuid) like lower(:search) or lower(t_full_name) like lower(:search) or lower(t_first_name) like lower(:search) or lower(t_last_name) like lower(:search) or lower(t_email_address) like lower(:search) or " +
" lower(tr_full_name) like lower(:search) or lower(tr_first_name) like lower(:search) or lower(tr_last_name) like lower(:search) or lower(tr_email_address) like lower(:search))) " +
" order by tr_confirmation_ts asc, tr_id, t_uuid";

@Query("select * from (" + FIND_ALL_MODIFIED_TICKETS_WITH_RESERVATION_AND_TRANSACTION + " limit :pageSize offset :page) as d_tbl")
List<TicketWithReservationAndTransaction> findAllModifiedTicketsWithReservationAndTransaction(@Bind("eventId") int eventId,
@Bind("categoryId") int categoryId,
@Bind("page") int page,
@Bind("pageSize") int pageSize,
@Bind("search") String search);

@Query("select count(*) from (" + FIND_ALL_MODIFIED_TICKETS_WITH_RESERVATION_AND_TRANSACTION +" ) as d_tbl")
Integer countAllModifiedTicketsWithReservationAndTransaction(@Bind("eventId") int eventId,
@Bind("categoryId") int categoryId,
@Bind("search") String search);

@Query("select count(*) from ticket where status in ("+CONFIRMED+", 'PENDING') and category_id = :categoryId and event_id = :eventId")
Integer countConfirmedAndPendingTickets(@Bind("eventId") int eventId, @Bind("categoryId") int categoryId);

Expand Down
16 changes: 0 additions & 16 deletions src/main/java/alfio/repository/TicketReservationRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -124,22 +124,6 @@ int postponePayment(@Bind("reservationId") String reservationId, @Bind("validity
Integer countInvoices(@Bind("eventId") int eventId);


String FIND_RESERVATIONS_IN_EVENT = " select * from tickets_reservation where event_id_fk = :eventId and status in (:status) " +
" and (:search is null or (lower(id) like lower(:search) or lower(full_name) like lower(:search) or lower(first_name) like lower(:search) or lower(last_name) like lower(:search) or lower(email_address) like lower(:search) or lower(billing_address) like lower(:search))) " +
" order by confirmation_ts desc, validity desc ";

@Query("select * from (" + FIND_RESERVATIONS_IN_EVENT +"limit :pageSize offset :offset) as r_tbl")
List<TicketReservation> findAllReservationsInEvent(@Bind("eventId") int eventId,
@Bind("offset") int offset,
@Bind("pageSize") int pageSize,
@Bind("search") String toSearch,
@Bind("status") List<String> toFilter);

@Query("select count(*) from (" + FIND_RESERVATIONS_IN_EVENT + ") as r_tbl")
Integer countAllReservationsInEvent(@Bind("eventId") int eventId,
@Bind("search") String toSearch,
@Bind("status") List<String> toFilter);

@Query("update tickets_reservation set vat_status = :vatStatus, vat_nr = :vatNr, vat_country = :vatCountry, invoice_requested = :invoiceRequested where id = :reservationId")
int updateBillingData(@Bind("vatStatus") PriceContainer.VatStatus vatStatus,
@Bind("vatNr") String vatNr,
Expand Down
68 changes: 68 additions & 0 deletions src/main/java/alfio/repository/TicketSearchRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/**
* 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/>.
*/
package alfio.repository;

import alfio.model.TicketReservation;
import alfio.model.TicketWithReservationAndTransaction;
import ch.digitalfondue.npjt.Bind;
import ch.digitalfondue.npjt.Query;
import ch.digitalfondue.npjt.QueryRepository;

import java.util.List;

@QueryRepository
public interface TicketSearchRepository {
String APPLY_FILTER = " (:search is null or (lower(t_uuid) like lower(:search) or lower(t_full_name) like lower(:search) or lower(t_first_name) like lower(:search) or lower(t_last_name) like lower(:search) or lower(t_email_address) like lower(:search) or " +
" lower(tr_full_name) like lower(:search) or lower(tr_first_name) like lower(:search) or lower(tr_last_name) like lower(:search) or lower(tr_email_address) like lower(:search))) ";

String FIND_ALL_MODIFIED_TICKETS_WITH_RESERVATION_AND_TRANSACTION = "select * from reservation_and_ticket_and_tx where t_id is not null and t_status in ('PENDING', 'ACQUIRED', 'TO_BE_PAID', 'CANCELLED', 'CHECKED_IN') and t_category_id = :categoryId and t_event_id = :eventId and " + APPLY_FILTER;

String FIND_ALL_TICKETS_INCLUDING_NEW = "select * from reservation_and_ticket_and_tx where tr_event_id = :eventId and tr_id is not null and tr_status in (:status) and " + APPLY_FILTER;

String RESERVATION_FIELDS = "tr_id id, tr_validity validity, tr_status status, tr_full_name full_name, tr_first_name first_name, tr_last_name last_name, tr_email_address email_address," +
"tr_billing_address billing_address, tr_confirmation_ts confirmation_ts, tr_latest_reminder_ts latest_reminder_ts, tr_payment_method payment_method," +
"tr_offline_payment_reminder_sent offline_payment_reminder_sent, tr_promo_code_id_fk promo_code_id_fk, tr_automatic automatic," +
"tr_user_language user_language, tr_direct_assignment direct_assignment, tr_invoice_number invoice_number, tr_invoice_model invoice_model," +
"tr_vat_status vat_status, tr_vat_nr vat_nr, tr_vat_country vat_country, tr_invoice_requested invoice_requested, tr_used_vat_percent used_vat_percent," +
"tr_vat_included vat_included";

@Query("select * from (" + FIND_ALL_MODIFIED_TICKETS_WITH_RESERVATION_AND_TRANSACTION + " limit :pageSize offset :page) as d_tbl order by tr_confirmation_ts asc, tr_id, t_uuid")
List<TicketWithReservationAndTransaction> findAllModifiedTicketsWithReservationAndTransaction(@Bind("eventId") int eventId,
@Bind("categoryId") int categoryId,
@Bind("page") int page,
@Bind("pageSize") int pageSize,
@Bind("search") String search);

@Query("select count(*) from (" + FIND_ALL_MODIFIED_TICKETS_WITH_RESERVATION_AND_TRANSACTION +" ) as d_tbl")
Integer countAllModifiedTicketsWithReservationAndTransaction(@Bind("eventId") int eventId,
@Bind("categoryId") int categoryId,
@Bind("search") String search);

@Query("select distinct "+RESERVATION_FIELDS+" from (" + FIND_ALL_TICKETS_INCLUDING_NEW + " limit :pageSize offset :page) as d_tbl order by tr_confirmation_ts desc nulls last, tr_validity")
List<TicketReservation> findReservationsForEvent(@Bind("eventId") int eventId,
@Bind("page") int page,
@Bind("pageSize") int pageSize,
@Bind("search") String search,
@Bind("status") List<String> toFilter);

@Query("select count(distinct tr_id) from (" + FIND_ALL_TICKETS_INCLUDING_NEW +" ) as d_tbl")
Integer countReservationsForEvent(@Bind("eventId") int eventId,
@Bind("search") String search,
@Bind("status") List<String> toFilter);


}
Loading

0 comments on commit 309461c

Please sign in to comment.