-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ISSUE5 기존의 코드 수정 및 중복 출석체크 시 한번만 insert 하도록 수정 email 알림 서비스 추가
- Loading branch information
Showing
8 changed files
with
152 additions
and
2 deletions.
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
70 changes: 70 additions & 0 deletions
70
src/main/java/com/commit/collaboration_board_server/controller/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,70 @@ | ||
package com.commit.collaboration_board_server.controller; | ||
|
||
import com.commit.collaboration_board_server.model.MonthlyWorkDate; | ||
import com.commit.collaboration_board_server.model.User; | ||
import com.commit.collaboration_board_server.repository.MonthlyWorkDateRepository; | ||
import com.commit.collaboration_board_server.repository.UserRepository; | ||
import com.commit.collaboration_board_server.service.EmailService; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.*; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.util.List; | ||
|
||
@RestController | ||
@RequestMapping("/email") | ||
public class EmailController { | ||
|
||
private static final Logger logger = LoggerFactory.getLogger(EmailController.class); | ||
|
||
private final UserRepository userRepository; | ||
private final MonthlyWorkDateRepository monthlyWorkDateRepository; | ||
private final EmailService emailService; | ||
|
||
@Autowired | ||
public EmailController(UserRepository userRepository, | ||
MonthlyWorkDateRepository monthlyWorkDateRepository, | ||
EmailService emailService) { | ||
this.userRepository = userRepository; | ||
this.monthlyWorkDateRepository = monthlyWorkDateRepository; | ||
this.emailService = emailService; | ||
} | ||
|
||
@PostMapping("/sendToLowWorkTimeUsers") | ||
public ResponseEntity<String> sendEmailToLowWorkTimeUsers(@RequestParam String subject, @RequestParam String text) { | ||
try { | ||
// 160시간 미만일 경우 이메일 발송 | ||
List<MonthlyWorkDate> lowWorkTimeUsers = monthlyWorkDateRepository.findByTotalWorkTimeLessThan(160); | ||
|
||
if (lowWorkTimeUsers.isEmpty()) { | ||
logger.info("No users found with total work time less than 160 hours."); | ||
return ResponseEntity.ok("No users found to send emails."); | ||
} | ||
|
||
for (MonthlyWorkDate workDate : lowWorkTimeUsers) { | ||
String userId = workDate.getUserId(); | ||
User user = userRepository.findByUserId(userId); | ||
|
||
if (user != null && user.getEmail() != null) { | ||
String email = user.getEmail(); | ||
//logger.info("Sending email to userID: {}, email: {}", userId, email); | ||
|
||
// 이메일 발송 | ||
emailService.sendEmail(email, subject, text); | ||
//logger.info("Email sent successfully to {}", email); | ||
} else { | ||
logger.warn("User with ID {} does not have a valid email.", userId); | ||
} | ||
} | ||
|
||
return ResponseEntity.ok("Emails sent to users with total work time less than 160 hours."); | ||
|
||
} catch (Exception e) { | ||
logger.error("Error sending emails to users with low work time.", e); | ||
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Failed to send emails."); | ||
} | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
src/main/java/com/commit/collaboration_board_server/model/MonthlyWorkDate.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,15 @@ | ||
package com.commit.collaboration_board_server.model; | ||
|
||
import jakarta.persistence.*; | ||
import lombok.Data; | ||
|
||
@Entity | ||
@Data | ||
@Table(name = "monthly_work_date") | ||
public class MonthlyWorkDate { | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
@Id | ||
private Long id; | ||
private String userId; | ||
private int totalWorkTime; | ||
} |
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
13 changes: 13 additions & 0 deletions
13
...main/java/com/commit/collaboration_board_server/repository/MonthlyWorkDateRepository.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,13 @@ | ||
package com.commit.collaboration_board_server.repository; | ||
|
||
import com.commit.collaboration_board_server.model.MonthlyWorkDate; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.stereotype.Repository; | ||
|
||
import java.util.List; | ||
|
||
@Repository | ||
public interface MonthlyWorkDateRepository extends JpaRepository<MonthlyWorkDate, Long> { | ||
|
||
List<MonthlyWorkDate> findByTotalWorkTimeLessThan(int hours); | ||
} |
10 changes: 10 additions & 0 deletions
10
src/main/java/com/commit/collaboration_board_server/repository/UserRepository.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,10 @@ | ||
package com.commit.collaboration_board_server.repository; | ||
|
||
import com.commit.collaboration_board_server.model.User; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.stereotype.Repository; | ||
|
||
@Repository | ||
public interface UserRepository extends JpaRepository<User, Long> { | ||
User findByUserId(String userId); | ||
} |
25 changes: 25 additions & 0 deletions
25
src/main/java/com/commit/collaboration_board_server/service/EmailService.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,25 @@ | ||
package com.commit.collaboration_board_server.service; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.mail.SimpleMailMessage; | ||
import org.springframework.mail.javamail.JavaMailSender; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
public class EmailService { | ||
|
||
private final JavaMailSender mailSender; | ||
|
||
@Autowired | ||
public EmailService(JavaMailSender mailSender) { | ||
this.mailSender = mailSender; | ||
} | ||
|
||
public void sendEmail(String to, String subject, String text) { | ||
SimpleMailMessage message = new SimpleMailMessage(); | ||
message.setTo(to); | ||
message.setSubject(subject); | ||
message.setText(text); | ||
mailSender.send(message); | ||
} | ||
} |
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 |
---|---|---|
|
@@ -5,4 +5,14 @@ spring.datasource.url=jdbc:mysql://localhost:3306/board | |
spring.datasource.username=root | ||
spring.datasource.password=1234 | ||
mybatis.mapper-locations=classpath:mapper/*.xml | ||
mybatis.type-aliases-package=com.commit.collaboration_board_server.model | ||
mybatis.type-aliases-package=com.commit.collaboration_board_server.model | ||
|
||
spring.mail.host=smtp.gmail.com | ||
spring.mail.port=587 | ||
spring.mail.username[email protected] | ||
spring.mail.password=ooam ubmt wnwg tgvq | ||
spring.mail.properties.mail.smtp.auth=true | ||
spring.mail.properties.mail.smtp.starttls.enable=true | ||
|
||
spring.jpa.show-sql=true | ||
spring.jpa.hibernate.ddl-auto=validate |