diff --git a/src/main/java/org/sunbird/workflow/config/Configuration.java b/src/main/java/org/sunbird/workflow/config/Configuration.java index f7f9650d..e987bbb5 100644 --- a/src/main/java/org/sunbird/workflow/config/Configuration.java +++ b/src/main/java/org/sunbird/workflow/config/Configuration.java @@ -164,6 +164,27 @@ public class Configuration { @Value("${blended.program.batch.in.progress.message}") private String batchInProgressMessage; + @Value("${bp.mail.body.forwarded.to}") + private String learnerForwardedMailBody; + + @Value("${bp.mail.body.rejected.or.remove}") + private String rejectedOrRemovedMailBody; + + @Value("${bp.mail.body.approve}") + private String approvedMailBody; + + @Value("${notify.email.template}") + private String notificationEmailTemplate; + + @Value("${bp.approval.request.mail.body}") + private String approvalRequetMailBody; + + @Value("${bp.request.forwarded.mail.body}") + private String requestForwardedMailBody; + + @Value("${bp.nomination.request.mail.body}") + private String nominationRequestMailBody; + public String getModificationRecordAllowActions() { return modificationRecordAllowActions; } @@ -597,4 +618,59 @@ public void setBatchInProgressMessage(String batchInProgressMessage) { this.batchInProgressMessage = batchInProgressMessage; } + public String getLearnerForwardedMailBody() { + return learnerForwardedMailBody; + } + + public void setLearnerForwardedMailBody(String learnerForwardedMailBody) { + this.learnerForwardedMailBody = learnerForwardedMailBody; + } + + public String getRejectedOrRemovedMailBody() { + return rejectedOrRemovedMailBody; + } + + public void setRejectedOrRemovedMailBody(String rejectedOrRemovedMailBody) { + this.rejectedOrRemovedMailBody = rejectedOrRemovedMailBody; + } + + public String getApprovedMailBody() { + return approvedMailBody; + } + + public void setApprovedMailBody(String approvedMailBody) { + this.approvedMailBody = approvedMailBody; + } + + public String getNotificationEmailTemplate() { + return notificationEmailTemplate; + } + + public void setNotificationEmailTemplate(String notificationEmailTemplate) { + this.notificationEmailTemplate = notificationEmailTemplate; + } + + public String getApprovalRequetMailBody() { + return approvalRequetMailBody; + } + + public void setApprovalRequetMailBody(String approvalRequetMailBody) { + this.approvalRequetMailBody = approvalRequetMailBody; + } + + public String getRequestForwardedMailBody() { + return requestForwardedMailBody; + } + + public void setRequestForwardedMailBody(String requestForwardedMailBody) { + this.requestForwardedMailBody = requestForwardedMailBody; + } + + public String getNominationRequestMailBody() { + return nominationRequestMailBody; + } + + public void setNominationRequestMailBody(String nominationRequestMailBody) { + this.nominationRequestMailBody = nominationRequestMailBody; + } } diff --git a/src/main/java/org/sunbird/workflow/config/Constants.java b/src/main/java/org/sunbird/workflow/config/Constants.java index 9e18c80a..51a7059a 100644 --- a/src/main/java/org/sunbird/workflow/config/Constants.java +++ b/src/main/java/org/sunbird/workflow/config/Constants.java @@ -238,6 +238,18 @@ private Constants() { public static final String BATCH_SIZE_ERROR = "Batch Size Error";; public static final String TRUE="True"; public static final String ACTIVE = "active"; + public static final String DESCRIPTION = "description"; + public static final String COURSE_NAME = "courseName"; + public static final String BATCH_NAME = "batchName"; + public static final String BATCH_START_DATE = "batchStartDate"; + public static final String FROM_EMAIL = "fromEmail"; + public static final String DEV_HIERARCHY_STORE = "dev_hierarchy_store"; + public static final String CONTENT_HIERARCHY = "content_hierarchy"; + public static final String IDENTIFIER = "identifier"; + public static final String HIERARCHY = "hierarchy"; + public static final Object KARMYOGI_BHARAT = "Karmyogi Bharat"; + public static final String TO_PROGRAMME_COORDINATOR = "Programme Coordinator"; + public static final String TO_MDO_ADMIN = "MDO Admin"; public static final String DOMAIN ="domain"; public static final String TABLE_MASTER_DATA = "master_data"; public static final String CONTEXT_TYPE = "contextType"; diff --git a/src/main/java/org/sunbird/workflow/consumer/NotificationConsumer.java b/src/main/java/org/sunbird/workflow/consumer/NotificationConsumer.java index 9579aa59..3304f200 100644 --- a/src/main/java/org/sunbird/workflow/consumer/NotificationConsumer.java +++ b/src/main/java/org/sunbird/workflow/consumer/NotificationConsumer.java @@ -1,5 +1,7 @@ package org.sunbird.workflow.consumer; +import com.google.gson.Gson; +import com.google.gson.reflect.TypeToken; import org.apache.kafka.clients.consumer.ConsumerRecord; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -7,10 +9,17 @@ import org.springframework.kafka.annotation.KafkaListener; import org.springframework.stereotype.Service; import org.sunbird.workflow.config.Constants; +import org.sunbird.workflow.models.WfNotification; import org.sunbird.workflow.models.WfRequest; import org.sunbird.workflow.service.impl.NotificationServiceImpl; import com.fasterxml.jackson.databind.ObjectMapper; +import org.sunbird.workflow.utils.CassandraOperation; + +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; @Service public class NotificationConsumer { @@ -22,30 +31,35 @@ public class NotificationConsumer { @Autowired private NotificationServiceImpl notificationService; + @Autowired + private CassandraOperation cassandraOperation; + @KafkaListener(groupId = "workflowNotificationTopic-consumer", topics = "${kafka.topics.workflow.notification}") public void processMessage(ConsumerRecord data) { - WfRequest wfRequest = null; + WfNotification wfNotification = null; try { String message = String.valueOf(data.value()); - wfRequest = mapper.readValue(message, WfRequest.class); - logger.info("Recevied data in notification consumer : {}", mapper.writeValueAsString(wfRequest)); - switch (wfRequest.getServiceName()) { + wfNotification = mapper.readValue(message, WfNotification.class); + Map courseAttributes = getCourseAttributes(wfNotification.getCourseId()); + wfNotification.setCourseName((String) courseAttributes.get(Constants.COURSE_NAME)); + logger.info("Recevied data in notification consumer : {}", mapper.writeValueAsString(wfNotification)); + switch (wfNotification.getServiceName()) { case Constants.PROFILE_SERVICE_NAME: - notificationService.sendNotification(wfRequest); - notificationService.sendNotificationToMdoAdmin(wfRequest); + notificationService.sendNotification(wfNotification); + notificationService.sendNotificationToMdoAdmin(wfNotification); break; case Constants.POSITION_SERVICE_NAME: case Constants.DOMAIN_SERVICE_NAME: case Constants.ORGANISATION_SERVICE_NAME: - notificationService.sendEmailNotification(wfRequest); + notificationService.sendEmailNotification(wfNotification); break; case Constants.BLENDED_PROGRAM_SERVICE_NAME: case Constants.ONE_STEP_MDO_APPROVAL: case Constants.ONE_STEP_PC_APPROVAL: case Constants.TWO_STEP_MDO_AND_PC_APPROVAL: case Constants.TWO_STEP_PC_AND_MDO_APPROVAL: - notificationService.sendNotification(wfRequest); - notificationService.sendNotificationToMdoAdminAndPC(wfRequest); + notificationService.sendNotification(wfNotification); + notificationService.sendNotificationToMdoAdminAndPC(wfNotification); break; case Constants.USER_REGISTRATION_SERVICE_NAME: // nothing to do @@ -58,4 +72,18 @@ public void processMessage(ConsumerRecord data) { logger.error("Error while deserialization the object value", ex); } } + + public Map getCourseAttributes(String courseId){ + Map propertiesMap = new HashMap<>(); + Map courseDetails = new HashMap<>(); + propertiesMap.put(Constants.IDENTIFIER, courseId); + List> coursesDataList = cassandraOperation.getRecordsByProperties(Constants.DEV_HIERARCHY_STORE, + Constants.CONTENT_HIERARCHY, + propertiesMap, + Arrays.asList(Constants.IDENTIFIER, Constants.HIERARCHY)); + Map hierarchy = new Gson().fromJson((String) coursesDataList.get(0).get("hierarchy"), new TypeToken>(){}.getType()); + courseDetails.put(Constants.COURSE_NAME, hierarchy.get(Constants.NAME)); + return courseDetails; + } + } diff --git a/src/main/java/org/sunbird/workflow/models/WfNotification.java b/src/main/java/org/sunbird/workflow/models/WfNotification.java new file mode 100644 index 00000000..b587c9d8 --- /dev/null +++ b/src/main/java/org/sunbird/workflow/models/WfNotification.java @@ -0,0 +1,36 @@ +package org.sunbird.workflow.models; + +import java.util.Date; + +public class WfNotification extends WfRequest{ + + private String courseName; + + private String batchName; + + private Date batchStartDate; + + public String getCourseName() { + return courseName; + } + + public void setCourseName(String courseName) { + this.courseName = courseName; + } + + public String getBatchName() { + return batchName; + } + + public void setBatchName(String batchName) { + this.batchName = batchName; + } + + public Date getBatchStartDate() { + return batchStartDate; + } + + public void setBatchStartDate(Date batchStartDate) { + this.batchStartDate = batchStartDate; + } +} \ No newline at end of file diff --git a/src/main/java/org/sunbird/workflow/service/impl/BPWorkFlowServiceImpl.java b/src/main/java/org/sunbird/workflow/service/impl/BPWorkFlowServiceImpl.java index 4f9af021..652851e4 100644 --- a/src/main/java/org/sunbird/workflow/service/impl/BPWorkFlowServiceImpl.java +++ b/src/main/java/org/sunbird/workflow/service/impl/BPWorkFlowServiceImpl.java @@ -98,6 +98,9 @@ public Response enrolBPWorkFlow(String rootOrg, String org, WfRequest wfRequest) response.put(Constants.STATUS, HttpStatus.BAD_REQUEST); return response; } + WfNotification wfNotification = getWorkFlowNotificationRequest(wfRequest); + wfNotification.setBatchName((String) courseBatchDetails.get(Constants.BATCH_NAME)); + wfNotification.setBatchStartDate((Date) courseBatchDetails.get(Constants.START_DATE)); Response response = saveEnrollUserIntoWfStatus(rootOrg, org, wfRequest); wfRequest.setServiceName(Constants.BLENDED_PROGRAM_SERVICE_NAME); producer.push(configuration.getWorkflowApplicationTopic(), wfRequest); @@ -107,7 +110,11 @@ public Response enrolBPWorkFlow(String rootOrg, String org, WfRequest wfRequest) @Override public Response updateBPWorkFlow(String rootOrg, String org, WfRequest wfRequest,String userId,String role) { Response response = new Response(); - String validationError = validateBatchUserRequestAccess(wfRequest); + Map batchDetailsMap = new HashMap<>(); + WfNotification wfNotification = getWorkFlowNotificationRequest(wfRequest); + String validationError = validateBatchUserRequestAccess(wfRequest, batchDetailsMap); + wfNotification.setBatchName((String) batchDetailsMap.get(Constants.BATCH_NAME)); + wfNotification.setBatchStartDate((Date) batchDetailsMap.get(Constants.START_DATE)); if (Constants.BATCH_START_DATE_ERROR.equals(validationError)) { response.put(Constants.ERROR_MESSAGE, configuration.getBatchInProgressMessage()); response.put(Constants.STATUS, HttpStatus.BAD_REQUEST); @@ -200,7 +207,7 @@ private Map getCurrentBatchAttributes(String batchId, String cou Constants.KEYSPACE_SUNBIRD_COURSES, Constants.TABLE_COURSE_BATCH, propertyMap, - Arrays.asList(Constants.BATCH_ATTRIBUTES, Constants.ENROLMENT_END_DATE, Constants.START_DATE)); + Arrays.asList(Constants.BATCH_ATTRIBUTES, Constants.ENROLMENT_END_DATE, Constants.START_DATE, Constants.NAME)); if (CollectionUtils.isNotEmpty(batchAttributesDetails)) { Map courseBatch = (Map) batchAttributesDetails.get(0); if (courseBatch.containsKey(Constants.BATCH_ATTRIBUTES)) { @@ -221,10 +228,15 @@ private Map getCurrentBatchAttributes(String batchId, String cou Date batchStartDate = courseBatch.containsKey(Constants.START_DATE) ? (Date) courseBatch.get(Constants.START_DATE) : null; + String batchName = batchAttributes != null + && courseBatch.containsKey(Constants.NAME) + ? (String) courseBatch.get(Constants.NAME) + : ""; Map result = new HashMap<>(); result.put(Constants.CURRENT_BATCH_SIZE, currentBatchSize); result.put(Constants.ENROLMENT_END_DATE, enrollmentEndDate); result.put(Constants.START_DATE, batchStartDate); + result.put(Constants.BATCH_NAME, batchName); return result; } catch (Exception e) { logger.error(String.format("Failed to retrieve course batch details. CourseId: %s, BatchId: %s", @@ -322,12 +334,14 @@ public void processWFRequest(WfRequest wfRequest) { } - private String validateBatchUserRequestAccess(WfRequest wfRequest) { + private String validateBatchUserRequestAccess(WfRequest wfRequest, Map batchDetailsMap) { + Map courseBatchDetails = getCurrentBatchAttributes(wfRequest.getApplicationId(), + wfRequest.getCourseId()); + batchDetailsMap.put(Constants.START_DATE, courseBatchDetails.get(Constants.START_DATE)); + batchDetailsMap.put(Constants.BATCH_NAME, courseBatchDetails.get(Constants.BATCH_NAME)); boolean nonEnrolmentState = configuration.getBpBatchFullValidationExcludeStates().contains(wfRequest.getAction()); if(nonEnrolmentState) return ""; - Map courseBatchDetails = getCurrentBatchAttributes(wfRequest.getApplicationId(), - wfRequest.getCourseId()); boolean batchStartDateValid = validateBatchStartDate(courseBatchDetails); if(!batchStartDateValid) return Constants.BATCH_START_DATE_ERROR; @@ -497,6 +511,9 @@ public Response adminEnrolBPWorkFlow(String rootOrg, String org, WfRequest wfReq response.put(Constants.MESSAGE, "Not allowed to enroll the user to the Blended Program"); response.put(Constants.STATUS, HttpStatus.OK); } else { + WfNotification wfNotification = getWorkFlowNotificationRequest(wfRequest); + wfNotification.setBatchName((String) courseBatchDetails.get(Constants.BATCH_NAME)); + wfNotification.setBatchStartDate((Date) courseBatchDetails.get(Constants.START_DATE)); response = saveAdminEnrollUserIntoWfStatus(rootOrg, org, wfRequest); // producer.push(configuration.getWorkFlowNotificationTopic(), wfRequest); wfRequest.setAction(Constants.INITIATE); @@ -567,9 +584,12 @@ public Response removeBPWorkFlow(String rootOrg, String org, WfRequest wfRequest response.put(Constants.ERROR_MESSAGE, HttpStatus.INTERNAL_SERVER_ERROR); } else if (approvedLearners.size() == 1) wfRequest.setWfId(approvedLearners.get(0).getWfId()); - response = workflowService.workflowTransition(rootOrg, org, wfRequest,userId,role); - + WfNotification wfNotification = getWorkFlowNotificationRequest(wfRequest); + wfNotification.setBatchName((String) courseBatchDetails.get(Constants.BATCH_NAME)); + wfNotification.setBatchStartDate((Date) courseBatchDetails.get(Constants.START_DATE)); + response = workflowService.workflowTransition(rootOrg, org, wfNotification, userId,role); response.put(Constants.STATUS, HttpStatus.OK); + return response; } @@ -996,7 +1016,24 @@ private void handleApprovalRequest(WfRequest wfRequest) { } catch (IOException e) { throw new ApplicationException(Constants.WORKFLOW_PARSING_ERROR_MESSAGE, e); } + } + private WfNotification getWorkFlowNotificationRequest(WfRequest wfRequest) { + WfNotification wfNotification = new WfNotification(); + wfNotification.setState(wfRequest.getState()); + wfNotification.setAction(wfRequest.getAction()); + wfNotification.setDeptName(wfRequest.getDeptName()); + wfNotification.setComment(wfRequest.getComment()); + wfNotification.setWfId(wfRequest.getWfId()); + wfNotification.setServiceName(wfRequest.getServiceName()); + wfNotification.setActorUserId(wfNotification.getActorUserId()); + wfNotification.setCourseId(wfRequest.getCourseId()); + wfNotification.setUpdateFieldValues(wfRequest.getUpdateFieldValues()); + wfNotification.setRootOrgId(wfRequest.getRootOrgId()); + wfNotification.setUserId(wfRequest.getUserId()); + wfNotification.setApplicationId(wfRequest.getApplicationId()); + + return wfNotification; } } diff --git a/src/main/java/org/sunbird/workflow/service/impl/NotificationServiceImpl.java b/src/main/java/org/sunbird/workflow/service/impl/NotificationServiceImpl.java index a3aa4d66..e355e389 100644 --- a/src/main/java/org/sunbird/workflow/service/impl/NotificationServiceImpl.java +++ b/src/main/java/org/sunbird/workflow/service/impl/NotificationServiceImpl.java @@ -14,6 +14,7 @@ import org.sunbird.workflow.config.Configuration; import org.sunbird.workflow.config.Constants; import org.sunbird.workflow.consumer.ApplicationProcessingConsumer; +import org.sunbird.workflow.models.WfNotification; import org.sunbird.workflow.models.WfRequest; import org.sunbird.workflow.models.WfStatus; import org.sunbird.workflow.models.notification.Config; @@ -72,19 +73,41 @@ public class NotificationServiceImpl { private static final String TO_VALUE_CONST = "toValue"; private static final String MAIL_SUBJECT = "Your request is #state"; + private static final String MDO_MAIL_SUBJECT = "Request for approval"; private static final String MAIL_BODY = "Your request to update #fieldKey to #toValue is #state "; + private static final String BP_MAIL_BODY = "Your request for batch enrollment is #state."; + private static final String COURSE_NAME_TAG = "#course_name"; + + private static final String BATCH_NAME_TAG = "#batch_name"; + + private static final String Role_TAG = "#role"; + + private static final String ACT_TAG = "#action"; + + private static final String BATCH_START_DATE_TAG = "#batch_start_date"; + + private static final String ENROLMENT_ACTION_SUBJECT = "Enrollment #action"; + + private static final String BP_MDO_PC_SUBJECT_LINE = "Enrollment Request Forwarded to #role"; + + private static final String USERNAMAE_TAG = "#username"; + + private static final String BLENDED_PROGRAME_NAME_TAG = "#blended_programme_name"; + + private static final String ROLE_TAG = "#role"; + /** * Send notification to the user based on state of application * - * @param wfRequest workflow request + * @param wfNotification workflow request */ - public void sendNotification(WfRequest wfRequest) { - WfStatusEntity wfStatusEntity = wfStatusRepo.findByApplicationIdAndWfId(wfRequest.getApplicationId(), - wfRequest.getWfId()); + public void sendNotification(WfNotification wfNotification) { + WfStatusEntity wfStatusEntity = wfStatusRepo.findByApplicationIdAndWfId(wfNotification.getApplicationId(), + wfNotification.getWfId()); WfStatus wfStatus = workflowservice.getWorkflowStates(wfStatusEntity.getRootOrg(), wfStatusEntity.getOrg(), wfStatusEntity.getServiceName(), wfStatusEntity.getCurrentStatus()); try { @@ -96,60 +119,79 @@ public void sendNotification(WfRequest wfRequest) { if (!ObjectUtils.isEmpty(wfStatus.getNotificationEnable()) && wfStatus.getNotificationEnable()) { logger.info("Enter's in the notification block"); Set usersId = new HashSet<>(); - usersId.add(wfRequest.getUserId()); - switch (wfRequest.getServiceName()) { - case Constants.BLENDED_PROGRAM_SERVICE_NAME: - case Constants.ONE_STEP_MDO_APPROVAL: - case Constants.ONE_STEP_PC_APPROVAL: - case Constants.TWO_STEP_MDO_AND_PC_APPROVAL: - case Constants.TWO_STEP_PC_AND_MDO_APPROVAL: - break; - default: - usersId.add(wfStatusEntity.getApplicationId()); + usersId.add(wfNotification.getUserId()); + Set blendedProgrammeServiceNames = new HashSet<>(Arrays.asList(Constants.BLENDED_PROGRAM_SERVICE_NAME, + Constants.ONE_STEP_MDO_APPROVAL, + Constants.ONE_STEP_PC_APPROVAL, + Constants.TWO_STEP_MDO_AND_PC_APPROVAL, + Constants.TWO_STEP_PC_AND_MDO_APPROVAL)); + if(!blendedProgrammeServiceNames.contains(wfNotification.getServiceName())){ + usersId.add(wfStatusEntity.getApplicationId()); } HashMap usersObj = userProfileWfService.getUsersResult(usersId); Map recipientInfo; - if (Constants.BLENDED_PROGRAM_SERVICE_NAME.equalsIgnoreCase(wfRequest.getServiceName())) { - recipientInfo = (Map)usersObj.get(wfRequest.getUserId()); + if (Constants.BLENDED_PROGRAM_SERVICE_NAME.equalsIgnoreCase(wfNotification.getServiceName())) { + recipientInfo = (Map)usersObj.get(wfNotification.getUserId()); } else { recipientInfo = (Map)usersObj.get(wfStatusEntity.getApplicationId()); } - Map senderInfo = (Map)usersObj.get(wfRequest.getUserId()); - Map params = new HashMap<>(); - NotificationRequest request = new NotificationRequest(); - request.setDeliveryType("message"); - request.setIds(Arrays.asList((String)recipientInfo.get("email"))); - request.setMode("email"); - Template template = new Template(); - template.setId(EMAILTEMPLATE); - Optional> updatedFieldValue = wfRequest.getUpdateFieldValues().stream().findFirst(); + Map senderInfo = (Map)usersObj.get(wfNotification.getUserId()); + Optional> updatedFieldValue = wfNotification.getUpdateFieldValues().stream().findFirst(); + String subjectLine = ""; + String body = ""; if (updatedFieldValue.isPresent()) { - if (Constants.BLENDED_PROGRAM_SERVICE_NAME.equalsIgnoreCase(wfRequest.getServiceName())) { - params.put("body", BP_MAIL_BODY.replace(STATE_NAME_TAG, wfStatusEntity.getCurrentStatus())); - } else { + if (blendedProgrammeServiceNames.contains(wfNotification.getServiceName())) { + String forwardedMailBody = configuration.getLearnerForwardedMailBody() + .replace(COURSE_NAME_TAG, wfNotification.getCourseName()) + .replace(BATCH_NAME_TAG, wfNotification.getBatchName()) + .replace(BATCH_START_DATE_TAG, wfNotification.getBatchStartDate().toString()); + switch (wfStatusEntity.getCurrentStatus()){ + case Constants.SEND_FOR_PC_APPROVAL: + subjectLine = BP_MDO_PC_SUBJECT_LINE.replace(Role_TAG,Constants.PROGRAM_COORDINATOR.replace("_"," ")); + body = forwardedMailBody.replace(Role_TAG,Constants.PROGRAM_COORDINATOR.replace("_"," ")); + break; + case Constants.SEND_FOR_MDO_APPROVAL: + subjectLine = BP_MDO_PC_SUBJECT_LINE.replace(Role_TAG,Constants.MDO_ADMIN.split("_")[0]); + body = forwardedMailBody.replace(Role_TAG,Constants.MDO_ADMIN.split("_")[0]); + break; + case Constants.APPROVED: + subjectLine = ENROLMENT_ACTION_SUBJECT.replace(ACT_TAG, wfStatusEntity.getCurrentStatus()); + body = configuration.getApprovedMailBody() + .replace(BATCH_NAME_TAG, wfNotification.getBatchName()) + .replace(COURSE_NAME_TAG, wfNotification.getCourseName()) + .replace(BATCH_START_DATE_TAG, wfNotification.getBatchStartDate().toString()); + break; + case Constants.REJECTED: + case Constants.REMOVED: + subjectLine = ENROLMENT_ACTION_SUBJECT.replace(ACT_TAG, wfStatusEntity.getCurrentStatus()); + body = configuration.getRejectedOrRemovedMailBody() + .replace(BATCH_NAME_TAG, wfNotification.getBatchName()) + .replace(COURSE_NAME_TAG, wfNotification.getCourseName()) + .replace(ACT_TAG,wfStatusEntity.getCurrentStatus()) + .replace(BATCH_START_DATE_TAG, wfNotification.getBatchStartDate().toString()); + break; + default: + subjectLine = MAIL_SUBJECT.replace(STATE_NAME_TAG, wfStatusEntity.getCurrentStatus()); + body = BP_MAIL_BODY.replace(STATE_NAME_TAG, wfStatusEntity.getCurrentStatus()); + break; + } + } + else { HashMap toValue = (HashMap) updatedFieldValue.get().get(TO_VALUE_CONST); - params.put("body", MAIL_BODY.replace(STATE_NAME_TAG, wfStatusEntity.getCurrentStatus()) - .replace(FIELD_KEY_TAG, toValue.entrySet().iterator().next().getKey()).replace(TO_VALUE_TAG, (String) toValue.entrySet().iterator().next().getValue())); + body = MAIL_BODY.replace(STATE_NAME_TAG, wfStatusEntity.getCurrentStatus()) + .replace(FIELD_KEY_TAG, toValue.entrySet().iterator().next().getKey()).replace(TO_VALUE_TAG, (String) toValue.entrySet().iterator().next().getValue()); + subjectLine = MAIL_SUBJECT.replace(STATE_NAME_TAG, wfStatusEntity.getCurrentStatus()); } } - if (StringUtils.isNotBlank(wfRequest.getComment())) { - String body = (String) params.get("body"); - body = body.substring(0, body.length() - 1); - body = body + ", due to " + wfRequest.getComment() + "."; - params.put("body", body); + if (StringUtils.isNotBlank(wfNotification.getComment())) { + body = body + ", due to " + wfNotification.getComment() + "."; } - params.put("orgImageUrl", null); - template.setParams(params); - Config config = new Config(); - config.setSubject(MAIL_SUBJECT.replace(STATE_NAME_TAG, wfStatusEntity.getCurrentStatus())); - config.setSender((String)senderInfo.get("email")); - Map req = new HashMap<>(); - request.setTemplate(template); - request.setConfig(config); - Map> notificationMap = new HashMap<>(); - notificationMap.put("notifications", Arrays.asList(request)); - req.put("request", notificationMap); - sendNotification(req); + Map mailNotificationDetails = new HashMap<>(); + mailNotificationDetails.put("emailTo", senderInfo.get(Constants.FIRST_NAME)); + mailNotificationDetails.put("body", body); + mailNotificationDetails.put("subject", subjectLine); + mailNotificationDetails.put("emailList", Collections.singletonList(senderInfo.get(Constants.EMAIL))); + sendNotificationEmail(mailNotificationDetails); } } @@ -263,47 +305,103 @@ public void sendNotificationToMdoAdmin(WfRequest wfRequest) { } } - public void sendNotificationToMdoAdminAndPC(WfRequest wfRequest) { - WfStatusEntity wfStatusEntity = wfStatusRepo.findByApplicationIdAndWfId(wfRequest.getApplicationId(), - wfRequest.getWfId()); + public void sendNotificationToMdoAdminAndPC(WfNotification wfNotification) { + WfStatusEntity wfStatusEntity = wfStatusRepo.findByApplicationIdAndWfId(wfNotification.getApplicationId(), + wfNotification.getWfId()); WfStatus wfStatus = workflowservice.getWorkflowStates(wfStatusEntity.getRootOrg(), wfStatusEntity.getOrg(), wfStatusEntity.getServiceName(), wfStatusEntity.getCurrentStatus()); if (!ObjectUtils.isEmpty(wfStatus.getNotificationEnable()) && wfStatus.getNotificationEnable() && !Arrays.asList(Constants.REJECTED, Constants.APPROVED).contains(wfStatus.getState())) { logger.info("Enter in the sendNotificationToMdoAdminAndPC block"); List emailToSend = new ArrayList<>(); - emailToSend.add(Constants.SEND_FOR_MDO_APPROVAL.equalsIgnoreCase(wfStatusEntity.getCurrentStatus()) ? Constants.MDO_ADMIN : - Constants.SEND_FOR_PC_APPROVAL.equalsIgnoreCase(wfStatusEntity.getCurrentStatus()) ? Constants.PROGRAM_COORDINATOR : null); - logger.info("current role to send notification "+emailToSend); - List mdoAdminList = userProfileWfService.getMdoAdminAndPCDetails(wfRequest.getRootOrgId(), emailToSend); - Map params = new HashMap<>(); - NotificationRequest request = new NotificationRequest(); - request.setDeliveryType("message"); - List mdoMailList = mdoAdminList.stream().collect(Collectors.toList()); - if (!CollectionUtils.isEmpty(mdoMailList)) { - request.setIds(mdoMailList); - request.setMode("email"); - Template template = new Template(); - template.setId(configuration.getMdoEmailTemplate()); - HashMap usersObj = userProfileWfService.getUsersResult(Collections.singleton(wfRequest.getUserId())); - Map recipientInfo = (Map) usersObj.get(wfStatusEntity.getUserId()); - params.put(Constants.USER_NAME, recipientInfo.get(Constants.FIRST_NAME)); - params.put(Constants.SUPPORT_EMAIL, configuration.getSenderMail()); - String constructedEmailTemplate = constructEmailTemplate(configuration.getBpAprroveAndRejectEmailTemplate(), params); - if (StringUtils.isNotEmpty(constructedEmailTemplate)) { - template.setData(constructedEmailTemplate); + List pcEmailList = userProfileWfService.getMdoAdminAndPCDetails(wfNotification.getRootOrgId(), Collections.singletonList(Constants.PROGRAM_COORDINATOR)); + List mdoEmailList = userProfileWfService.getMdoAdminAndPCDetails(wfNotification.getRootOrgId(), Collections.singletonList(Constants.MDO_ADMIN)); + HashMap usersObj = userProfileWfService.getUsersResult(Collections.singleton(wfNotification.getUserId())); + Map recipientInfo = (Map) usersObj.get(wfStatusEntity.getUserId()); + String userName = (String) recipientInfo.get(Constants.FIRST_NAME); + Map mailNotificationDetails = new HashMap<>(); + String subjectLine = ""; + String body = ""; + String enrolmentStateSubject = "Enrolment #state"; + String approvalRequestSubject = "Enrollment Approval Request"; + String requestForwardedSubject = "Enrollment Request Forwarded to #role"; + String date = wfNotification.getBatchStartDate().toString(); + String nominationRequestMailBody = configuration.getNominationRequestMailBody().replace(USERNAMAE_TAG, userName) + .replace(BATCH_NAME_TAG, wfNotification.getBatchName()) + .replace(BLENDED_PROGRAME_NAME_TAG, wfNotification.getCourseName()); + nominationRequestMailBody = nominationRequestMailBody.replace(BATCH_START_DATE_TAG, wfNotification.getBatchStartDate().toString()); + String approvalRequestMailBody = configuration.getApprovalRequetMailBody().replace(USERNAMAE_TAG, userName) + .replace(BATCH_NAME_TAG, wfNotification.getBatchName()) + .replace(BLENDED_PROGRAME_NAME_TAG, wfNotification.getCourseName()); + approvalRequestMailBody = approvalRequestMailBody.replace(BATCH_START_DATE_TAG, date); + String requestForwardedMailBody = configuration.getRequestForwardedMailBody().replace(USERNAMAE_TAG, userName) + .replace(BATCH_NAME_TAG, wfNotification.getBatchName()) + .replace(BLENDED_PROGRAME_NAME_TAG, wfNotification.getCourseName()) + .replace(BATCH_START_DATE_TAG, wfNotification.getBatchStartDate().toString()); + mailNotificationDetails.put("subject", approvalRequestSubject); + mailNotificationDetails.put("body", approvalRequestMailBody); + if(Constants.INITIATE.equalsIgnoreCase(wfNotification.getState()) && Constants.SEND_FOR_PC_APPROVAL.equalsIgnoreCase(wfStatusEntity.getCurrentStatus())){ + mailNotificationDetails.put("emailList", pcEmailList); + mailNotificationDetails.put("emailTo", Constants.TO_PROGRAMME_COORDINATOR); + sendNotificationEmail(mailNotificationDetails); + } else if(Constants.INITIATE.equalsIgnoreCase(wfNotification.getState()) && Constants.SEND_FOR_MDO_APPROVAL.equalsIgnoreCase(wfStatusEntity.getCurrentStatus())){ + mailNotificationDetails.put("emailList", mdoEmailList); + mailNotificationDetails.put("emailTo", Constants.TO_MDO_ADMIN); + sendNotificationEmail(mailNotificationDetails); + } else if(Constants.SEND_FOR_MDO_APPROVAL.equalsIgnoreCase(wfNotification.getState()) && Constants.SEND_FOR_PC_APPROVAL.equalsIgnoreCase(wfStatusEntity.getCurrentStatus())){ + mailNotificationDetails.put("emailList", pcEmailList); + mailNotificationDetails.put("emailTo", Constants.TO_PROGRAMME_COORDINATOR); + sendNotificationEmail(mailNotificationDetails); + + subjectLine = requestForwardedSubject.replace(ROLE_TAG,Constants.PROGRAM_COORDINATOR.replace("_", " ")); + body = requestForwardedMailBody.replace(ROLE_TAG,Constants.PROGRAM_COORDINATOR.replace("_", " ")); + mailNotificationDetails.put("subject", subjectLine); + mailNotificationDetails.put("body", body); + mailNotificationDetails.put("emailList", mdoEmailList); + mailNotificationDetails.put("emailTo", Constants.TO_MDO_ADMIN); + sendNotificationEmail(mailNotificationDetails); + + } else if(Constants.SEND_FOR_PC_APPROVAL.equalsIgnoreCase(wfNotification.getState()) && Constants.SEND_FOR_MDO_APPROVAL.equalsIgnoreCase(wfStatusEntity.getCurrentStatus())){ + mailNotificationDetails.put("emailList", mdoEmailList); + mailNotificationDetails.put("emailTo", Constants.TO_MDO_ADMIN); + sendNotificationEmail(mailNotificationDetails); + + subjectLine = requestForwardedSubject.replace(ROLE_TAG,Constants.MDO_ADMIN.split("_")[0]); + body = requestForwardedMailBody.replace(ROLE_TAG,Constants.MDO_ADMIN.split("_")[0]); + mailNotificationDetails.put("subject", subjectLine); + mailNotificationDetails.put("body", body); + mailNotificationDetails.put("emailList", pcEmailList); + mailNotificationDetails.put("emailTo", Constants.TO_PROGRAMME_COORDINATOR); + sendNotificationEmail(mailNotificationDetails); + + } + if(Constants.INITIATE.equalsIgnoreCase(wfNotification.getState()) && Constants.ADMIN_ENROLL_IS_IN_PROGRESS.equalsIgnoreCase(wfStatusEntity.getCurrentStatus())) { + mailNotificationDetails.put("emailList", pcEmailList); + mailNotificationDetails.put("emailTo", Constants.TO_PROGRAMME_COORDINATOR); + sendNotificationEmail(mailNotificationDetails); + } + if(Constants.ADMIN_ENROLL_IS_IN_PROGRESS.equalsIgnoreCase(wfNotification.getState())){ + if (Constants.APPROVED.equalsIgnoreCase(wfStatusEntity.getCurrentStatus())) { + subjectLine = enrolmentStateSubject.replace("#state", Constants.APPROVED); + body = nominationRequestMailBody.replace("#state", Constants.APPROVED); + mailNotificationDetails.put("emailList", mdoEmailList); + mailNotificationDetails.put("subject", subjectLine); + mailNotificationDetails.put("body", body); + mailNotificationDetails.put("emailTo", Constants.TO_MDO_ADMIN); + sendNotificationEmail(mailNotificationDetails); + } else if (Constants.REJECTED.equalsIgnoreCase(wfStatusEntity.getCurrentStatus())) { + subjectLine = enrolmentStateSubject.replace("#state", Constants.APPROVED); + body = nominationRequestMailBody.replace("#state", Constants.APPROVED); + if (StringUtils.isNotBlank(wfNotification.getComment())) { + body = body + ", due to " + wfNotification.getComment() + "."; + } + mailNotificationDetails.put("emailList", mdoEmailList); + mailNotificationDetails.put("subject", subjectLine); + mailNotificationDetails.put("body", body); + mailNotificationDetails.put("emailTo", Constants.TO_MDO_ADMIN); + sendNotificationEmail(mailNotificationDetails); } - template.setParams(params); - Config config = new Config(); - config.setSubject(MDO_MAIL_SUBJECT); - config.setSender(configuration.getSenderMail()); - Map req = new HashMap<>(); - request.setTemplate(template); - request.setConfig(config); - Map> notificationMap = new HashMap<>(); - notificationMap.put("notifications", Arrays.asList(request)); - req.put("request", notificationMap); - sendNotification(req); + logger.info("current role to send notification "+emailToSend); } } } @@ -345,6 +443,38 @@ public void sendNotification(Map request) { } catch (Exception e) { logger.error("Exception while posting the data in notification service: ", e); } + } + private void sendNotificationEmail(Map mailNotificationDetails){ + Map params = new HashMap<>(); + NotificationRequest request = new NotificationRequest(); + List emailList = (List) mailNotificationDetails.get("emailList"); + request.setDeliveryType("message"); + if (!CollectionUtils.isEmpty(emailList)) { + request.setIds(emailList); + request.setMode("email"); + Template template = new Template(); + template.setId(configuration.getNotificationEmailTemplate()); + params.put(Constants.NAME, mailNotificationDetails.get("emailTo")); + params.put("body", mailNotificationDetails.get("body")); + params.put(Constants.ORG_NAME, Constants.KARMYOGI_BHARAT); + params.put(Constants.FROM_EMAIL, configuration.getSenderMail()); + String constructedEmailTemplate = constructEmailTemplate(configuration.getNotificationEmailTemplate(), params); + if (StringUtils.isNotEmpty(constructedEmailTemplate)) { + template.setData(constructedEmailTemplate); + } + template.setParams(params); + Config config = new Config(); + config.setSubject((String) mailNotificationDetails.get("subject")); + config.setSender(configuration.getSenderMail()); + Map req = new HashMap<>(); + request.setTemplate(template); + request.setConfig(config); + Map> notificationMap = new HashMap<>(); + notificationMap.put("notifications", Arrays.asList(request)); + req.put("request", notificationMap); + sendNotification(req); + } } + } diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 0868e150..be7d17c1 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -106,3 +106,14 @@ blended.program.enrol.conflict.reject.reason=Conflict, User already enrolled to blended.program.enrol.batch.full.message=This batch is full wfstatus.allowed.action.for.modification.history.entry=REMOVE,REJECT,APPROVE blended.program.batch.in.progress.message=This batch is already in progress + + +#blended_mail_subject_body +bp.mail.body.forwarded.to=Your enrollment request for #batch_name of the #course_name Program, starting on #batch_start_date has been forwarded to the #role for approval. +bp.mail.body.rejected.or.remove=We regret to inform you that your enrollment request for #batch_name of the #course_name Program, starting on #batch_start_date has been #action. +bp.mail.body.approve=We are pleased to inform you that your enrollment request for #batch_name of the #course_name Program, starting on #batch_start_date has been approved. + +notify.email.template=notificationEmailTemplate +bp.approval.request.mail.body=You have received a request for enrollment for #username in #batch_name of the #blended_programme_name starting on #batch_start_date, which requires your approval. +bp.request.forwarded.mail.body=The enrollment request for #username in #batch_name of the #blended_programme_name Program, starting on #batch_start_date , has been forwarded to the #role for approval. +bp.nomination.request.mail.body=The enrollment request for #username in #batch_name of the #blended_programme_name Program, starting on #batch_start_date , has been #state. \ No newline at end of file