261029_02_ENH: 템플릿 삭제 후 기존 발송 유지 및 벌크 발송 한도 검증
This commit is contained in:
@@ -21,8 +21,6 @@ import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
public interface SendRepository extends JpaRepository<TSendEntity, Long> {
|
||||
|
||||
boolean existsByTmplatIdxAndSendStatus(long tmplatIdx, short sendStatus);
|
||||
|
||||
// 목록과 벌크 집계에서 동일한 상태 기준을 사용한다. 취소 API의 잠금 후 판단이 최종이다.
|
||||
String CANCELABLE_CONDITION = """
|
||||
s.resve = true and s.sendStatus = 0 and s.cancelDt is null
|
||||
|
||||
@@ -12,10 +12,6 @@ import org.springframework.data.jpa.repository.Query;
|
||||
public interface TemplateRepository extends JpaRepository<TTmplatEntity, Long>,
|
||||
JpaSpecificationExecutor<TTmplatEntity> {
|
||||
|
||||
@Lock(LockModeType.PESSIMISTIC_READ)
|
||||
@Query("select t from TTmplatEntity t where t.tmplatIdx = :idx and t.del = false")
|
||||
Optional<TTmplatEntity> findActiveForSend(long idx);
|
||||
|
||||
@Lock(LockModeType.PESSIMISTIC_WRITE)
|
||||
@Query("select t from TTmplatEntity t where t.tmplatIdx = :idx and t.del = false")
|
||||
Optional<TTmplatEntity> findActiveForUpdate(long idx);
|
||||
|
||||
@@ -91,9 +91,16 @@ public abstract class AbstractPringoSendService extends AbstractSendService {
|
||||
|
||||
return SendInternalResultDto.of(cmid, SendStatusType.STANDBY, NotihubConst.EMAIL_STANDBY);
|
||||
} else {
|
||||
// 단건도 발송 중 상태를 먼저 저장하고, 커밋 후 트랜잭션 밖에서 외부 발송을 호출한다.
|
||||
return SendInternalResultDto.of(CommonUtil.uuid32(), SendStatusType.STANDBY,
|
||||
NotihubConst.EMAIL_SENDING);
|
||||
PringoSendRequestDto pringoReq = buildPringoSendRequest(
|
||||
sendEml,
|
||||
sendNm,
|
||||
msgTitle,
|
||||
msgBody,
|
||||
tmplatIdx,
|
||||
atchFl,
|
||||
dest
|
||||
);
|
||||
return sendPringoEmail(pringoReq);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -51,7 +51,6 @@ public abstract class AbstractSendService {
|
||||
protected final TemplateService templateService;
|
||||
protected final PhoneVerificationService phoneVerificationService;
|
||||
|
||||
@Transactional(isolation = Isolation.READ_COMMITTED)
|
||||
public String issueBulkKey(BulkSendCreateRequestDto req) {
|
||||
LocalDateTime now = DateTimeUtil.nowKstLocalDateTime();
|
||||
|
||||
@@ -73,7 +72,7 @@ public abstract class AbstractSendService {
|
||||
}
|
||||
}
|
||||
|
||||
TemplateDto tmplat = templateService.getTemplateForSend(req.getTmplatIdx());
|
||||
TemplateDto tmplat = templateService.getTemplate(req.getTmplatIdx());
|
||||
validateTemplateType(tmplat);
|
||||
validateBulkTemplatePurpose(tmplat);
|
||||
|
||||
@@ -106,7 +105,6 @@ public abstract class AbstractSendService {
|
||||
}
|
||||
}
|
||||
|
||||
@Transactional(isolation = Isolation.READ_COMMITTED)
|
||||
public SendSingleResponseDto doSendSingle(
|
||||
SendRequestSingleDto req,
|
||||
String regCmpn,
|
||||
@@ -129,7 +127,7 @@ public abstract class AbstractSendService {
|
||||
}
|
||||
}
|
||||
|
||||
TemplateDto tmplat = templateService.getTemplateForSend(req.getTmplatIdx());
|
||||
TemplateDto tmplat = templateService.getTemplate(req.getTmplatIdx());
|
||||
validateTemplateType(tmplat);
|
||||
validateSingleTemplatePurpose(tmplat, req);
|
||||
|
||||
@@ -245,14 +243,18 @@ public abstract class AbstractSendService {
|
||||
throw BusinessException.badRequest("BULK_SEND_CANCELED", "취소 요청된 벌크에는 수신자를 추가할 수 없습니다.");
|
||||
}
|
||||
|
||||
if (bulk.getTrgtCnt() <= bulk.getSendCnt()) {
|
||||
BusinessException.badRequest("OVERFLOW_SEND_CNT", "해당 bulkSendKey로 발송할 수 있는 한도를 초과 했습니다.")
|
||||
int requestCnt = req.getDestInfoList().size();
|
||||
if ((long) bulk.getSendCnt() + requestCnt > bulk.getTrgtCnt()) {
|
||||
throw BusinessException.badRequest("OVERFLOW_SEND_CNT", "해당 bulkSendKey로 발송할 수 있는 한도를 초과 했습니다.")
|
||||
.with("bulkSendKey", bulkSendKey)
|
||||
.with("trgtCnt", bulk.getTrgtCnt())
|
||||
.with("sendCnt", bulk.getSendCnt());
|
||||
}
|
||||
|
||||
TemplateDto tmplat = templateService.getTemplateForSend(bulk.getTmplatIdx());
|
||||
TemplateDto tmplat = templateRepository.findById(bulk.getTmplatIdx())
|
||||
.map(TemplateDto::from)
|
||||
.orElseThrow(() -> BusinessException.notFound(
|
||||
"TEMPLATE_NOT_FOUND", "템플릿을 찾을 수 없습니다.").with("tmplatIdx", bulk.getTmplatIdx()));
|
||||
validateTemplateType(tmplat);
|
||||
validateBulkTemplatePurpose(tmplat);
|
||||
|
||||
@@ -276,7 +278,6 @@ public abstract class AbstractSendService {
|
||||
req.setSendTel(sendTel);
|
||||
req.setSendNm(sendNm);
|
||||
|
||||
int requestCnt = req.getDestInfoList().size();
|
||||
int acceptCnt = 0;
|
||||
int successCnt = 0;
|
||||
int failCnt = 0;
|
||||
|
||||
@@ -6,32 +6,20 @@ import com.kri.notihub.server.common.enums.ChannelType;
|
||||
import com.kri.notihub.server.common.enums.SendStatusType;
|
||||
import com.kri.notihub.server.common.enums.SendType;
|
||||
import com.kri.notihub.server.common.external.pringo.PringoApiClient;
|
||||
import com.kri.notihub.server.dto.send.DestInfoDto;
|
||||
import com.kri.notihub.server.dto.send.SendInternalResultDto;
|
||||
import com.kri.notihub.server.dto.send.SendRequestSingleDto;
|
||||
import com.kri.notihub.server.dto.send.SendSingleResponseDto;
|
||||
import com.kri.notihub.server.persistence.entity.TSendEntity;
|
||||
import com.kri.notihub.server.persistence.jdbc.SendBulkJdbcWriter;
|
||||
import com.kri.notihub.server.persistence.repository.BulkSendRepository;
|
||||
import com.kri.notihub.server.persistence.repository.SendRepository;
|
||||
import com.kri.notihub.server.persistence.repository.TemplateRepository;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.PlatformTransactionManager;
|
||||
import org.springframework.transaction.TransactionDefinition;
|
||||
import org.springframework.transaction.annotation.Propagation;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.transaction.support.TransactionTemplate;
|
||||
|
||||
@Slf4j
|
||||
@Service
|
||||
public class SendEmailService extends AbstractPringoSendService {
|
||||
|
||||
private final PlatformTransactionManager transactionManager;
|
||||
private final SendResultService resultService;
|
||||
|
||||
public SendEmailService(
|
||||
SendBulkJdbcWriter sendBulkJdbcWriter,
|
||||
TemplateRepository templateRepository,
|
||||
@@ -42,50 +30,11 @@ public class SendEmailService extends AbstractPringoSendService {
|
||||
EmailResultPoller emailResultPoller,
|
||||
EmailBodyRenderer emailBodyRenderer,
|
||||
EmailAttachmentService emailAttachmentService,
|
||||
PhoneVerificationService phoneVerificationService,
|
||||
PlatformTransactionManager transactionManager,
|
||||
SendResultService resultService
|
||||
PhoneVerificationService phoneVerificationService
|
||||
) {
|
||||
super(sendBulkJdbcWriter, templateRepository, bulkSendRepository, sendRepository, templateService,
|
||||
pringoApiClient, emailResultPoller, emailBodyRenderer, emailAttachmentService,
|
||||
phoneVerificationService);
|
||||
this.transactionManager = transactionManager;
|
||||
this.resultService = resultService;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(propagation = Propagation.NOT_SUPPORTED)
|
||||
public SendSingleResponseDto doSendSingle(SendRequestSingleDto req, String regCmpn, String regUser) {
|
||||
TransactionTemplate registration = new TransactionTemplate(transactionManager);
|
||||
registration.setIsolationLevel(TransactionDefinition.ISOLATION_READ_COMMITTED);
|
||||
SendSingleResponseDto registered = Objects.requireNonNull(registration.execute(status ->
|
||||
super.doSendSingle(req, regCmpn, regUser)));
|
||||
if (Boolean.TRUE.equals(req.getResve())) {
|
||||
return registered;
|
||||
}
|
||||
|
||||
TSendEntity send = sendRepository.findById(registered.sendIdx()).orElseThrow();
|
||||
SendInternalResultDto result;
|
||||
try {
|
||||
result = sendPringoEmail(buildPringoSendRequest(
|
||||
send.getSendEml(), send.getSendNm(), send.getMsgTitle(), send.getMsgBody(),
|
||||
send.getTmplatIdx(), send.getAtchFl(), DestInfoDto.from(send)));
|
||||
} catch (RuntimeException ex) {
|
||||
log.warn("Single email preparation failed (sendIdx={})", send.getSendIdx());
|
||||
try {
|
||||
resultService.record(send.getSendIdx(), send.getCmid(), SendStatusType.FAIL,
|
||||
NotihubConst.EMAIL_SEND_FAIL, null);
|
||||
} catch (RuntimeException recordFailure) {
|
||||
ex.addSuppressed(recordFailure);
|
||||
}
|
||||
throw ex;
|
||||
}
|
||||
TSendEntity persisted = resultService.record(send.getSendIdx(), result.cmid(),
|
||||
result.status(), result.resultCode(), null);
|
||||
if (persisted != null) {
|
||||
afterPersistSendEntities(List.of(persisted));
|
||||
}
|
||||
return SendSingleResponseDto.of(registered.sendIdx(), result.cmid(), registered.data());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -5,7 +5,6 @@ import com.kri.common.feature.util.StringUtil;
|
||||
import com.kri.common.feature.web.BusinessException;
|
||||
import com.kri.notihub.server.common.consts.NotihubConst;
|
||||
import com.kri.notihub.server.common.enums.ChannelType;
|
||||
import com.kri.notihub.server.common.enums.SendStatusType;
|
||||
import com.kri.notihub.server.common.enums.SendType;
|
||||
import com.kri.notihub.server.common.enums.TemplateEventType;
|
||||
import com.kri.notihub.server.common.enums.TemplatePurpose;
|
||||
@@ -16,7 +15,6 @@ import com.kri.notihub.server.dto.template.TemplateDto;
|
||||
import com.kri.notihub.server.dto.template.TemplateSummaryDto;
|
||||
import com.kri.notihub.server.persistence.entity.TTmplatEntity;
|
||||
import com.kri.notihub.server.persistence.entity.TTmplatEventLogEntity;
|
||||
import com.kri.notihub.server.persistence.repository.SendRepository;
|
||||
import com.kri.notihub.server.persistence.repository.TemplateEventLogRepository;
|
||||
import com.kri.notihub.server.persistence.repository.TemplateRepository;
|
||||
import java.time.LocalDateTime;
|
||||
@@ -26,7 +24,6 @@ import lombok.extern.log4j.Log4j2;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.PlatformTransactionManager;
|
||||
import org.springframework.transaction.annotation.Isolation;
|
||||
import org.springframework.transaction.annotation.Propagation;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.transaction.support.TransactionTemplate;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
@@ -37,7 +34,6 @@ import org.springframework.web.multipart.MultipartFile;
|
||||
public class TemplateService {
|
||||
|
||||
private final TemplateRepository templateRepository;
|
||||
private final SendRepository sendRepository;
|
||||
private final TemplateEventLogRepository templateEventLogRepository;
|
||||
private final FileService fileService;
|
||||
private final EmailAttachmentService emailAttachmentService;
|
||||
@@ -141,15 +137,6 @@ public class TemplateService {
|
||||
);
|
||||
}
|
||||
|
||||
// 발송 등록 트랜잭션이 커밋될 때까지 템플릿 공유 잠금을 유지한다.
|
||||
@Transactional(propagation = Propagation.MANDATORY)
|
||||
public TemplateDto getTemplateForSend(long tmplatIdx) {
|
||||
return templateRepository.findActiveForSend(tmplatIdx)
|
||||
.map(TemplateDto::from)
|
||||
.orElseThrow(() -> BusinessException.notFound(
|
||||
"TEMPLATE_NOT_FOUND", "템플릿을 찾을 수 없습니다.").with("tmplatIdx", tmplatIdx));
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public EmailAttachmentDto downloadAttachment(long tmplatIdx, String fileName) {
|
||||
TTmplatEntity entity = templateRepository.findByTmplatIdxAndDelFalse(tmplatIdx)
|
||||
@@ -245,10 +232,6 @@ public class TemplateService {
|
||||
if (Boolean.TRUE.equals(entity.getDel())) {
|
||||
return;
|
||||
}
|
||||
if (sendRepository.existsByTmplatIdxAndSendStatus(tmplatIdx, SendStatusType.STANDBY.getCode())) {
|
||||
throw BusinessException.conflict("TEMPLATE_IN_USE", "발송 중이거나 예약된 발송이 있어 삭제할 수 없습니다.")
|
||||
.with("tmplatIdx", tmplatIdx);
|
||||
}
|
||||
|
||||
LocalDateTime now = DateTimeUtil.nowKstLocalDateTime();
|
||||
TemplateDto snapshot = TemplateDto.from(entity);
|
||||
|
||||
Reference in New Issue
Block a user