2026-03-17 16:35:27 +07:00
|
|
|
package ru.soune.nocopy.handler;
|
|
|
|
|
|
|
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
import ru.soune.nocopy.dto.BaseRequest;
|
|
|
|
|
import ru.soune.nocopy.dto.BaseResponse;
|
|
|
|
|
import ru.soune.nocopy.dto.MessageCode;
|
|
|
|
|
import ru.soune.nocopy.dto.violation.*;
|
|
|
|
|
import ru.soune.nocopy.entity.user.AuthToken;
|
|
|
|
|
import ru.soune.nocopy.repository.AuthTokenRepository;
|
|
|
|
|
import ru.soune.nocopy.service.violation.ViolationNotionService;
|
|
|
|
|
|
2026-05-21 14:26:50 +07:00
|
|
|
import java.util.List;
|
2026-03-17 16:35:27 +07:00
|
|
|
import java.util.Optional;
|
|
|
|
|
|
|
|
|
|
@Slf4j
|
|
|
|
|
@Component
|
|
|
|
|
@RequiredArgsConstructor
|
|
|
|
|
public class ViolationNotionHandler implements RequestHandler {
|
|
|
|
|
private final ObjectMapper objectMapper;
|
|
|
|
|
|
|
|
|
|
private final ViolationNotionService notionService;
|
|
|
|
|
|
|
|
|
|
private final AuthTokenRepository authTokenRepository;
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public BaseResponse handle(BaseRequest request) {
|
|
|
|
|
try {
|
|
|
|
|
ViolationNotionRequest notionRequest = objectMapper.convertValue(
|
|
|
|
|
request.getMessageBody(), ViolationNotionRequest.class);
|
|
|
|
|
|
|
|
|
|
if (notionRequest.getToken() == null) {
|
|
|
|
|
return new BaseResponse(
|
|
|
|
|
request.getMsgId(),
|
|
|
|
|
MessageCode.INVALID_TOKEN.getCode(),
|
|
|
|
|
"Token is required",
|
2026-05-21 14:26:50 +07:00
|
|
|
"field: token");
|
2026-03-17 16:35:27 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Optional<AuthToken> tokenOptional = authTokenRepository.findByToken(notionRequest.getToken());
|
|
|
|
|
if (tokenOptional.isEmpty()) {
|
|
|
|
|
return new BaseResponse(
|
|
|
|
|
request.getMsgId(),
|
|
|
|
|
MessageCode.INVALID_TOKEN.getCode(),
|
|
|
|
|
"Invalid token",
|
2026-05-21 14:26:50 +07:00
|
|
|
notionRequest.getToken());
|
2026-03-17 16:35:27 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Long userId = tokenOptional.get().getUser().getId();
|
|
|
|
|
String action = notionRequest.getAction();
|
|
|
|
|
|
|
|
|
|
if (action == null || action.trim().isEmpty()) {
|
|
|
|
|
return new BaseResponse(
|
|
|
|
|
request.getMsgId(),
|
|
|
|
|
MessageCode.INVALID_ACTION.getCode(),
|
|
|
|
|
"Action is required",
|
2026-05-21 14:26:50 +07:00
|
|
|
List.of("get_notions", "get_notion",
|
|
|
|
|
"add_notion", "update_notion",
|
|
|
|
|
"delete_notion"));
|
2026-03-17 16:35:27 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch (action) {
|
|
|
|
|
case "get_notions":
|
|
|
|
|
return handleGetNotions(request, notionRequest, userId);
|
|
|
|
|
case "get_notion":
|
|
|
|
|
return handleGetNotion(request, notionRequest, userId);
|
|
|
|
|
case "add_notion":
|
|
|
|
|
return handleAddNotion(request, notionRequest, userId);
|
|
|
|
|
case "update_notion":
|
|
|
|
|
return handleUpdateNotion(request, notionRequest, userId);
|
|
|
|
|
case "delete_notion":
|
|
|
|
|
return handleDeleteNotion(request, notionRequest, userId);
|
|
|
|
|
default:
|
|
|
|
|
return new BaseResponse(
|
|
|
|
|
request.getMsgId(),
|
|
|
|
|
MessageCode.INVALID_ACTION.getCode(),
|
|
|
|
|
"Invalid action: " + action,
|
2026-05-21 14:26:50 +07:00
|
|
|
List.of("get_notions", "get_notion",
|
|
|
|
|
"add_notion", "update_notion",
|
|
|
|
|
"delete_notion"));
|
2026-03-17 16:35:27 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} catch (IllegalArgumentException e) {
|
2026-03-17 22:23:19 +07:00
|
|
|
log.error("Validation error: {}", e.getMessage());
|
2026-03-17 16:35:27 +07:00
|
|
|
return new BaseResponse(
|
|
|
|
|
request.getMsgId(),
|
|
|
|
|
MessageCode.VALIDATION_ERROR.getCode(),
|
|
|
|
|
e.getMessage(),
|
2026-05-21 14:26:50 +07:00
|
|
|
"Validation error: " + e.getMessage());
|
2026-03-17 16:35:27 +07:00
|
|
|
} catch (SecurityException e) {
|
2026-03-17 22:23:19 +07:00
|
|
|
log.error("Security error: {}", e.getMessage());
|
2026-03-17 16:35:27 +07:00
|
|
|
return new BaseResponse(
|
|
|
|
|
request.getMsgId(),
|
|
|
|
|
MessageCode.ACCESS_DENIED.getCode(),
|
|
|
|
|
e.getMessage(),
|
2026-05-21 14:26:50 +07:00
|
|
|
"Security error: " + e.getMessage());
|
2026-03-17 16:35:27 +07:00
|
|
|
} catch (Exception e) {
|
2026-03-17 22:23:19 +07:00
|
|
|
log.error("Unexpected error", e);
|
2026-03-17 16:35:27 +07:00
|
|
|
return new BaseResponse(
|
|
|
|
|
request.getMsgId(),
|
|
|
|
|
MessageCode.INTERNAL_ERROR.getCode(),
|
|
|
|
|
"Internal server error",
|
2026-05-21 14:26:50 +07:00
|
|
|
"Unexpected error: " + e);
|
2026-03-17 16:35:27 +07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private BaseResponse handleGetNotions(BaseRequest request, ViolationNotionRequest notionRequest, Long userId) {
|
2026-03-17 22:23:19 +07:00
|
|
|
if (notionRequest.getViolationId() == null) {
|
2026-03-17 16:35:27 +07:00
|
|
|
return new BaseResponse(
|
|
|
|
|
request.getMsgId(),
|
|
|
|
|
MessageCode.INVALID_JSON_BODY.getCode(),
|
2026-03-17 22:23:19 +07:00
|
|
|
"ViolationId is required for get_notions action",
|
2026-05-21 14:26:50 +07:00
|
|
|
"field: violation_id");
|
2026-03-17 16:35:27 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
2026-03-17 22:23:19 +07:00
|
|
|
ViolationNotionsListResponse notions = notionService.getNotionsByViolationId(
|
|
|
|
|
notionRequest.getViolationId(), userId);
|
2026-03-17 16:35:27 +07:00
|
|
|
return BaseResponse.builder()
|
|
|
|
|
.msgId(request.getMsgId())
|
|
|
|
|
.messageBody(notions)
|
2026-05-21 14:38:33 +07:00
|
|
|
.messageDesc(MessageCode.SUCCESS.getDescription())
|
|
|
|
|
.messageCode(MessageCode.SUCCESS.getCode())
|
2026-03-17 16:35:27 +07:00
|
|
|
.build();
|
|
|
|
|
} catch (IllegalArgumentException e) {
|
|
|
|
|
return new BaseResponse(
|
|
|
|
|
request.getMsgId(),
|
2026-03-17 22:23:19 +07:00
|
|
|
MessageCode.NOTION_NOT_FOUND.getCode(),
|
2026-03-17 16:35:27 +07:00
|
|
|
e.getMessage(),
|
2026-05-21 14:26:50 +07:00
|
|
|
"Unexpected error: " + e);
|
2026-03-17 16:35:27 +07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private BaseResponse handleGetNotion(BaseRequest request, ViolationNotionRequest notionRequest, Long userId) {
|
|
|
|
|
if (notionRequest.getNotionId() == null) {
|
|
|
|
|
return new BaseResponse(
|
|
|
|
|
request.getMsgId(),
|
2026-03-17 22:23:19 +07:00
|
|
|
MessageCode.INVALID_JSON_BODY.getCode(),
|
2026-03-17 16:35:27 +07:00
|
|
|
"NotionId is required for get_notion action",
|
2026-05-21 14:26:50 +07:00
|
|
|
"field: notion_id");
|
2026-03-17 16:35:27 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
ViolationNotionResponse notion = notionService.getNotionById(
|
|
|
|
|
notionRequest.getNotionId(), userId);
|
|
|
|
|
return BaseResponse.builder()
|
|
|
|
|
.msgId(request.getMsgId())
|
|
|
|
|
.messageBody(notion)
|
2026-05-21 14:38:33 +07:00
|
|
|
.messageDesc(MessageCode.SUCCESS.getDescription())
|
|
|
|
|
.messageCode(MessageCode.SUCCESS.getCode())
|
2026-03-17 16:35:27 +07:00
|
|
|
.build();
|
|
|
|
|
} catch (IllegalArgumentException e) {
|
|
|
|
|
return new BaseResponse(
|
|
|
|
|
request.getMsgId(),
|
|
|
|
|
MessageCode.NOTION_NOT_FOUND.getCode(),
|
|
|
|
|
e.getMessage(),
|
2026-05-21 14:26:50 +07:00
|
|
|
"Unexpected error: " + e);
|
2026-03-17 16:35:27 +07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private BaseResponse handleAddNotion(BaseRequest request, ViolationNotionRequest notionRequest, Long userId) {
|
2026-03-17 22:23:19 +07:00
|
|
|
if (notionRequest.getViolationId() == null) {
|
2026-03-17 16:35:27 +07:00
|
|
|
return new BaseResponse(
|
|
|
|
|
request.getMsgId(),
|
2026-03-17 22:23:19 +07:00
|
|
|
MessageCode.INVALID_JSON_BODY.getCode(),
|
|
|
|
|
"ViolationId is required for add_notion action",
|
2026-05-21 14:26:50 +07:00
|
|
|
"field: notion_id");
|
2026-03-17 16:35:27 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (notionRequest.getMessage() == null || notionRequest.getMessage().trim().isEmpty()) {
|
|
|
|
|
return new BaseResponse(
|
|
|
|
|
request.getMsgId(),
|
2026-03-17 22:23:19 +07:00
|
|
|
MessageCode.INVALID_JSON_BODY.getCode(),
|
2026-03-17 16:35:27 +07:00
|
|
|
"Message is required for add_notion action",
|
2026-05-21 14:26:50 +07:00
|
|
|
"field: message");
|
2026-03-17 16:35:27 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
ViolationNotionResponse created = notionService.createNotion(
|
2026-03-17 22:23:19 +07:00
|
|
|
notionRequest.getViolationId(), userId, notionRequest.getMessage());
|
2026-03-17 16:35:27 +07:00
|
|
|
return BaseResponse.builder()
|
|
|
|
|
.msgId(request.getMsgId())
|
|
|
|
|
.messageBody(created)
|
2026-05-21 14:38:33 +07:00
|
|
|
.messageDesc(MessageCode.SUCCESS.getDescription())
|
|
|
|
|
.messageCode(MessageCode.SUCCESS.getCode())
|
2026-03-17 16:35:27 +07:00
|
|
|
.build();
|
|
|
|
|
} catch (IllegalArgumentException e) {
|
|
|
|
|
return new BaseResponse(
|
|
|
|
|
request.getMsgId(),
|
|
|
|
|
MessageCode.NOTION_NOT_FOUND.getCode(),
|
|
|
|
|
e.getMessage(),
|
2026-05-21 14:26:50 +07:00
|
|
|
"Unexpected error: " + e);
|
2026-03-17 16:35:27 +07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private BaseResponse handleUpdateNotion(BaseRequest request, ViolationNotionRequest notionRequest, Long userId) {
|
|
|
|
|
if (notionRequest.getNotionId() == null) {
|
|
|
|
|
return new BaseResponse(
|
|
|
|
|
request.getMsgId(),
|
2026-03-17 22:23:19 +07:00
|
|
|
MessageCode.INVALID_JSON_BODY.getCode(),
|
2026-03-17 16:35:27 +07:00
|
|
|
"NotionId is required for update_notion action",
|
2026-05-21 14:26:50 +07:00
|
|
|
"field: notion_id");
|
2026-03-17 16:35:27 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (notionRequest.getMessage() == null || notionRequest.getMessage().trim().isEmpty()) {
|
|
|
|
|
return new BaseResponse(
|
|
|
|
|
request.getMsgId(),
|
2026-03-17 22:23:19 +07:00
|
|
|
MessageCode.INVALID_JSON_BODY.getCode(),
|
2026-03-17 16:35:27 +07:00
|
|
|
"Message is required for update_notion action",
|
2026-05-21 14:26:50 +07:00
|
|
|
"field: message");
|
2026-03-17 16:35:27 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
ViolationNotionResponse updated = notionService.updateNotion(
|
|
|
|
|
notionRequest.getNotionId(), userId, notionRequest.getMessage());
|
|
|
|
|
return BaseResponse.builder()
|
|
|
|
|
.msgId(request.getMsgId())
|
|
|
|
|
.messageBody(updated)
|
2026-05-21 14:38:33 +07:00
|
|
|
.messageDesc(MessageCode.SUCCESS.getDescription())
|
|
|
|
|
.messageCode(MessageCode.SUCCESS.getCode())
|
2026-03-17 16:35:27 +07:00
|
|
|
.build();
|
|
|
|
|
} catch (IllegalArgumentException e) {
|
|
|
|
|
return new BaseResponse(
|
|
|
|
|
request.getMsgId(),
|
|
|
|
|
MessageCode.NOTION_NOT_FOUND.getCode(),
|
|
|
|
|
e.getMessage(),
|
2026-05-21 14:26:50 +07:00
|
|
|
"IllegalArgumentException error: " + e);
|
2026-03-17 16:35:27 +07:00
|
|
|
} catch (SecurityException e) {
|
|
|
|
|
return new BaseResponse(
|
|
|
|
|
request.getMsgId(),
|
|
|
|
|
MessageCode.ACCESS_DENIED.getCode(),
|
|
|
|
|
e.getMessage(),
|
2026-05-21 14:26:50 +07:00
|
|
|
"SecurityException error: " + e);
|
2026-03-17 16:35:27 +07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private BaseResponse handleDeleteNotion(BaseRequest request, ViolationNotionRequest notionRequest, Long userId) {
|
|
|
|
|
if (notionRequest.getNotionId() == null) {
|
|
|
|
|
return new BaseResponse(
|
|
|
|
|
request.getMsgId(),
|
2026-03-17 22:23:19 +07:00
|
|
|
MessageCode.INVALID_JSON_BODY.getCode(),
|
2026-03-17 16:35:27 +07:00
|
|
|
"NotionId is required for delete_notion action",
|
2026-05-21 14:26:50 +07:00
|
|
|
"field: notion_id");
|
2026-03-17 16:35:27 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
notionService.deleteNotion(notionRequest.getNotionId(), userId);
|
|
|
|
|
DeleteNotionResponse deleteResponse = DeleteNotionResponse.builder()
|
|
|
|
|
.notionId(notionRequest.getNotionId())
|
|
|
|
|
.message("Notion deleted successfully")
|
|
|
|
|
.deleted(true)
|
|
|
|
|
.build();
|
2026-05-21 14:38:33 +07:00
|
|
|
|
2026-03-17 16:35:27 +07:00
|
|
|
return BaseResponse.builder()
|
|
|
|
|
.msgId(request.getMsgId())
|
|
|
|
|
.messageBody(deleteResponse)
|
2026-05-21 14:38:33 +07:00
|
|
|
.messageDesc(MessageCode.SUCCESS.getDescription())
|
|
|
|
|
.messageCode(MessageCode.SUCCESS.getCode())
|
2026-03-17 16:35:27 +07:00
|
|
|
.build();
|
|
|
|
|
} catch (IllegalArgumentException e) {
|
|
|
|
|
return new BaseResponse(
|
|
|
|
|
request.getMsgId(),
|
|
|
|
|
MessageCode.NOTION_NOT_FOUND.getCode(),
|
|
|
|
|
e.getMessage(),
|
|
|
|
|
null);
|
|
|
|
|
} catch (SecurityException e) {
|
|
|
|
|
return new BaseResponse(
|
|
|
|
|
request.getMsgId(),
|
|
|
|
|
MessageCode.ACCESS_DENIED.getCode(),
|
|
|
|
|
e.getMessage(),
|
2026-03-17 22:23:19 +07:00
|
|
|
null);
|
2026-03-17 16:35:27 +07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|