This is an automated email from the ASF dual-hosted git repository. ikamga pushed a commit to branch develop in repository https://gitbox.apache.org/repos/asf/fineract-cn-notifications.git
commit 1fba09958b51cb5eaeaea2661f19c8b8f24188f0 Merge: f3deeaa a202f47 Author: Ebenezer Graham <[email protected]> AuthorDate: Fri May 24 16:31:35 2019 +0400 Merge branch 'develop' into permitted-feign-client-auth .travis.yml | 2 +- README.md | 5 +- .../api/v1/events/NotificationEventConstants.java | 8 +- .../fineract/cn/notification/TestEmailService.java | 15 +-- .../fineract/cn/notification/TestSMSService.java | 9 +- .../cn/notification/listener/EventListener.java | 103 +++++++++++++++++++++ service/build.gradle | 1 - .../service/internal/service/EmailService.java | 7 +- .../service/internal/service/SMSService.java | 10 +- shared.gradle | 1 - 10 files changed, 128 insertions(+), 33 deletions(-) diff --cc .travis.yml index 06d4b4d,b70bfc3..bffe9d0 --- a/.travis.yml +++ b/.travis.yml @@@ -27,4 -27,4 +27,4 @@@ env - BUILD_SNAPSHOTS_BRANCH=develop - ARTIFACTORY_URL=https://mifos.jfrog.io/mifos - ARTIFACTORY_USER=travis-ci - - secure: "E5IyImya7x5cHLB1vGLGIJUC1M+7RxKZivb5iUM+RR/xW39/7b4Qd3JeX4yBUeiPjf9lNMMxcGVyqI/4htT95++WCNc0u6OoxwGgxQEB9JHE8OWecaYgK4uLO7+651XF4DNbSiH3NVmc2b8Q0c0jBlu3MHAeEUIQ6BOiTXSsqnewtWAW0XETBooNmo2nTqZyKvzez0698oJuuYfZeFbqm8wJ9rIwtptyJluRzqcTlzw1cH/CvcIskLDmITVqbuZxAW59j1BqUnZNQpuOFZ692iqntdxq78CxliCG1MEYy4zWY8xcIWNVp28OKkMULXdi+V80oZXY4TyTwZfH6e2oZFz6/X9GpNNR6EwaHV0tT3wZi2NrkoBokD8bEBxQubIgxDCaDAFG9VMByC+++nwm0GJzerKBV4CpNSy/93XLrFAQ8+lOVauVtkVHuzFBWleLLQYcBGAXRz/C/UF4/m0m014jvD7v [...] - - secure: "NHjl/OM08+Q8zRXH1GRowPmFTCarpZVgpfzvW23DnHI9kwPcAuvXHUs0liBD1FgUr3oFNlIQ8QknlWSAb2yI9CTs/+v6f7wl4t4Xx4t5gV0wFuffb3W7a41bE+m1OSUunleSGLKr4qGffjPQ63EUyAb4wrywg23iY12OHbQ3a988UvqoDg6IjK7NXKeLhkAXg6qLDRh9aHdM6NknFNUUIbYyX25PMTQj/SShDQ3xptOhrj1wOvKbBEBc2t2X4+0/3pKt36W0VwJt68HPL4YKwFNiErqSXDAmKFwcf/aZxlXAGreCYN9rNuGuP5RMpblY1bNi6sW2COG0IgTjNSvQgULfIb42GI68O9w0supKZ8OKQLnDJDIQ5q1A1rE54bqs1ey8WD9bmwc24NTUkmjKTqDMTMqCLE+gXLM7xnS2JNisOETfEfRughUFBvmHVPgnQ6fCDJ0brPWBO9Se/elPP/XWlWAk [...] ++ - secure: "NHjl/OM08+Q8zRXH1GRowPmFTCarpZVgpfzvW23DnHI9kwPcAuvXHUs0liBD1FgUr3oFNlIQ8QknlWSAb2yI9CTs/+v6f7wl4t4Xx4t5gV0wFuffb3W7a41bE+m1OSUunleSGLKr4qGffjPQ63EUyAb4wrywg23iY12OHbQ3a988UvqoDg6IjK7NXKeLhkAXg6qLDRh9aHdM6NknFNUUIbYyX25PMTQj/SShDQ3xptOhrj1wOvKbBEBc2t2X4+0/3pKt36W0VwJt68HPL4YKwFNiErqSXDAmKFwcf/aZxlXAGreCYN9rNuGuP5RMpblY1bNi6sW2COG0IgTjNSvQgULfIb42GI68O9w0supKZ8OKQLnDJDIQ5q1A1rE54bqs1ey8WD9bmwc24NTUkmjKTqDMTMqCLE+gXLM7xnS2JNisOETfEfRughUFBvmHVPgnQ6fCDJ0brPWBO9Se/elPP/XWlWAk [...] diff --cc README.md index 7528145,c993eee..7a6daff --- a/README.md +++ b/README.md @@@ -1,6 -1,6 +1,7 @@@ - # Apache Fineract CN Notification [](https://travis-ci.com/apache/fineract-cn-notification) + - This mircroservice contains all the functionalities for SMS and Email Notification + # Apache Fineract CN Notification [](https://travis-ci.com/apache/fineract-cn-notifications) + -This mircroservice contains all the functionalities for SMS and Email Notification ++This mircroservice implements SMS and Email Notification functionalities ## Abstract Apache Fineract CN is an application framework for digital financial services, a system to support nationwide and cross-national financial transactions and help to level and speed the creation of an inclusive, interconnected digital economy for every nation in the world. diff --cc api/src/main/java/org/apache/fineract/cn/notification/api/v1/events/NotificationEventConstants.java index b6855b1,f4d1be1..cf4ba28 --- a/api/src/main/java/org/apache/fineract/cn/notification/api/v1/events/NotificationEventConstants.java +++ b/api/src/main/java/org/apache/fineract/cn/notification/api/v1/events/NotificationEventConstants.java @@@ -27,15 -27,8 +27,15 @@@ public interface NotificationEventConst String POST_SMS_CONFIGURATION = "post-sms-configuration"; String POST_EMAIL_CONFIGURATION = "post-email-configuration"; - String POST_SMS_NOTIFICATION = "post-sms-notification"; - String POST_EMAIL_NOTIFICATION = "post-email-notification"; + String POST_SOURCE_APPLICATION = "post-source-application"; + String UPDATE_SMS_CONFIGURATION = "update-sms-configuration"; + String UPDATE_EMAIL_CONFIGURATION = "update-email-configuration"; + String DELETE_SMS_CONFIGURATION = "delete-sms-configuration"; + String DELETE_EMAIL_CONFIGURATION = "delete-email-configuration"; + String DELETE_SOURCE_APPLICATION = "delete-source-application"; + - String POST_SEND_EMAIL_NOTIFICATION = "post-send-email-notification"; - String POST_SEND_SMS_NOTIFICATION = "post-send-sms-notification"; ++ String SEND_EMAIL_NOTIFICATION = "post-send-email-notification"; ++ String SEND_SMS_NOTIFICATION = "post-send-sms-notification"; String POST_ENABLE_CUSTOMER_CREATED_EVENT = "post-enable-customer-created-event"; String POST_ENABLE_CUSTOMER_UPDATED_EVENT = "post-enable-customer-updated-event"; @@@ -47,13 -40,6 +47,13 @@@ String SELECTOR_INITIALIZE = SELECTOR_NAME + " = '" + INITIALIZE + "'"; String SELECTOR_POST_SMS_CONFIGURATION = SELECTOR_NAME + " = '" + POST_SMS_CONFIGURATION + "'"; String SELECTOR_POST_EMAIL_CONFIGURATION = SELECTOR_NAME + " = '" + POST_EMAIL_CONFIGURATION + "'"; - String SELECTOR_POST_SMS_NOTIFICATION = SELECTOR_NAME + " = '" + POST_SMS_NOTIFICATION + "'"; - String SELECTOR_POST_EMAIL_NOTIFICATION = SELECTOR_NAME + " = '" + POST_EMAIL_NOTIFICATION + "'"; + String SELECTOR_UPDATE_SMS_CONFIGURATION = SELECTOR_NAME + " = '" + UPDATE_SMS_CONFIGURATION + "'"; + String SELECTOR_UPDATE_EMAIL_CONFIGURATION = SELECTOR_NAME + " = '" + UPDATE_EMAIL_CONFIGURATION + "'"; + String SELECTOR_DELETE_SMS_CONFIGURATION = SELECTOR_NAME + " = '" + DELETE_SMS_CONFIGURATION + "'"; + String SELECTOR_DELETE_EMAIL_CONFIGURATION = SELECTOR_NAME + " = '" + DELETE_EMAIL_CONFIGURATION + "'"; + String SELECTOR_POST_SOURCE_APPLICATION = SELECTOR_NAME + " = '" + POST_SOURCE_APPLICATION + "'"; + String SELECTOR_DELETE_SOURCE_APPLICATION = SELECTOR_NAME + " = '" + DELETE_SOURCE_APPLICATION + "'"; + - String SELECTOR_POST_SEND_EMAIL_NOTIFICATION = SELECTOR_NAME + " = '" + POST_SEND_EMAIL_NOTIFICATION + "'"; - String SELECTOR_POST_SEND_SMS_NOTIFICATION = SELECTOR_NAME + " = '" + POST_SEND_SMS_NOTIFICATION + "'"; ++ String SELECTOR_SEND_EMAIL_NOTIFICATION = SELECTOR_NAME + " = '" + SEND_EMAIL_NOTIFICATION + "'"; ++ String SELECTOR_SEND_SMS_NOTIFICATION = SELECTOR_NAME + " = '" + SEND_SMS_NOTIFICATION + "'"; } diff --cc component-test/src/main/java/org/apache/fineract/cn/notification/TestEmailService.java index 6808678,2855f68..69a1133 --- a/component-test/src/main/java/org/apache/fineract/cn/notification/TestEmailService.java +++ b/component-test/src/main/java/org/apache/fineract/cn/notification/TestEmailService.java @@@ -20,16 -20,14 +20,13 @@@ package org.apache.fineract.cn.notifica import org.apache.commons.lang3.RandomStringUtils; import org.apache.fineract.cn.api.util.NotFoundException; --import org.apache.fineract.cn.customer.api.v1.client.CustomerNotFoundException; import org.apache.fineract.cn.notification.api.v1.client.ConfigurationNotFoundException; import org.apache.fineract.cn.notification.api.v1.client.NotificationManager; import org.apache.fineract.cn.notification.api.v1.domain.EmailConfiguration; import org.apache.fineract.cn.notification.api.v1.events.NotificationEventConstants; +import org.apache.fineract.cn.notification.service.internal.service.EmailService; - import org.apache.fineract.cn.notification.service.internal.service.EventHelper; import org.apache.fineract.cn.notification.service.internal.service.NotificationService; import org.apache.fineract.cn.notification.util.DomainObjectGenerator; --import org.apache.fineract.cn.test.listener.EventRecorder; import org.junit.Assert; import org.junit.Test; import org.springframework.beans.factory.annotation.Autowired; @@@ -46,26 -44,38 +43,26 @@@ public class TestEmailService extends A public TestEmailService() { super(); + emailConfiguration = DomainObjectGenerator.emailConfiguration(); } + @Test - public void sendEmail() throws InterruptedException{ + public void shouldSendAnEmail() throws InterruptedException { this.logger.info("Send Email Notification"); - String messageHash = notificationService.sendEmail("[email protected]", - notificationService.sendEmail("[email protected]", ++ String to = notificationService.sendEmail("[email protected]", "[email protected]", "Address Details Changed", "Dear Valued Customer," + "\n\nYour address has been changed successfully" + - "\nStreet: Test Street"+ - "\nCity: Test City"+ - "\nState: Test State"+ - "\nCountry: Mauritius"+ + "\nStreet: Test Street" + + "\nCity: Test City" + + "\nState: Test State" + + "\nCountry: Mauritius" + "\n\nBest Regards" + "\nMFI"); - } - - @Test - public void shouldRetrieveEmailConfigurationEntity() { - logger.info("Create and retrieve Email Gateway configuration"); - EmailConfiguration sampleRetrieved = this.notificationManager.findEmailConfigurationByIdentifier(configIdentifier); - Assert.assertNotNull(sampleRetrieved); - Assert.assertEquals(sampleRetrieved.getIdentifier(), configIdentifier); - } - - @Test - public void shouldCreateNewEmailConfigurationEntity() throws InterruptedException{ - logger.info("Create Email Gateway configuration"); - this.notificationManager.createEmailConfiguration(DomainObjectGenerator.emailConfiguration()); - Assert.assertNotNull(messageHash); - eventRecorder.wait(NotificationEventConstants.POST_EMAIL_CONFIGURATION,EmailConfiguration.class); ++ Assert.assertNotNull(to); } @Test(expected = NotFoundException.class) @@@ -79,29 -89,8 +76,27 @@@ } @Test - public void checkEmailConfigurationEntityExist() { + public void shouldCreateAndRetrieveEmailConfigurationEntity() throws InterruptedException { + logger.info("Create and Retrieve Email Gateway configuration"); + this.notificationManager.createEmailConfiguration(emailConfiguration); + - this.eventRecorder.wait(NotificationEventConstants.POST_EMAIL_CONFIGURATION, emailConfiguration.getIdentifier()); ++ Assert.assertTrue(this.eventRecorder.wait(NotificationEventConstants.POST_EMAIL_CONFIGURATION, emailConfiguration.getIdentifier())); + + EmailConfiguration sampleRetrieved = this.notificationManager.findEmailConfigurationByIdentifier(emailConfiguration.getIdentifier()); - Assert.assertNotNull(sampleRetrieved); - Assert.assertEquals(sampleRetrieved.getIdentifier(), emailConfiguration.getIdentifier()); ++ Assert.assertEquals(sampleRetrieved.getIdentifier(),emailConfiguration.getIdentifier()); + } + + @Test + public void checkEmailConfigurationEntityExist() throws InterruptedException { logger.info("Email Gateway configuration Exist"); - Assert.assertTrue(this.notificationService.emailConfigurationExists(configIdentifier)); + this.notificationManager.createEmailConfiguration(emailConfiguration); - super.eventRecorder.wait(NotificationEventConstants.POST_EMAIL_CONFIGURATION, emailConfiguration.getIdentifier()); - ++ Assert.assertTrue(eventRecorder.wait(NotificationEventConstants.POST_EMAIL_CONFIGURATION, emailConfiguration.getIdentifier())); + Assert.assertTrue(this.emailService.emailConfigurationExists(emailConfiguration.getIdentifier())); + } + + @Test + public void shouldFindActiveGateway() { + this.logger.info("Find Active Gateway"); + Assert.assertNotNull(this.emailService.findActiveEmailConfigurationEntity()); } } diff --cc component-test/src/main/java/org/apache/fineract/cn/notification/TestSMSService.java index 37ab85f,33536ed..bd34c44 --- a/component-test/src/main/java/org/apache/fineract/cn/notification/TestSMSService.java +++ b/component-test/src/main/java/org/apache/fineract/cn/notification/TestSMSService.java @@@ -57,15 -59,20 +57,14 @@@ public class TestSMSService extends Abs } @Test - public void shouldTriggerCustomerCreated() throws InterruptedException{ - logger.info("Create SMS Gateway configuration"); + public void shouldCreateAndRetrieveSMSConfigurationEntity() throws InterruptedException { + logger.info("Create and Retrieve SMS Gateway configuration"); + this.notificationManager.createSMSConfiguration(smsConfiguration); - this.eventRecorder.wait(NotificationEventConstants.POST_SMS_CONFIGURATION, smsConfiguration.getIdentifier()); - eventRecorder.wait(NotificationEventConstants.POST_EMAIL_CONFIGURATION, SMSConfiguration.class); - } - - @Test(expected = NotFoundException.class) - public void smsConfigurationNotFound() throws CustomerNotFoundException { - logger.info("SMS Gateway configuration Not Found"); - try { - this.notificationManager.findSMSConfigurationByIdentifier(RandomStringUtils.randomAlphanumeric(8)); - } catch (final ConfigurationNotFoundException ex) { - logger.info("Error Asserted"); - } ++ Assert.assertTrue(this.eventRecorder.wait(NotificationEventConstants.POST_SMS_CONFIGURATION, smsConfiguration.getIdentifier())); + + SMSConfiguration sampleRetrieved = this.notificationManager.findSMSConfigurationByIdentifier(smsConfiguration.getIdentifier()); - Assert.assertNotNull(sampleRetrieved); + Assert.assertEquals(sampleRetrieved.getIdentifier(), smsConfiguration.getIdentifier()); } @Test @@@ -85,10 -94,8 +84,10 @@@ } @Test - public void shouldSendAnSMS() { - public void checkSMSConfigurationEntityExist() { - logger.info("SMS Gateway configuration Exist"); - Assert.assertTrue(this.notificationService.smsConfigurationExists(configIdentifier)); ++ public void shouldSendAnSMS(){ + this.logger.info("Send SMS Notification"); - String messageHash = this.notificationService.sendSMS("+23058409206", ++ String to = this.notificationService.sendSMS("+23058409206", + "Dear Valued Customer\n\nTalk is cheap show me the code\n\nBest Regards\nYour MFI"); - Assert.assertNotNull(messageHash); ++ Assert.assertNotNull(to); } } diff --cc component-test/src/main/java/org/apache/fineract/cn/notification/listener/EventListener.java index 0000000,0000000..8aab1ca new file mode 100644 --- /dev/null +++ b/component-test/src/main/java/org/apache/fineract/cn/notification/listener/EventListener.java @@@ -1,0 -1,0 +1,103 @@@ ++/* ++ * Licensed to the Apache Software Foundation (ASF) under one ++ * or more contributor license agreements. See the NOTICE file ++ * distributed with this work for additional information ++ * regarding copyright ownership. The ASF licenses this file ++ * to you under the Apache License, Version 2.0 (the ++ * "License"); you may not use this file except in compliance ++ * with the License. You may obtain a copy of the License at ++ * ++ * http://www.apache.org/licenses/LICENSE-2.0 ++ * ++ * Unless required by applicable law or agreed to in writing, ++ * software distributed under the License is distributed on an ++ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY ++ * KIND, either express or implied. See the License for the ++ * specific language governing permissions and limitations ++ * under the License. ++ */ ++ /* ++ * Licensed to the Apache Software Foundation (ASF) under one ++ * or more contributor license agreements. See the NOTICE file ++ * distributed with this work for additional information ++ * regarding copyright ownership. The ASF licenses this file ++ * to you under the Apache License, Version 2.0 (the ++ * "License"); you may not use this file except in compliance ++ * with the License. You may obtain a copy of the License at ++ * ++ * http://www.apache.org/licenses/LICENSE-2.0 ++ * ++ * Unless required by applicable law or agreed to in writing, ++ * software distributed under the License is distributed on an ++ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY ++ * KIND, either express or implied. See the License for the ++ * specific language governing permissions and limitations ++ * under the License. ++ */ ++ package org.apache.fineract.cn.notification.listener; ++ ++import org.apache.fineract.cn.lang.config.TenantHeaderFilter; ++import org.apache.fineract.cn.notification.api.v1.events.NotificationEventConstants; ++import org.apache.fineract.cn.notification.service.ServiceConstants; ++import org.apache.fineract.cn.test.listener.EventRecorder; ++import org.slf4j.Logger; ++import org.springframework.beans.factory.annotation.Autowired; ++import org.springframework.beans.factory.annotation.Qualifier; ++import org.springframework.jms.annotation.JmsListener; ++import org.springframework.messaging.handler.annotation.Header; ++import org.springframework.stereotype.Component; ++ ++@SuppressWarnings("unused") ++@Component ++public class EventListener { ++ private final EventRecorder eventRecorder; ++ private final Logger logger; ++ ++ @Autowired ++ public EventListener(final EventRecorder eventRecorder, ++ @Qualifier(ServiceConstants.LOGGER_NAME) final Logger logger) { ++ this.logger = logger; ++ this.eventRecorder = eventRecorder; ++ } ++ ++ @JmsListener( ++ subscription = NotificationEventConstants.DESTINATION, ++ destination = NotificationEventConstants.DESTINATION, ++ selector = NotificationEventConstants.SELECTOR_POST_EMAIL_CONFIGURATION ++ ) ++ public void postEmailConfiguration(@Header(TenantHeaderFilter.TENANT_HEADER) final String tenant, ++ final String payload) { ++ this.eventRecorder.event(tenant, NotificationEventConstants.POST_EMAIL_CONFIGURATION, payload, String.class); ++ } ++ ++ @JmsListener( ++ subscription = NotificationEventConstants.DESTINATION, ++ destination = NotificationEventConstants.DESTINATION, ++ selector = NotificationEventConstants.SELECTOR_POST_SMS_CONFIGURATION ++ ) ++ public void postSMSConfiguration(@Header(TenantHeaderFilter.TENANT_HEADER) final String tenant, ++ final String payload) { ++ this.eventRecorder.event(tenant, NotificationEventConstants.POST_SMS_CONFIGURATION, payload, String.class); ++ } ++ ++ @JmsListener( ++ subscription = NotificationEventConstants.DESTINATION, ++ destination = NotificationEventConstants.DESTINATION, ++ selector = NotificationEventConstants.SELECTOR_SEND_EMAIL_NOTIFICATION ++ ) ++ public void onSendEmailTrigger(@Header(TenantHeaderFilter.TENANT_HEADER) final String tenant, ++ final String payload) { ++ this.eventRecorder.event(tenant, NotificationEventConstants.SEND_EMAIL_NOTIFICATION, payload, String.class); ++ } ++ ++ @JmsListener( ++ subscription = NotificationEventConstants.DESTINATION, ++ destination = NotificationEventConstants.DESTINATION, ++ selector = NotificationEventConstants.SELECTOR_SEND_SMS_NOTIFICATION ++ ) ++ public void onSendSmsTrigger(@Header(TenantHeaderFilter.TENANT_HEADER) final String tenant, ++ final String payload) { ++ this.eventRecorder.event(tenant, NotificationEventConstants.SEND_SMS_NOTIFICATION, payload, String.class); ++ } ++ ++} diff --cc service/src/main/java/org/apache/fineract/cn/notification/service/internal/service/EmailService.java index bc006c6,5260e98..6a6a78f --- a/service/src/main/java/org/apache/fineract/cn/notification/service/internal/service/EmailService.java +++ b/service/src/main/java/org/apache/fineract/cn/notification/service/internal/service/EmailService.java @@@ -33,13 -24,10 +33,12 @@@ import org.springframework.beans.factor import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.mail.MailException; import org.springframework.mail.SimpleMailMessage; -import org.springframework.mail.javamail.JavaMailSender; import org.springframework.mail.javamail.JavaMailSenderImpl; import org.springframework.stereotype.Component; +import org.springframework.transaction.annotation.Transactional; - import javax.annotation.PostConstruct; +import java.util.List; +import java.util.Optional; import java.util.Properties; @Component @@@ -129,42 -72,8 +128,42 @@@ public class EmailService ServiceConstants.MAIL_SMTP_AUTH_VALUE); properties.put(ServiceConstants.MAIL_SMTP_STARTTLS_ENABLE_PROPERTY, ServiceConstants.MAIL_SMTP_STARTTLS_ENABLE_VALUE); - mailSender.setJavaMailProperties(properties); + this.mailSender.setJavaMailProperties(properties); + return true; + } + + public boolean setCustomProperties(String identifier) { + EmailConfiguration configuration = findEmailConfigurationByIdentifier(identifier).get(); + this.mailSender.setHost(configuration.getHost()); + this.mailSender.setPort(Integer.parseInt(configuration.getPort())); + this.mailSender.setUsername(configuration.getUsername()); + this.mailSender.setPassword(configuration.getApp_password()); - return mailSender; + Properties properties = new Properties(); + properties.put(ServiceConstants.MAIL_TRANSPORT_PROTOCOL_PROPERTY, configuration.getProtocol()); + properties.put(ServiceConstants.MAIL_SMTP_AUTH_PROPERTY, configuration.getSmtp_auth()); + properties.put(ServiceConstants.MAIL_SMTP_STARTTLS_ENABLE_PROPERTY, configuration.getStart_tls()); + //properties.put(ServiceConstants.MAIL_SMTP_TIMEOUT_PROPERTY, ServiceConstants.MAIL_SMTP_TIMEOUT_VALUE); + this.mailSender.setJavaMailProperties(properties); + return true; + } + + @CommandHandler(logStart = CommandLogLevel.INFO, logFinish = CommandLogLevel.INFO) + @Transactional - @EventEmitter(selectorName = NotificationEventConstants.SELECTOR_NAME, selectorValue = NotificationEventConstants.POST_SEND_EMAIL_NOTIFICATION) ++ @EventEmitter(selectorName = NotificationEventConstants.SELECTOR_NAME, selectorValue = NotificationEventConstants.SEND_EMAIL_NOTIFICATION) + public String sendEmail(String from, String to, String subject, String message) { + SimpleMailMessage mail = new SimpleMailMessage(); + + try { + mail.setFrom(from); + mail.setTo(to); + mail.setSubject(subject); + mail.setText(message); + + this.mailSender.send(mail); + } catch (MailException exception) { + logger.debug("Caused by:" + exception.getCause().toString()); + } - return to.concat(" - " + mailSender.hashCode()); ++ return to; } - } + } diff --cc service/src/main/java/org/apache/fineract/cn/notification/service/internal/service/SMSService.java index f618d1a,fca2476..0bfe73a --- a/service/src/main/java/org/apache/fineract/cn/notification/service/internal/service/SMSService.java +++ b/service/src/main/java/org/apache/fineract/cn/notification/service/internal/service/SMSService.java @@@ -23,95 -23,46 +23,95 @@@ import com.twilio.Twilio import com.twilio.rest.api.v2010.account.Message; import com.twilio.rest.api.v2010.account.MessageCreator; import com.twilio.type.PhoneNumber; ++import org.apache.fineract.cn.command.annotation.Aggregate; +import org.apache.fineract.cn.command.annotation.CommandHandler; +import org.apache.fineract.cn.command.annotation.CommandLogLevel; +import org.apache.fineract.cn.command.annotation.EventEmitter; +import org.apache.fineract.cn.notification.api.v1.domain.SMSConfiguration; +import org.apache.fineract.cn.notification.api.v1.events.NotificationEventConstants; import org.apache.fineract.cn.notification.service.ServiceConstants; +import org.apache.fineract.cn.notification.service.internal.mapper.SMSConfigurationMapper; +import org.apache.fineract.cn.notification.service.internal.repository.SMSGatewayConfigurationRepository; import org.slf4j.Logger; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; --import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + - import javax.annotation.PostConstruct; +import java.util.List; +import java.util.Optional; @Service ++@Aggregate public class SMSService { + static boolean isConfigured; + private final SMSGatewayConfigurationRepository smsGatewayConfigurationRepository; private final Logger logger; - @Value("${smssender.accountSID}") - private String ACCOUNT_SID; - @Value("${smssender.authToken}") - private String AUTH_TOKEN; - @Value("${smssender.senderNumber}") - private String SENDERNUMBER; + private String accountSid; + private String authToken; + private String senderNumber; @Autowired - public SMSService(@Qualifier(ServiceConstants.LOGGER_NAME) Logger logger) { + public SMSService(final SMSGatewayConfigurationRepository smsGatewayConfigurationRepository, + @Qualifier(ServiceConstants.LOGGER_NAME) Logger logger) { super(); this.logger = logger; + this.isConfigured = false; + this.smsGatewayConfigurationRepository = smsGatewayConfigurationRepository; + } + + //@PostConstruct + public void init() { + if (findActiveSMSConfigurationEntity().isPresent()){ + configureSMSGatewayWithActiveConfiguration(); + }else{ + //Todo: Send an alert on the interface to configure the service + } + } + + public boolean configureSMSGatewayWithActiveConfiguration() { + SMSConfiguration configuration = findActiveSMSConfigurationEntity().get(); + this.accountSid = configuration.getAccount_sid(); + this.authToken = configuration.getAuth_token(); + this.senderNumber = configuration.getSender_number(); + return this.isConfigured = true; + } + + public boolean customConfiguration(String identifier) { + SMSConfiguration configuration = findSMSConfigurationByIdentifier(identifier).get(); + this.accountSid = configuration.getAccount_sid(); + this.authToken = configuration.getAuth_token(); + this.senderNumber = configuration.getSender_number(); + return this.isConfigured = true; + } + + public Optional<SMSConfiguration> findActiveSMSConfigurationEntity() { + return this.smsGatewayConfigurationRepository.active().map(SMSConfigurationMapper::map); + } + + public Boolean smsConfigurationExists(final String identifier) { + return this.smsGatewayConfigurationRepository.existsByIdentifier(identifier); + } + + public Optional<SMSConfiguration> findSMSConfigurationByIdentifier(final String identifier) { + return this.smsGatewayConfigurationRepository.findByIdentifier(identifier).map(SMSConfigurationMapper::map); } - public void configure(String accountSID, - String authToken, - String senderNumber) { - ACCOUNT_SID = accountSID; - AUTH_TOKEN = authToken; - SENDERNUMBER = senderNumber; + public List<SMSConfiguration> findAllActiveSMSConfigurationEntities() { + return SMSConfigurationMapper.map(this.smsGatewayConfigurationRepository.findAll()); } - public void sendSMS(String receiver, String template) { - this.logger.debug("sendSMS invoked"); - Twilio.init(ACCOUNT_SID, AUTH_TOKEN); - MessageCreator messageCreator = Message.creator(ACCOUNT_SID, + @CommandHandler(logStart = CommandLogLevel.INFO, logFinish = CommandLogLevel.INFO) + @Transactional - @EventEmitter(selectorName = NotificationEventConstants.SELECTOR_NAME, selectorValue = NotificationEventConstants.POST_SEND_SMS_NOTIFICATION) ++ @EventEmitter(selectorName = NotificationEventConstants.SELECTOR_NAME, selectorValue = NotificationEventConstants.SEND_SMS_NOTIFICATION) + public String sendSMS(String receiver, String template) { + Twilio.init(this.accountSid, this.authToken); + MessageCreator messageCreator = Message.creator(this.accountSid, new PhoneNumber(receiver), - new PhoneNumber(SENDERNUMBER), + new PhoneNumber(this.senderNumber), template); Message message = messageCreator.create(); - return message.getTo().concat(" - " + message.getSid()); - System.out.println(message.getSid()); ++ return message.getTo(); } --} ++}
