From 1a29daf06584047eb777685025e91bf98c1cebbf Mon Sep 17 00:00:00 2001 From: Sylvain Jermini Date: Wed, 19 Aug 2015 23:20:56 +0200 Subject: [PATCH] #54 refactor: move in a single method the send confirmation email action --- .../alfio/controller/ReservationController.java | 9 +-------- .../java/alfio/manager/NotificationManager.java | 11 +++++++++-- .../alfio/manager/TicketReservationManager.java | 15 +++++++++++++-- .../manager/TicketReservationManagerTest.java | 12 +++++++++++- 4 files changed, 34 insertions(+), 13 deletions(-) diff --git a/src/main/java/alfio/controller/ReservationController.java b/src/main/java/alfio/controller/ReservationController.java index 7d9d864786..22b20ec692 100644 --- a/src/main/java/alfio/controller/ReservationController.java +++ b/src/main/java/alfio/controller/ReservationController.java @@ -392,15 +392,8 @@ public String assignTicketToPerson(@PathVariable("eventName") String eventName, } private void sendReservationCompleteEmail(HttpServletRequest request, Event event, TicketReservation reservation) { - Locale locale = RequestContextUtils.getLocale(request); - - String shortReservationID = ticketReservationManager.getShortReservationID(reservation.getId()); - - notificationManager.sendSimpleEmail(event, reservation.getEmail(), messageSource.getMessage("reservation-email-subject", - new Object[] { shortReservationID, event.getDisplayName() }, locale), () -> { - return templateManager.renderClassPathResource("/alfio/templates/confirmation-email-txt.ms", ticketReservationManager.prepareModelForReservationEmail(event, reservation), locale, TemplateOutput.TEXT); - }); + ticketReservationManager.sendConfirmationEmail(event, reservation, locale); } private void sendReservationCompleteEmailToOrganizer(HttpServletRequest request, Event event, TicketReservation reservation) { diff --git a/src/main/java/alfio/manager/NotificationManager.java b/src/main/java/alfio/manager/NotificationManager.java index bcf1f37a42..ee36881265 100644 --- a/src/main/java/alfio/manager/NotificationManager.java +++ b/src/main/java/alfio/manager/NotificationManager.java @@ -111,9 +111,16 @@ public void sendTicketByEmail(Ticket ticket, Event event, Locale locale, Partial } public void sendSimpleEmail(Event event, String recipient, String subject, TextTemplateGenerator textBuilder) { + sendSimpleEmail(event, recipient, subject, textBuilder, Collections.emptyList()); + } + + public void sendSimpleEmail(Event event, String recipient, String subject, TextTemplateGenerator textBuilder, List attachments) { + + String encodedAttachments = attachments.isEmpty() ? null : encodeAttachments(attachments.toArray(new Mailer.Attachment[attachments.size()])); + String text = textBuilder.generate(); - String checksum = calculateChecksum(recipient, null, subject, text); - emailMessageRepository.insert(event.getId(), recipient, subject, text, null, checksum, ZonedDateTime.now(UTC)); + String checksum = calculateChecksum(recipient, encodedAttachments, subject, text); + emailMessageRepository.insert(event.getId(), recipient, subject, text, encodedAttachments, checksum, ZonedDateTime.now(UTC)); messages.offer(new EmailMessage(-1, event.getId(), WAITING.name(), recipient, subject, text, null, checksum)); } diff --git a/src/main/java/alfio/manager/TicketReservationManager.java b/src/main/java/alfio/manager/TicketReservationManager.java index b475f3356d..ddeeb09f88 100644 --- a/src/main/java/alfio/manager/TicketReservationManager.java +++ b/src/main/java/alfio/manager/TicketReservationManager.java @@ -322,12 +322,23 @@ public void confirmOfflinePayment(Event event, String reservationId) { Validate.isTrue(ticketReservation.getStatus() == TicketReservationStatus.OFFLINE_PAYMENT, "invalid status"); ticketReservationRepository.updateTicketReservationStatus(reservationId, TicketReservationStatus.COMPLETE.name()); acquireTickets(TicketStatus.ACQUIRED, PaymentProxy.OFFLINE, reservationId, ticketReservation.getEmail(), ticketReservation.getFullName(), ticketReservation.getUserLanguage(), ticketReservation.getBillingAddress()); + Locale language = findReservationLanguage(reservationId); - notificationManager.sendSimpleEmail(event, ticketReservation.getEmail(), messageSource.getMessage("reservation-email-subject", - new Object[]{ getShortReservationID(reservationId), event.getDisplayName()}, language), () -> templateManager.renderClassPathResource("/alfio/templates/confirmation-email-txt.ms", prepareModelForReservationEmail(event, ticketReservation), language, TemplateOutput.TEXT)); + + sendConfirmationEmail(event, ticketReservation, language); + pluginManager.handleReservationConfirmation(ticketReservationRepository.findReservationById(reservationId), event.getId()); } + public void sendConfirmationEmail(Event event, TicketReservation ticketReservation, Locale language) { + String reservationId = ticketReservation.getId(); + + Map reservationEmailModel = prepareModelForReservationEmail(event, ticketReservation); + notificationManager.sendSimpleEmail(event, ticketReservation.getEmail(), messageSource.getMessage("reservation-email-subject", + new Object[]{ getShortReservationID(reservationId), event.getDisplayName()}, language), + () -> templateManager.renderClassPathResource("/alfio/templates/confirmation-email-txt.ms", reservationEmailModel, language, TemplateOutput.TEXT)); + } + private Locale findReservationLanguage(String reservationId) { return Optional.ofNullable(ticketReservationRepository.findReservationById(reservationId).getUserLanguage()).map(Locale::forLanguageTag).orElse(Locale.ENGLISH); } diff --git a/src/test/java/alfio/manager/TicketReservationManagerTest.java b/src/test/java/alfio/manager/TicketReservationManagerTest.java index def454642b..be100932fe 100644 --- a/src/test/java/alfio/manager/TicketReservationManagerTest.java +++ b/src/test/java/alfio/manager/TicketReservationManagerTest.java @@ -517,10 +517,15 @@ public class TicketReservationManagerTest {{ NotificationManager notificationManager = it.usesMock(NotificationManager.class); MessageSource messageSource = it.usesMock(MessageSource.class); PluginManager pluginManager = mock(PluginManager.class); - TicketReservationManager ticketReservationManager = new TicketReservationManager(null, null, ticketRepository, ticketReservationRepository, null, null, configurationManager, paymentManager, null, specialPriceRepository, null, notificationManager, messageSource, null, platformTransactionManager, waitingQueueManager, pluginManager); + OrganizationRepository organizationRepository = mock(OrganizationRepository.class); + PromoCodeDiscountRepository promoCodeDiscountRepository = mock(PromoCodeDiscountRepository.class); + EventRepository eventRepository = it.usesMock(EventRepository.class); + TicketReservationManager ticketReservationManager = new TicketReservationManager(eventRepository, organizationRepository, ticketRepository, ticketReservationRepository, null, null, configurationManager, paymentManager, promoCodeDiscountRepository, specialPriceRepository, null, notificationManager, messageSource, null, platformTransactionManager, waitingQueueManager, pluginManager); Event event = mock(Event.class); when(event.getZoneId()).thenReturn(ZoneId.systemDefault()); when(event.getBegin()).thenReturn(ZonedDateTime.now().plusDays(5)); + + it.should("confirm a paid reservation", expect -> { when(ticketReservationRepository.updateTicketReservation(eq(reservationId), eq(TicketReservationStatus.COMPLETE.toString()), anyString(), anyString(), anyString(), anyString(), any(), eq(PaymentProxy.STRIPE.toString()))).thenReturn(1); when(ticketRepository.updateTicketsStatusWithReservationId(eq(reservationId), eq(TicketStatus.ACQUIRED.toString()))).thenReturn(1); @@ -574,11 +579,16 @@ public class TicketReservationManagerTest {{ it.should("confirm OFFLINE payments", expect -> { TicketReservation reservation = it.usesMock(TicketReservation.class); + when(reservation.getId()).thenReturn(reservationId); when(reservation.getPaymentMethod()).thenReturn(PaymentProxy.OFFLINE); when(reservation.getStatus()).thenReturn(OFFLINE_PAYMENT); when(ticketReservationRepository.findReservationById(eq(reservationId))).thenReturn(reservation); when(ticketRepository.updateTicketsStatusWithReservationId(eq(reservationId), eq(TicketStatus.ACQUIRED.toString()))).thenReturn(1); when(ticketReservationRepository.updateTicketReservation(eq(reservationId), eq(COMPLETE.toString()), anyString(), anyString(), anyString(), anyString(), any(ZonedDateTime.class), eq(PaymentProxy.OFFLINE.toString()))).thenReturn(1); + when(configurationManager.getStringConfigValue(any())).thenReturn(Optional.of("vatnr")); + when(ticketRepository.findTicketsInReservation(eq(reservationId))).thenReturn(Collections.emptyList()); + when(eventRepository.findByReservationId(eq(reservationId))).thenReturn(event); + ticketReservationManager.confirmOfflinePayment(event, reservationId); verify(ticketReservationRepository).lockReservationForUpdate(eq(reservationId)); verify(ticketReservationRepository).updateTicketReservationStatus(eq(reservationId), eq(COMPLETE.toString()));