Skip to content

Commit

Permalink
ES-515 (mosip#36)
Browse files Browse the repository at this point in the history
* change successfull registration sms content

* send sms success notification to user

* change SMS notification helper class for sendSMSNotificationAsync

* update SMS helper name and rename properties key for SMS message template

---------

Co-authored-by: Mengleang <[email protected]>
Signed-off-by: Sreang Rathanak <[email protected]>
  • Loading branch information
2 people authored and Sreang Rathanak committed Jan 15, 2024
1 parent 70a51b9 commit 5c12ab6
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 43 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package io.mosip.signup.helper;

import io.mosip.esignet.core.util.IdentityProviderUtil;
import io.mosip.signup.dto.NotificationRequest;
import io.mosip.signup.dto.NotificationResponse;
import io.mosip.signup.dto.RestRequestWrapper;
import io.mosip.signup.dto.RestResponseWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.core.env.Environment;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.Map;
import java.util.concurrent.CompletableFuture;

@Component
public class NotificationHelper {

@Autowired
@Qualifier("selfTokenRestTemplate")
private RestTemplate selfTokenRestTemplate;

@Autowired
private Environment environment;

@Value("${mosip.signup.send-notification.endpoint}")
private String sendNotificationEndpoint;


@Async
public CompletableFuture<RestResponseWrapper<NotificationResponse>> sendSMSNotificationAsync
(String number, String locale, String templateKey, Map<String, String> params){

locale = locale != null ? locale : "khm";
String message = environment.getProperty(templateKey + "." + locale);

if(params != null){
for (Map.Entry<String, String> entry: params.entrySet()){
message = message.replace(entry.getKey(), entry.getValue());
}
}

NotificationRequest notificationRequest = new NotificationRequest(number.substring(4), message);

RestRequestWrapper<NotificationRequest> restRequestWrapper = new RestRequestWrapper<>();
restRequestWrapper.setRequesttime(IdentityProviderUtil.getUTCDateTime());
restRequestWrapper.setRequest(notificationRequest);

return CompletableFuture.supplyAsync(() -> selfTokenRestTemplate
.exchange(sendNotificationEndpoint,
HttpMethod.POST,
new HttpEntity<>(restRequestWrapper),
new ParameterizedTypeReference<RestResponseWrapper<NotificationResponse>>(){}).getBody());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,22 @@
import io.mosip.signup.exception.CaptchaException;
import io.mosip.signup.util.SignUpConstants;
import io.mosip.signup.exception.GenerateChallengeException;
import io.mosip.signup.helper.NotificationHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.client.RestTemplate;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;

import java.util.concurrent.CompletableFuture;
import java.util.HashMap;

import static io.mosip.signup.util.SignUpConstants.CONSENT_DISAGREE;

Expand All @@ -44,6 +44,9 @@ public class RegistrationService {
@Autowired
private ChallengeManagerService challengeManagerService;

@Autowired
private NotificationHelper notificationHelper;

@Autowired
HttpServletResponse response;

Expand Down Expand Up @@ -78,17 +81,9 @@ public class RegistrationService {
@Value("${mosip.signup.challenge.resend-delay}")
private long resendDelay;

@Value("${mosip.signup.otp-registration.sms.khm}")
private String otpRegistrationKhmMessage;

@Value("${mosip.signup.otp-registration.sms.eng}")
private String otpRegistrationEngMessage;

@Value("${mosip.signup.successfully.registration.sms.khm}")
private String registrationSuccessKhmMessage;
private String otpRegistrationMessageTemplatekey = "mosip.signup.sms-notification-template.send-otp";

@Value("${mosip.signup.successfully.registration.sms.eng}")
private String registrationSuccessEngMessage;
private String registrationSMSNotificationTemplate = "mosip.signup.sms-notification-template.success-registration";

@Value("${mosip.signup.unauthenticated.txn.timeout}")
private int unauthenticatedTransactionTimeout;
Expand Down Expand Up @@ -136,7 +131,8 @@ public GenerateChallengeResponse generateChallenge(GenerateChallengeRequest gene
transaction.setLocale(generateChallengeRequest.getLocale());
cacheUtilService.setChallengeGeneratedTransaction(transactionId, transaction);

sendNotificationAsync(generateChallengeRequest.getIdentifier(), transaction.getLocale(), challenge)
notificationHelper.sendSMSNotificationAsync(generateChallengeRequest.getIdentifier(), transaction.getLocale(),
otpRegistrationMessageTemplatekey, new HashMap<>(){{put("{challenge}", challenge);}})
.thenAccept(notificationResponseRestResponseWrapper -> {
log.debug("Notification response -> {}", notificationResponseRestResponseWrapper);
});
Expand Down Expand Up @@ -194,6 +190,12 @@ public RegisterResponse register(RegisterRequest registerRequest, String transac
transaction.setRegistrationStatus(RegistrationStatus.PENDING);
cacheUtilService.setRegisteredTransaction(transactionId, transaction);

notificationHelper.sendSMSNotificationAsync(transaction.getIdentifier(), transaction.getLocale(),
registrationSMSNotificationTemplate, null)
.thenAccept(notificationResponseRestResponseWrapper -> {
log.debug("Notification response -> {}", notificationResponseRestResponseWrapper);
});

RegisterResponse registration = new RegisterResponse();
registration.setStatus(ActionStatus.PENDING);
log.debug("Transaction {} : registration status {}", transactionId, RegistrationStatus.PENDING);
Expand Down Expand Up @@ -299,25 +301,6 @@ private String getUniqueIdentifier(String transactionId) throws SignUpException
restResponseWrapper.getErrors().get(0).getErrorCode() : ErrorConstants.GET_UIN_FAILED);
}

@Async
private CompletableFuture<RestResponseWrapper<NotificationResponse>> sendNotificationAsync
(String number, String locale, String challenge) {

NotificationRequest notificationRequest = new NotificationRequest(number.substring(4),
locale == null || locale.equals("khm") ? otpRegistrationKhmMessage : otpRegistrationEngMessage );

notificationRequest.setMessage(notificationRequest.getMessage().replace("XXXXXX", challenge));

RestRequestWrapper<NotificationRequest> restRequestWrapper = new RestRequestWrapper<>();
restRequestWrapper.setRequest(notificationRequest);

return CompletableFuture.supplyAsync(() -> selfTokenRestTemplate
.exchange(sendNotificationEndpoint,
HttpMethod.POST,
new HttpEntity<>(restRequestWrapper),
new ParameterizedTypeReference<RestResponseWrapper<NotificationResponse>>(){}).getBody());
}

private void validateTransaction(RegistrationTransaction transaction, String identifier) {
if(transaction == null) {
log.error("generate-challenge failed: validate transaction null");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,9 @@ mosip.signup.ui.config.key-values={\
'status.request.limit': '${mosip.signup.status.request.limit}' \
}
## ----------------------------- SMS-message -----------------------------------------------------------------------------
mosip.signup.otp-registration.sms.khm=ប្រើ XXXXXX ដើម្បីផ្ទៀងផ្ទាត់គណនី KhID របស់អ្នក។
mosip.signup.otp-registration.sms.eng=Use XXXXXX to verify your KhID account.
mosip.signup.successfully.registration.sms.khm=ប្រើ XXXXXX ដើម្បីផ្ទៀងផ្ទាត់គណនី KhID របស់អ្នក។
mosip.signup.successfully.registration.sms.eng=Use XXXXXX to verify your KhID account.
mosip.signup.sms-notification-template.send-otp.khm=ប្រើ {challenge} ដើម្បីផ្ទៀងផ្ទាត់គណនី KhID របស់អ្នក។
mosip.signup.sms-notification-template.send-otp.eng=Use {challenge} to verify your KhID account.
mosip.signup.sms-notification-template.success-registration.khm=អ្នកបានចុះឈ្មោះគណនី KhID ដោយជោគជ័យ។
mosip.signup.sms-notification-template.success-registration.eng=You successfully registered to KhID account.
#------------------------------------------ Others ---------------------------------------------------------------------
logging.level.io.mosip.signup=DEBUG
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import io.mosip.signup.exception.ChallengeFailedException;
import io.mosip.signup.exception.InvalidIdentifierException;
import io.mosip.signup.exception.InvalidTransactionException;
import io.mosip.signup.helper.NotificationHelper;
import io.mosip.signup.util.ErrorConstants;
import io.mosip.signup.util.RegistrationStatus;
import io.mosip.signup.exception.SignUpException;
Expand All @@ -21,7 +22,6 @@
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.util.ReflectionTestUtils;
import org.springframework.web.client.RestTemplate;
Expand All @@ -40,6 +40,7 @@
import io.mosip.esignet.core.exception.EsignetException;

import java.time.LocalDateTime;
import java.util.concurrent.CompletableFuture;

@RunWith(SpringRunner.class)
@ActiveProfiles(value = {"test"})
Expand All @@ -63,6 +64,9 @@ public class RegistrationServiceTest {
@Mock
GoogleRecaptchaValidatorService googleRecaptchaValidatorService;

@Mock
NotificationHelper notificationHelper;

private final String addIdentityEndpoint = "addIdentityEndpoint";
private final String generateHashEndpoint = "generateHashEndpoint";
private final String getUinEndpoint = "getUinEndpoint";
Expand All @@ -76,12 +80,6 @@ public void setUp(){
registrationService, "resendAttempts", 3);
ReflectionTestUtils.setField(
registrationService, "resendDelay", 30);

ReflectionTestUtils.setField(
registrationService, "otpRegistrationKhmMessage", "ប្រើ XXXXXX ដើម្បីផ្ទៀងផ្ទាត់គណនី KhID របស់អ្នក។");

ReflectionTestUtils.setField(
registrationService, "otpRegistrationEngMessage", "Use XXXXXX to verify your KhID account.");
}

@Test
Expand Down Expand Up @@ -230,6 +228,9 @@ public void register_thenPass() throws Exception{
any(HttpEntity.class),
any(ParameterizedTypeReference.class))).thenReturn(new ResponseEntity<>(mockRestResponseWrapperAddIdentityResponse, HttpStatus.OK));

when(notificationHelper.sendSMSNotificationAsync(any(), any(), any(), any()))
.thenReturn(new CompletableFuture<>());

RegisterResponse registerResponse = registrationService.register(registerRequest, mockTransactionID);
Assert.assertNotNull(registerResponse);
Assert.assertEquals("PENDING", registerResponse.getStatus());
Expand Down Expand Up @@ -636,6 +637,8 @@ public void doGenerateChallenge_withoutTransactionId_thenPass() throws SignUpExc
when(challengeManagerService.generateChallenge(any())).thenReturn("1111");
when(googleRecaptchaValidatorService.validateCaptcha(
generateChallengeRequest.getCaptchaToken())).thenReturn(true);
when(notificationHelper.sendSMSNotificationAsync(any(), any(), any(), any()))
.thenReturn(new CompletableFuture<>());

GenerateChallengeResponse generateChallengeResponse =
registrationService.generateChallenge(
Expand All @@ -659,6 +662,8 @@ public void doGenerateChallenge_withTransactionId_thenPass() throws SignUpExcept
when(challengeManagerService.generateChallenge(transaction)).thenReturn("1111");
when(googleRecaptchaValidatorService.validateCaptcha(
generateChallengeRequest.getCaptchaToken())).thenReturn(true);
when(notificationHelper.sendSMSNotificationAsync(any(), any(), any(), any()))
.thenReturn(new CompletableFuture<>());

GenerateChallengeResponse generateChallengeResponse =
registrationService.generateChallenge(
Expand Down

0 comments on commit 5c12ab6

Please sign in to comment.