Skip to content

Commit

Permalink
ISSUE5 기존의 코드 수정 및 중복 출석체크 시 한번만 insert 하도록 수정 email 알림 서비스 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
dongryeol-kim committed Jan 8, 2025
2 parents 992bd4a + a7494d2 commit 56bfe8a
Show file tree
Hide file tree
Showing 8 changed files with 152 additions and 2 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
네이버 웍스 같은 사내 게시판 협업 툴 서버

# ERD
https://user-images.githubusercontent.com/75170367/205051107-97f36259-f782-423d-a3cc-ca9ca6985b7b.png
![collaboration-board-server png](https://github.com/user-attachments/assets/6e247b48-fc82-4bbd-a0dd-8d0b0883659f)


# 기획서
### **1. 사용자 관리 (User Management)**
Expand Down
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.");
}
}
}
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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,15 @@

import lombok.Data;

import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.Table;

@Data
@Entity
@Table(name = "user")
public class User {
@Id
private Long id;
private String userId;
private String username;
Expand Down
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);
}
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);
}
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);
}
}
12 changes: 11 additions & 1 deletion src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 56bfe8a

Please sign in to comment.