diff --git a/course-mw/course-actors-common/src/main/java/org/sunbird/learner/actors/coursebatch/dao/BatchUserDao.java b/course-mw/course-actors-common/src/main/java/org/sunbird/learner/actors/coursebatch/dao/BatchUserDao.java index eca218bc7..c8975b52f 100644 --- a/course-mw/course-actors-common/src/main/java/org/sunbird/learner/actors/coursebatch/dao/BatchUserDao.java +++ b/course-mw/course-actors-common/src/main/java/org/sunbird/learner/actors/coursebatch/dao/BatchUserDao.java @@ -18,7 +18,7 @@ public interface BatchUserDao { * @return User batch information */ - BatchUser readById(RequestContext requestContext, String batchId); + List readById(RequestContext requestContext, String batchId); BatchUser read(RequestContext requestContext, String batchId, String userId); Response insertBatchLookupRecord(RequestContext requestContext, Map userCoursesDetails); diff --git a/course-mw/course-actors-common/src/main/java/org/sunbird/learner/actors/coursebatch/dao/impl/BatchUserDaoImpl.java b/course-mw/course-actors-common/src/main/java/org/sunbird/learner/actors/coursebatch/dao/impl/BatchUserDaoImpl.java index 1ca13cefa..d87388fd1 100644 --- a/course-mw/course-actors-common/src/main/java/org/sunbird/learner/actors/coursebatch/dao/impl/BatchUserDaoImpl.java +++ b/course-mw/course-actors-common/src/main/java/org/sunbird/learner/actors/coursebatch/dao/impl/BatchUserDaoImpl.java @@ -14,10 +14,7 @@ import org.sunbird.models.batch.user.BatchUser; import org.sunbird.common.models.util.LoggerUtil; - -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.util.*; public class BatchUserDaoImpl implements BatchUserDao{ protected LoggerUtil logger = new LoggerUtil(this.getClass()); @@ -37,17 +34,25 @@ public static BatchUserDao getInstance() { } @Override - public BatchUser readById(RequestContext requestContext, String batchId) { + public List readById(RequestContext requestContext, String batchId) { Map primaryKey = new HashMap<>(); primaryKey.put(JsonKey.BATCH_ID, batchId); Response response = cassandraOperation.getRecordByIdentifier(requestContext, KEYSPACE_NAME, ENROLLMENT_BATCH, primaryKey, null); try { List> batchUserList = - (List>) response.get(JsonKey.RESPONSE); + (List>) response.get(JsonKey.RESPONSE); if (CollectionUtils.isEmpty(batchUserList)) { return null; } - return mapper.convertValue((Map) batchUserList.get(0), BatchUser.class); + List batchUsers = new ArrayList<>(); + for (Map userDetail : batchUserList) { + BatchUser batchUser = new BatchUser(); + batchUser.setBatchId((String) userDetail.get("batchId")); + batchUser.setUserId((String) userDetail.get("userId")); + batchUser.setActive((Boolean) userDetail.get("active")); + batchUsers.add(batchUser); + } + return batchUsers; } catch (Exception e) { logger.error(requestContext, "Failed to read BatchUser Table. Exception: ", e); return null; diff --git a/course-mw/course-actors/src/main/java/org/sunbird/learner/actors/coursebatch/CourseBatchManagementActor.java b/course-mw/course-actors/src/main/java/org/sunbird/learner/actors/coursebatch/CourseBatchManagementActor.java index b760e5e03..ae0c7dab7 100644 --- a/course-mw/course-actors/src/main/java/org/sunbird/learner/actors/coursebatch/CourseBatchManagementActor.java +++ b/course-mw/course-actors/src/main/java/org/sunbird/learner/actors/coursebatch/CourseBatchManagementActor.java @@ -233,6 +233,10 @@ private void updateCourseBatch(Request actorMessage) throws Exception { if (courseNotificationActive()) { batchOperationNotifier(actorMessage, courseBatch, participantsMap); } + if (batchDatesUpdateNotificationActive()) { + batchDatesUpdateNotifier(actorMessage, courseBatch, oldBatch); + } + } private Map getMentorLists( @@ -729,5 +733,23 @@ private static boolean enrolmentDateValidationEnabled() { PropertiesCache.getInstance() .getProperty(JsonKey.COURSE_BATCH_ENROLL_END_DATE_LESS)); } + private boolean batchDatesUpdateNotificationActive() { + return Boolean.parseBoolean( + PropertiesCache.getInstance() + .getProperty(JsonKey.SUNBIRD_BATCH_UPDATE_NOTIFICATIONS_ENABLED)); + } + + private void batchDatesUpdateNotifier(Request actorMessage, CourseBatch updatedCourseBatch, CourseBatch oldCourseBatch) { + Request batchNotification = new Request(actorMessage.getRequestContext()); + batchNotification.getContext().putAll(actorMessage.getContext()); + batchNotification.setOperation(ActorOperations.COURSE_BATCH_DATE_NOTIFICATION.getValue()); + Map request = new HashMap<>(); + request.put(Constants.OLD_COURSE_BATCH, oldCourseBatch); + request.put(Constants.UPDATED_COURSE_BATCH, updatedCourseBatch); + request.put(Constants.REQUEST_CONTEXT, actorMessage.getRequestContext()); + request.put(Constants.REQUEST_CONTEXT,actorMessage.getRequest()); + batchNotification.setRequest(request); + courseBatchNotificationActorRef.tell(batchNotification, getSelf()); + } } diff --git a/course-mw/course-actors/src/main/java/org/sunbird/learner/actors/coursebatch/CourseBatchNotificationActor.java b/course-mw/course-actors/src/main/java/org/sunbird/learner/actors/coursebatch/CourseBatchNotificationActor.java index db79c9324..6fd0b0859 100644 --- a/course-mw/course-actors/src/main/java/org/sunbird/learner/actors/coursebatch/CourseBatchNotificationActor.java +++ b/course-mw/course-actors/src/main/java/org/sunbird/learner/actors/coursebatch/CourseBatchNotificationActor.java @@ -1,25 +1,32 @@ package org.sunbird.learner.actors.coursebatch; import com.fasterxml.jackson.databind.ObjectMapper; +import com.mashape.unirest.http.HttpResponse; +import com.mashape.unirest.http.Unirest; +import com.mashape.unirest.http.exceptions.UnirestException; import org.apache.commons.collections.CollectionUtils; +import org.apache.commons.httpclient.HttpStatus; import org.sunbird.actor.base.BaseActor; +import org.sunbird.common.Constants; +import org.sunbird.common.exception.ProjectCommonException; import org.sunbird.common.models.util.ActorOperations; import org.sunbird.common.models.util.JsonKey; -import org.sunbird.common.models.util.LoggerUtil; import org.sunbird.common.models.util.PropertiesCache; import org.sunbird.common.request.Request; import org.sunbird.common.request.RequestContext; +import org.sunbird.common.responsecode.ResponseCode; import org.sunbird.common.util.JsonUtil; +import org.sunbird.learner.actors.coursebatch.dao.BatchUserDao; +import org.sunbird.learner.actors.coursebatch.dao.impl.BatchUserDaoImpl; import org.sunbird.learner.util.ContentUtil; import org.sunbird.learner.util.CourseBatchSchedulerUtil; +import org.sunbird.models.batch.user.BatchUser; import org.sunbird.models.course.batch.CourseBatch; import org.sunbird.userorg.UserOrgService; import org.sunbird.userorg.UserOrgServiceImpl; -import java.util.Arrays; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.util.*; +import java.util.stream.Collectors; /** * Actor responsible to sending email notifications to participants and mentors in open and @@ -27,13 +34,15 @@ */ public class CourseBatchNotificationActor extends BaseActor { private static String courseBatchNotificationSignature = - PropertiesCache.getInstance() - .getProperty(JsonKey.SUNBIRD_COURSE_BATCH_NOTIFICATION_SIGNATURE); + PropertiesCache.getInstance() + .getProperty(JsonKey.SUNBIRD_COURSE_BATCH_NOTIFICATION_SIGNATURE); private static String baseUrl = - PropertiesCache.getInstance().getProperty(JsonKey.SUNBIRD_WEB_URL); + PropertiesCache.getInstance().getProperty(JsonKey.SUNBIRD_WEB_URL); private static String courseBatchPath = PropertiesCache.getInstance().getProperty(JsonKey.COURSE_BATCH_PATH); private UserOrgService userOrgService = UserOrgServiceImpl.getInstance(); + private BatchUserDao batchUserDao = new BatchUserDaoImpl(); + private ObjectMapper mapper = new ObjectMapper(); @Override public void onReceive(Request request) throws Throwable { @@ -42,6 +51,8 @@ public void onReceive(Request request) throws Throwable { if (requestedOperation.equals(ActorOperations.COURSE_BATCH_NOTIFICATION.getValue())) { logger.info(request.getRequestContext(), "CourseBatchNotificationActor:onReceive: operation = " + request.getOperation()); courseBatchNotification(request); + } else if (requestedOperation.equals(ActorOperations.COURSE_BATCH_DATE_NOTIFICATION.getValue())) { + courseBatchDatesUpdateNotification(request); } else { logger.error(request.getRequestContext(), "CourseBatchNotificationActor:onReceive: Unsupported operation = " + request.getOperation(), null); @@ -182,7 +193,80 @@ private void sendMail(RequestContext requestContext, Map request logger.info(requestContext, "CourseBatchNotificationActor:sendMail: Email sent successfully"); } catch (Exception e) { logger.error(requestContext, "CourseBatchNotificationActor:sendMail: Exception occurred with error message = " - + e.getMessage(), e); + + e.getMessage(), e); } } -} + + private void courseBatchDatesUpdateNotification(Request request) throws UnirestException { + CourseBatch oldCourseBatch = (CourseBatch) request.getRequest().get("oldCourseBatch"); + CourseBatch updatedCourseBatch = (CourseBatch) request.getRequest().get("updatedCourseBatch"); + RequestContext context = (RequestContext) request.getRequest().get("requestContext"); + Map requestBody = (Map) request.getRequest().get("requestBody"); + Map batchAttributes = (Map) requestBody.get("batchAttributes"); + List mentorsIds = (List) requestBody.get("mentors"); + List reciepientList = new ArrayList<>(); + reciepientList.addAll(mentorsIds); + if (null != batchAttributes) { + List> sessionDetails = (List>) batchAttributes.get("sessionDetails_v2"); + if (null != sessionDetails) { + for (Map sessionDetail : sessionDetails) { + List facilatorIds = (List) sessionDetail.get("facilatorIDs"); + reciepientList.addAll(facilatorIds); + } + } + } + List batchUsers = batchUserDao.readById(context, updatedCourseBatch.getBatchId()); + List batchUserIdList = batchUsers.stream().filter(e -> e.getActive()).map(user -> user.getUserId()).collect(Collectors.toList()); + reciepientList.addAll(batchUserIdList); + sendEmailNotificationMailForBatchDatesUpdate(reciepientList, oldCourseBatch, updatedCourseBatch); + } + + private void sendEmailNotificationMailForBatchDatesUpdate(List reciepientList, CourseBatch oldBatch, CourseBatch updatedBatch) { + Map request = new HashMap<>(); + request.put(Constants.COURSE_NAME, updatedBatch.getName()); + request.put(Constants.BATCH_NAME, updatedBatch.getDescription()); + request.put(Constants.RECIPIENT_IDS, reciepientList); + request.put(JsonKey.SUBJECT, getBatchUpdateEmailSubject()); + request.put(Constants.TRAINING_NAME, updatedBatch.getName()); + request.put(JsonKey.REGARDS, Constants.KARMAYOGI_BHARAT); + request.put(JsonKey.EMAIL_TEMPLATE_TYPE, Constants.BATCH_DATE_UPDATE_TEMPLATE); + request.put(Constants.START_DATE, updatedBatch.getStartDate()); + request.put(Constants.END_DATE, updatedBatch.getEndDate()); + request.put(Constants.ENROLLMENT_END_DATE, updatedBatch.getEnrollmentEndDate()); + request.put(JsonKey.BODY, Constants.EMAIL_BODY); + HashMap headers = new HashMap<>(); + headers.put(Constants.CONTENT_TYPE, Constants.APPLICATION_JSON); + HashMap requestBody = new HashMap<>(); + requestBody.put(JsonKey.REQUEST, request); + StringBuilder url = new StringBuilder(); + HttpResponse httpResponse = null; + String requeststr = null; + try { + requeststr = mapper.writeValueAsString(requestBody); + url.append(getLearnerHost()).append(getLearnerPath()); + httpResponse = Unirest.post(String.valueOf(url)).headers(headers).body(requeststr).asString(); + if (httpResponse != null && !ResponseCode.OK.equals(httpResponse.getStatus())) { + throw new RuntimeException("An error occured while sending mail notification"); + } + } catch (Exception e) { + if (e instanceof RuntimeException) { + logger.error(null, e.getMessage() + "Headers : " + httpResponse.getHeaders() + "Body : " + httpResponse.getBody(), e); + } + logger.error(null, "Exception occurred with error message = " + e.getMessage(), e); + } + } + + private String getLearnerHost() { + return PropertiesCache.getInstance() + .getProperty(JsonKey.LMS_SERVICE_HOST); + } + + private String getLearnerPath() { + return PropertiesCache.getInstance() + .getProperty(JsonKey.LMS_SEND_EMAIL_NOTIFICATION_PATH); + } + private String getBatchUpdateEmailSubject() { + return PropertiesCache.getInstance() + .getProperty(JsonKey.SUNBIRD_BATCH_DATE_UPDATE_NOTIFICATIONS_SUBJECT); + } +} \ No newline at end of file diff --git a/course-mw/sunbird-util/sunbird-cassandra-utils/src/main/java/org/sunbird/common/Constants.java b/course-mw/sunbird-util/sunbird-cassandra-utils/src/main/java/org/sunbird/common/Constants.java index 5d3272311..df5f709b3 100644 --- a/course-mw/sunbird-util/sunbird-cassandra-utils/src/main/java/org/sunbird/common/Constants.java +++ b/course-mw/sunbird-util/sunbird-cassandra-utils/src/main/java/org/sunbird/common/Constants.java @@ -64,4 +64,23 @@ public interface Constants { public static final String GT = ">"; public static final String ID = "id"; public static final Integer DEFAULT_LIMIT = 250; + public static final String CONTENT_TYPE = "Content-Type"; + public static final String APPLICATION_JSON = "application/json"; + public static final String OLD_COURSE_BATCH = "oldCourseBatch"; + public static final String UPDATED_COURSE_BATCH = "updatedCourseBatch"; + public static final String REQUEST_CONTEXT = "requestContext"; + public static final String COURSE_NAME = "courseName"; + public static final String BATCH_NAME = "batchName"; + public static final String RECIPIENT_IDS = "recipientUserIds"; + public static final String TRAINING_NAME = "trainingName"; + public static final String START_DATE = "startDate"; + public static final String END_DATE = "endDate"; + public static final String ENROLLMENT_END_DATE = "enrollmentEndDate"; + public static final String BATCH_DATE_UPDATE_TEMPLATE = "batchdateupdatetemplate"; + public static final String EMAIL_BODY = "emailBody"; + public static final String KARMAYOGI_BHARAT = "Karmayogi Bharat"; + + + + } diff --git a/course-mw/sunbird-util/sunbird-platform-core/common-util/src/main/java/org/sunbird/common/models/util/ActorOperations.java b/course-mw/sunbird-util/sunbird-platform-core/common-util/src/main/java/org/sunbird/common/models/util/ActorOperations.java index 186721530..b09114783 100644 --- a/course-mw/sunbird-util/sunbird-platform-core/common-util/src/main/java/org/sunbird/common/models/util/ActorOperations.java +++ b/course-mw/sunbird-util/sunbird-platform-core/common-util/src/main/java/org/sunbird/common/models/util/ActorOperations.java @@ -175,7 +175,8 @@ public enum ActorOperations { GROUP_ACTIVITY_AGGREGATES("groupActivityAggregates"), UPDATE_EVENT_SET("updateEventSet"), DELETE_EVENT_SET("deleteEventSet"), - DELETE_EVENT("deleteEvent"); + DELETE_EVENT("deleteEvent"), + COURSE_BATCH_DATE_NOTIFICATION("courseBatchDateNotification"); private String value; diff --git a/course-mw/sunbird-util/sunbird-platform-core/common-util/src/main/java/org/sunbird/common/models/util/JsonKey.java b/course-mw/sunbird-util/sunbird-platform-core/common-util/src/main/java/org/sunbird/common/models/util/JsonKey.java index 9e9e366d5..bf15ac2d7 100644 --- a/course-mw/sunbird-util/sunbird-platform-core/common-util/src/main/java/org/sunbird/common/models/util/JsonKey.java +++ b/course-mw/sunbird-util/sunbird-platform-core/common-util/src/main/java/org/sunbird/common/models/util/JsonKey.java @@ -1086,6 +1086,13 @@ public final class JsonKey { public static final String IS_ADMIN_API = "isAdminAPI"; public static final String RETIRED_COURE_ENABLED = "retiredCoursesEnabled"; public static final String FALSE = "false"; - + public static final String LMS_SERVICE_HOST = "lms.service.host"; + public static final String SUNBIRD_BATCH_UPDATE_NOTIFICATIONS_ENABLED = + "sunbird_batch_update_notification_enabled"; + + public static final String REGARDS = "regards"; + public static final String SUNBIRD_BATCH_DATE_UPDATE_NOTIFICATIONS_SUBJECT = "sunbird_batch_date_update_notifications_subject" ; + public static final String LMS_SEND_EMAIL_NOTIFICATION_PATH = "lms.send.email.notification"; + private JsonKey() {} } diff --git a/course-mw/sunbird-util/sunbird-platform-core/common-util/src/main/resources/externalresource.properties b/course-mw/sunbird-util/sunbird-platform-core/common-util/src/main/resources/externalresource.properties index bfef09917..b25d2e9b0 100644 --- a/course-mw/sunbird-util/sunbird-platform-core/common-util/src/main/resources/externalresource.properties +++ b/course-mw/sunbird-util/sunbird-platform-core/common-util/src/main/resources/externalresource.properties @@ -206,4 +206,7 @@ sunbird_api_mgr_base_url=https://dev.sunbirded.org/api enrollment_list_size=1000 sunbird_course_batch_path=/app/toc/{courseId}/overview?batchId={batchId} enrolment_list_include_retired_courses=false -enrol_end_date_allow_lesser_value=false +enrol_end_date_allow_lesser_value=true +lms.service.host=http://learner-service:9000/ +sunbird_batch_date_update_notifications_subject=Notification for changes in the batch dates +lms.send.email.notification=/v1/notification/email