260219_01_ENH: 공용라이브러리 버전업, 알림톡 발송 구현 완료
This commit is contained in:
@@ -0,0 +1,168 @@
|
||||
package com.kri.notihub.server.service;
|
||||
|
||||
import com.kri.common.datetime.DateTimeUtil;
|
||||
import com.kri.common.util.CommonUtil;
|
||||
import com.kri.common.util.NumericUtil;
|
||||
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.PringoJsonSendRequestDto;
|
||||
import com.kri.notihub.server.common.external.pringo.dto.PringoJsonSendResponseDto;
|
||||
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.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 SendService {
|
||||
|
||||
private final PringoApiClient pringoApiClient;
|
||||
private final EmailResultPoller emailResultPoller;
|
||||
|
||||
protected AbstractPringoSendService(
|
||||
TemplateRepository templateRepository,
|
||||
BulkSendRepository bulkSendRepository,
|
||||
SendRepository sendRepository,
|
||||
TemplateService templateService,
|
||||
PringoApiClient pringoApiClient,
|
||||
EmailResultPoller emailResultPoller
|
||||
) {
|
||||
super(templateRepository, bulkSendRepository, sendRepository, templateService);
|
||||
this.pringoApiClient = pringoApiClient;
|
||||
this.emailResultPoller = emailResultPoller;
|
||||
}
|
||||
|
||||
protected abstract ChannelType defaultChannelType();
|
||||
|
||||
@Override
|
||||
protected final ChannelType getChannelType() {
|
||||
return defaultChannelType();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected SendInternalResultDto doSendInternal(
|
||||
Boolean isResve,
|
||||
String bulkSendKey,
|
||||
SendType sendType, //Sms에서 사용 Email에서는 의미 없음
|
||||
String sendEml,
|
||||
String sendNm,
|
||||
String msgTitle,
|
||||
String msgBody,
|
||||
DestInfoDto dest
|
||||
) {
|
||||
if (Boolean.TRUE.equals(isResve)) {
|
||||
//예약 용 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 {
|
||||
PringoJsonSendRequestDto pringoReq = buildPringoSendRequest(sendEml, sendNm, msgTitle,
|
||||
msgBody, dest);
|
||||
return sendPringoEmail(pringoReq);
|
||||
}
|
||||
}
|
||||
|
||||
public SendInternalResultDto sendPringoEmail(PringoJsonSendRequestDto pringoReq) {
|
||||
PringoJsonSendResponseDto pringoRes = null;
|
||||
try {
|
||||
pringoRes = pringoApiClient.jsonSend(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.getReturnKey() != 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 PringoJsonSendRequestDto buildPringoSendRequest(
|
||||
String sendEml,
|
||||
String sendNm,
|
||||
String msgTitle,
|
||||
String msgBody,
|
||||
DestInfoDto dest
|
||||
) {
|
||||
Map<String, Object> vars = new HashMap<>();
|
||||
if (dest.getMsgParam() != null) {
|
||||
vars.putAll(dest.getMsgParam());
|
||||
}
|
||||
|
||||
return PringoJsonSendRequestDto.builder()
|
||||
.sender(sendEml)
|
||||
.senderName(Optional.ofNullable(sendNm).orElse(""))
|
||||
.recipient(dest.getDestEml())
|
||||
.recipientName(Optional.ofNullable(dest.getDestNm()).orElse(""))
|
||||
.subject(Optional.ofNullable(msgTitle).orElse(""))
|
||||
.body(Optional.ofNullable(msgBody).orElse(""))
|
||||
.vars(vars)
|
||||
.build();
|
||||
}
|
||||
|
||||
private String resolveEmailCmid(PringoJsonSendResponseDto pringoRes) {
|
||||
if (pringoRes != null && pringoRes.isSuccess() && pringoRes.getReturnKey() != null) {
|
||||
return String.valueOf(pringoRes.getReturnKey());
|
||||
}
|
||||
// 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());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user