Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.commonLib.common.request.CommonPageRequest;
import com.commonLib.common.response.CommonPageResponse;
import com.commonLib.common.utils.PagingUtils;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.hubEleven.notification.ai.domain.repository.AiRequestLogRepository;
import com.hubEleven.notification.ai.exception.AiErrorCode;
Expand All @@ -20,7 +21,6 @@
import com.hubEleven.notification.slack.domain.vo.SlackMessageContext;
import com.hubEleven.notification.slack.domain.vo.SlackMessageItem;
import com.hubEleven.notification.slack.exception.SlackErrorCode;
import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;
import java.util.UUID;
Expand Down Expand Up @@ -48,11 +48,11 @@ public class SlackServiceImpl implements SlackService {
public SlackMessageResult createMessage(CreateSlackMessageCommand command) {
slackValidator.slackCreate(command.orderId());

ResponsePayload payload = findAiPayloadOrThrow(command.orderId());
SlackMessageContext context = buildSlackMessageContext(command);

SlackMessageContext context = buildSlackMessageContext(command.orderId(), payload);
AiPayload aiPayload = findAiPayloadOrThrow(command.orderId());

String formattedMessage = slackDomainService.formatMessage(context, payload.messageBody);
String formattedMessage = slackDomainService.formatMessage(context, aiPayload.messageBody());

SlackMessage slackMessage =
SlackMessage.create(
Expand Down Expand Up @@ -124,25 +124,36 @@ public CommonPageResponse<SlackMessageResult> searchMessages(
SlackMessageResult::from);
}

private ResponsePayload findAiPayloadOrThrow(UUID orderId) {
private AiPayload findAiPayloadOrThrow(UUID orderId) {
return aiRequestLogRepository
.findByOrderId(orderId)
.map(
logEntry -> {
String messageBody = logEntry.getMessageBody();
String finalDeadline = logEntry.getFinalDispatchDeadline();

if (messageBody != null && !messageBody.isBlank()) {
return new AiPayload(finalDeadline, messageBody);
}

String raw = logEntry.getRawResponse();
if (raw == null || raw.isBlank()) {
throw new GlobalException(AiErrorCode.AI_RESPONSE_PARSE_FAIL);
}

String cleaned = cleanJsonResponse(raw);

try {
ResponsePayload payload = objectMapper.readValue(cleaned, ResponsePayload.class);
JsonNode node = objectMapper.readTree(cleaned);

if (payload.finalDispatchDeadline == null
|| payload.finalDispatchDeadline.isBlank()) {
throw new GlobalException(AiErrorCode.AI_RESPONSE_PARSE_FAIL);
}
if (payload.messageBody == null || payload.messageBody.isBlank()) {
String parsedDeadline = node.path("finalDispatchDeadline").asText(null);
String parsedBody = node.path("messageBody").asText(null);

if (parsedBody == null || parsedBody.isBlank()) {
throw new GlobalException(AiErrorCode.AI_RESPONSE_PARSE_FAIL);
}
return payload;

return new AiPayload(parsedDeadline, parsedBody);
} catch (Exception e) {
throw new GlobalException(AiErrorCode.AI_RESPONSE_PARSE_FAIL);
}
Expand Down Expand Up @@ -170,47 +181,34 @@ private String cleanJsonResponse(String rawJson) {
return cleaned.trim();
}

private SlackMessageContext buildSlackMessageContext(UUID orderId, ResponsePayload payload) {
LocalDateTime orderDateTime = parseLocalDateTime(payload.orderDateTime);

List<String> viaHubs = (payload.viaHubs == null) ? Collections.emptyList() : payload.viaHubs;
private SlackMessageContext buildSlackMessageContext(CreateSlackMessageCommand cmd) {
List<String> viaHubs = (cmd.viaHubs() == null) ? Collections.emptyList() : cmd.viaHubs();

List<SlackMessageItem> items =
(payload.items == null)
(cmd.items() == null)
? Collections.emptyList()
: payload.items.stream()
: cmd.items().stream()
.map(
it ->
new SlackMessageItem(
nullToEmpty(it.name), it.quantity, nullToEmpty(it.note)))
nullToEmpty(it.name()), it.quantity(), nullToEmpty(it.note())))
.toList();

return new SlackMessageContext(
orderId,
blankToNull(payload.customerName),
blankToNull(payload.customerEmail),
orderDateTime,
blankToNull(payload.sourceHub),
cmd.orderId(),
blankToNull(cmd.customerName()),
blankToNull(cmd.customerEmail()),
cmd.orderDateTime(),
blankToNull(cmd.sourceHub()),
viaHubs,
blankToNull(payload.destinationHub),
blankToNull(payload.destinationAddress),
blankToNull(payload.requestNote),
blankToNull(payload.deliveryManagerName),
blankToNull(payload.deliveryManagerEmail),
blankToNull(cmd.destinationHub()),
blankToNull(cmd.destinationAddress()),
blankToNull(cmd.requestNote()),
blankToNull(cmd.deliveryManagerName()),
blankToNull(cmd.deliveryManagerEmail()),
items);
}

private LocalDateTime parseLocalDateTime(String value) {
if (value == null || value.isBlank()) {
return null;
}
try {
return LocalDateTime.parse(value.trim());
} catch (Exception ignore) {
return null;
}
}

private String blankToNull(String s) {
return (s == null || s.isBlank()) ? null : s;
}
Expand All @@ -219,26 +217,5 @@ private String nullToEmpty(String s) {
return (s == null) ? "" : s;
}

private static final class ResponsePayload {
public String finalDispatchDeadline;
public String messageBody;

public String customerName;
public String customerEmail;
public String orderDateTime;
public String sourceHub;
public List<String> viaHubs;
public String destinationHub;
public String destinationAddress;
public String requestNote;
public String deliveryManagerName;
public String deliveryManagerEmail;
public List<ItemPayload> items;
}

private static final class ItemPayload {
public String name;
public int quantity;
public String note;
}
private record AiPayload(String finalDispatchDeadline, String messageBody) {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.event.TransactionPhase;
import org.springframework.transaction.event.TransactionalEventListener;

Expand All @@ -23,6 +25,7 @@ public class SlackDomainEventHandler {
private final SlackClient slackClient;

@TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT)
@Transactional(propagation = Propagation.REQUIRES_NEW)
public void handle(SlackMessageSavedEvent event) {
UUID messageId = event.messageId();

Expand Down