Skip to content

Commit

Permalink
Introduce communication service (#369)
Browse files Browse the repository at this point in the history
Introduces a new concept of communication service as a domain concept and the implementation is now part of the infrastructure.
  • Loading branch information
sven1103 authored Sep 14, 2023
1 parent df0a8e7 commit 30ef461
Show file tree
Hide file tree
Showing 85 changed files with 466 additions and 428 deletions.
File renamed without changes.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

2 changes: 1 addition & 1 deletion authorization/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
</dependency>
<dependency>
<groupId>life.qbic</groupId>
<artifactId>authentication</artifactId>
<artifactId>user</artifactId>
<version>0.23.1</version>
<scope>compile</scope>
</dependency>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package life.qbic.authorization.application;

/**
* <b>Messages</b>
*
* <p>A collection of message templates to notify users.</p>
*
* @since 1.0.0
*/
public class Messages {

private Messages() {

}

/**
* A pre-formatted message that informs a user about their new access grant to a project in the
* data manager.
*
* @param fullNameUser the name of the user to inform for addressing them politely
* @param projectTitle the title of the project, will be in the message to inform the user about
* which project they have been granted access with
* @param projectUri a uniform resource identifier of the project, that the user can use to
* access the project
* @return the filled out template message
* @since 1.0.0
*/
public static String projectAccessToUser(String fullNameUser, String projectTitle,
String projectUri) {
return String.format("""
Dear %s,
you have been granted access to project:
'%s'
Please click the link below to access the project after login:
%s
""", fullNameUser, projectTitle, projectUri);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@
import life.qbic.authentication.domain.user.concept.UserId;
import life.qbic.authentication.domain.user.repository.UserRepository;
import life.qbic.authorization.application.AppContextProvider;
import life.qbic.authorization.application.Messages;
import life.qbic.domain.concepts.DomainEvent;
import life.qbic.domain.concepts.DomainEventSubscriber;
import life.qbic.domain.concepts.communication.EmailService;
import life.qbic.domain.concepts.communication.CommunicationService;
import life.qbic.domain.concepts.communication.Content;
import life.qbic.domain.concepts.communication.Recipient;
import life.qbic.domain.concepts.communication.Subject;
import life.qbic.projectmanagement.domain.project.service.event.ProjectAccessGranted;
import org.jobrunr.jobs.annotations.Job;
import org.jobrunr.scheduling.JobScheduler;
Expand All @@ -23,38 +27,24 @@
@Component
public class InformUserAboutGrantedAccess implements DomainEventSubscriber<ProjectAccessGranted> {

private final EmailService emailService;
private final CommunicationService communicationService;

private final JobScheduler jobScheduler;
private final UserRepository userRepository;

private final AppContextProvider appContextProvider;

public InformUserAboutGrantedAccess(EmailService emailService, JobScheduler jobScheduler,
public InformUserAboutGrantedAccess(CommunicationService communicationService, JobScheduler jobScheduler,
UserRepository userRepository, AppContextProvider appContextProvider) {
this.emailService = Objects.requireNonNull(emailService);
this.communicationService = Objects.requireNonNull(communicationService);
this.jobScheduler = Objects.requireNonNull(jobScheduler);
this.userRepository = Objects.requireNonNull(userRepository);
this.appContextProvider = Objects.requireNonNull(appContextProvider);
}

private String composeMessage(String projectId, User recipient, String projectTitle) {
return String.format("""
Dear %s,
you have been granted access to project:
'%s'
Please click the link below to access the project after login:
%s
Need help? Contact us for further questions at [email protected]
Best regards,\
The QBiC team
""", recipient.fullName(), projectTitle, appContextProvider.urlToProject(projectId));
return Messages.projectAccessToUser(recipient.fullName().get(), projectTitle,
appContextProvider.urlToProject(projectId));
}

@Override
Expand All @@ -64,14 +54,15 @@ public Class<? extends DomainEvent> subscribedToEventType() {

@Override
public void handleEvent(ProjectAccessGranted event) {
jobScheduler.enqueue(() -> notifyUser(event.forUserId(), event.forProjectId(), event.forProjectTitle()));
jobScheduler.enqueue(
() -> notifyUser(event.forUserId(), event.forProjectId(), event.forProjectTitle()));
}

@Job(name = "Notify user about granted project access")
public void notifyUser(String userId, String projectId, String projectTitle)
throws RuntimeException {
var recipient = userRepository.findById(UserId.from(userId)).get();
emailService.send(recipient.emailAddress().get(), recipient.fullName().get(),
"Project access granted", composeMessage(projectId, recipient, projectTitle));
var message = Messages.projectAccessToUser(recipient.fullName().get(), projectTitle, projectId);
communicationService.send(new Subject("Project access granted"), new Recipient(recipient.emailAddress().get(), recipient.fullName().get()), new Content(message));
}
}
2 changes: 1 addition & 1 deletion database-connector/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<dependencies>
<dependency>
<groupId>life.qbic</groupId>
<artifactId>authentication</artifactId>
<artifactId>user</artifactId>
<version>0.23.1</version>
<scope>compile</scope>
</dependency>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package life.qbic.domain.concepts.communication;

import java.io.Serial;

/**
* <b>Communication Exception</b>
*
* <p>Exception that shall be thrown to indicate issues during communication with the user</p>
*
* @since 1.0.0
*/
public class CommunicationException extends RuntimeException {

@Serial
private static final long serialVersionUID = 7816744418299591709L;

public CommunicationException() {
super();
}

public CommunicationException(String message) {
super(message);
}

public CommunicationException(String message, Throwable cause) {
super(message, cause);
}

}
Loading

0 comments on commit 30ef461

Please sign in to comment.