Skip to content

Commit

Permalink
Issue 64: Use mailgun to send email when user volunteers for a project
Browse files Browse the repository at this point in the history
issue: #64
pr: #105
  • Loading branch information
thhu committed Feb 2, 2019
1 parent 3d0f0cb commit 8be3d1b
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 2 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ dependencies {
compile('org.springframework.boot:spring-boot-starter-thymeleaf')
compile('com.auth0:java-jwt:3.4.0')
compile group: 'com.google.guava', name: 'guava', version: '23.5-jre'
compile group: 'com.mashape.unirest', name: 'unirest-java', version: '1.3.1'
runtime 'org.springframework.boot:spring-boot-starter-tomcat:1.5.6.RELEASE'
runtime 'org.springframework.security:spring-security-taglibs:4.2.3.RELEASE'
runtime 'org.apache.tomcat.embed:tomcat-embed-jasper:8.5.20'
Expand Down
29 changes: 29 additions & 0 deletions src/main/java/org/seattlevoluntech/SeattlevoluntechAppConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package org.seattlevoluntech;

import org.seattlevoluntech.Utilities.EmailUtility;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class SeattlevoluntechAppConfig {

// Bean configuration for Mail Sender

/**
* Domain for mailgun mail sender api
*/
@Value(value = "${mailgun.domain}")
private String mailgunDomain;

/**
* API key required to send emails through mailgun
*/
@Value(value = "${mailgun.api.key}")
private String mailgunApiKey;

@Bean
public EmailUtility emailUtility() {
return new EmailUtility(mailgunDomain, mailgunApiKey);
}
}
25 changes: 25 additions & 0 deletions src/main/java/org/seattlevoluntech/Utilities/EmailUtility.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.seattlevoluntech.Utilities;

import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.JsonNode;
import com.mashape.unirest.http.Unirest;
import com.mashape.unirest.http.exceptions.UnirestException;
import lombok.RequiredArgsConstructor;
import org.seattlevoluntech.models.Email;

@RequiredArgsConstructor
public class EmailUtility {
private final String DOMAIN;
private final String API_KEY;

public JsonNode SendSimpleMessage(Email email) throws UnirestException {
HttpResponse<JsonNode> request = Unirest.post("https://api.mailgun.net/v3/" + DOMAIN + "/messages")
.basicAuth("api", API_KEY)
.field("from", email.getFrom())
.field("to", email.getTo())
.field("subject", email.getSubject())
.field("text", email.getMessage())
.asJson();
return request.getBody();
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package org.seattlevoluntech.controllers;

import com.google.common.collect.Lists;
import com.mashape.unirest.http.exceptions.UnirestException;
import org.seattlevoluntech.Utilities.EmailUtility;
import org.seattlevoluntech.models.Email;
import org.seattlevoluntech.storage.Project;
import org.seattlevoluntech.storage.*;
import org.slf4j.Logger;
Expand All @@ -21,6 +24,8 @@ public class ProjectController {
private ProjectsRepository projectsRepository;
@Autowired
private UsersRepository usersRepository;
@Autowired
private EmailUtility emailUtility;

// Create project
@PostMapping(path="/projects")
Expand Down Expand Up @@ -112,8 +117,12 @@ public Optional<Project> linkUserWithProject(
}


optionalUser.get().addProject(optionalProject.get());
Optional newProjectList = Optional.ofNullable(optionalUser.get().addProject(optionalProject.get()));

usersRepository.save(optionalUser.get());
if (newProjectList.isPresent()) {
sendEmail(optionalUser.get(), optionalProject.get());
}

return projectsRepository.findById(projectId);
}
Expand All @@ -128,4 +137,25 @@ public List<Project> deleteProject(@PathVariable("id") Long id) {
projectsRepository.deleteById(id);
return Lists.newArrayList(projectsRepository.findAll());
}


private void sendEmail(User user, Project project) {
try {
Email email = new Email();
email.setFrom("no-reply@seattlevoluntech.com");
// TODO: Set this email up dynamically based on the owner of the project
email.setTo("jennyyuan88+voluntechTest@gmail.com");
email.setSubject("You got a volunteer for: " + project.getProjectName());
email.setMessage(
"Hello, a volunteer joined your project named "
+ project.getProjectName()
+ ". Their name is "
+ user.getFirstName() + " " + user.getLastName() + "."
+ " You can reach them at: " + user.getEmail()
);
emailUtility.SendSimpleMessage(email);
} catch (UnirestException e) {
logger.error(e.toString());
}
}
}
11 changes: 11 additions & 0 deletions src/main/java/org/seattlevoluntech/models/Email.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.seattlevoluntech.models;

import lombok.Data;

@Data
public class Email {
private String from;
private String to;
private String subject;
private String message;
}
4 changes: 3 additions & 1 deletion src/main/java/org/seattlevoluntech/storage/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,13 @@ public void setLastName(String lastName) {

public void setEmail(String email) { this.email = email; }

public void addProject(Project project) {
public List<Project> addProject(Project project) {
if (!this.projects.contains(project))
{
projects.add(project);
return projects;
}
return null;
}

@JsonIgnoreProperties("users")
Expand Down
4 changes: 4 additions & 0 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,7 @@ spring.datasource.password=${DB_PASS}

#Do nothing
spring.jpa.hibernate.ddl-auto=none

#Mailgun email sender properties:
mailgun.domain: sandbox08786ed216874ad697286dcf97357a43.mailgun.org
mailgun.api.key: 41a98182603ad234d941c01860d8e426-c8c889c9-270b8e5a

0 comments on commit 8be3d1b

Please sign in to comment.