@@ -110,37 +110,36 @@ public class GlobalSearchController {
|
||||
return ResponseEntity.ok(taskOptional.get());
|
||||
}
|
||||
|
||||
// @GetMapping("/violation-summary")
|
||||
// public ResponseEntity<?> getSummary(@RequestHeader(value = "Authorization", required = false) String tokenHeader) {
|
||||
//
|
||||
// if (tokenHeader == null || tokenHeader.isBlank()) {
|
||||
// Map<String, Object> errorData = new HashMap<>();
|
||||
// errorData.put("token", tokenHeader);
|
||||
//
|
||||
// return ResponseEntity.ok().body(Map.of("error", errorData));
|
||||
// }
|
||||
//
|
||||
// List<FileViolationSummaryDTO> fileViolationsSummary;
|
||||
//
|
||||
// try {
|
||||
// Long userId = authService.useUserAuthToken(tokenHeader);
|
||||
//
|
||||
// fileViolationsSummary =
|
||||
// violationService.getFileViolationsSummary(userId);
|
||||
//
|
||||
// if (fileViolationsSummary.isEmpty()) return ResponseEntity.ok().body("Violations summary not found");
|
||||
// } catch (NotFoundAuthToken e) {
|
||||
// Map<String, Object> errorData = new HashMap<>();
|
||||
// errorData.put("token", tokenHeader);
|
||||
//
|
||||
// return ResponseEntity.ok().body(Map.of("error", errorData));
|
||||
// }
|
||||
//
|
||||
// return ResponseEntity.ok(fileViolationsSummary);
|
||||
// }
|
||||
@GetMapping("/violation-summary")
|
||||
public ResponseEntity<?> getSummary(@RequestHeader(value = "Authorization", required = false) String tokenHeader) {
|
||||
|
||||
if (tokenHeader == null || tokenHeader.isBlank()) {
|
||||
Map<String, Object> errorData = new HashMap<>();
|
||||
errorData.put("token", tokenHeader);
|
||||
|
||||
return ResponseEntity.ok().body(Map.of("error", errorData));
|
||||
}
|
||||
|
||||
List<FileViolationSummaryDTO> fileViolationsSummary;
|
||||
|
||||
try {
|
||||
Long userId = authService.useUserAuthToken(tokenHeader);
|
||||
|
||||
fileViolationsSummary = violationService.getFileViolationsSummary(userId);
|
||||
|
||||
if (fileViolationsSummary.isEmpty()) return ResponseEntity.ok().body("Violations summary not found");
|
||||
} catch (NotFoundAuthToken e) {
|
||||
Map<String, Object> errorData = new HashMap<>();
|
||||
errorData.put("token", tokenHeader);
|
||||
|
||||
return ResponseEntity.ok().body(Map.of("error", errorData));
|
||||
}
|
||||
|
||||
return ResponseEntity.ok(fileViolationsSummary);
|
||||
}
|
||||
|
||||
|
||||
@PostMapping("/violation-summary")
|
||||
@PostMapping("v2/violation-summary")
|
||||
public ResponseEntity<?> getViolations(@RequestBody ViolationRequest request) {
|
||||
if (request.getToken() == null || request.getToken().isBlank()) {
|
||||
Map<String, Object> errorData = new HashMap<>();
|
||||
|
||||
@@ -118,6 +118,42 @@ public class ViolationService {
|
||||
return new PageImpl<>(dtoList, pageable, violations.getTotalElements());
|
||||
}
|
||||
|
||||
public List<FileViolationSummaryDTO> getFileViolationsSummary(Long userId) {
|
||||
List<FileEntity> userFiles = fileEntityService.getAllUserFiles(userId);
|
||||
|
||||
List<Violation> violations = violationRepository.findByFileEntityInAndStatusNot( userFiles,
|
||||
ViolationStatus.NOT_OWNER_FILE.name());
|
||||
|
||||
Map<String, List<Violation>> violationsByFileId = violations.stream()
|
||||
.collect(Collectors.groupingBy(v -> v.getFileEntity().getId()));
|
||||
|
||||
List<FileViolationSummaryDTO> dtoList = violationsByFileId.entrySet().stream()
|
||||
.map(entry -> {
|
||||
List<Violation> fileViolations = entry.getValue();
|
||||
FileEntity file = fileViolations.get(0).getFileEntity();
|
||||
|
||||
LocalDateTime latestDate = fileViolations.stream()
|
||||
.map(Violation::getCreatedDate)
|
||||
.max(LocalDateTime::compareTo)
|
||||
.orElse(null);
|
||||
|
||||
return FileViolationSummaryDTO.builder()
|
||||
.fileId(file.getId())
|
||||
.supportId(file.getSupportId())
|
||||
.fileName(file.getOriginalFileName())
|
||||
.fileSize(file.getFileSize())
|
||||
.mimeType(file.getMimeType())
|
||||
.status(file.getStatus())
|
||||
.createdAt(file.getCreatedAt())
|
||||
.violationCount(fileViolations.size())
|
||||
.latestViolationDate(latestDate)
|
||||
.build();
|
||||
})
|
||||
.collect(Collectors.toList());
|
||||
|
||||
return dtoList;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void changeStatus(ViolationStatus status, Long violationId) {
|
||||
Violation violation = violationRepository.findById(violationId).orElseThrow();
|
||||
|
||||
Reference in New Issue
Block a user