Skip to content

Commit

Permalink
Added comments and resolved some coderabbit review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
tanishi-egov committed Jan 16, 2025
1 parent d30a3e1 commit 75ee244
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ public EmployeeResponse create(EmployeeRequest employeeRequest) {
pwdMap.put(employee.getUuid(), employee.getUser().getPassword());
});
hrmsProducer.push(propertiesManager.getHrmsEmailNotifTopic(), employeeRequest);

// Setting password as null after sending employeeRequest to email notification topic to send email.
employeeRequest.getEmployees().forEach(employee -> employee.getUser().setPassword(null));

hrmsProducer.push(propertiesManager.getSaveEmployeeTopic(), employeeRequest);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.jayway.jsonpath.JsonPath;

import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;
import org.springframework.web.client.RestTemplate;

import static org.egov.hrms.utils.HRMSConstants.HEALTH_HRMS_EMAIL_LOCALIZATION_CODE;
Expand Down Expand Up @@ -203,14 +204,25 @@ public Map<String, Map<String, String>> getLocalisedMessages(RequestInfo request
}

/**
* Creates and sends email notification for the given employee request.
* Creates and sends email notification to the employees whose details are provided in the employeeRequest.
*
* @param employeeRequest The employee request
* @param employeeRequest The employee request with employee details.
*/
public void processEmailNotification(EmployeeRequest employeeRequest) {
String localizationMessages = notificationUtil.getLocalizationMessages(employeeRequest);
String messageTemplate = notificationUtil.getMessageTemplate(HEALTH_HRMS_EMAIL_LOCALIZATION_CODE, localizationMessages);
List<EmailRequest> emailRequests = notificationUtil.createEmailRequest(employeeRequest, messageTemplate);
notificationUtil.sendEmail(emailRequests);
if (employeeRequest == null || CollectionUtils.isEmpty(employeeRequest.getEmployees())) {
log.error("Invalid employee request received for email notification");
return;
}
try {
// Fetch localization messages and get email message template for HEALTH_HRMS_EMAIL_LOCALIZATION_CODE template code.
String localizationMessages = notificationUtil.getLocalizationMessages(employeeRequest);
String messageTemplate = notificationUtil.getMessageTemplate(HEALTH_HRMS_EMAIL_LOCALIZATION_CODE, localizationMessages);

// Create email requests from the employee details provided in the employeeRequest.
List<EmailRequest> emailRequests = notificationUtil.createEmailRequest(employeeRequest, messageTemplate);
notificationUtil.sendEmail(emailRequests);
} catch (Exception e) {
log.error("Error processing email notification for given employees");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,19 @@ public NotificationUtil(RestCallRepository restCallRepository, PropertiesManager
}

/**
* Extracts message for the specific code.
* Extracts message for the specific code from the localization messages.
*
* @param notificationCode The code for which message is required
* @param localizationMessage The localization messages
* @return message for the specific code
* @param notificationCode The code for which message is required.
* @param localizationMessage The localization messages.
* @return message for the specific code.
*/
public String getMessageTemplate(String notificationCode, String localizationMessage) {
// Create the path to get the message for the provided notification code from localization messages.
String path = "$..messages[?(@.code==\"{}\")].message";
path = path.replace("{}", notificationCode);
String message = null;
try {
// Tries to get the message for the provided notificationCode.
List data = JsonPath.parse(localizationMessage).read(path);
if (!CollectionUtils.isEmpty(data))
message = data.get(0).toString();
Expand All @@ -63,7 +65,7 @@ public String getMessageTemplate(String notificationCode, String localizationMes
}

/**
* Fetches messages from localization service.
* Fetches all the localization messages from localization service.
*
* @param employeeRequest The employee request
* @return Localization messages for the module
Expand All @@ -78,10 +80,10 @@ public String getLocalizationMessages(EmployeeRequest employeeRequest) {
}

/**
* Returns the uri for the localization call.
* Returns the search uri for the localization search to get localization messages from "rainmaker-hr" module.
*
* @param employeeRequest The employee request
* @return The uri for localization search call
* @param employeeRequest The employee request with locale code.
* @return The uri for localization search call.
*/
public StringBuilder getUri(EmployeeRequest employeeRequest) {
String tenantId = employeeRequest.getEmployees().get(0).getTenantId().split("\\.")[0];
Expand All @@ -98,7 +100,8 @@ public StringBuilder getUri(EmployeeRequest employeeRequest) {
}

/**
* Replaces the placeholders and creates an email request for the given employee request.
* Replaces the placeholders from the email template and creates an email request from the given employee request.
*
*
* @param employeeRequest The employee request
* @param emailTemplate the email template
Expand All @@ -108,16 +111,20 @@ public List<EmailRequest> createEmailRequest(EmployeeRequest employeeRequest, St
RequestInfo requestInfo = employeeRequest.getRequestInfo();

List<EmailRequest> emailRequest = new LinkedList<>();

// Iterate over each employee details and create email request for each employee.
for (Employee employee : employeeRequest.getEmployees()) {
String customizedMsg = emailTemplate.replace("{User's name}", employee.getUser().getName());
customizedMsg = customizedMsg.replace("{Username}", employee.getCode());
customizedMsg = customizedMsg.replace("{Password}", employee.getUser().getPassword());
customizedMsg = customizedMsg.replace("{website URL}", propertiesManager.getEmailNotificationWebsiteLink());
customizedMsg = customizedMsg.replace("{Implementation partner}", propertiesManager.getEmailNotificationImplementationPartner());

// Get email subject and email body from the provided email template.
String subject = customizedMsg.substring(customizedMsg.indexOf("<h2>")+4, customizedMsg.indexOf("</h2>"));
String body = customizedMsg.substring(customizedMsg.indexOf("</h2>")+5);

// Create the email object with the employee's email id, subject and customized email body created.
Email emailObj = Email.builder().emailTo(Collections.singleton(employee.getUser().getEmailId())).isHTML(true).body(body).subject(subject).build();
EmailRequest email = new EmailRequest(requestInfo, emailObj);
emailRequest.add(email);
Expand All @@ -126,15 +133,16 @@ public List<EmailRequest> createEmailRequest(EmployeeRequest employeeRequest, St
}

/**
* Pushes email into email notification topic.
* Pushes email request list into email notification topic if email notification is enabled.
*
* @param emailRequestList The list of emailRequests
* @param emailRequestList The list of emailRequests.
*/
public void sendEmail(List<EmailRequest> emailRequestList) {
if (propertiesManager.getIsEmailNotificationEnabled()) {
if (CollectionUtils.isEmpty(emailRequestList)) {
log.error("No Emails Found!");
} else {
// Iterate over each email and push them into emailNotifTopic to send emails.
for (EmailRequest emailRequest : emailRequestList) {
producer.push(propertiesManager.getEmailNotifTopic(), emailRequest);
log.info("Email Request -> " + emailRequest.toString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.*;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.util.Set;

@Setter
Expand All @@ -13,8 +16,14 @@
@Builder
public class Email {

@NotNull
@Size(min = 1, message = "At least one recipient is required")
private Set<String> emailTo;

@NotBlank(message = "Subject is required")
private String subject;

@NotBlank(message = "Body is required")
private String body;
@JsonProperty("isHTML")
private boolean isHTML;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,40 +23,40 @@
@Builder
public class EmployeeSearchCriteria {

public List<String> codes;
private List<String> codes;

public List<String> names;
private List<String> names;

public List<String> departments;
private List<String> departments;

public List<String> designations;
private List<String> designations;

public Long asOnDate;
private Long asOnDate;

public List<String> roles;
private List<String> roles;

public List<Long> ids;
private List<Long> ids;

public List<String> employeestatuses;
private List<String> employeestatuses;

public List<String> employeetypes;
private List<String> employeetypes;

public List<String> uuids;
private List<String> uuids;

public List<String> userServiceUuids;
private List<String> userServiceUuids;

public List<Long> positions;
private List<Long> positions;

public Boolean isActive;
private Boolean isActive;

@Size(max = 250)
public String tenantId;
private String tenantId;

public String phone;
private String phone;

public Integer offset;
private Integer offset;

public Integer limit;
private Integer limit;

private Boolean includeUnassigned = false;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import javax.validation.constraints.NotNull;

/**
* IndividualResponse
* IndividualBulkResponse
*/
@Validated

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ state.level.tenant.id=default

egov.hrms.auto.generate.password=true

# EMAIL NOTIFICATION CONFIG
notification.email.enabled=true

hrms.email.notification.implementation.partner=NMCP Mozambique
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
Expand All @@ -15,6 +13,9 @@
import org.egov.common.models.individual.Individual;
import org.springframework.validation.annotation.Validated;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;

/**
* IndividualResponse
*/
Expand Down

0 comments on commit 75ee244

Please sign in to comment.