Skip to content

Commit

Permalink
Merge pull request #96 from Nivetha-M/4.0.1-userRegistration
Browse files Browse the repository at this point in the history
Fixed registration notification
  • Loading branch information
karthik-tarento authored Jun 6, 2022
2 parents ef2480e + a5a570c commit 9271a8c
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 52 deletions.
15 changes: 15 additions & 0 deletions src/main/java/org/sunbird/common/util/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,21 @@ public class Constants {
public static final String DESCRIPTION = "description";
public static final String TEMPLATE = "template";
public static final String USER_REGISTERATION_TEMPLATE = "user-registration";
public static final String STATUS_PARAM = "{status}";
public static final String REG_CODE_PARAM = "{regCode}";
public static final String BUTTON_URL = "btn-url";
public static final String BUTTON_NAME = "btn-name";

// notification reuest params
public static final String DELIVERY_TYPE = "deliveryType";
public static final String CONFIG = "config";
public static final String IDS = "ids";

// wf status
public static final String INITIATED = "initiated";
public static final String APPROVED = "approved";
public static final String DENIED = "denied";

public static final String EMAIL_EXIST_ERROR = "Email id already registered";
public static final String EMAIL_VERIFIED = "emailVerified";
public static final String USER_NAME = "userName";
Expand Down
101 changes: 67 additions & 34 deletions src/main/java/org/sunbird/common/util/NotificationUtil.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
package org.sunbird.common.util;

import static org.sunbird.common.util.Constants.INCOMPLETE_COURSES;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
Expand All @@ -12,41 +21,65 @@
import org.sunbird.common.model.Notification;
import org.sunbird.common.model.Template;

import java.util.*;

import static org.sunbird.common.util.Constants.INCOMPLETE_COURSES;

@Service(Constants.NOTIFICATION_UTIL)
public class NotificationUtil {
public static final Logger Logger;
private static final String EXCEPTION = "Exception in Send Notification %s";

static {
Logger = LoggerFactory.getLogger(NotificationUtil.class);
}

public <params> void sendNotification(List<String> sendTo, Map<String, Object> params, String senderMail, String notificationUrl) {
new Thread(() -> {
try {
HttpHeaders headers = new HttpHeaders();
RestTemplate restTemplate = new RestTemplate();
headers.setContentType(MediaType.APPLICATION_JSON);
Map<String, Object> notificationRequest = new HashMap<>();
List<Object> notificationTosend = new ArrayList<>(Arrays.asList(new Notification(Constants.EMAIL, Constants.MESSAGE,
new EmailConfig(senderMail, Constants.INCOMPLETE_COURSES_MAIL_SUBJECT), sendTo,
new Template(null, INCOMPLETE_COURSES, params))));
notificationRequest.put("request", new HashMap<String, List<Object>>() {
{
put("notifications", notificationTosend);
}
});
Logger.info(String.format("Notification Request : %s", notificationRequest));
HttpEntity<Object> req = new HttpEntity<>(notificationRequest, headers);
ResponseEntity<String> resp = restTemplate.postForEntity(notificationUrl, req, String.class);
} catch (Exception e) {
Logger.error(String.format(EXCEPTION, e.getMessage()));
}
}).start();
}
public static final Logger Logger;
private static final String EXCEPTION = "Exception in Send Notification %s";

@Autowired
RestTemplate restTemplate;

static {
Logger = LoggerFactory.getLogger(NotificationUtil.class);
}

public <params> void sendNotification(List<String> sendTo, Map<String, Object> params, String senderMail,
String notificationUrl) {
new Thread(() -> {
try {
HttpHeaders headers = new HttpHeaders();
RestTemplate restTemplate = new RestTemplate();
headers.setContentType(MediaType.APPLICATION_JSON);
Map<String, Object> notificationRequest = new HashMap<>();
List<Object> notificationTosend = new ArrayList<>(Arrays.asList(new Notification(Constants.EMAIL,
Constants.MESSAGE, new EmailConfig(senderMail, Constants.INCOMPLETE_COURSES_MAIL_SUBJECT),
sendTo, new Template(null, INCOMPLETE_COURSES, params))));
notificationRequest.put(Constants.REQUEST, new HashMap<String, List<Object>>() {
{
put(Constants.NOTIFICATIONS, notificationTosend);
}
});
Logger.info(String.format("Notification Request : %s", notificationRequest));
HttpEntity<Object> req = new HttpEntity<>(notificationRequest, headers);
ResponseEntity<String> resp = restTemplate.postForEntity(notificationUrl, req, String.class);
} catch (Exception e) {
Logger.error(String.format(EXCEPTION, e.getMessage()));
}
}).start();
}

public void sendNotification(List<Map<String, Object>> notifications) {
new Thread(() -> {
try {
String notificationUrl = PropertiesCache.getInstance().getProperty(Constants.NOTIFICATION_HOST)
+ PropertiesCache.getInstance().getProperty(Constants.NOTIFICATION_ENDPOINT);

HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
Map<String, Object> notificationRequest = new HashMap<>();
notificationRequest.put(Constants.REQUEST, new HashMap<String, Object>() {
{
put(Constants.NOTIFICATIONS, notifications);
}
});

HttpEntity<Object> req = new HttpEntity<>(notificationRequest, headers);
Logger.info(String.format("Notification Request : %s", notificationRequest));
restTemplate.postForEntity(notificationUrl, req, Object.class);
} catch (Exception e) {
Logger.error(String.format(EXCEPTION, e.getMessage()));
}
}).start();
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.sunbird.user.registration.service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -10,18 +11,17 @@
import org.sunbird.common.util.CbExtServerProperties;
import org.sunbird.common.util.Constants;
import org.sunbird.common.util.NotificationUtil;
import org.sunbird.common.util.PropertiesCache;
import org.sunbird.user.registration.model.UserRegistration;

@Service
public class UserRegistrationNotificationServiceImpl implements UserRegistrationNotificationService {

@Autowired
CbExtServerProperties serverProperties;

@Autowired
NotificationUtil notificationUtil;

@Override
public void sendNotification(UserRegistration userRegistration) {
List<String> sendTo = new ArrayList<String>() {
Expand All @@ -33,46 +33,58 @@ public void sendNotification(UserRegistration userRegistration) {
};

Map<String, Object> notificationObj = new HashMap<>();
notificationObj.put("mode", Constants.EMAIL);
notificationObj.put("deliveryType", Constants.MESSAGE);
notificationObj.put("config", new HashMap<String, Object>() {
notificationObj.put(Constants.MODE, Constants.EMAIL);
notificationObj.put(Constants.DELIVERY_TYPE, Constants.MESSAGE);
notificationObj.put(Constants.CONFIG, new HashMap<String, Object>() {
{
put(Constants.SUBJECT, serverProperties.getUserRegistrationSubject());
}
});
notificationObj.put("ids", sendTo);
notificationObj.put(Constants.IDS, sendTo);
notificationObj.put(Constants.TEMPLATE,
notificationMessage(userRegistration.getStatus(), userRegistration.getRegistrationCode()));

if (notificationObj.get(Constants.TEMPLATE) != null) {
notificationUtil.sendNotification(sendTo, notificationObj,
PropertiesCache.getInstance().getProperty(Constants.SENDER_MAIL),
PropertiesCache.getInstance().getProperty(Constants.NOTIFICATION_HOST)
+ PropertiesCache.getInstance().getProperty(Constants.NOTIFICATION_ENDPOINT));
notificationUtil.sendNotification(Arrays.asList(notificationObj));
}
}

private Map<String, Object> notificationMessage(String status, String regCode) {
Map<String, Object> template = new HashMap<>();
template.put(Constants.ID, Constants.USER_REGISTERATION_TEMPLATE);
Map<String, Object> params = new HashMap<>();
params.put(Constants.STATUS, serverProperties.getUserRegistrationStatus().replace("{status}", status));
params.put(Constants.TITLE, serverProperties.getUserRegistrationTitle().replace("{status}", status));
template.put("params", params);

template.put(Constants.PARAMS, params);
switch (status) {
case "WF_INITIATED":
params.put(Constants.TITLE, serverProperties.getUserRegistrationThankyouMessage());
params.put(Constants.STATUS,
serverProperties.getUserRegistrationStatus().replace(Constants.STATUS_PARAM, Constants.INITIATED));
params.put(Constants.TITLE,
serverProperties.getUserRegistrationTitle().replace(Constants.STATUS_PARAM, Constants.INITIATED));
params.put(Constants.DESCRIPTION, serverProperties.getUserRegistrationInitiatedMessage()
.replace("{regCode}", "<b>" + regCode + "</b>"));
.replace(Constants.REG_CODE_PARAM, "<b>" + regCode + "</b>"));
break;
case "WF_APPROVED":
params.put(Constants.STATUS,
serverProperties.getUserRegistrationStatus().replace(Constants.STATUS_PARAM, Constants.APPROVED));
params.put(Constants.TITLE,
serverProperties.getUserRegistrationTitle().replace(Constants.STATUS_PARAM, Constants.APPROVED));
params.put(Constants.DESCRIPTION, serverProperties.getUserRegistrationApprovedMessage());
params.put("btn-url", serverProperties.getUserRegistrationDomainName());
params.put("btn-name", serverProperties.getUserRegisterationButtonName());
params.put(Constants.BUTTON_URL, serverProperties.getUserRegistrationDomainName());
params.put(Constants.BUTTON_NAME, serverProperties.getUserRegisterationButtonName());
break;
case "WF_DENIED":
params.put(Constants.STATUS,
serverProperties.getUserRegistrationStatus().replace(Constants.STATUS_PARAM, Constants.DENIED));
params.put(Constants.TITLE,
serverProperties.getUserRegistrationTitle().replace(Constants.STATUS_PARAM, Constants.DENIED));
break;
case "FAILED":
params.put(Constants.STATUS, serverProperties.getUserRegistrationStatus().replace(Constants.STATUS_PARAM,
Constants.FAILED.toLowerCase()));
params.put(Constants.TITLE, serverProperties.getUserRegistrationTitle().replace(Constants.STATUS_PARAM,
Constants.FAILED.toLowerCase()));
params.put(Constants.STATUS, serverProperties.getUserRegistrationFailedMessage());
break;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ public enum UserRegistrationStatus {
public int getStatus() {
return status;
}

}

0 comments on commit 9271a8c

Please sign in to comment.