84 lines
3.1 KiB
Java
84 lines
3.1 KiB
Java
package com.kri.kart.server.service;
|
|
|
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
|
import com.fasterxml.jackson.databind.JsonNode;
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
import com.kri.common.feature.web.BusinessException;
|
|
import com.kri.common.feature.web.RequestUserContext;
|
|
import com.kri.kart.server.common.enums.ProjectType;
|
|
import com.kri.kart.server.dto.project.ProjectRefDto;
|
|
import com.kri.kart.server.persistence.repository.LogRepository;
|
|
import lombok.RequiredArgsConstructor;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
@Service
|
|
@RequiredArgsConstructor
|
|
@Slf4j
|
|
@ConditionalOnProperty(prefix = "kri.kart.database", name = "enabled", havingValue = "true")
|
|
public class LogService {
|
|
private final LogRepository logRepository;
|
|
private final ObjectMapper objectMapper;
|
|
|
|
public void writeLoginLog(String userId, String clientIp) {
|
|
try {
|
|
logRepository.writeLoginLog(userId, clientIp);
|
|
} catch (Exception ignored) {
|
|
log.warn("로그인 이력 저장 실패");
|
|
}
|
|
}
|
|
|
|
public void writeVisitLog(
|
|
ProjectType projectType, String pkey, String content, RequestUserContext ctx) {
|
|
String projectKey = visitProjectKey(projectType, pkey);
|
|
try {
|
|
logRepository.writeVisitLog(ctx.getUserId(), projectKey, content, ctx.getClientIp());
|
|
} catch (Exception ignored) {
|
|
// Preserve legacy best-effort logging; do not log SQL, identity, IP or supplied content.
|
|
log.warn("방문 로그 기록 실패");
|
|
}
|
|
}
|
|
|
|
public void writeSiteLog(
|
|
ProjectRefDto project, String title, JsonNode content, RequestUserContext ctx) {
|
|
var pageInfo = objectMapper.createObjectNode();
|
|
// The existing frontend calls the v2 endpoint, which stores projectCode rather than PKEY.
|
|
pageInfo.put("projectCode", project.pkey());
|
|
pageInfo.put("projectType", project.projectType().getCode());
|
|
pageInfo.put("LOGTITLE", title);
|
|
pageInfo.set("LOGCONTENT", normalizeContent(content));
|
|
try {
|
|
logRepository.writeSiteLog(
|
|
ctx.getUserId(), objectMapper.writeValueAsString(pageInfo), ctx.getClientIp());
|
|
} catch (Exception ignored) {
|
|
log.warn("활동 로그 기록 실패");
|
|
}
|
|
}
|
|
|
|
private String visitProjectKey(ProjectType projectType, String pkey) {
|
|
boolean hasKey = pkey != null && !pkey.isBlank();
|
|
if (projectType == null && !hasKey) {
|
|
return "";
|
|
}
|
|
if (projectType == null || !hasKey) {
|
|
throw BusinessException.badRequest("프로젝트 유형과 키는 함께 입력하거나 모두 생략해 주세요.");
|
|
}
|
|
return new ProjectRefDto(projectType, pkey).pkey();
|
|
}
|
|
|
|
private JsonNode normalizeContent(JsonNode content) {
|
|
if (content != null && content.isTextual()) {
|
|
try {
|
|
JsonNode parsed = objectMapper.readTree(content.asText());
|
|
if (parsed != null) {
|
|
return parsed;
|
|
}
|
|
} catch (JsonProcessingException ignored) {
|
|
// Plain text is safely stored as a JSON string; never concatenate raw JSON/SQL.
|
|
}
|
|
}
|
|
return content;
|
|
}
|
|
}
|