Skip to content

Commit

Permalink
Merge pull request #104 from PEC-CSS/bulk-mail
Browse files Browse the repository at this point in the history
Feat: Bulk mail
  • Loading branch information
13jksingh authored Nov 11, 2023
2 parents 5ad12d5 + 34b1ea9 commit bec2d6b
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 1 deletion.
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
<groupId>org.commonmark</groupId>
<artifactId>commonmark</artifactId>
<version>0.21.0</version>
</dependency>
</dependencies>

<build>
Expand Down
44 changes: 44 additions & 0 deletions src/main/java/com/pecacm/backend/controllers/EmailController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.pecacm.backend.controllers;

import com.pecacm.backend.constants.Constants;
import com.pecacm.backend.entities.User;
import com.pecacm.backend.model.EmailRequest;
import com.pecacm.backend.services.EmailService;
import com.pecacm.backend.services.UserService;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/v1/email")
public class EmailController {
private final EmailService emailService;
private final UserService userService;

public EmailController(EmailService emailService, UserService userService) {
this.emailService = emailService;
this.userService = userService;
}

@PostMapping
@PreAuthorize(Constants.HAS_ROLE_CORE_AND_ABOVE)
public ResponseEntity<String> sendBulkEmail(@RequestBody EmailRequest emailRequest) {
List<User> users;

if (emailRequest.getEmails() != null) {
users = userService.getUserByEmailIds(emailRequest.getEmails());
} else if (emailRequest.getRole() != null) {
users = userService.getUserByRole(emailRequest.getRole());
} else {
users = userService.getAllUsers();
}

emailService.sendEmail(users, emailRequest.getSubject(), emailRequest.getBody());
return ResponseEntity.ok("Mails sent.");
}
}
23 changes: 23 additions & 0 deletions src/main/java/com/pecacm/backend/model/EmailRequest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.pecacm.backend.model;

import com.pecacm.backend.enums.Role;
import jakarta.annotation.Nullable;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.util.List;

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class EmailRequest {
@Nullable
private Role role;
@Nullable
private List<String> emails;
private String subject;
private String body;
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,9 @@ public interface UserRepository extends JpaRepository<User, Integer> {
Page<User> findAllByOrderByXpDesc(PageRequest pageRequest);
@Query("SELECT u FROM User u WHERE SPLIT_PART(u.email, '.', 1) ILIKE %:query% ORDER BY u.id LIMIT 10")
List<User> findAllBySearchQuery(String query);

List<User> findAllByDesignation(Role role);

List<User> findByEmailIn(List<String> emails);

}
34 changes: 33 additions & 1 deletion src/main/java/com/pecacm/backend/services/EmailService.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
package com.pecacm.backend.services;

import com.pecacm.backend.entities.User;
import com.pecacm.backend.entities.VerificationToken;
import com.pecacm.backend.exception.AcmException;
import com.pecacm.backend.repository.UserRepository;
import com.pecacm.backend.repository.VerificationTokenRepository;
import jakarta.mail.MessagingException;
import jakarta.mail.internet.MimeMessage;
import org.commonmark.node.Node;
import org.commonmark.renderer.html.HtmlRenderer;
import org.springframework.http.HttpStatus;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.logging.Logger;

import org.commonmark.parser.Parser;

@Service
public class EmailService {

Expand Down Expand Up @@ -39,4 +49,26 @@ public void sendVerificationEmail(String username) {

javaMailSender.send(mailMessage);
}

public void sendEmail(List<User> users, String subject, String body) {
String[] recipients = users.stream().map(User::getEmail).toArray(String[]::new);
String htmlBody = convertMarkdownToHtml(body);
try {
MimeMessage message = javaMailSender.createMimeMessage();
message.setSubject(subject);
MimeMessageHelper helper;
helper = new MimeMessageHelper(message, true);
helper.setTo(recipients);
helper.setText(htmlBody, true);
javaMailSender.send(message);
} catch (MessagingException ex) {
throw new AcmException("Not able to send mail");
}
}
private String convertMarkdownToHtml(String markdownContent) {
Parser parser = Parser.builder().build();
Node document = parser.parse(markdownContent);
HtmlRenderer renderer = HtmlRenderer.builder().build();
return renderer.render(document);
}
}
12 changes: 12 additions & 0 deletions src/main/java/com/pecacm/backend/services/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -228,4 +228,16 @@ public List<Transaction> getUserTransactions(String email, Integer offset, Integ
public List<User> getFilteredUserList(String query){
return userRepository.findAllBySearchQuery(query);
}

public List<User> getUserByRole(Role role) {
return userRepository.findAllByDesignation(role);
}

public List<User> getUserByEmailIds(List<String> emails) {
return userRepository.findByEmailIn(emails);
}

public List<User> getAllUsers(){
return userRepository.findAll();
}
}

0 comments on commit bec2d6b

Please sign in to comment.