-
Notifications
You must be signed in to change notification settings - Fork 5
feat/#629 메일 발송 이력 저장 구조 #662
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
3bb8dc0
feat: 메일 발송 이력 저장 구조 추가
023-dev 5b9a5ff
fix: 메일 발송 요청 시각 기록
023-dev e5e374b
style: 메일 발송 테스트 설명 보완
023-dev 09f2a2b
style: 발송 대상 테스트 설명 보완
023-dev d6682f9
feat: 메일 발송 중복 실행 방지 추가
023-dev b29e3fa
fix: 발송 작업 대상 건수 검증 추가
023-dev 05013f0
docs: 메일 발송 상태 분기 주석 보강
023-dev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
162 changes: 162 additions & 0 deletions
162
src/main/java/org/ject/support/admin/mail/domain/MailDispatchJob.java
This file contains hidden or 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,162 @@ | ||
| package org.ject.support.admin.mail.domain; | ||
|
|
||
| import jakarta.persistence.Column; | ||
| import jakarta.persistence.Entity; | ||
| import jakarta.persistence.EnumType; | ||
| import jakarta.persistence.Enumerated; | ||
| import jakarta.persistence.GeneratedValue; | ||
| import jakarta.persistence.GenerationType; | ||
| import jakarta.persistence.Id; | ||
| import jakarta.persistence.Table; | ||
| import jakarta.persistence.UniqueConstraint; | ||
| import jakarta.persistence.Version; | ||
| import java.time.LocalDateTime; | ||
| import lombok.AccessLevel; | ||
| import lombok.Getter; | ||
| import lombok.NoArgsConstructor; | ||
| import org.ject.support.admin.mail.exception.MailErrorCode; | ||
| import org.ject.support.admin.mail.exception.MailException; | ||
| import org.ject.support.domain.base.BaseTimeEntity; | ||
|
|
||
| @Entity | ||
| @Getter | ||
| @NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
| @Table(name = "mail_dispatch_job", uniqueConstraints = @UniqueConstraint( | ||
| name = "uk_mail_dispatch_job_requester_key", | ||
| columnNames = {"requested_by_admin_id", "idempotency_key"})) | ||
| public class MailDispatchJob extends BaseTimeEntity { | ||
|
|
||
| @Id | ||
| @GeneratedValue(strategy = GenerationType.IDENTITY) | ||
| private Long id; | ||
|
|
||
| @Column(name = "scenario_id", nullable = false) | ||
| private Long scenarioId; | ||
|
|
||
| @Column(name = "recruit_id", nullable = false) | ||
| private Long recruitId; | ||
|
|
||
| @Column(name = "requested_by_admin_id", nullable = false) | ||
| private Long requestedByAdminId; | ||
|
|
||
| @Column(name = "idempotency_key", nullable = false, length = 255) | ||
| private String idempotencyKey; | ||
|
|
||
| @Enumerated(EnumType.STRING) | ||
| @Column(nullable = false, length = 30) | ||
| private MailDispatchJobStatus status; | ||
|
|
||
| @Column(name = "target_count", nullable = false) | ||
| private int targetCount; | ||
|
|
||
| @Column(name = "processing_count", nullable = false) | ||
| private int processingCount; | ||
|
|
||
| @Column(name = "success_count", nullable = false) | ||
| private int successCount; | ||
|
|
||
| @Column(name = "failed_count", nullable = false) | ||
| private int failedCount; | ||
|
|
||
| @Column(name = "requested_at", nullable = false) | ||
| private LocalDateTime requestedAt; | ||
|
|
||
| @Column(name = "subject_template", nullable = false, columnDefinition = "TEXT") | ||
| private String subjectTemplate; | ||
|
|
||
| @Column(name = "body_template", nullable = false, columnDefinition = "TEXT") | ||
| private String bodyTemplate; | ||
|
|
||
| @Column(name = "input_variables_json", columnDefinition = "TEXT") | ||
| private String inputVariablesJson; | ||
|
|
||
| @Column(name = "started_at") | ||
| private LocalDateTime startedAt; | ||
|
|
||
| @Column(name = "finished_at") | ||
| private LocalDateTime finishedAt; | ||
|
|
||
| @Version | ||
| private Long version; | ||
|
|
||
| private MailDispatchJob(Long scenarioId, | ||
| Long recruitId, | ||
| Long requestedByAdminId, | ||
| String idempotencyKey, | ||
| String subjectTemplate, | ||
| String bodyTemplate, | ||
| String inputVariablesJson, | ||
| int targetCount) { | ||
| this.scenarioId = scenarioId; | ||
| this.recruitId = recruitId; | ||
| this.requestedByAdminId = requestedByAdminId; | ||
| this.idempotencyKey = idempotencyKey; | ||
| this.subjectTemplate = subjectTemplate; | ||
| this.bodyTemplate = bodyTemplate; | ||
| this.inputVariablesJson = inputVariablesJson; | ||
| this.targetCount = targetCount; | ||
| this.status = MailDispatchJobStatus.REQUESTED; | ||
| this.requestedAt = LocalDateTime.now(); | ||
| } | ||
|
|
||
| public static MailDispatchJob create(Long scenarioId, | ||
| Long recruitId, | ||
| Long requestedByAdminId, | ||
| String idempotencyKey, | ||
| String subjectTemplate, | ||
| String bodyTemplate, | ||
| String inputVariablesJson, | ||
| int targetCount) { | ||
| if (targetCount <= 0) { | ||
| throw new MailException(MailErrorCode.INVALID_DISPATCH_TARGET_COUNT); | ||
| } | ||
| return new MailDispatchJob( | ||
| scenarioId, | ||
| recruitId, | ||
| requestedByAdminId, | ||
| idempotencyKey, | ||
| subjectTemplate, | ||
| bodyTemplate, | ||
| inputVariablesJson, | ||
| targetCount | ||
| ); | ||
| } | ||
|
|
||
| public void startProcessing() { | ||
| validateStatus(MailDispatchJobStatus.REQUESTED); | ||
| status = MailDispatchJobStatus.PROCESSING; | ||
| processingCount = targetCount; | ||
| startedAt = LocalDateTime.now(); | ||
| } | ||
|
|
||
| public void recordSuccess() { | ||
| validateStatus(MailDispatchJobStatus.PROCESSING); | ||
| processingCount--; | ||
| successCount++; | ||
| finishIfCompleted(); | ||
| } | ||
|
|
||
| public void recordFailure() { | ||
| validateStatus(MailDispatchJobStatus.PROCESSING); | ||
| processingCount--; | ||
| failedCount++; | ||
| finishIfCompleted(); | ||
| } | ||
|
|
||
| private void finishIfCompleted() { | ||
| if (processingCount > 0) { | ||
| return; | ||
| } | ||
| // 모든 대상이 실패한 경우에만 작업을 실패로 마무리한다. | ||
| status = failedCount == targetCount | ||
| ? MailDispatchJobStatus.FAILED | ||
| : MailDispatchJobStatus.COMPLETED; | ||
| finishedAt = LocalDateTime.now(); | ||
| } | ||
|
|
||
| private void validateStatus(MailDispatchJobStatus expectedStatus) { | ||
| if (status != expectedStatus) { | ||
| throw new MailException(MailErrorCode.INVALID_DISPATCH_JOB_STATUS); | ||
| } | ||
| } | ||
| } | ||
8 changes: 8 additions & 0 deletions
8
src/main/java/org/ject/support/admin/mail/domain/MailDispatchJobStatus.java
This file contains hidden or 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,8 @@ | ||
| package org.ject.support.admin.mail.domain; | ||
|
|
||
| public enum MailDispatchJobStatus { | ||
| REQUESTED, | ||
| PROCESSING, | ||
| COMPLETED, | ||
| FAILED | ||
| } |
86 changes: 86 additions & 0 deletions
86
src/main/java/org/ject/support/admin/mail/domain/MailDispatchTarget.java
This file contains hidden or 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,86 @@ | ||
| package org.ject.support.admin.mail.domain; | ||
|
|
||
| import jakarta.persistence.Column; | ||
| import jakarta.persistence.Entity; | ||
| import jakarta.persistence.EnumType; | ||
| import jakarta.persistence.Enumerated; | ||
| import jakarta.persistence.FetchType; | ||
| import jakarta.persistence.GeneratedValue; | ||
| import jakarta.persistence.GenerationType; | ||
| import jakarta.persistence.Id; | ||
| import jakarta.persistence.JoinColumn; | ||
| import jakarta.persistence.ManyToOne; | ||
| import jakarta.persistence.Table; | ||
| import jakarta.persistence.Version; | ||
| import java.time.LocalDateTime; | ||
| import lombok.AccessLevel; | ||
| import lombok.Getter; | ||
| import lombok.NoArgsConstructor; | ||
| import org.ject.support.admin.mail.exception.MailErrorCode; | ||
| import org.ject.support.admin.mail.exception.MailException; | ||
| import org.ject.support.domain.base.BaseTimeEntity; | ||
|
|
||
| @Entity | ||
| @Getter | ||
| @NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
| @Table(name = "mail_dispatch_target") | ||
| public class MailDispatchTarget extends BaseTimeEntity { | ||
|
|
||
| @Id | ||
| @GeneratedValue(strategy = GenerationType.IDENTITY) | ||
| private Long id; | ||
|
|
||
| @ManyToOne(fetch = FetchType.LAZY, optional = false) | ||
| @JoinColumn(name = "dispatch_job_id", nullable = false) | ||
| private MailDispatchJob dispatchJob; | ||
|
|
||
| @Column(name = "apply_id", nullable = false) | ||
| private Long applyId; | ||
|
|
||
| @Column(nullable = false, length = 255) | ||
| private String email; | ||
|
|
||
| @Enumerated(EnumType.STRING) | ||
| @Column(nullable = false, length = 30) | ||
| private MailDispatchTargetStatus status; | ||
|
|
||
| @Column(name = "failure_reason", columnDefinition = "TEXT") | ||
| private String failureReason; | ||
|
|
||
| @Column(name = "sent_at") | ||
| private LocalDateTime sentAt; | ||
|
|
||
| @Version | ||
| private Long version; | ||
|
|
||
| private MailDispatchTarget(MailDispatchJob dispatchJob, Long applyId, String email) { | ||
| this.dispatchJob = dispatchJob; | ||
| this.applyId = applyId; | ||
| this.email = email; | ||
| this.status = MailDispatchTargetStatus.PENDING; | ||
| } | ||
|
|
||
| public static MailDispatchTarget pending(MailDispatchJob dispatchJob, Long applyId, String email) { | ||
| return new MailDispatchTarget(dispatchJob, applyId, email); | ||
| } | ||
|
|
||
| public void markSent() { | ||
| validatePending(); | ||
| status = MailDispatchTargetStatus.SENT; | ||
| sentAt = LocalDateTime.now(); | ||
| failureReason = null; | ||
| } | ||
|
|
||
| public void markFailed(String failureReason) { | ||
| validatePending(); | ||
| status = MailDispatchTargetStatus.FAILED; | ||
| this.failureReason = failureReason; | ||
| sentAt = null; | ||
| } | ||
|
|
||
| private void validatePending() { | ||
| if (status != MailDispatchTargetStatus.PENDING) { | ||
| throw new MailException(MailErrorCode.INVALID_DISPATCH_TARGET_STATUS); | ||
| } | ||
| } | ||
| } |
7 changes: 7 additions & 0 deletions
7
src/main/java/org/ject/support/admin/mail/domain/MailDispatchTargetStatus.java
This file contains hidden or 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,7 @@ | ||
| package org.ject.support.admin.mail.domain; | ||
|
|
||
| public enum MailDispatchTargetStatus { | ||
| PENDING, | ||
| SENT, | ||
| FAILED | ||
| } |
This file contains hidden or 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
11 changes: 11 additions & 0 deletions
11
src/main/java/org/ject/support/admin/mail/repository/MailDispatchJobRepository.java
This file contains hidden or 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,11 @@ | ||
| package org.ject.support.admin.mail.repository; | ||
|
|
||
| import java.util.Optional; | ||
| import org.ject.support.admin.mail.domain.MailDispatchJob; | ||
| import org.springframework.data.jpa.repository.JpaRepository; | ||
|
|
||
| public interface MailDispatchJobRepository extends JpaRepository<MailDispatchJob, Long> { | ||
|
|
||
| Optional<MailDispatchJob> findByRequestedByAdminIdAndIdempotencyKey( | ||
| Long requestedByAdminId, String idempotencyKey); | ||
| } |
13 changes: 13 additions & 0 deletions
13
src/main/java/org/ject/support/admin/mail/repository/MailDispatchTargetRepository.java
This file contains hidden or 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 org.ject.support.admin.mail.repository; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Optional; | ||
| import org.ject.support.admin.mail.domain.MailDispatchTarget; | ||
| import org.springframework.data.jpa.repository.JpaRepository; | ||
|
|
||
| public interface MailDispatchTargetRepository extends JpaRepository<MailDispatchTarget, Long> { | ||
|
|
||
| Optional<MailDispatchTarget> findByDispatchJobIdAndApplyId(Long dispatchJobId, Long applyId); | ||
|
|
||
| List<MailDispatchTarget> findAllByDispatchJobIdOrderByIdAsc(Long dispatchJobId); | ||
| } |
45 changes: 45 additions & 0 deletions
45
src/main/resources/db/migration/V39__create_mail_dispatch_tables.sql
This file contains hidden or 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,45 @@ | ||
| CREATE TABLE mail_dispatch_job | ||
| ( | ||
| id BIGINT AUTO_INCREMENT NOT NULL, | ||
| scenario_id BIGINT NOT NULL, | ||
| recruit_id BIGINT NOT NULL, | ||
| requested_by_admin_id BIGINT NOT NULL, | ||
| idempotency_key VARCHAR(255) NOT NULL, | ||
| status VARCHAR(30) NOT NULL, | ||
| target_count INT NOT NULL, | ||
| processing_count INT NOT NULL DEFAULT 0, | ||
| success_count INT NOT NULL DEFAULT 0, | ||
| failed_count INT NOT NULL DEFAULT 0, | ||
| requested_at datetime(6) NOT NULL, | ||
| subject_template TEXT NOT NULL, | ||
| body_template TEXT NOT NULL, | ||
| input_variables_json TEXT, | ||
| started_at datetime(6), | ||
| finished_at datetime(6), | ||
| version BIGINT NOT NULL DEFAULT 0, | ||
| created_at datetime(6), | ||
| updated_at datetime(6), | ||
| CONSTRAINT `PRIMARY` PRIMARY KEY (id), | ||
| CONSTRAINT uk_mail_dispatch_job_requester_key UNIQUE (requested_by_admin_id, idempotency_key) | ||
| ) ENGINE = InnoDB; | ||
|
|
||
| CREATE TABLE mail_dispatch_target | ||
| ( | ||
| id BIGINT AUTO_INCREMENT NOT NULL, | ||
| dispatch_job_id BIGINT NOT NULL, | ||
| apply_id BIGINT NOT NULL, | ||
| email VARCHAR(255) NOT NULL, | ||
| status VARCHAR(30) NOT NULL, | ||
| failure_reason TEXT, | ||
| sent_at datetime(6), | ||
| version BIGINT NOT NULL DEFAULT 0, | ||
| created_at datetime(6), | ||
| updated_at datetime(6), | ||
| CONSTRAINT `PRIMARY` PRIMARY KEY (id), | ||
| CONSTRAINT fk_mail_dispatch_target_job | ||
| FOREIGN KEY (dispatch_job_id) REFERENCES mail_dispatch_job (id) ON DELETE CASCADE, | ||
| CONSTRAINT uk_mail_dispatch_target_job_apply UNIQUE (dispatch_job_id, apply_id) | ||
| ) ENGINE = InnoDB; | ||
|
|
||
| CREATE INDEX idx_mail_dispatch_target_job_status | ||
| ON mail_dispatch_target (dispatch_job_id, status); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.