Skip to content

Commit

Permalink
Merge pull request #298 from LearnsMate/feature/campaign
Browse files Browse the repository at this point in the history
♻️코드 수정: scheduleCampaigns 수정
  • Loading branch information
YuJeeun authored Nov 29, 2024
2 parents 6a2965a + 7d84312 commit 8d11873
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ public CampaignDTO registerCampaign(CampaignDTO requestCampaign
LocalDateTime sendTime;
AdminDTO adminDTO = adminService.findByAdminCode(requestCampaign.getAdminCode());
Admin admin = adminMapper.toEntity(adminDTO);

if (Objects.equals(requestCampaign.getCampaignType(), CampaignTypeEnum.INSTANT.getType())) {
sendTime = LocalDateTime.now();
} else {
Expand All @@ -83,6 +84,7 @@ public CampaignDTO registerCampaign(CampaignDTO requestCampaign
.campaignContents(requestCampaign.getCampaignContents())
.campaignType(CampaignTypeEnum.valueOf(requestCampaign.getCampaignType()))
.campaignSendDate(sendTime)
.campaignSendFlag(false)
.createdAt(requestCampaign.getCreatedAt())
.updatedAt(LocalDateTime.now())
.admin(admin)
Expand Down Expand Up @@ -131,17 +133,7 @@ else if (foundCoupon.getTutorCode() != null) {
public void registerScheduledCampaign(List<MemberDTO> studentList, List<CouponDTO> couponList, LocalDateTime scheduledTime) {
System.out.println("캠페인 예약 작업이 등록되었습니다. 예약 시간: " + scheduledTime);
try {

couponMemberReader.setStudentCouponPairs(studentList, couponList);


// Job 파라미터 설정: 예약 시간과 관련된 정보를 파라미터에 추가
JobParameters jobParameters = new JobParametersBuilder()
.addDate("scheduledTime", Timestamp.valueOf(scheduledTime))
.toJobParameters();

// Job 실행
jobLauncher.run(campaignJob, jobParameters);
} catch (Exception e) {
// 에러 처리
System.err.println("캠페인 예약 작업 등록 실패: " + e.getMessage());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package intbyte4.learnsmate.common.config;


import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
Expand All @@ -10,6 +11,7 @@
import java.util.Properties;

@Configuration
@Slf4j
public class EmailConfig {

@Value("${spring.mail.host}")
Expand Down Expand Up @@ -43,7 +45,9 @@ public JavaMailSender mailSender() {
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.auth", auth);
props.put("mail.smtp.starttls.enable", sslEnable);
props.put("mail.smtp.starttls.trust", host);
props.put("mail.smtp.starttls.required", "true");
props.put("mail.smtp.timeout", 5000);


return mailSender;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import intbyte4.learnsmate.admin.service.EmailService;
import intbyte4.learnsmate.campaign.domain.dto.CampaignDTO;
import intbyte4.learnsmate.campaign.domain.entity.CampaignTypeEnum;
import intbyte4.learnsmate.campaign.service.CampaignService;
import intbyte4.learnsmate.member.domain.MemberType;
import intbyte4.learnsmate.member.domain.dto.MemberDTO;
Expand Down Expand Up @@ -52,23 +53,28 @@ public void scheduleVocAnalysis() {

@Scheduled(cron = "0 0 */3 * * *")
public void scheduleCampaigns() {
List<CampaignDTO> readyCampaigns = campaignService.getReadyCampaigns(LocalDateTime.now());
LocalDateTime currentTime = LocalDateTime.now();

List<CampaignDTO> readyCampaigns = campaignService.getReadyCampaigns(currentTime);

for (CampaignDTO campaign : readyCampaigns) {
try {
JobParameters jobParameters = new JobParametersBuilder()
.addLong("campaignCode", campaign.getCampaignCode())
.addDate("startTime", new Date())
.toJobParameters();
jobLauncher.run(campaignJob, jobParameters);
if (CampaignTypeEnum.RESERVATION.name().equals(campaign.getCampaignType())) {
JobParameters jobParameters = new JobParametersBuilder()
.addLong("campaignCode", campaign.getCampaignCode())
.addDate("startTime", new Date())
.toJobParameters();
jobLauncher.run(campaignJob, jobParameters);

List<UserPerCampaignDTO> userPerCampaignDTOList = userPerCampaignService.findUserByCampaignCode(campaign.getCampaignCode());
for (UserPerCampaignDTO userPerCampaignDTO : userPerCampaignDTOList) {
MemberDTO member = memberService.findMemberByMemberCode(userPerCampaignDTO.getStudentCode(), MemberType.STUDENT);
if (member != null) {
emailService.sendCampaignEmail(member.getMemberEmail(), campaign.getCampaignTitle(), campaign.getCampaignContents());
List<UserPerCampaignDTO> userPerCampaignDTOList = userPerCampaignService.findUserByCampaignCode(campaign.getCampaignCode());
for (UserPerCampaignDTO userPerCampaignDTO : userPerCampaignDTOList) {
MemberDTO member = memberService.findMemberByMemberCode(userPerCampaignDTO.getStudentCode(), MemberType.STUDENT);
if (member != null) {
emailService.sendCampaignEmail(member.getMemberEmail(), campaign.getCampaignTitle(), campaign.getCampaignContents());
}
}
campaignService.updateCampaignSendFlag(campaign.getCampaignCode());
}
campaignService.updateCampaignSendFlag(campaign.getCampaignCode());
} catch (Exception e) {
System.err.println("Failed to launch campaign job: " + e.getMessage());
}
Expand Down

0 comments on commit 8d11873

Please sign in to comment.