Merge branch 'dev' into NCBACK-35
# Conflicts: # src/main/java/ru/soune/nocopy/controller/ApiController.java # src/main/java/ru/soune/nocopy/repository/FileEntityRepository.java # src/main/java/ru/soune/nocopy/service/file/impl/FileUploadServiceImpl.java
This commit is contained in:
@@ -77,10 +77,9 @@ public class ComplaintEntityHandler implements RequestHandler {
|
||||
private BaseResponse handleGetAll(Integer msgId, ComplaintRequest req) {
|
||||
Pageable pageable = PageRequest.of(
|
||||
req.getPage() != null ? req.getPage() : 0,
|
||||
req.getSize() != null ? req.getSize() : 20,
|
||||
req.getSize() != null ? req.getSize() : 5,
|
||||
Sort.by(Sort.Direction.fromString(req.getSortDirection() != null ? req.getSortDirection() : "desc"),
|
||||
req.getSortBy() != null ? req.getSortBy() : "createdAt")
|
||||
);
|
||||
req.getSortBy() != null ? req.getSortBy() : "updatedAt"));
|
||||
|
||||
var page = complaintService.getAllComplaints(pageable);
|
||||
Map<String, Object> data = new HashMap<>();
|
||||
|
||||
@@ -4,6 +4,9 @@ import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.domain.Sort;
|
||||
import org.springframework.stereotype.Component;
|
||||
import ru.soune.nocopy.dto.*;
|
||||
import ru.soune.nocopy.dto.file.*;
|
||||
@@ -225,9 +228,55 @@ public class FileEntityHandler implements RequestHandler {
|
||||
return new BaseResponse(request.getMsgId(),
|
||||
MessageCode.SUCCESS.getCode(),
|
||||
MessageCode.SUCCESS.getDescription(),
|
||||
FileEntityResponse.builder().status(newStatus).build());
|
||||
Map.of("file_status", newStatus, "file_id", fileId));
|
||||
}
|
||||
|
||||
// private BaseResponse handleSearchFiles(BaseRequest request, FileEntityRequest fileRequest) {
|
||||
// try {
|
||||
// Long userId = authService.useUserAuthToken(fileRequest.getToken());
|
||||
// int page = fileRequest.getPage() != null ? fileRequest.getPage() : 1;
|
||||
// int pageSize = fileRequest.getPageSize() != null ? fileRequest.getPageSize() : 20;
|
||||
// String fileRequestSortBy = fileRequest.getSortBy();
|
||||
// String sortBy = !fileRequestSortBy.equals("") ? fileRequest.getSortBy(): "createdAt";
|
||||
// sortBy = sortBy.equals("fileName") ? "originalFileName" : sortBy;
|
||||
//
|
||||
// String sortOrder = fileRequest.getSortOrder();
|
||||
// Sort.Direction direction = (sortOrder != null && sortOrder.equalsIgnoreCase("desc"))
|
||||
// ? Sort.Direction.DESC
|
||||
// : Sort.Direction.ASC;
|
||||
// Sort sort = Sort.by(direction, sortBy);
|
||||
//
|
||||
// Pageable pageable = PageRequest.of(page, pageSize, sort);
|
||||
// FileResponse userFiles = fileEntityService.getUserFiles(userId, pageable, request.getVersion());
|
||||
//
|
||||
// FileListResponse response = FileListResponse.builder()
|
||||
// .files(userFiles.getFiles())
|
||||
// .totalCount(userFiles.getTotalCount())
|
||||
// .totalSize(userFiles.getTotalSize())
|
||||
// .formattedTotalSize(fileEntityService.formatFileSize(userFiles.getTotalSize()))
|
||||
// .page(page)
|
||||
// .pageSize(pageSize)
|
||||
// .sortBy(sortBy)
|
||||
// .sortOrder(sortOrder)
|
||||
// .build();
|
||||
//
|
||||
// return new BaseResponse(request.getMsgId(),
|
||||
// MessageCode.SUCCESS.getCode(),
|
||||
// MessageCode.SUCCESS.getDescription(),
|
||||
// response);
|
||||
//
|
||||
// } catch (NotFoundAuthToken e) {
|
||||
// return new BaseResponse(request.getMsgId(),
|
||||
// MessageCode.INVALID_TOKEN.getCode(),
|
||||
// "Authentication required", null);
|
||||
// } catch (Exception e) {
|
||||
// log.error("Error searching files", e);
|
||||
// return new BaseResponse(request.getMsgId(),
|
||||
// MessageCode.FILE_UPLOAD_ERROR.getCode(),
|
||||
// "Failed to search files: " + e.getMessage(), null);
|
||||
// }
|
||||
// }
|
||||
|
||||
private BaseResponse handleSearchFiles(BaseRequest request, FileEntityRequest fileRequest) {
|
||||
try {
|
||||
Long userId = authService.useUserAuthToken(fileRequest.getToken());
|
||||
@@ -292,6 +341,73 @@ public class FileEntityHandler implements RequestHandler {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* private BaseResponse handleSearchFiles(BaseRequest request, FileEntityRequest fileRequest) {
|
||||
* try {
|
||||
* Long userId = authService.useUserAuthToken(fileRequest.getToken());
|
||||
* int page = fileRequest.getPage() != null ? fileRequest.getPage() : 1;
|
||||
* int pageSize = fileRequest.getPageSize() != null ? fileRequest.getPageSize() : 20;
|
||||
*
|
||||
* String sortBy = fileRequest.getSortBy() != null ? fileRequest.getSortBy() : "fileName";
|
||||
* SortOrder sortOrder = SortOrder.fromString(fileRequest.getSortOrder());
|
||||
*
|
||||
* FileResponse allFiles = fileEntityService.getUserFiles(userId, 1, 1000, request.getVersion());
|
||||
*
|
||||
* String searchQuery = fileRequest.getQuery() != null ? fileRequest.getQuery().toLowerCase().trim() : "";
|
||||
* String[] searchTerms = searchQuery.split("\\s+");
|
||||
* String filterType = fileRequest.getType();
|
||||
* String dateFilter = fileRequest.getDateFilter();
|
||||
*
|
||||
* List<FileEntityResponse> filteredFiles = allFiles.getFiles().stream()
|
||||
* .filter(f -> matchesSearch(f, searchTerms, searchQuery, filterType))
|
||||
* .filter(f -> matchesDateFilter(f, dateFilter))
|
||||
* .collect(Collectors.toList());
|
||||
*
|
||||
* Comparator<FileEntityResponse> comparator = getComparator(sortBy, sortOrder);
|
||||
* filteredFiles.sort(comparator);
|
||||
*
|
||||
* int start = (page - 1) * pageSize;
|
||||
* int end = Math.min(start + pageSize, filteredFiles.size());
|
||||
*
|
||||
* if (start >= filteredFiles.size()) {
|
||||
* return createEmptyResponse(request, page, pageSize, sortBy, sortOrder.getValue());
|
||||
* }
|
||||
*
|
||||
* List<FileEntityResponse> paginatedFiles = filteredFiles.subList(start, end);
|
||||
* long totalSize = paginatedFiles.stream()
|
||||
* .mapToLong(FileEntityResponse::getFileSize)
|
||||
* .sum();
|
||||
*
|
||||
* FileListResponse response = FileListResponse.builder()
|
||||
* .files(paginatedFiles)
|
||||
* .totalCount(filteredFiles.size())
|
||||
* .totalSize(totalSize)
|
||||
* .formattedTotalSize(fileEntityService.formatFileSize(totalSize))
|
||||
* .page(page)
|
||||
* .pageSize(pageSize)
|
||||
* .sortBy(sortBy)
|
||||
* .sortOrder(sortOrder.getValue())
|
||||
* .build();
|
||||
*
|
||||
* return new BaseResponse(request.getMsgId(),
|
||||
* MessageCode.SUCCESS.getCode(),
|
||||
* MessageCode.SUCCESS.getDescription(),
|
||||
* response);
|
||||
*
|
||||
* } catch (NotFoundAuthToken e) {
|
||||
* return new BaseResponse(request.getMsgId(),
|
||||
* MessageCode.INVALID_TOKEN.getCode(),
|
||||
* "Authentication required", null);
|
||||
* } catch (Exception e) {
|
||||
* log.error("Error searching files", e);
|
||||
* return new BaseResponse(request.getMsgId(),
|
||||
* MessageCode.FILE_UPLOAD_ERROR.getCode(),
|
||||
* "Failed to search files: " + e.getMessage(), null);
|
||||
* }
|
||||
* }
|
||||
*/
|
||||
|
||||
private boolean matchesDateFilter(FileEntityResponse file, String dateFilter) {
|
||||
if (dateFilter == null || dateFilter.isEmpty()) {
|
||||
return true;
|
||||
@@ -423,6 +539,12 @@ public class FileEntityHandler implements RequestHandler {
|
||||
Comparator.nullsLast(Comparator.naturalOrder())
|
||||
);
|
||||
break;
|
||||
case "monitoring":
|
||||
comparator = Comparator.comparing(
|
||||
FileEntityResponse::getMonitoring,
|
||||
Comparator.nullsLast(Comparator.naturalOrder())
|
||||
);
|
||||
break;
|
||||
|
||||
default:
|
||||
comparator = Comparator.comparing(
|
||||
|
||||
@@ -11,6 +11,7 @@ import ru.soune.nocopy.dto.MessageCode;
|
||||
import ru.soune.nocopy.dto.file.ImageSearchRequest;
|
||||
import ru.soune.nocopy.dto.file.YandexSearchResponse;
|
||||
import ru.soune.nocopy.entity.file.FileEntity;
|
||||
import ru.soune.nocopy.entity.tokenoperation.OperationType;
|
||||
import ru.soune.nocopy.exception.NotValidFieldException;
|
||||
import ru.soune.nocopy.repository.FileEntityRepository;
|
||||
import ru.soune.nocopy.service.file.CheckCounterService;
|
||||
@@ -117,7 +118,8 @@ public class ImageFoundRequestHandler implements RequestHandler {
|
||||
log.info("Results only from Yandex: {} images", allUniqueImages.size());
|
||||
}
|
||||
|
||||
tariffInfoService.writeOffTokens(fileEntity.getUserId(), TariffConstants.TOKEN_VALUE_FOR_SEARCH);
|
||||
tariffInfoService.writeOffTokens(fileEntity.getUserId(), TariffConstants.TOKEN_VALUE_FOR_SEARCH,
|
||||
OperationType.SEARCH);
|
||||
checkCounterService.incrementCheckCount(fileEntity.getUserId(), fileEntity.getMimeType());
|
||||
|
||||
int page = imageSearchRequest.getPage() != null ? imageSearchRequest.getPage() : 1;
|
||||
|
||||
@@ -0,0 +1,200 @@
|
||||
package ru.soune.nocopy.handler;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import jakarta.mail.MessagingException;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.data.domain.Page;
|
||||
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.complaint.LawCaseRequest;
|
||||
import ru.soune.nocopy.dto.complaint.LawCaseResponse;
|
||||
import ru.soune.nocopy.dto.complaint.PaginatedResponse;
|
||||
import ru.soune.nocopy.entity.complaint.LawCase;
|
||||
import ru.soune.nocopy.entity.complaint.LawCasePriority;
|
||||
import ru.soune.nocopy.entity.complaint.LawCaseType;
|
||||
import ru.soune.nocopy.entity.tokenoperation.OperationType;
|
||||
import ru.soune.nocopy.entity.user.User;
|
||||
import ru.soune.nocopy.exception.NotValidFieldException;
|
||||
import ru.soune.nocopy.exception.TariffNotFoundException;
|
||||
import ru.soune.nocopy.exception.UserNotFoundException;
|
||||
import ru.soune.nocopy.repository.UserRepository;
|
||||
import ru.soune.nocopy.service.complaint.LawCaseService;
|
||||
import ru.soune.nocopy.service.register.AuthService;
|
||||
import ru.soune.nocopy.service.tariff.TariffConstants;
|
||||
import ru.soune.nocopy.service.tariff.TariffInfoService;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Slf4j
|
||||
@Component
|
||||
@RequiredArgsConstructor
|
||||
public class LawCaseHandler implements RequestHandler {
|
||||
|
||||
private final LawCaseService lawCaseService;
|
||||
|
||||
private final AuthService authService;
|
||||
|
||||
private final ObjectMapper objectMapper;
|
||||
|
||||
private final UserRepository userRepository;
|
||||
|
||||
private final TariffInfoService tariffInfoService;
|
||||
|
||||
@Override
|
||||
public BaseResponse handle(BaseRequest request) throws Exception {
|
||||
|
||||
LawCaseRequest lawCaseRequest = objectMapper.convertValue(request.getMessageBody(),
|
||||
LawCaseRequest.class);
|
||||
Long userId = authService.useUserAuthToken(lawCaseRequest.getToken());
|
||||
String action = lawCaseRequest.getAction();
|
||||
|
||||
return switch (action) {
|
||||
case "get_all" -> getAllUserLawCases(userId, lawCaseRequest, request);
|
||||
case "get_byId" -> getLawCaseById(lawCaseRequest.getId(), request);
|
||||
case "create" -> createLawCase(userId, lawCaseRequest, request);
|
||||
case "update_priority" -> updatePriority(lawCaseRequest, request);
|
||||
case "update_type" -> updateType(lawCaseRequest, request);
|
||||
case "update_amount" -> updateAmount(lawCaseRequest, request);
|
||||
case "delete" -> deleteLawCase(lawCaseRequest.getId(), request);
|
||||
default -> new BaseResponse(
|
||||
request.getMsgId(),
|
||||
MessageCode.INVALID_ACTION.getCode(),
|
||||
MessageCode.INVALID_ACTION.getDescription(),
|
||||
Map.of("actions", "get_all, get_byId, create, update_priority, update_type, " +
|
||||
"update_amount, delete")
|
||||
);
|
||||
};
|
||||
}
|
||||
|
||||
private BaseResponse getAllUserLawCases(Long userId, LawCaseRequest lawCaseRequest, BaseRequest request) {
|
||||
Page<LawCase> lawCases = lawCaseService.getAllUserLawCases(
|
||||
userId,
|
||||
lawCaseRequest.getPageSize(),
|
||||
lawCaseRequest.getPageNumber(),
|
||||
lawCaseRequest.getFilterType(),
|
||||
lawCaseRequest.getFilterLawyer(),
|
||||
lawCaseRequest.getFilterPriority(),
|
||||
lawCaseRequest.getViolationId(),
|
||||
lawCaseRequest.getSortBy(),
|
||||
lawCaseRequest.getSortDir());
|
||||
|
||||
List<LawCaseResponse> responses = lawCases.getContent().stream()
|
||||
.map(LawCaseResponse::fromEntity)
|
||||
.collect(Collectors.toList());
|
||||
|
||||
PaginatedResponse<LawCaseResponse> paginatedResponse = new PaginatedResponse<>(
|
||||
responses,
|
||||
lawCases.getTotalElements(),
|
||||
lawCases.getTotalPages(),
|
||||
lawCases.getNumber(),
|
||||
lawCases.getSize(),
|
||||
lawCases.isFirst(),
|
||||
lawCases.isLast());
|
||||
|
||||
return new BaseResponse(request.getMsgId(), MessageCode.SUCCESS.getCode(),
|
||||
MessageCode.SUCCESS.getDescription(), paginatedResponse);
|
||||
}
|
||||
|
||||
private BaseResponse getLawCaseById(Long id, BaseRequest request) {
|
||||
LawCase lawCase = lawCaseService.getById(id);
|
||||
|
||||
if (lawCase == null) {
|
||||
return new BaseResponse(request.getMsgId(), MessageCode.LAW_CASE_NOT_FOUND.getCode(),
|
||||
MessageCode.LAW_CASE_NOT_FOUND.getDescription(), Map.of("id", id));
|
||||
}
|
||||
|
||||
return new BaseResponse(request.getMsgId(), MessageCode.SUCCESS.getCode(),
|
||||
MessageCode.SUCCESS.getDescription(), LawCaseResponse.fromEntity(lawCase));
|
||||
}
|
||||
|
||||
private BaseResponse createLawCase(Long userId, LawCaseRequest lawCaseRequest, BaseRequest request) {
|
||||
try {
|
||||
tariffInfoService.writeOffTokens(userId, TariffConstants.LEGAL_COST, OperationType.CREATE_LEGAL_CASE);
|
||||
} catch (TariffNotFoundException e) {
|
||||
return new BaseResponse(request.getMsgId(), MessageCode.USER_NOT_HAVE_TOKEN.getCode(),
|
||||
MessageCode.USER_NOT_HAVE_TOKEN.getDescription(),
|
||||
Map.of("error", "Tariff not found", "message", e.getMessage()));
|
||||
} catch (MessagingException | IOException e) {
|
||||
return new BaseResponse(request.getMsgId(), MessageCode.INVALID_FIELD.getCode(),
|
||||
MessageCode.INVALID_FIELD.getDescription(), Map.of("id", lawCaseRequest.getId()));
|
||||
}
|
||||
|
||||
validateRequiredParameters(lawCaseRequest, request);
|
||||
|
||||
User user = userRepository.findById(userId).orElseThrow(() -> new UserNotFoundException("User not found"));
|
||||
|
||||
LawCase lawCase = lawCaseService.addLawCase(
|
||||
lawCaseRequest.getAmount(),
|
||||
lawCaseRequest.getDescription(),
|
||||
lawCaseRequest.getName(),
|
||||
LawCasePriority.valueOf(lawCaseRequest.getPriority()),
|
||||
lawCaseRequest.getViolationId(), user);
|
||||
|
||||
return new BaseResponse(request.getMsgId(), MessageCode.SUCCESS.getCode(),
|
||||
MessageCode.SUCCESS.getDescription(), LawCaseResponse.fromEntity(lawCase));
|
||||
}
|
||||
|
||||
private BaseResponse updatePriority(LawCaseRequest lawCaseRequest, BaseRequest request) {
|
||||
LawCase lawCase = lawCaseService.changePriority(
|
||||
lawCaseRequest.getId(),
|
||||
LawCasePriority.valueOf(lawCaseRequest.getPriority()));
|
||||
|
||||
if (lawCase == null) {
|
||||
return new BaseResponse(request.getMsgId(), MessageCode.LAW_CASE_NOT_FOUND.getCode(),
|
||||
MessageCode.LAW_CASE_NOT_FOUND.getDescription(), Map.of("id", lawCaseRequest.getId()));
|
||||
}
|
||||
|
||||
return new BaseResponse(request.getMsgId(), MessageCode.SUCCESS.getCode(),
|
||||
MessageCode.SUCCESS.getDescription(), LawCaseResponse.fromEntity(lawCase));
|
||||
}
|
||||
|
||||
private BaseResponse updateType(LawCaseRequest lawCaseRequest, BaseRequest request) {
|
||||
LawCase lawCase = lawCaseService.changeStatus(
|
||||
lawCaseRequest.getId(),
|
||||
LawCaseType.valueOf(lawCaseRequest.getType()));
|
||||
|
||||
if (lawCase == null) {
|
||||
return new BaseResponse(request.getMsgId(), MessageCode.LAW_CASE_NOT_FOUND.getCode(),
|
||||
MessageCode.LAW_CASE_NOT_FOUND.getDescription(), Map.of("id", lawCaseRequest.getId()));
|
||||
}
|
||||
|
||||
return new BaseResponse(request.getMsgId(), MessageCode.SUCCESS.getCode(),
|
||||
MessageCode.SUCCESS.getDescription(), LawCaseResponse.fromEntity(lawCase));
|
||||
}
|
||||
|
||||
private BaseResponse updateAmount(LawCaseRequest lawCaseRequest, BaseRequest request) {
|
||||
LawCase lawCase = lawCaseService.changeDamage(
|
||||
lawCaseRequest.getId(),
|
||||
lawCaseRequest.getAmount());
|
||||
|
||||
if (lawCase == null) {
|
||||
return new BaseResponse(request.getMsgId(), MessageCode.LAW_CASE_NOT_FOUND.getCode(),
|
||||
MessageCode.LAW_CASE_NOT_FOUND.getDescription(), Map.of("id", lawCaseRequest.getId()));
|
||||
}
|
||||
|
||||
return new BaseResponse(request.getMsgId(), MessageCode.SUCCESS.getCode(),
|
||||
MessageCode.SUCCESS.getDescription(), LawCaseResponse.fromEntity(lawCase));
|
||||
}
|
||||
|
||||
private BaseResponse deleteLawCase(Long id, BaseRequest request) {
|
||||
lawCaseService.deleteById(id);
|
||||
return new BaseResponse(request.getMsgId(), MessageCode.SUCCESS.getCode(),
|
||||
MessageCode.SUCCESS.getDescription(), Map.of("id", id));
|
||||
}
|
||||
|
||||
private void validateRequiredParameters(LawCaseRequest lawCaseRequest, BaseRequest request) {
|
||||
if (lawCaseRequest.getName() == null) throw new NotValidFieldException("Name is required",
|
||||
new BaseResponse(request.getMsgId(), MessageCode.NOT_VALID_FIELD.getCode(),
|
||||
MessageCode.NOTION_NOT_FOUND.getDescription(), Map.of("fields", "name")));
|
||||
|
||||
if (lawCaseRequest.getDescription() == null) throw new NotValidFieldException("Description is required",
|
||||
new BaseResponse(request.getMsgId(), MessageCode.NOT_VALID_FIELD.getCode(),
|
||||
MessageCode.NOTION_NOT_FOUND.getDescription(), Map.of("fields", "description")));
|
||||
}
|
||||
}
|
||||
@@ -122,10 +122,27 @@ public class NotificationHandler implements RequestHandler {
|
||||
response);
|
||||
}
|
||||
|
||||
case "delete": {
|
||||
MarkAsReadRequest markRequest = objectMapper.convertValue(request.getMessageBody(),
|
||||
MarkAsReadRequest.class);
|
||||
|
||||
int delete = notificationService.delete(markRequest.getNotificationIds());
|
||||
|
||||
MarkAsReadResponse response = MarkAsReadResponse.builder()
|
||||
.success(true)
|
||||
.updatedCount(delete)
|
||||
.build();
|
||||
|
||||
return new BaseResponse(request.getMsgId(),
|
||||
MessageCode.SUCCESS.getCode(),
|
||||
MessageCode.SUCCESS.getDescription(),
|
||||
response);
|
||||
}
|
||||
|
||||
default:
|
||||
ActionResponse response = ActionResponse.builder()
|
||||
.action(action)
|
||||
.availableActions(Arrays.asList("list", "active", "markRead"))
|
||||
.availableActions(Arrays.asList("list", "active", "markRead", "delete"))
|
||||
.build();
|
||||
|
||||
return new BaseResponse(request.getMsgId(),
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
package ru.soune.nocopy.handler;
|
||||
|
||||
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.statistic.IncomeStatisticResponse;
|
||||
import ru.soune.nocopy.service.statistic.IncomeStatisticService;
|
||||
|
||||
@Component
|
||||
@RequiredArgsConstructor
|
||||
@Slf4j
|
||||
public class StatisticIncomeHandler implements RequestHandler {
|
||||
|
||||
private final IncomeStatisticService statisticService;
|
||||
|
||||
@Override
|
||||
public BaseResponse handle(BaseRequest request) throws Exception {
|
||||
IncomeStatisticResponse statistics = statisticService.getIncomeStatistics();
|
||||
|
||||
return new BaseResponse(
|
||||
request.getMsgId(),
|
||||
MessageCode.SUCCESS.getCode(),
|
||||
MessageCode.SUCCESS.getDescription(),
|
||||
statistics
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package ru.soune.nocopy.handler;
|
||||
|
||||
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.statistic.ProtectedFilesStatisticResponse;
|
||||
import ru.soune.nocopy.service.statistic.ProtectedFilesStatisticService;
|
||||
|
||||
@Component
|
||||
@RequiredArgsConstructor
|
||||
@Slf4j
|
||||
public class StatisticProtectedFilesHandler implements RequestHandler {
|
||||
private final ProtectedFilesStatisticService statisticService;
|
||||
|
||||
@Override
|
||||
public BaseResponse handle(BaseRequest request) throws Exception {
|
||||
ProtectedFilesStatisticResponse statistics = statisticService.getProtectedFilesStatistics();
|
||||
|
||||
return new BaseResponse(
|
||||
request.getMsgId(),
|
||||
MessageCode.SUCCESS.getCode(),
|
||||
MessageCode.SUCCESS.getDescription(),
|
||||
statistics);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package ru.soune.nocopy.handler;
|
||||
|
||||
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.statistic.SubscriberStatisticResponse;
|
||||
import ru.soune.nocopy.service.statistic.SubscriberStatisticService;
|
||||
|
||||
@Component
|
||||
@RequiredArgsConstructor
|
||||
@Slf4j
|
||||
public class StatisticSubscriberHandler implements RequestHandler {
|
||||
private final SubscriberStatisticService statisticService;
|
||||
|
||||
@Override
|
||||
public BaseResponse handle(BaseRequest request) throws Exception {
|
||||
SubscriberStatisticResponse statistics = statisticService.getSubscriberStatistics();
|
||||
|
||||
return new BaseResponse(
|
||||
request.getMsgId(),
|
||||
MessageCode.SUCCESS.getCode(),
|
||||
MessageCode.SUCCESS.getDescription(),
|
||||
statistics
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package ru.soune.nocopy.handler;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import lombok.AllArgsConstructor;
|
||||
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.statistic.TariffStatisticFullResponse;
|
||||
import ru.soune.nocopy.dto.statistic.TariffStatisticRequest;
|
||||
import ru.soune.nocopy.dto.statistic.TariffStatisticResponse;
|
||||
import ru.soune.nocopy.repository.TariffInfoRepository;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Component
|
||||
@AllArgsConstructor
|
||||
public class StatisticTariffInfoFileHandler implements RequestHandler {
|
||||
|
||||
private ObjectMapper objectMapper;
|
||||
|
||||
private TariffInfoRepository tariffInfoRepository;
|
||||
|
||||
@Override
|
||||
public BaseResponse handle(BaseRequest request) throws Exception {
|
||||
TariffStatisticRequest tariffRequest =
|
||||
objectMapper.convertValue(request.getMessageBody(), TariffStatisticRequest.class);
|
||||
|
||||
boolean activeOnly = tariffRequest.getActiveOnly() != null ? tariffRequest.getActiveOnly() : true;
|
||||
List<Object[]> results = tariffInfoRepository.getTariffStatisticsNative(activeOnly);
|
||||
|
||||
List<TariffStatisticResponse> tariffs = results.stream()
|
||||
.map(row -> new TariffStatisticResponse(
|
||||
(String) row[0],
|
||||
((Number) row[1]).longValue(),
|
||||
((Number) row[2]).doubleValue()
|
||||
))
|
||||
.toList();
|
||||
|
||||
TariffStatisticFullResponse statistics = new TariffStatisticFullResponse(tariffs);
|
||||
|
||||
return new BaseResponse(
|
||||
request.getMsgId(),
|
||||
MessageCode.SUCCESS.getCode(),
|
||||
MessageCode.SUCCESS.getDescription(),
|
||||
statistics);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package ru.soune.nocopy.handler;
|
||||
|
||||
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.statistic.TokenStatisticResponse;
|
||||
import ru.soune.nocopy.service.statistic.TokenStatisticService;
|
||||
|
||||
@Component
|
||||
@RequiredArgsConstructor
|
||||
@Slf4j
|
||||
public class StatisticTokenHandler implements RequestHandler {
|
||||
private final TokenStatisticService statisticService;
|
||||
|
||||
@Override
|
||||
public BaseResponse handle(BaseRequest request) throws Exception {
|
||||
TokenStatisticResponse statistics = statisticService.getTokenStatistics();
|
||||
|
||||
return new BaseResponse(
|
||||
request.getMsgId(),
|
||||
MessageCode.SUCCESS.getCode(),
|
||||
MessageCode.SUCCESS.getDescription(),
|
||||
statistics
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
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.statistic.UserDynamicStatisticResponse;
|
||||
import ru.soune.nocopy.service.statistic.UserDynamicStatisticService;
|
||||
|
||||
@Component
|
||||
@RequiredArgsConstructor
|
||||
@Slf4j
|
||||
public class StatisticUserDynamicHandler implements RequestHandler {
|
||||
private final UserDynamicStatisticService statisticService;
|
||||
|
||||
@Override
|
||||
public BaseResponse handle(BaseRequest request) throws Exception {
|
||||
UserDynamicStatisticResponse statistics = statisticService.getUserDynamicStatistics();
|
||||
|
||||
return new BaseResponse(
|
||||
request.getMsgId(),
|
||||
MessageCode.SUCCESS.getCode(),
|
||||
MessageCode.SUCCESS.getDescription(),
|
||||
statistics);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
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.statistic.UserFilesStatisticRequest;
|
||||
import ru.soune.nocopy.dto.statistic.UserFilesStatisticResponse;
|
||||
import ru.soune.nocopy.repository.FileEntityRepository;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Component
|
||||
@RequiredArgsConstructor
|
||||
@Slf4j
|
||||
public class StatisticUserFilesHandler implements RequestHandler {
|
||||
|
||||
private final ObjectMapper objectMapper;
|
||||
|
||||
private final FileEntityRepository fileRepository;
|
||||
|
||||
@Override
|
||||
public BaseResponse handle(BaseRequest request) throws Exception {
|
||||
UserFilesStatisticRequest userFilesStatisticRequest =
|
||||
objectMapper.convertValue(request.getMessageBody(), UserFilesStatisticRequest.class);
|
||||
|
||||
int topUsers = userFilesStatisticRequest.getTopUsers();
|
||||
String fileMimeType = userFilesStatisticRequest.getFileMimeType();
|
||||
|
||||
List<UserFilesStatisticResponse> files = fileRepository.findTopUsers(topUsers, fileMimeType);
|
||||
|
||||
return new BaseResponse(request.getMsgId(), MessageCode.SUCCESS.getCode(),
|
||||
MessageCode.SUCCESS.getDescription(), files);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
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.statistic.ViolationStatisticResponse;
|
||||
import ru.soune.nocopy.service.statistic.ViolationStatisticService;
|
||||
|
||||
@Component
|
||||
@RequiredArgsConstructor
|
||||
@Slf4j
|
||||
public class StatisticViolationHandler implements RequestHandler {
|
||||
|
||||
private final ObjectMapper objectMapper;
|
||||
private final ViolationStatisticService statisticService;
|
||||
|
||||
@Override
|
||||
public BaseResponse handle(BaseRequest request) throws Exception {
|
||||
log.debug("Handling violation statistic request, msgId: {}", request.getMsgId());
|
||||
|
||||
ViolationStatisticResponse statistics = statisticService.getViolationStatistics();
|
||||
|
||||
return new BaseResponse(
|
||||
request.getMsgId(),
|
||||
MessageCode.SUCCESS.getCode(),
|
||||
MessageCode.SUCCESS.getDescription(),
|
||||
statistics
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,472 @@
|
||||
package ru.soune.nocopy.handler;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.data.domain.Page;
|
||||
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.tokenoperation.TokenOperationPageDto;
|
||||
import ru.soune.nocopy.dto.tokenoperation.TokenOperationRequest;
|
||||
import ru.soune.nocopy.entity.tokenoperation.OperationType;
|
||||
import ru.soune.nocopy.entity.tokenoperation.TokenOperation;
|
||||
import ru.soune.nocopy.service.register.AuthService;
|
||||
import ru.soune.nocopy.service.tokenoperation.TokenOperationService;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@Slf4j
|
||||
@Component
|
||||
@RequiredArgsConstructor
|
||||
public class TokenOperationHandler implements RequestHandler {
|
||||
private final TokenOperationService tokenOperationService;
|
||||
|
||||
private final ObjectMapper objectMapper;
|
||||
|
||||
private final AuthService authService;
|
||||
|
||||
@Override
|
||||
public BaseResponse handle(BaseRequest request) throws Exception {
|
||||
log.info("Handling TokenOperation request: msg_id={}, version={}",
|
||||
request.getMsgId(), request.getVersion());
|
||||
|
||||
try {
|
||||
TokenOperationRequest tokenRequest = objectMapper.convertValue(
|
||||
request.getMessageBody(),
|
||||
TokenOperationRequest.class
|
||||
);
|
||||
|
||||
String action = tokenRequest.getAction();
|
||||
if (action == null) {
|
||||
return new BaseResponse(
|
||||
request.getMsgId(),
|
||||
MessageCode.INVALID_ACTION.getCode(),
|
||||
MessageCode.INVALID_ACTION.getDescription(),
|
||||
Map.of("error", "Action is required")
|
||||
);
|
||||
}
|
||||
|
||||
switch (action.toLowerCase()) {
|
||||
case "get_list":
|
||||
return handleGetList(request.getMsgId(), tokenRequest);
|
||||
case "get_by_id":
|
||||
return handleGetById(request.getMsgId(), tokenRequest);
|
||||
case "get_by_user":
|
||||
return handleGetByUser(request.getMsgId(), tokenRequest);
|
||||
case "create":
|
||||
return handleCreate(request.getMsgId(), tokenRequest);
|
||||
case "update":
|
||||
return handleUpdate(request.getMsgId(), tokenRequest);
|
||||
case "delete":
|
||||
return handleDelete(request.getMsgId(), tokenRequest);
|
||||
case "delete_all_by_user":
|
||||
return handleDeleteAllByUser(request.getMsgId(), tokenRequest);
|
||||
case "get_stats":
|
||||
return handleGetStats(request.getMsgId(), tokenRequest);
|
||||
case "filter":
|
||||
return handleFilter(request.getMsgId(), tokenRequest);
|
||||
default:
|
||||
return new BaseResponse(
|
||||
request.getMsgId(),
|
||||
MessageCode.INVALID_ACTION.getCode(),
|
||||
MessageCode.INVALID_ACTION.getDescription(),
|
||||
Map.of("available_actions",
|
||||
"get_list, get_by_id, get_by_user, create, update, " +
|
||||
"delete, delete_all_by_user, get_stats, filter")
|
||||
);
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("Error handling TokenOperation request", e);
|
||||
return new BaseResponse(
|
||||
request.getMsgId(),
|
||||
MessageCode.INTERNAL_ERROR.getCode(),
|
||||
MessageCode.INTERNAL_ERROR.getDescription(),
|
||||
Map.of("error", e.getMessage())
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
private BaseResponse handleGetList(Integer msgId, TokenOperationRequest request) {
|
||||
if (request.getToken() == null) {
|
||||
return new BaseResponse(
|
||||
msgId,
|
||||
MessageCode.USER_NOT_FOUND.getCode(),
|
||||
MessageCode.USER_NOT_FOUND.getDescription(),
|
||||
Map.of("error", "User ID is required")
|
||||
);
|
||||
}
|
||||
|
||||
Long userId = authService.useUserAuthToken(request.getToken());
|
||||
|
||||
try {
|
||||
Page<TokenOperation> page = tokenOperationService.findByUserId(
|
||||
userId,
|
||||
request.getPage() != null ? request.getPage() : 0,
|
||||
request.getSize() != null ? request.getSize() : 20
|
||||
);
|
||||
|
||||
TokenOperationPageDto pageDto = TokenOperationPageDto.builder()
|
||||
.content(page.getContent())
|
||||
.pageNumber(page.getNumber())
|
||||
.pageSize(page.getSize())
|
||||
.totalElements(page.getTotalElements())
|
||||
.totalPages(page.getTotalPages())
|
||||
.last(page.isLast())
|
||||
.first(page.isFirst())
|
||||
.build();
|
||||
|
||||
Map<String, Object> responseData = new HashMap<>();
|
||||
responseData.put("operations", pageDto);
|
||||
responseData.put("total_elements", page.getTotalElements());
|
||||
responseData.put("total_pages", page.getTotalPages());
|
||||
responseData.put("current_page", page.getNumber());
|
||||
responseData.put("page_size", page.getSize());
|
||||
|
||||
return new BaseResponse(
|
||||
msgId,
|
||||
MessageCode.SUCCESS.getCode(),
|
||||
MessageCode.SUCCESS.getDescription(),
|
||||
responseData
|
||||
);
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("Error getting token operations list", e);
|
||||
return new BaseResponse(
|
||||
msgId,
|
||||
MessageCode.INTERNAL_ERROR.getCode(),
|
||||
MessageCode.INTERNAL_ERROR.getDescription(),
|
||||
Map.of("error", "Error getting operations: " + e.getMessage())
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
private BaseResponse handleGetById(Integer msgId, TokenOperationRequest request) {
|
||||
log.info("Getting token operation by id: {}", request.getOperationId());
|
||||
|
||||
if (request.getOperationId() == null) {
|
||||
return new BaseResponse(
|
||||
msgId,
|
||||
MessageCode.OPERATION_NOT_FOUND.getCode(),
|
||||
MessageCode.OPERATION_NOT_FOUND.getDescription(),
|
||||
Map.of("error", "Operation ID is required")
|
||||
);
|
||||
}
|
||||
|
||||
try {
|
||||
TokenOperation operation = tokenOperationService.findById(request.getOperationId());
|
||||
|
||||
return new BaseResponse(
|
||||
msgId,
|
||||
MessageCode.SUCCESS.getCode(),
|
||||
MessageCode.SUCCESS.getDescription(),
|
||||
operation
|
||||
);
|
||||
|
||||
} catch (jakarta.persistence.EntityNotFoundException e) {
|
||||
return new BaseResponse(
|
||||
msgId,
|
||||
MessageCode.NOT_FOUND.getCode(),
|
||||
MessageCode.NOT_FOUND.getDescription(),
|
||||
Map.of("error", "Operation not found with id: " + request.getOperationId())
|
||||
);
|
||||
} catch (Exception e) {
|
||||
log.error("Error getting token operation by id", e);
|
||||
return new BaseResponse(
|
||||
msgId,
|
||||
MessageCode.INTERNAL_ERROR.getCode(),
|
||||
MessageCode.INTERNAL_ERROR.getDescription(),
|
||||
Map.of("error", e.getMessage())
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
private BaseResponse handleGetByUser(Integer msgId, TokenOperationRequest request) {
|
||||
return handleGetList(msgId, request);
|
||||
}
|
||||
|
||||
private BaseResponse handleCreate(Integer msgId, TokenOperationRequest request) {
|
||||
if (request.getToken() == null) {
|
||||
return new BaseResponse(
|
||||
msgId,
|
||||
MessageCode.AUTH_TOKEN_NOT_FOUND.getCode(),
|
||||
MessageCode.AUTH_TOKEN_NOT_FOUND.getDescription(),
|
||||
Map.of("error", "User ID is required")
|
||||
);
|
||||
}
|
||||
|
||||
Long userId = authService.useUserAuthToken(request.getToken());
|
||||
|
||||
try {
|
||||
TokenOperation operation = tokenOperationService.create(
|
||||
request.getOperationType(),
|
||||
userId,
|
||||
request.getSpent()
|
||||
);
|
||||
|
||||
return new BaseResponse(
|
||||
msgId,
|
||||
MessageCode.SUCCESS.getCode(),
|
||||
MessageCode.SUCCESS.getDescription(),
|
||||
operation
|
||||
);
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("Error creating token operation", e);
|
||||
return new BaseResponse(
|
||||
msgId,
|
||||
MessageCode.INTERNAL_ERROR.getCode(),
|
||||
MessageCode.INTERNAL_ERROR.getDescription(),
|
||||
Map.of("error", "Error creating operation: " + e.getMessage())
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
private BaseResponse handleUpdate(Integer msgId, TokenOperationRequest request) {
|
||||
log.info("Updating token operation: {}", request.getOperationId());
|
||||
|
||||
if (request.getOperationId() == null) {
|
||||
return new BaseResponse(
|
||||
msgId,
|
||||
MessageCode.OPERATION_NOT_FOUND.getCode(),
|
||||
MessageCode.OPERATION_NOT_FOUND.getDescription(),
|
||||
Map.of("error", "Operation ID is required")
|
||||
);
|
||||
}
|
||||
|
||||
try {
|
||||
TokenOperation existing = tokenOperationService.findById(request.getOperationId());
|
||||
|
||||
if (request.getOperationType() != null) {
|
||||
existing.setOperationType(request.getOperationType());
|
||||
}
|
||||
if (request.getSpent() != null) {
|
||||
existing.setSpent(request.getSpent());
|
||||
}
|
||||
|
||||
TokenOperation updated = tokenOperationService.update(request.getOperationId(), existing);
|
||||
|
||||
return new BaseResponse(
|
||||
msgId,
|
||||
MessageCode.SUCCESS.getCode(),
|
||||
MessageCode.SUCCESS.getDescription(),
|
||||
updated
|
||||
);
|
||||
|
||||
} catch (jakarta.persistence.EntityNotFoundException e) {
|
||||
return new BaseResponse(
|
||||
msgId,
|
||||
MessageCode.NOT_FOUND.getCode(),
|
||||
MessageCode.NOT_FOUND.getDescription(),
|
||||
Map.of("error", "Operation not found with id: " + request.getOperationId())
|
||||
);
|
||||
} catch (Exception e) {
|
||||
log.error("Error updating token operation", e);
|
||||
return new BaseResponse(
|
||||
msgId,
|
||||
MessageCode.INTERNAL_ERROR.getCode(),
|
||||
MessageCode.INTERNAL_ERROR.getDescription(),
|
||||
Map.of("error", "Error updating operation: " + e.getMessage())
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
private BaseResponse handleDelete(Integer msgId, TokenOperationRequest request) {
|
||||
log.info("Deleting token operation: {}", request.getOperationId());
|
||||
|
||||
if (request.getOperationId() == null) {
|
||||
return new BaseResponse(
|
||||
msgId,
|
||||
MessageCode.OPERATION_NOT_FOUND.getCode(),
|
||||
MessageCode.OPERATION_NOT_FOUND.getDescription(),
|
||||
Map.of("error", "Operation ID is required")
|
||||
);
|
||||
}
|
||||
|
||||
try {
|
||||
tokenOperationService.deleteById(request.getOperationId());
|
||||
|
||||
return new BaseResponse(
|
||||
msgId,
|
||||
MessageCode.SUCCESS.getCode(),
|
||||
MessageCode.SUCCESS.getDescription(),
|
||||
Map.of("message", "Operation deleted successfully",
|
||||
"operation_id", request.getOperationId())
|
||||
);
|
||||
|
||||
} catch (jakarta.persistence.EntityNotFoundException e) {
|
||||
return new BaseResponse(
|
||||
msgId,
|
||||
MessageCode.NOT_FOUND.getCode(),
|
||||
MessageCode.NOT_FOUND.getDescription(),
|
||||
Map.of("error", "Operation not found with id: " + request.getOperationId())
|
||||
);
|
||||
} catch (Exception e) {
|
||||
log.error("Error deleting token operation", e);
|
||||
return new BaseResponse(
|
||||
msgId,
|
||||
MessageCode.INTERNAL_ERROR.getCode(),
|
||||
MessageCode.INTERNAL_ERROR.getDescription(),
|
||||
Map.of("error", "Error deleting operation: " + e.getMessage())
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
private BaseResponse handleDeleteAllByUser(Integer msgId, TokenOperationRequest request) {
|
||||
if (request.getToken() == null) {
|
||||
return new BaseResponse(
|
||||
msgId,
|
||||
MessageCode.AUTH_TOKEN_NOT_FOUND.getCode(),
|
||||
MessageCode.AUTH_TOKEN_NOT_FOUND.getDescription(),
|
||||
Map.of("error", "User ID is required")
|
||||
);
|
||||
}
|
||||
|
||||
Long userId = authService.useUserAuthToken(request.getToken());
|
||||
|
||||
try {
|
||||
tokenOperationService.deleteByUserId(userId);
|
||||
|
||||
return new BaseResponse(
|
||||
msgId,
|
||||
MessageCode.SUCCESS.getCode(),
|
||||
MessageCode.SUCCESS.getDescription(),
|
||||
Map.of("message", "All operations deleted successfully for user",
|
||||
"user_id", userId)
|
||||
);
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("Error deleting all operations for user", e);
|
||||
return new BaseResponse(
|
||||
msgId,
|
||||
MessageCode.INTERNAL_ERROR.getCode(),
|
||||
MessageCode.INTERNAL_ERROR.getDescription(),
|
||||
Map.of("error", "Error deleting operations: " + e.getMessage())
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
private BaseResponse handleGetStats(Integer msgId, TokenOperationRequest request) {
|
||||
if (request.getToken() == null) {
|
||||
return new BaseResponse(
|
||||
msgId,
|
||||
MessageCode.AUTH_TOKEN_NOT_FOUND.getCode(),
|
||||
MessageCode.AUTH_TOKEN_NOT_FOUND.getDescription(),
|
||||
Map.of("error", "User ID is required")
|
||||
);
|
||||
}
|
||||
|
||||
Long userId = authService.useUserAuthToken(request.getToken());
|
||||
|
||||
if (userId == null) {
|
||||
return new BaseResponse(
|
||||
msgId,
|
||||
MessageCode.USER_NOT_FOUND.getCode(),
|
||||
MessageCode.USER_NOT_FOUND.getDescription(),
|
||||
Map.of("error", "User ID is required")
|
||||
);
|
||||
}
|
||||
|
||||
try {
|
||||
Map<String, Long> statsByType = new HashMap<>();
|
||||
long totalSpent = 0L;
|
||||
|
||||
for (OperationType type : OperationType.values()) {
|
||||
Long spent = tokenOperationService.getTotalSpentByUserAndType(userId, type);
|
||||
statsByType.put(type.name().toLowerCase(), spent != null ? spent : 0L);
|
||||
totalSpent += spent != null ? spent : 0L;
|
||||
}
|
||||
|
||||
Map<String, Object> statistics = new HashMap<>();
|
||||
statistics.put("user_id", userId);
|
||||
statistics.put("total_spent", totalSpent);
|
||||
statistics.put("total_operations", tokenOperationService.countByUserId(userId));
|
||||
statistics.put("stats_by_type", statsByType);
|
||||
|
||||
return new BaseResponse(
|
||||
msgId,
|
||||
MessageCode.SUCCESS.getCode(),
|
||||
MessageCode.SUCCESS.getDescription(),
|
||||
statistics
|
||||
);
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("Error getting statistics", e);
|
||||
return new BaseResponse(
|
||||
msgId,
|
||||
MessageCode.INTERNAL_ERROR.getCode(),
|
||||
MessageCode.INTERNAL_ERROR.getDescription(),
|
||||
Map.of("error", "Error getting statistics: " + e.getMessage())
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
private BaseResponse handleFilter(Integer msgId, TokenOperationRequest request) {
|
||||
if (request.getToken() == null) {
|
||||
return new BaseResponse(
|
||||
msgId,
|
||||
MessageCode.AUTH_TOKEN_NOT_FOUND.getCode(),
|
||||
MessageCode.AUTH_TOKEN_NOT_FOUND.getDescription(),
|
||||
Map.of("error", "User ID is required")
|
||||
);
|
||||
}
|
||||
|
||||
Long userId = authService.useUserAuthToken(request.getToken());
|
||||
|
||||
try {
|
||||
Page<TokenOperation> page = tokenOperationService.findByFilters(
|
||||
userId,
|
||||
request.getOperationType(),
|
||||
request.getMinSpent(),
|
||||
request.getMaxSpent(),
|
||||
request.getPage() != null ? request.getPage() : 0,
|
||||
request.getSize() != null ? request.getSize() : 20,
|
||||
request.getSortBy() != null ? request.getSortBy() : "createdAt",
|
||||
request.getSortDirection() != null ? request.getSortDirection() : "desc"
|
||||
);
|
||||
|
||||
TokenOperationPageDto pageDto = TokenOperationPageDto.builder()
|
||||
.content(page.getContent())
|
||||
.pageNumber(page.getNumber())
|
||||
.pageSize(page.getSize())
|
||||
.totalElements(page.getTotalElements())
|
||||
.totalPages(page.getTotalPages())
|
||||
.last(page.isLast())
|
||||
.first(page.isFirst())
|
||||
.build();
|
||||
|
||||
Map<String, Object> responseData = new HashMap<>();
|
||||
responseData.put("operations", pageDto);
|
||||
responseData.put("total_elements", page.getTotalElements());
|
||||
responseData.put("total_pages", page.getTotalPages());
|
||||
responseData.put("current_page", page.getNumber());
|
||||
responseData.put("page_size", page.getSize());
|
||||
|
||||
responseData.put("applied_filters", Map.of(
|
||||
"user_id",userId != null ? userId : "all",
|
||||
"operation_type", request.getOperationType() != null ? request.getOperationType() : "all",
|
||||
"min_spent", request.getMinSpent() != null ? request.getMinSpent() : "none",
|
||||
"max_spent", request.getMaxSpent() != null ? request.getMaxSpent() : "none"
|
||||
));
|
||||
|
||||
return new BaseResponse(
|
||||
msgId,
|
||||
MessageCode.SUCCESS.getCode(),
|
||||
MessageCode.SUCCESS.getDescription(),
|
||||
responseData
|
||||
);
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("Error filtering operations", e);
|
||||
return new BaseResponse(
|
||||
msgId,
|
||||
MessageCode.INTERNAL_ERROR.getCode(),
|
||||
MessageCode.INTERNAL_ERROR.getDescription(),
|
||||
Map.of("error", "Error filtering operations: " + e.getMessage())
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,108 @@
|
||||
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.file.ActionResponse;
|
||||
import ru.soune.nocopy.dto.user.moderation.UserVerificationRequest;
|
||||
import ru.soune.nocopy.dto.user.moderation.UserVerificationResponse;
|
||||
import ru.soune.nocopy.entity.notification.NotificationType;
|
||||
import ru.soune.nocopy.entity.user.ModerationStatus;
|
||||
import ru.soune.nocopy.entity.user.moderation.UserVerification;
|
||||
import ru.soune.nocopy.service.notification.NotificationService;
|
||||
import ru.soune.nocopy.service.user.moderation.UserVerificationService;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
@Component
|
||||
@RequiredArgsConstructor
|
||||
@Slf4j
|
||||
public class UserVerificationHandler implements RequestHandler {
|
||||
|
||||
private final UserVerificationService userVerificationService;
|
||||
|
||||
private final ObjectMapper objectMapper;
|
||||
|
||||
private final NotificationService notificationService;
|
||||
|
||||
@Override
|
||||
public BaseResponse handle(BaseRequest request) throws Exception {
|
||||
UserVerificationRequest userVerificationRequest = objectMapper.convertValue(request.getMessageBody(),
|
||||
UserVerificationRequest.class);
|
||||
String action = userVerificationRequest.getAction();
|
||||
|
||||
switch (action) {
|
||||
case "get_all_verifications":
|
||||
return handleVerifications(request, userVerificationRequest);
|
||||
case "verified":
|
||||
return handleVerified(request, userVerificationRequest);
|
||||
default:
|
||||
ActionResponse response = ActionResponse.builder()
|
||||
.action(action)
|
||||
.availableActions(Arrays.asList(
|
||||
"get_all_verifications", "verified"))
|
||||
.build();
|
||||
return new BaseResponse(request.getMsgId(),
|
||||
MessageCode.INVALID_ACTION.getCode(),
|
||||
"Invalid action: " + action,
|
||||
response);
|
||||
}
|
||||
}
|
||||
|
||||
private BaseResponse handleVerifications(BaseRequest request, UserVerificationRequest userVerificationRequest) throws Exception {
|
||||
List<UserVerification> verifications = userVerificationService.verifications();
|
||||
|
||||
if (verifications.isEmpty()) {
|
||||
return new BaseResponse(request.getMsgId(), MessageCode.USER_VERIFICATIONS_NOT_FOUND.getCode(),
|
||||
MessageCode.USER_VERIFICATIONS_NOT_FOUND.getDescription(), null);
|
||||
}
|
||||
|
||||
List<UserVerificationResponse> verificationResponses = verifications.stream().map(this::convertDto).toList();
|
||||
|
||||
return BaseResponse.builder()
|
||||
.msgId(request.getMsgId())
|
||||
.messageBody(verificationResponses)
|
||||
.build();
|
||||
}
|
||||
|
||||
private BaseResponse handleVerified(BaseRequest request, UserVerificationRequest userVerificationRequest) throws Exception {
|
||||
UserVerification userVerification = userVerificationService.verifyUser(userVerificationRequest.getUserId(), userVerificationRequest.getMessage(),
|
||||
userVerificationRequest.getAdminId(), userVerificationRequest.getVerified());
|
||||
|
||||
if (userVerification == null) {
|
||||
return new BaseResponse(request.getMsgId(),
|
||||
MessageCode.ADMIN_USER_NOT_FOUND.getCode(),
|
||||
MessageCode.ADMIN_USER_NOT_FOUND.getDescription(),
|
||||
null);
|
||||
}
|
||||
|
||||
NotificationType notificationType = userVerification.getModerationStatus() == ModerationStatus.VERIFIED?
|
||||
NotificationType.USER_VERIFIED: NotificationType.USER_NOT_VERIFIED;
|
||||
|
||||
notificationService.addNotification(notificationType, userVerification.getUserId());
|
||||
|
||||
return BaseResponse.builder()
|
||||
.msgId(request.getMsgId())
|
||||
.messageBody(UserVerificationResponse.builder()
|
||||
.userVerificationId(userVerification.getId())
|
||||
.moderationStatus(userVerification.getModerationStatus().toString())
|
||||
.userId(userVerification.getUserId()))
|
||||
.build();
|
||||
}
|
||||
|
||||
|
||||
private UserVerificationResponse convertDto(UserVerification userVerification) {
|
||||
return UserVerificationResponse.builder()
|
||||
.userVerificationId(userVerification.getId())
|
||||
.userId(userVerification.getUserId())
|
||||
.moderationStatus(userVerification.getModerationStatus().name())
|
||||
.updateTime(userVerification.getUpdatedAt())
|
||||
.build();
|
||||
|
||||
}
|
||||
}
|
||||
@@ -13,6 +13,7 @@ import ru.soune.nocopy.entity.file.FileEntity;
|
||||
import ru.soune.nocopy.entity.violation.Violation;
|
||||
import ru.soune.nocopy.repository.FileEntityRepository;
|
||||
import ru.soune.nocopy.service.file.FileEntityService;
|
||||
import ru.soune.nocopy.service.geo.GeoCountryService;
|
||||
import ru.soune.nocopy.service.register.AuthService;
|
||||
import ru.soune.nocopy.service.violation.ViolationService;
|
||||
import ru.soune.nocopy.service.violation.ViolationStatus;
|
||||
@@ -21,6 +22,7 @@ import java.io.FileNotFoundException;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.time.format.DateTimeParseException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@@ -38,6 +40,8 @@ public class ViolationHandler implements RequestHandler {
|
||||
|
||||
private final FileEntityService fileEntityService;
|
||||
|
||||
private final GeoCountryService geoCountryService;
|
||||
|
||||
private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
|
||||
|
||||
@Override
|
||||
@@ -112,10 +116,16 @@ public class ViolationHandler implements RequestHandler {
|
||||
);
|
||||
}
|
||||
|
||||
List<ViolationResponse.ViolationDto> violationDtos = violationPage.getContent()
|
||||
.stream()
|
||||
.map(ViolationResponse.ViolationDto::fromEntity)
|
||||
.toList();
|
||||
List<Violation> content = violationPage.getContent();
|
||||
List<ViolationResponse.ViolationDto> violationDtos = new ArrayList<>();
|
||||
|
||||
for (Violation violation : content) {
|
||||
ViolationResponse.ViolationDto violationDto = ViolationResponse.ViolationDto.fromEntity(violation);
|
||||
violationDto.setCountry(geoCountryService.getCountryName(violation.getPageUrl()));
|
||||
violationDto.setCountryCode(geoCountryService.getCountryCode(violation.getPageUrl()));
|
||||
|
||||
violationDtos.add(violationDto);
|
||||
}
|
||||
|
||||
ViolationResponse response = ViolationResponse.builder()
|
||||
.violations(violationDtos)
|
||||
|
||||
Reference in New Issue
Block a user