use of alfio.model.modification.AdminReservationModification in project alf.io by alfio-event.
the class AdminReservationManager method reserveForTicketsInfo.
private Result<List<Ticket>> reserveForTicketsInfo(Event event, AdminReservationModification arm, String reservationId, String specialPriceSessionId, Pair<TicketCategory, TicketsInfo> pair) {
TicketCategory category = pair.getLeft();
TicketsInfo ticketsInfo = pair.getRight();
int categoryId = category.getId();
List<Attendee> attendees = ticketsInfo.getAttendees();
List<Integer> reservedForUpdate = ticketReservationManager.reserveTickets(event.getId(), categoryId, attendees.size(), singletonList(Ticket.TicketStatus.FREE));
if (reservedForUpdate.size() != attendees.size()) {
return Result.error(ErrorCode.CategoryError.NOT_ENOUGH_SEATS);
}
ticketRepository.reserveTickets(reservationId, reservedForUpdate, categoryId, arm.getLanguage(), category.getSrcPriceCts());
Ticket ticket = ticketRepository.findById(reservedForUpdate.get(0), categoryId);
TicketPriceContainer priceContainer = TicketPriceContainer.from(ticket, null, event, null);
ticketRepository.updateTicketPrice(reservedForUpdate, categoryId, event.getId(), category.getSrcPriceCts(), MonetaryUtil.unitToCents(priceContainer.getFinalPrice()), MonetaryUtil.unitToCents(priceContainer.getVAT()), MonetaryUtil.unitToCents(priceContainer.getAppliedDiscount()));
List<SpecialPrice> codes = category.isAccessRestricted() ? bindSpecialPriceTokens(specialPriceSessionId, categoryId, attendees) : Collections.emptyList();
assignTickets(event, attendees, categoryId, reservedForUpdate, codes, reservationId, arm.getLanguage(), category.getSrcPriceCts());
List<Ticket> tickets = reservedForUpdate.stream().map(id -> ticketRepository.findById(id, categoryId)).collect(toList());
return Result.success(tickets);
}
use of alfio.model.modification.AdminReservationModification in project alf.io by alfio-event.
the class AdminReservationManager method createReservation.
private Result<Pair<TicketReservation, List<Ticket>>> createReservation(Result<List<TicketsInfo>> input, Event event, AdminReservationModification arm) {
final TicketsInfo empty = new TicketsInfo(null, null, false, false);
return input.flatMap(t -> {
String reservationId = UUID.randomUUID().toString();
String specialPriceSessionId = UUID.randomUUID().toString();
Date validity = Date.from(arm.getExpiration().toZonedDateTime(event.getZoneId()).toInstant());
ticketReservationRepository.createNewReservation(reservationId, validity, null, arm.getLanguage(), event.getId(), event.getVat(), event.isVatIncluded());
AdminReservationModification.CustomerData customerData = arm.getCustomerData();
ticketReservationRepository.updateTicketReservation(reservationId, TicketReservationStatus.PENDING.name(), customerData.getEmailAddress(), customerData.getFullName(), customerData.getFirstName(), customerData.getLastName(), arm.getLanguage(), null, null, null);
Result<List<Ticket>> result = flattenTicketsInfo(event, empty, t).map(pair -> reserveForTicketsInfo(event, arm, reservationId, specialPriceSessionId, pair)).reduce(this::reduceReservationResults).orElseGet(() -> Result.error(ErrorCode.custom("", "unknown error")));
updateInvoiceReceiptModel(event, arm.getLanguage(), reservationId);
return result.map(list -> Pair.of(ticketReservationRepository.findReservationById(reservationId), list));
});
}
use of alfio.model.modification.AdminReservationModification in project alf.io by alfio-event.
the class AdminReservationManager method createReservation.
public Result<Pair<TicketReservation, List<Ticket>>> createReservation(AdminReservationModification input, String eventName, String username) {
DefaultTransactionDefinition definition = new DefaultTransactionDefinition();
TransactionTemplate template = new TransactionTemplate(transactionManager, definition);
return template.execute(status -> {
try {
Result<Pair<TicketReservation, List<Ticket>>> result = eventRepository.findOptionalByShortNameForUpdate(eventName).map(e -> validateTickets(input, e)).map(r -> r.flatMap(p -> transactionalCreateReservation(p.getRight(), p.getLeft(), username))).orElse(Result.error(ErrorCode.EventError.NOT_FOUND));
if (!result.isSuccess()) {
log.debug("Error during update of reservation eventName: {}, username: {}, reservation: {}", eventName, username, AdminReservationModification.summary(input));
status.setRollbackOnly();
}
return result;
} catch (Exception e) {
log.error("Error during update of reservation eventName: {}, username: {}, reservation: {}", eventName, username, AdminReservationModification.summary(input));
status.setRollbackOnly();
return Result.error(singletonList(ErrorCode.custom(e instanceof DuplicateReferenceException ? "duplicate-reference" : "", e.getMessage())));
}
});
}
use of alfio.model.modification.AdminReservationModification in project alf.io by alfio-event.
the class AdminReservationRequestManager method insertRequest.
private Result<String> insertRequest(AdminReservationModification body, Event event, boolean singleReservation, String username) {
try {
String requestId = UUID.randomUUID().toString();
long userId = userRepository.findIdByUserName(username).orElseThrow(IllegalArgumentException::new);
MapSqlParameterSource[] requests = spread(body, singleReservation).map(res -> new MapSqlParameterSource("userId", userId).addValue("requestId", requestId).addValue("requestType", AdminReservationRequest.RequestType.IMPORT.name()).addValue("status", AdminReservationRequest.Status.PENDING.name()).addValue("eventId", event.getId()).addValue("body", Json.toJson(res))).toArray(MapSqlParameterSource[]::new);
jdbc.batchUpdate(adminReservationRequestRepository.insertRequest(), requests);
return Result.success(requestId);
} catch (Exception e) {
log.error("can't insert reservation request", e);
return Result.error(ErrorCode.custom("internal_server_error", e.getMessage()));
}
}
use of alfio.model.modification.AdminReservationModification in project alf.io by alfio-event.
the class AdminReservationManager method updateReservation.
public Result<Boolean> updateReservation(String eventName, String reservationId, AdminReservationModification adminReservationModification, String username) {
DefaultTransactionDefinition definition = new DefaultTransactionDefinition();
TransactionTemplate template = new TransactionTemplate(transactionManager, definition);
return template.execute(status -> {
try {
Result<Boolean> result = eventRepository.findOptionalByShortName(eventName).flatMap(e -> optionally(() -> {
eventManager.checkOwnership(e, username, e.getOrganizationId());
return e;
})).map(event -> ticketReservationRepository.findOptionalReservationById(reservationId).map(r -> performUpdate(reservationId, event, r, adminReservationModification)).orElseGet(() -> Result.error(ErrorCode.ReservationError.UPDATE_FAILED))).orElseGet(() -> Result.error(ErrorCode.ReservationError.NOT_FOUND));
if (!result.isSuccess()) {
log.debug("Application error detected eventName: {} reservationId: {}, username: {}, reservation: {}", eventName, reservationId, username, AdminReservationModification.summary(adminReservationModification));
status.setRollbackOnly();
}
return result;
} catch (Exception e) {
log.error("Error during update of reservation eventName: {} reservationId: {}, username: {}, reservation: {}", eventName, reservationId, username, AdminReservationModification.summary(adminReservationModification));
status.setRollbackOnly();
return Result.error(singletonList(ErrorCode.custom("", e.getMessage())));
}
});
}
Aggregations