Search in sources :

Example 1 with ConfigurationLevel

use of alfio.manager.system.ConfigurationLevel in project alf.io by alfio-event.

the class NotificationManager method processMessage.

private int processMessage(EmailMessage message, PurchaseContext purchaseContext) {
    int messageId = message.getId();
    ConfigurationLevel configurationLevel = ConfigurationLevel.purchaseContext(purchaseContext);
    if (message.getAttempts() >= configurationManager.getFor(ConfigurationKeys.MAIL_ATTEMPTS_COUNT, configurationLevel).getValueAsIntOrDefault(10)) {
        tx.execute(status -> emailMessageRepository.updateStatusAndAttempts(messageId, ERROR.name(), message.getAttempts(), Arrays.asList(IN_PROCESS.name(), WAITING.name(), RETRY.name())));
        log.warn("Message with id {} will be discarded", messageId);
        return 0;
    }
    try {
        int result = Optional.ofNullable(tx.execute(status -> emailMessageRepository.updateStatus(messageId, message.getChecksum(), IN_PROCESS.name(), Arrays.asList(WAITING.name(), RETRY.name())))).orElse(0);
        if (result > 0) {
            return Optional.ofNullable(tx.execute(status -> {
                sendMessage(purchaseContext, message);
                return 1;
            })).orElse(0);
        } else {
            log.debug("no messages have been updated on DB for the following criteria: id: {}, checksum: {}", messageId, message.getChecksum());
        }
    } catch (Exception e) {
        tx.execute(status -> emailMessageRepository.updateStatusAndAttempts(message.getId(), RETRY.name(), ZonedDateTime.now(clockProvider.getClock()).plusMinutes(message.getAttempts() + 1L), message.getAttempts() + 1, Arrays.asList(IN_PROCESS.name(), WAITING.name(), RETRY.name())));
        log.warn("could not send message: ", e);
    }
    return 0;
}
Also used : alfio.repository(alfio.repository) TemplateProcessor(alfio.controller.support.TemplateProcessor) ZonedDateTime(java.time.ZonedDateTime) BiFunction(java.util.function.BiFunction) Autowired(org.springframework.beans.factory.annotation.Autowired) PartialTicketTextGenerator(alfio.manager.support.PartialTicketTextGenerator) Mailer(alfio.manager.system.Mailer) StringUtils(org.apache.commons.lang3.StringUtils) TicketCheckInUtil(alfio.util.checkin.TicketCheckInUtil) Pair(org.apache.commons.lang3.tuple.Pair) TypeReference(com.fasterxml.jackson.core.type.TypeReference) DefaultTransactionDefinition(org.springframework.transaction.support.DefaultTransactionDefinition) Triple(org.apache.commons.lang3.tuple.Triple) SubscriptionDescriptor(alfio.model.subscription.SubscriptionDescriptor) OrganizationRepository(alfio.repository.user.OrganizationRepository) Organization(alfio.model.user.Organization) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) Stream(java.util.stream.Stream) PlatformTransactionManager(org.springframework.transaction.PlatformTransactionManager) alfio.model(alfio.model) Type(java.lang.reflect.Type) Hex(org.springframework.security.crypto.codec.Hex) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) TemplateGenerator(alfio.manager.support.TemplateGenerator) java.util(java.util) ConfigurationLevel(alfio.manager.system.ConfigurationLevel) ByteArrayOutputStream(java.io.ByteArrayOutputStream) MessageSourceManager(alfio.manager.i18n.MessageSourceManager) TransactionDefinition(org.springframework.transaction.TransactionDefinition) MessageDigest(java.security.MessageDigest) PurchaseContextType(alfio.model.PurchaseContext.PurchaseContextType) ConfigurationManager(alfio.manager.system.ConfigurationManager) Function(java.util.function.Function) Supplier(java.util.function.Supplier) alfio.util(alfio.util) Status(alfio.model.EmailMessage.Status) Objects.requireNonNullElse(java.util.Objects.requireNonNullElse) Objects.requireNonNullElseGet(java.util.Objects.requireNonNullElseGet) MessageSource(org.springframework.context.MessageSource) INCLUDE_CHECK_IN_URL_ICAL(alfio.model.system.ConfigurationKeys.INCLUDE_CHECK_IN_URL_ICAL) IOException(java.io.IOException) CustomMessageManager(alfio.manager.support.CustomMessageManager) Component(org.springframework.stereotype.Component) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) Log4j2(lombok.extern.log4j.Log4j2) ConfigurationKeys(alfio.model.system.ConfigurationKeys) com.google.gson(com.google.gson) Transactional(org.springframework.transaction.annotation.Transactional) ConfigurationLevel(alfio.manager.system.ConfigurationLevel) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) IOException(java.io.IOException)

Example 2 with ConfigurationLevel

use of alfio.manager.system.ConfigurationLevel in project alf.io by alfio-event.

the class CheckInManagerTest method setUp.

@BeforeEach
public void setUp() {
    eventRepository = mock(EventRepository.class);
    configurationManager = mock(ConfigurationManager.class);
    OrganizationRepository organizationRepository = mock(OrganizationRepository.class);
    Event event = mock(Event.class);
    Organization organization = mock(Organization.class);
    ConfigurationLevel cl = ConfigurationLevel.event(event);
    when(event.getConfigurationLevel()).thenReturn(cl);
    when(eventRepository.findOptionalByShortName(EVENT_NAME)).thenReturn(Optional.of(event));
    when(event.getId()).thenReturn(EVENT_ID);
    when(event.getOrganizationId()).thenReturn(ORG_ID);
    when(organizationRepository.findOrganizationForUser(USERNAME, ORG_ID)).thenReturn(Optional.of(organization));
    when(organization.getId()).thenReturn(ORG_ID);
    when(eventRepository.retrieveCheckInStatisticsForEvent(EVENT_ID)).thenReturn(new CheckInStatistics(0, 0, new Date()));
    checkInManager = new CheckInManager(null, eventRepository, null, null, null, null, null, configurationManager, organizationRepository, null, null, null, null, null, TestUtil.clockProvider());
}
Also used : ConfigurationLevel(alfio.manager.system.ConfigurationLevel) Organization(alfio.model.user.Organization) Event(alfio.model.Event) EventRepository(alfio.repository.EventRepository) OrganizationRepository(alfio.repository.user.OrganizationRepository) CheckInStatistics(alfio.manager.support.CheckInStatistics) ConfigurationManager(alfio.manager.system.ConfigurationManager) Date(java.util.Date) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 3 with ConfigurationLevel

use of alfio.manager.system.ConfigurationLevel in project alf.io by alfio-event.

the class EuVatCheckerTest method init.

@BeforeEach
public void init() {
    client = mock(EUVatChecker.class);
    configurationManager = mock(ConfigurationManager.class);
    eventAndOrganizationId = mock(EventAndOrganizationId.class);
    ConfigurationLevel cl = ConfigurationLevel.event(eventAndOrganizationId);
    when(eventAndOrganizationId.getConfigurationLevel()).thenReturn(cl);
    when(configurationManager.getFor(eq(ConfigurationKeys.ENABLE_EU_VAT_DIRECTIVE), any(ConfigurationLevel.class))).thenReturn(existing(ConfigurationKeys.ENABLE_EU_VAT_DIRECTIVE, "true"));
    when(configurationManager.getFor(eq(ConfigurationKeys.ENABLE_VIES_VALIDATION), any(ConfigurationLevel.class))).thenReturn(existing(ConfigurationKeys.ENABLE_VIES_VALIDATION, "true"));
    when(configurationManager.getForSystem(ConfigurationKeys.EU_COUNTRIES_LIST)).thenReturn(existing(ConfigurationKeys.EU_COUNTRIES_LIST, "IE"));
    when(configurationManager.getFor(eq(ConfigurationKeys.COUNTRY_OF_BUSINESS), any(ConfigurationLevel.class))).thenReturn(existing(ConfigurationKeys.COUNTRY_OF_BUSINESS, "IT"));
    when(configurationManager.getFor(eq(EnumSet.of(ConfigurationKeys.ENABLE_EU_VAT_DIRECTIVE, ConfigurationKeys.COUNTRY_OF_BUSINESS, ConfigurationKeys.ENABLE_REVERSE_CHARGE_ONLINE, ConfigurationKeys.ENABLE_REVERSE_CHARGE_IN_PERSON)), any())).thenReturn(Map.of(ConfigurationKeys.ENABLE_EU_VAT_DIRECTIVE, existing(ConfigurationKeys.ENABLE_EU_VAT_DIRECTIVE, "true"), ConfigurationKeys.COUNTRY_OF_BUSINESS, existing(ConfigurationKeys.COUNTRY_OF_BUSINESS, "IT"), ConfigurationKeys.ENABLE_REVERSE_CHARGE_ONLINE, missing(ConfigurationKeys.ENABLE_REVERSE_CHARGE_ONLINE), ConfigurationKeys.ENABLE_REVERSE_CHARGE_IN_PERSON, missing(ConfigurationKeys.ENABLE_REVERSE_CHARGE_IN_PERSON)));
}
Also used : ConfigurationLevel(alfio.manager.system.ConfigurationLevel) EventAndOrganizationId(alfio.model.EventAndOrganizationId) EUVatChecker(ch.digitalfondue.vatchecker.EUVatChecker) ConfigurationManager(alfio.manager.system.ConfigurationManager) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 4 with ConfigurationLevel

use of alfio.manager.system.ConfigurationLevel in project alf.io by alfio-event.

the class MollieConnectManager method storeConnectedAccountId.

@Override
public AccessTokenResponseDetails storeConnectedAccountId(String code, int organizationId) {
    try {
        ConfigurationLevel configurationLevel = ConfigurationLevel.organization(organizationId);
        var options = configurationManager.getFor(Set.of(MOLLIE_API_KEY, MOLLIE_CONNECT_CLIENT_ID, MOLLIE_CONNECT_CLIENT_SECRET, MOLLIE_CONNECT_CALLBACK, BASE_URL), configurationLevel);
        OAuth20Service service = new ServiceBuilder(options.get(MOLLIE_CONNECT_CLIENT_ID).getRequiredValue()).apiSecret(options.get(MOLLIE_CONNECT_CLIENT_SECRET).getRequiredValue()).callback(options.get(MOLLIE_CONNECT_CALLBACK).getRequiredValue()).build(new MollieConnectApi());
        OAuth2AccessToken accessTokenResponse = service.getAccessToken(code);
        var refreshToken = accessTokenResponse.getRefreshToken();
        if (refreshToken != null) {
            configurationManager.saveConfig(Configuration.from(organizationId, MOLLIE_CONNECT_REFRESH_TOKEN), refreshToken);
        }
        return new AccessTokenResponseDetails(accessTokenResponse.getAccessToken(), refreshToken, null, true);
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
        log.warn("Request interrupted while retrieving access token", e);
        return new AccessTokenResponseDetails(null, null, e.getMessage(), false);
    } catch (Exception e) {
        log.warn("Got exception while retrieving access token", e);
        return new AccessTokenResponseDetails(null, null, e.getMessage(), false);
    }
}
Also used : ConfigurationLevel(alfio.manager.system.ConfigurationLevel) OAuth2AccessToken(com.github.scribejava.core.model.OAuth2AccessToken) AccessTokenResponseDetails(alfio.util.oauth2.AccessTokenResponseDetails) OAuth20Service(com.github.scribejava.core.oauth.OAuth20Service) ServiceBuilder(com.github.scribejava.core.builder.ServiceBuilder)

Example 5 with ConfigurationLevel

use of alfio.manager.system.ConfigurationLevel in project alf.io by alfio-event.

the class MollieWebhookPaymentManager method getInfo.

@Override
public Optional<PaymentInformation> getInfo(Transaction transaction, PurchaseContext purchaseContext) {
    ConfigurationLevel configurationLevel = purchaseContext.getConfigurationLevel();
    var configuration = getConfiguration(configurationLevel);
    try {
        var getPaymentResponse = callGetPayment(transaction.getPaymentId(), configuration, configurationLevel);
        if (HttpUtils.callSuccessful(getPaymentResponse)) {
            try (var responseReader = new InputStreamReader(getPaymentResponse.body(), UTF_8)) {
                var body = new MolliePaymentDetails(JsonParser.parseReader(responseReader).getAsJsonObject());
                var paidAmount = body.getPaidAmount();
                var refundAmount = body.getRefundAmount().map(PaymentAmount::getValue).orElse(null);
                return Optional.of(new PaymentInformation(paidAmount.getValue(), refundAmount, null, null));
            }
        }
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
        log.warn("Request interrupted while calling getInfo", e);
    } catch (Exception e) {
        log.warn("got exception while calling getInfo", e);
    }
    return Optional.empty();
}
Also used : ConfigurationLevel(alfio.manager.system.ConfigurationLevel) InputStreamReader(java.io.InputStreamReader) PaymentInformation(alfio.model.PaymentInformation) IOException(java.io.IOException)

Aggregations

ConfigurationLevel (alfio.manager.system.ConfigurationLevel)5 ConfigurationManager (alfio.manager.system.ConfigurationManager)3 Organization (alfio.model.user.Organization)2 OrganizationRepository (alfio.repository.user.OrganizationRepository)2 IOException (java.io.IOException)2 BeforeEach (org.junit.jupiter.api.BeforeEach)2 TemplateProcessor (alfio.controller.support.TemplateProcessor)1 MessageSourceManager (alfio.manager.i18n.MessageSourceManager)1 CheckInStatistics (alfio.manager.support.CheckInStatistics)1 CustomMessageManager (alfio.manager.support.CustomMessageManager)1 PartialTicketTextGenerator (alfio.manager.support.PartialTicketTextGenerator)1 TemplateGenerator (alfio.manager.support.TemplateGenerator)1 Mailer (alfio.manager.system.Mailer)1 alfio.model (alfio.model)1 Status (alfio.model.EmailMessage.Status)1 Event (alfio.model.Event)1 EventAndOrganizationId (alfio.model.EventAndOrganizationId)1 PaymentInformation (alfio.model.PaymentInformation)1 PurchaseContextType (alfio.model.PurchaseContext.PurchaseContextType)1 SubscriptionDescriptor (alfio.model.subscription.SubscriptionDescriptor)1