Skip to content

Commit

Permalink
Updated Mailer Service
Browse files Browse the repository at this point in the history
  • Loading branch information
akumar074 committed Jan 3, 2023
1 parent 6246dd1 commit d4a9d3f
Show file tree
Hide file tree
Showing 11 changed files with 231 additions and 61 deletions.
16 changes: 16 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,22 @@
<groupId>io.quarkus</groupId>
<artifactId>quarkus-smallrye-health</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-scheduler</artifactId>
</dependency>
<dependency>
<groupId>io.smallrye.reactive</groupId>
<artifactId>smallrye-mutiny-vertx-web-client</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-rest-client</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-rest-client-jackson</artifactId>
</dependency>

<!--ALL JJWT Dependencies-->
<dependency>
Expand Down
19 changes: 19 additions & 0 deletions src/main/java/com/shareNwork/domain/EmailData.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.shareNwork.domain;

import java.util.Map;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class EmailData {

private String emailType;
private String mailTo;
private Map<String, String> emailTemplateVariables;
}
22 changes: 22 additions & 0 deletions src/main/java/com/shareNwork/domain/constants/EmailType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.shareNwork.domain.constants;

public enum EmailType {

OPENROTA_INVITATION("openRotaInvitation"),
INVITATION_EXPIRATION("invitationExpiration"),
NEW_ACCESS_REQ("newAccessReq"),
ACCESS_REQ_STATUS("accessReqStatus"),
NEW_RESOURCE_REQ("newResourceReq"),
RESOURCE_REQUEST_STATUS("resourceRequestStatus"),
PROJECT_CLOSURE_DUE_REMINDER("projectClosureDueReminder"),
PROJECT_CLOSURE_REMINDER("projectClosureReminder");

private final String value;
EmailType(String value) {
this.value = value;
}

public String value() {
return this.value;
}
}
26 changes: 26 additions & 0 deletions src/main/java/com/shareNwork/proxy/MailerProxy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.shareNwork.proxy;

import java.util.List;

import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import com.shareNwork.domain.EmailData;
import org.eclipse.microprofile.rest.client.inject.RegisterRestClient;

@Path("/mailer")
@Produces(MediaType.APPLICATION_JSON)
@RegisterRestClient(baseUri = "http://localhost:8082")
public interface MailerProxy {

@POST
@Path("/mail")
Response sendEmail(EmailData emailData);

@POST
@Path("/multi-mail")
Response sendMultipleEmail(List<EmailData> emailDataList);
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
package com.shareNwork.repository;

import com.shareNwork.domain.AccessRequest;
import com.shareNwork.domain.EmailData;
import com.shareNwork.domain.constants.EmailType;
import com.shareNwork.domain.constants.InvitationStatus;
import com.shareNwork.domain.constants.RoleType;
import com.shareNwork.domain.constants.RowAction;
import com.shareNwork.proxy.MailerProxy;
import io.quarkus.hibernate.orm.panache.PanacheRepository;
import org.eclipse.microprofile.rest.client.inject.RestClient;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.transaction.Transactional;
import javax.ws.rs.NotFoundException;
import java.text.ParseException;
import java.util.Map;
import java.util.Set;

@ApplicationScoped
Expand All @@ -26,6 +31,9 @@ public class AccessRequestRepository implements PanacheRepository<AccessRequest>
@Inject
RoleRepository roleRepository;

@RestClient
MailerProxy mailerProxy;

@Transactional
public AccessRequest handleActions(RowAction actionName, AccessRequest accessRequest) throws ParseException {
AccessRequest request = findById(accessRequest.id);
Expand All @@ -38,10 +46,18 @@ public AccessRequest handleActions(RowAction actionName, AccessRequest accessReq
// send an email here
request.setStatus(InvitationStatus.REJECTED);
}
sendEmail(new EmailData(EmailType.ACCESS_REQ_STATUS.value(),
request.getEmailId(),
Map.of("approved",
String.valueOf(actionName.equals(RowAction.APPROVE)))));
em.merge(request);
} else {
throw new NotFoundException();
}
return request;
}

public void sendEmail(final EmailData emailData) {
mailerProxy.sendEmail(emailData);
}
}
84 changes: 25 additions & 59 deletions src/main/java/com/shareNwork/repository/InvitationRepository.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package com.shareNwork.repository;

import java.text.ParseException;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Base64;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.Set;
import java.util.stream.Collectors;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
Expand All @@ -16,26 +17,24 @@
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Response;

import com.shareNwork.domain.Employee;
import com.shareNwork.domain.EmailData;
import com.shareNwork.domain.Invitation;
import com.shareNwork.domain.InvitationResponse;
import com.shareNwork.domain.QueryParams;
import com.shareNwork.domain.Role;
import com.shareNwork.domain.constants.EmailType;
import com.shareNwork.domain.constants.InvitationStatus;
import com.shareNwork.domain.constants.RoleType;
import com.shareNwork.proxy.MailerProxy;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.impl.TextCodec;
import io.quarkus.hibernate.orm.panache.PanacheRepository;
import io.quarkus.mailer.MailTemplate;
import io.quarkus.qute.Location;
import org.apache.commons.lang3.time.DateUtils;
import org.eclipse.microprofile.config.inject.ConfigProperty;
import org.eclipse.microprofile.rest.client.inject.RestClient;
import org.jboss.logging.Logger;

import static io.jsonwebtoken.SignatureAlgorithm.HS256;

@ApplicationScoped

public class InvitationRepository implements PanacheRepository<Invitation> {

private static final Logger LOGGER = Logger.getLogger(InvitationRepository.class);
Expand All @@ -51,9 +50,8 @@ public class InvitationRepository implements PanacheRepository<Invitation> {
@Inject
EmployeeRepository employeeRepository;

@Inject
@Location("sharedResourceInvitation")
MailTemplate sharedResourceInvitation;
@RestClient
MailerProxy mailerProxy;

public static String decodeToken(String token) {
Base64.Decoder decoder = Base64.getDecoder();
Expand All @@ -65,49 +63,6 @@ public static String decodeToken(String token) {
return payload;
}

@Transactional
public List<InvitationResponse> createInvitationToken(List<Invitation> invitationlist) {
List<InvitationResponse> responses = new ArrayList<>();
for (Invitation invitation : invitationlist) {

InvitationResponse invitationResponse = new InvitationResponse();
List<Invitation> invitations = listAll();

for (Invitation invitation1 : invitations) {
if (invitation1.getEmailId().equals(invitation.getEmailId())) {
invitationResponse.setResponseStatus(Response.Status.CONFLICT.getStatusCode());
invitationResponse.setToken(null);
responses.add(invitationResponse);
return responses;
}
}

String token = Jwts.builder()
.claim("email", invitation.getEmailId())
.setIssuedAt(new java.util.Date())
.setExpiration(DateUtils.addHours(new java.util.Date(), 3))
.signWith(
HS256,
TextCodec.BASE64.decode("Yn2kjibddFAWtnPJ2AFlL8WXmohJMCvigQggaEypa5E=")
)
.compact();

invitation.setStatus(InvitationStatus.PENDING);
final String inviteURL = getInvitationParams(token, invitation.getEmailId());
invitation.setInvitationLinkParams(inviteURL);
persist(invitation);
invitationResponse.setToken(token);
invitationResponse.setResponseStatus(Response.Status.OK.getStatusCode());

sharedResourceInvitation.to(invitation.getEmailId())
.subject("[Action Required] Invitation from OpenRota")
.data("invitationLink", inviteURL)
.send().subscribe().with(t -> LOGGER.info("Mail sent to " + invitation.getEmailId()));
responses.add(invitationResponse);
}
return responses;
}

@Transactional
public InvitationResponse createInvitation(Invitation invitation) {
InvitationResponse invitationResponse = new InvitationResponse();
Expand Down Expand Up @@ -175,11 +130,14 @@ private String generateToken(String claim, String str) {
.compact();
}

public void sendEmail(String token, String email){
sharedResourceInvitation.to(email)
.subject("[Action Required] Invitation from OpenRota")
.data("invitationLink", getInvitationParams(token, email))
.send().subscribe().with(t -> LOGGER.info("Mail sent to " + email));
private void sendEmail(String token, String email){
final EmailData emailData = EmailData.builder()
.emailType(EmailType.OPENROTA_INVITATION.value())
.mailTo(email)
.emailTemplateVariables(Map.of("invitationLink", getInvitationParams(token, email)))
.build();
Response response = mailerProxy.sendEmail(emailData);
LOGGER.info(response.getStatus());
}

private String getInvitationParams(final String token, final String email) {
Expand Down Expand Up @@ -227,4 +185,12 @@ public InvitationResponse verifyToken(String emailId, String token, String name)
return (new InvitationResponse(Response.Status.NOT_FOUND.getStatusCode(), "No invitations found for email " + emailId));
}

@Transactional
public List<Invitation> expiringInvitations(final int days) {
return streamAll()
.filter(invitation -> invitation.getStatus().equals(InvitationStatus.PENDING)
&& Duration.between(invitation.getCreatedAt(), LocalDateTime.now()).toDays() <= days)
.collect(Collectors.toList());
}

}
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package com.shareNwork.repository;

import com.shareNwork.domain.*;
import com.shareNwork.domain.constants.EmailType;
import com.shareNwork.domain.constants.ProjectStatus;
import com.shareNwork.domain.constants.ResourceRequestStatus;
import com.shareNwork.domain.constants.RowAction;
import com.shareNwork.domain.constants.SkillProficiencyLevel;
import com.shareNwork.proxy.MailerProxy;
import io.quarkus.hibernate.orm.panache.PanacheRepository;
import org.eclipse.microprofile.rest.client.inject.RestClient;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
Expand All @@ -28,6 +31,9 @@ public class ResourceRequestRepository implements PanacheRepository<ResourceRequ
@Inject
EntityManager em;

@RestClient
MailerProxy mailerProxy;

@Transactional
public List<ResourceRequest> getSharedResourceByRequestor(long requestorId) {
return listAll().stream().filter(resourceRequest -> resourceRequest.getRequester().id.equals(requestorId)).collect(Collectors.toList());
Expand All @@ -44,6 +50,10 @@ public ResourceRequest updateOrCreate(ResourceRequest shareResourceRequest) thro
persist(shareResourceRequest);
}
addSkillsToResourceRequests(shareResourceRequest.id, shareResourceRequest.getSkillProficiencies());
mailerProxy.sendEmail(new EmailData(EmailType.NEW_RESOURCE_REQ.value(),
shareResourceRequest.getRequester().getEmailId(),
Map.of("requestId", String.valueOf(shareResourceRequest.id),
"projectName", shareResourceRequest.getProject())));
return em.merge(shareResourceRequest);
}

Expand All @@ -64,6 +74,10 @@ public ResourceRequest handleActions(RowAction actionName, ResourceRequest resou
request.setStatus(ResourceRequestStatus.CANCELLED);
}
em.merge(request);
mailerProxy.sendEmail(new EmailData(EmailType.RESOURCE_REQUEST_STATUS.value(),
request.getRequester().getEmailId(),
Map.of("approved", String.valueOf(actionName.equals(RowAction.APPROVE)),
"resourceName", request.getResource().getEmailId())));
} else {
throw new NotFoundException();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,20 @@

import com.shareNwork.domain.AccessRequest;
import com.shareNwork.domain.AllowedDesignationResponse;
import com.shareNwork.domain.EmailData;
import com.shareNwork.domain.constants.EmailType;
import com.shareNwork.domain.constants.InvitationStatus;
import com.shareNwork.domain.constants.RowAction;

import com.shareNwork.repository.AccessRequestRepository;
import lombok.AllArgsConstructor;
import org.eclipse.microprofile.graphql.Description;
import org.eclipse.microprofile.graphql.GraphQLApi;
import org.eclipse.microprofile.graphql.Mutation;
import org.eclipse.microprofile.graphql.Query;

import javax.transaction.Transactional;
import java.text.ParseException;
import java.util.Collections;
import java.util.List;

@AllArgsConstructor
Expand Down Expand Up @@ -55,6 +58,11 @@ public AccessRequest createAccessRequest(AccessRequest accessRequest) {
if (accessRequest.id == null) {
accessRequest.setStatus(InvitationStatus.PENDING);
accessRequest.persist();
accessRequestRepository.sendEmail(EmailData.builder()
.emailType(EmailType.NEW_ACCESS_REQ.value())
.mailTo(accessRequest.getEmailId())
.emailTemplateVariables(Collections.emptyMap())
.build());
return accessRequest;
}
return null;
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/com/shareNwork/service/ReminderSchedulers.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.shareNwork.service;

public interface ReminderSchedulers {

void invitationExpireReminder();

void projectClosureDueReminder();

void ProjectCompletionReminder();
}
Loading

0 comments on commit d4a9d3f

Please sign in to comment.