-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #104 from PEC-CSS/bulk-mail
Feat: Bulk mail
- Loading branch information
Showing
6 changed files
with
122 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
src/main/java/com/pecacm/backend/controllers/EmailController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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."); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters