196 lines
6.9 KiB
Java
196 lines
6.9 KiB
Java
package com.kri.notihub.server.service;
|
|
|
|
import com.kri.common.feature.datetime.DateTimeUtil;
|
|
import com.kri.common.feature.util.CommonUtil;
|
|
import com.kri.common.feature.util.NumericUtil;
|
|
import com.kri.common.feature.util.StringUtil;
|
|
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.external.pringo.PringoApiClient;
|
|
import com.kri.notihub.server.common.external.pringo.dto.PringoSendRequestDto;
|
|
import com.kri.notihub.server.common.external.pringo.dto.PringoSendResponseDto;
|
|
import com.kri.notihub.server.dto.send.DestInfoDto;
|
|
import com.kri.notihub.server.dto.send.SendInternalResultDto;
|
|
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.time.format.DateTimeFormatter;
|
|
import java.util.HashMap;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
import java.util.Optional;
|
|
import java.util.concurrent.ThreadLocalRandom;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
@Slf4j
|
|
public abstract class AbstractPringoSendService extends AbstractSendService {
|
|
|
|
private final PringoApiClient pringoApiClient;
|
|
private final EmailResultPoller emailResultPoller;
|
|
private final EmailBodyRenderer emailBodyRenderer;
|
|
private final EmailAttachmentService emailAttachmentService;
|
|
|
|
protected AbstractPringoSendService(
|
|
SendBulkJdbcWriter sendBulkJdbcWriter,
|
|
TemplateRepository templateRepository,
|
|
BulkSendRepository bulkSendRepository,
|
|
SendRepository sendRepository,
|
|
TemplateService templateService,
|
|
PringoApiClient pringoApiClient,
|
|
EmailResultPoller emailResultPoller,
|
|
EmailBodyRenderer emailBodyRenderer,
|
|
EmailAttachmentService emailAttachmentService,
|
|
PhoneVerificationService phoneVerificationService
|
|
) {
|
|
super(sendBulkJdbcWriter, templateRepository, bulkSendRepository, sendRepository, templateService,
|
|
phoneVerificationService);
|
|
this.pringoApiClient = pringoApiClient;
|
|
this.emailResultPoller = emailResultPoller;
|
|
this.emailBodyRenderer = emailBodyRenderer;
|
|
this.emailAttachmentService = emailAttachmentService;
|
|
}
|
|
|
|
protected abstract ChannelType defaultChannelType();
|
|
|
|
@Override
|
|
protected final ChannelType getChannelType() {
|
|
return defaultChannelType();
|
|
}
|
|
|
|
@Override
|
|
protected String renderMessageBody(String body, Map<String, String> msgParam) {
|
|
return emailBodyRenderer.render(body, msgParam);
|
|
}
|
|
|
|
@Override
|
|
protected SendInternalResultDto doSendInternal(
|
|
Boolean isResve,
|
|
String bulkSendKey,
|
|
SendType sendType, //Sms에서 사용 Email에서는 의미 없음
|
|
Long tmplatIdx,
|
|
String atchFl,
|
|
String sendEml,
|
|
String sendNm,
|
|
String msgTitle,
|
|
String msgBody,
|
|
DestInfoDto dest
|
|
) {
|
|
if (Boolean.TRUE.equals(isResve) || !StringUtil.isNullOrBlank (bulkSendKey)) {
|
|
//예약 또는 bulk send 용 cmid 생성 추후 실제 발송 후 갱신
|
|
String ts = DateTimeUtil.nowKstLocalDateTime()
|
|
.format(DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS"));
|
|
|
|
int r = ThreadLocalRandom.current().nextInt(36 * 36 * 36);
|
|
String suffix = String.format("%3s", Integer.toString(r, 36)).replace(' ', '0');
|
|
|
|
String cmid = NotihubConst.EMAIL_RESERVATION_PREFIX + ts + suffix;
|
|
|
|
return SendInternalResultDto.of(cmid, SendStatusType.STANDBY, NotihubConst.EMAIL_STANDBY);
|
|
} else {
|
|
// 단건도 발송 중 상태를 먼저 저장하고, 커밋 후 트랜잭션 밖에서 외부 발송을 호출한다.
|
|
return SendInternalResultDto.of(CommonUtil.uuid32(), SendStatusType.STANDBY,
|
|
NotihubConst.EMAIL_SENDING);
|
|
}
|
|
}
|
|
|
|
public SendInternalResultDto sendPringoEmail(PringoSendRequestDto pringoReq) {
|
|
PringoSendResponseDto pringoRes = null;
|
|
try {
|
|
if(StringUtil.isNullOrBlank(pringoReq.getRecipientName())){
|
|
pringoReq.setRecipientName(pringoReq.getRecipient());
|
|
}
|
|
// pringoRes = pringoApiClient.jsonSend(pringoReq);
|
|
pringoRes = pringoApiClient.parameterSend(pringoReq);
|
|
} catch (Exception e) {
|
|
log.warn("Pringo json-send failed (pringoReq={})", pringoReq, e);
|
|
}
|
|
|
|
String cmid = resolveEmailCmid(pringoRes);
|
|
SendStatusType sendStatus;
|
|
short resultCode;
|
|
|
|
if (pringoRes != null && pringoRes.isSuccess() && pringoRes.returnKey() != null) {
|
|
//발송 요청에 성공한 것이지 발송이 성공이 아니므로 STANDBY 유지
|
|
sendStatus = SendStatusType.STANDBY;
|
|
resultCode = NotihubConst.EMAIL_PENDING;
|
|
} else {
|
|
sendStatus = SendStatusType.FAIL;
|
|
resultCode = NotihubConst.EMAIL_SEND_FAIL;
|
|
}
|
|
|
|
return SendInternalResultDto.of(cmid, sendStatus, resultCode);
|
|
}
|
|
|
|
@Override
|
|
public void afterPersistSendEntities(List<TSendEntity> persisted) {
|
|
if (persisted == null || persisted.isEmpty()) {
|
|
return;
|
|
}
|
|
|
|
for (TSendEntity e : persisted) {
|
|
if (validateSendEntity(e)) {
|
|
emailResultPoller.enqueue(e.getCmid());
|
|
}
|
|
}
|
|
}
|
|
|
|
public PringoSendRequestDto buildPringoSendRequest(
|
|
String sendEml,
|
|
String sendNm,
|
|
String msgTitle,
|
|
String msgBody,
|
|
Long tmplatIdx,
|
|
String atchFl,
|
|
DestInfoDto dest
|
|
) {
|
|
Map<String, Object> vars = new HashMap<>();
|
|
if (dest.msgParam() != null) {
|
|
vars.putAll(dest.msgParam());
|
|
}
|
|
|
|
return PringoSendRequestDto.builder()
|
|
.sender(sendEml)
|
|
.senderName(Optional.ofNullable(sendNm).orElse(""))
|
|
.recipient(dest.destEml())
|
|
.recipientName(Optional.ofNullable(dest.destNm()).orElse(dest.destEml()))
|
|
.subject(Optional.ofNullable(msgTitle).orElse(""))
|
|
.body(Optional.ofNullable(msgBody).orElse(""))
|
|
.attachments(tmplatIdx == null
|
|
? List.of()
|
|
: emailAttachmentService.load(tmplatIdx, atchFl))
|
|
.vars(vars)
|
|
.build();
|
|
}
|
|
|
|
private String resolveEmailCmid(PringoSendResponseDto pringoRes) {
|
|
if (pringoRes != null && pringoRes.isSuccess() && pringoRes.returnKey() != null) {
|
|
return String.valueOf(pringoRes.returnKey());
|
|
}
|
|
// return_key 미수신 케이스는 SendFau
|
|
return CommonUtil.uuid32();
|
|
}
|
|
|
|
private boolean validateSendEntity(TSendEntity e) {
|
|
if (e == null) {
|
|
return false;
|
|
}
|
|
|
|
// Pringo return polling이 필요한 상태만
|
|
if (e.getResultCode() == null || e.getResultCode() != NotihubConst.EMAIL_PENDING) {
|
|
return false;
|
|
}
|
|
|
|
//예약은 갱신용 큐에 넣지 않음
|
|
if (e.getCmid().startsWith(NotihubConst.EMAIL_RESERVATION_PREFIX)) {
|
|
return false;
|
|
}
|
|
|
|
// return_key(숫자)인 경우만
|
|
return NumericUtil.isNumeric(e.getCmid());
|
|
}
|
|
}
|