dev add lawcase logic
Test Workflow / test (push) Successful in 4s

This commit is contained in:
vladp
2026-04-06 14:51:00 +07:00
parent 5f063354e6
commit 077902f4ab
14 changed files with 559 additions and 1 deletions
@@ -36,7 +36,8 @@ public class HandlerConfig {
ComplaintEntityHandler complaintEntityHandler, ComplaintEntityHandler complaintEntityHandler,
UserInfoHandler userInfoHandler, UserInfoHandler userInfoHandler,
NotificationHandler notificationHandler, NotificationHandler notificationHandler,
UserVerificationHandler userVerificationHandler UserVerificationHandler userVerificationHandler,
LawCaseHandler lawCaseHandler
) { ) {
Map<Integer, RequestHandler> map = new HashMap<>(); Map<Integer, RequestHandler> map = new HashMap<>();
map.put(20001, login); map.put(20001, login);
@@ -64,6 +65,7 @@ public class HandlerConfig {
map.put(30014, userInfoHandler); map.put(30014, userInfoHandler);
map.put(30015, notificationHandler); map.put(30015, notificationHandler);
map.put(30016, userVerificationHandler); map.put(30016, userVerificationHandler);
map.put(30017, lawCaseHandler);
return map; return map;
} }
@@ -19,6 +19,7 @@ public enum MessageCode {
FILE_DELETE(2, "File was deleted"), FILE_DELETE(2, "File was deleted"),
USER_NOT_HAD_PERMISSION(2, "User not have permission for file"), USER_NOT_HAD_PERMISSION(2, "User not have permission for file"),
USER_NOT_VERIFIED(2, "User not verified"), USER_NOT_VERIFIED(2, "User not verified"),
LAW_CASE_NOT_FOUND(4, "Law case not found"),
PERMISSION_NOT_FOUND(2, "Permission not found"), PERMISSION_NOT_FOUND(2, "Permission not found"),
USER_NOT_FOUND(2, "User not found"), USER_NOT_FOUND(2, "User not found"),
FILE_DOWNLOAD_ERROR_NOT_CORRECT_FIELD(2, "Not correct field"), FILE_DOWNLOAD_ERROR_NOT_CORRECT_FIELD(2, "Not correct field"),
@@ -26,8 +27,10 @@ public enum MessageCode {
IMAGE_FOUND_ERROR(2, "Image found error"), IMAGE_FOUND_ERROR(2, "Image found error"),
INVALID_JSON_BODY(2, "Invalid fields in JSON object"), INVALID_JSON_BODY(2, "Invalid fields in JSON object"),
INCOMPLETE_UPLOAD(2, "Not load all chunks"), INCOMPLETE_UPLOAD(2, "Not load all chunks"),
USER_NOT_HAVE_TOKEN(2, "Not have tokens"),
MSG_ID_NOT_FOUND(4, "Message id not found"), MSG_ID_NOT_FOUND(4, "Message id not found"),
FILE_ENTITY_ERROR(2, "File entity error"), FILE_ENTITY_ERROR(2, "File entity error"),
NOT_VALID_FIELD(2, "Not valid field"),
ACCESS_DENIED(2, "Access denied"), ACCESS_DENIED(2, "Access denied"),
AUTH_EMAIL_NOT_FOUND(4, "Email not found"), AUTH_EMAIL_NOT_FOUND(4, "Email not found"),
AUTH_EMAIL_OR_TOKEN_NOT_FOUND(4, "Email or Token not found "), AUTH_EMAIL_OR_TOKEN_NOT_FOUND(4, "Email or Token not found "),
@@ -0,0 +1,35 @@
package ru.soune.nocopy.dto.complaint;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Builder;
import lombok.Data;
@Data
@Builder
public class BaseResponse {
@JsonProperty("status")
private String status;
@JsonProperty("message")
private String message;
@JsonProperty("data")
private Object data;
@JsonProperty("error_code")
private String errorCode;
public static BaseResponse success(Object data) {
return BaseResponse.builder()
.status("SUCCESS")
.data(data)
.build();
}
public static BaseResponse error(String message, String errorCode) {
return BaseResponse.builder()
.status("ERROR")
.message(message)
.errorCode(errorCode)
.build();
}
}
@@ -0,0 +1,17 @@
package ru.soune.nocopy.dto.complaint;
import lombok.Builder;
import lombok.Data;
import java.util.List;
@Data
@Builder
public class LawCasePageResponse {
private List<LawCaseResponse> content;
private int pageNumber;
private int pageSize;
private long totalElements;
private int totalPages;
private boolean last;
private boolean first;
}
@@ -0,0 +1,30 @@
package ru.soune.nocopy.dto.complaint;
import lombok.Data;
import ru.soune.nocopy.entity.complaint.LawCasePriority;
import ru.soune.nocopy.entity.complaint.LawCaseType;
import java.math.BigDecimal;
@Data
public class LawCaseRequest {
private Long id;
//required
private String name;
//required
private String description;
private BigDecimal amount;
private String priority = LawCasePriority.MIDDLE.toString();
private String type;
private String lawyer;
private String token;
private Integer pageSize = 10;
private Integer pageNumber = 0;
private String sortBy = "createdAt";
private String sortDir = "asc";
private String action;
private LawCaseType filterType;
private String filterLawyer;
private LawCasePriority filterPriority;
}
@@ -0,0 +1,44 @@
package ru.soune.nocopy.dto.complaint;
import lombok.Builder;
import lombok.Data;
import ru.soune.nocopy.entity.complaint.LawCase;
import ru.soune.nocopy.entity.complaint.LawCasePriority;
import ru.soune.nocopy.entity.complaint.LawCaseType;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
@Data
@Builder
public class LawCaseResponse {
private Long id;
private String name;
private String description;
private BigDecimal amount;
private LawCasePriority priority;
private LawCaseType type;
private String lawyer;
private LocalDateTime createdAt;
private LocalDateTime updatedAt;
private int pageNumber;
private int pageSize;
private long totalElements;
private int totalPages;
private List<LawCaseResponse> content;
public static LawCaseResponse fromEntity(LawCase lawCase) {
return LawCaseResponse.builder()
.id(lawCase.getId())
.name(lawCase.getName())
.description(lawCase.getDescription())
.amount(lawCase.getAmount())
.priority(lawCase.getPriority())
.type(lawCase.getType())
.lawyer(lawCase.getLawyer())
.createdAt(lawCase.getCreatedAt())
.updatedAt(lawCase.getUpdatedAt())
.build();
}
}
@@ -0,0 +1,17 @@
package ru.soune.nocopy.dto.complaint;
import lombok.AllArgsConstructor;
import lombok.Data;
import java.util.List;
@Data
@AllArgsConstructor
public class PaginatedResponse<T> {
private List<T> content;
private long totalElements;
private int totalPages;
private int pageNumber;
private int pageSize;
private boolean first;
private boolean last;
}
@@ -2,6 +2,12 @@ package ru.soune.nocopy.entity.complaint;
import jakarta.persistence.*; import jakarta.persistence.*;
import lombok.*; import lombok.*;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import ru.soune.nocopy.entity.user.User;
import java.math.BigDecimal;
import java.time.LocalDateTime;
@Entity @Entity
@AllArgsConstructor @AllArgsConstructor
@@ -14,4 +20,45 @@ public class LawCase {
@Id @Id
@GeneratedValue(strategy = GenerationType.IDENTITY) @GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id; private Long id;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_id", nullable = false)
@ToString.Exclude
private User user;
@Column(name = "name")
private String name;
@Column(length = 500, name = "description")
private String description;
@Column(name = "amount")
private BigDecimal amount;
@Column(name = "priority")
@Enumerated(EnumType.STRING)
private LawCasePriority priority;
@Column(name = "type")
@Enumerated(EnumType.STRING)
private LawCaseType type = LawCaseType.ACTIVE;
@CreatedDate
@Column(name = "created_at", updatable = false)
private LocalDateTime createdAt;
@LastModifiedDate
@Column(name = "updated_at")
private LocalDateTime updatedAt;
@Column(name = "lawyer")
private String lawyer;
@PrePersist
public void prePersist() {
if (this.createdAt == null) {
this.createdAt = LocalDateTime.now();
}
this.updatedAt = LocalDateTime.now();
}
} }
@@ -0,0 +1,12 @@
package ru.soune.nocopy.entity.complaint;
public enum LawCasePriority {
CRITICAL("critical"), HIGH("high"),
MIDDLE("middle"), LOW("low");
private String name;
private LawCasePriority(String name) {
this.name = name;
}
}
@@ -0,0 +1,15 @@
package ru.soune.nocopy.entity.complaint;
import lombok.Getter;
@Getter
public enum LawCaseType {
ACTIVE("active"), CLOSED("closed"),
COMPLETED("completed");
private String name;
private LawCaseType(String name) {
this.name = name;
}
}
@@ -0,0 +1,199 @@
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.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.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);
} 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()));
lawCase.setUser(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")));
}
}
@@ -0,0 +1,40 @@
package ru.soune.nocopy.repository;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import ru.soune.nocopy.entity.complaint.LawCase;
import ru.soune.nocopy.entity.complaint.LawCasePriority;
import ru.soune.nocopy.entity.complaint.LawCaseType;
import java.util.List;
@Repository
public interface LawCaseRepository extends JpaRepository<LawCase, Long> {
@Query("SELECT l FROM LawCase l WHERE l.user.Id = :userId")
List<LawCase> getUserLawCases(@Param("userId") Long userId, Pageable pageable);
// @Query("SELECT l FROM LawCase l WHERE l.user.Id = :userId " +
// "AND (:type IS NULL OR l.type = :type) " +
// "AND (:lawyer IS NULL OR l.lawyer = :lawyer) " +
// "AND (:priority IS NULL OR l.priority = :priority)")
// List<LawCase> getUserLawCases(@Param("userId") Long userId,
// @Param("type") LawCaseType type,
// @Param("lawyer") String lawyer,
// @Param("priority") LawCasePriority priority,
// Pageable pageable);
@Query("SELECT l FROM LawCase l WHERE l.user.Id = :userId " +
"AND (:type IS NULL OR l.type = :type) " +
"AND (:lawyer IS NULL OR l.lawyer = :lawyer) " +
"AND (:priority IS NULL OR l.priority = :priority)")
Page<LawCase> getUserLawCases(@Param("userId") Long userId,
@Param("type") LawCaseType type,
@Param("lawyer") String lawyer,
@Param("priority") LawCasePriority priority,
Pageable pageable);
}
@@ -0,0 +1,96 @@
package ru.soune.nocopy.service.complaint;
import lombok.RequiredArgsConstructor;
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.Service;
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.repository.LawCaseRepository;
import java.math.BigDecimal;
import java.util.List;
@Service
@RequiredArgsConstructor
public class LawCaseService {
private final LawCaseRepository lawCaseRepository;
public LawCase addLawCase(BigDecimal amount, String description, String name,
LawCasePriority priority) {
LawCase lawCase = new LawCase();
BigDecimal damage = amount == null ? BigDecimal.ZERO : amount;
lawCase.setAmount(damage);
lawCase.setName(name);
lawCase.setDescription(description);
lawCase.setPriority(priority);
return lawCaseRepository.save(lawCase);
}
public LawCase getById(Long id) {
return lawCaseRepository.findById(id).orElse(null);
}
public LawCase changeStatus(Long id, LawCaseType type) {
LawCase lawCase = lawCaseRepository.findById(id).orElse(null);
if (lawCase == null) {
return null;
}
lawCase.setType(type);
return lawCaseRepository.save(lawCase);
}
public LawCase changePriority(Long id, LawCasePriority priority) {
LawCase lawCase = lawCaseRepository.findById(id).orElse(null);
if (lawCase == null) {
return null;
}
lawCase.setPriority(priority);
return lawCaseRepository.save(lawCase);
}
public LawCase changeDamage(Long id, BigDecimal damage) {
LawCase lawCase = lawCaseRepository.findById(id).orElse(null);
if (lawCase == null) {
return null;
}
lawCase.setAmount(damage);
return lawCaseRepository.save(lawCase);
}
public List<LawCase> getAllUserLawCases(Long userId, int pageSize, int pageNumber, String propertySort,
String ascDesc) {
Sort.Direction direction = ascDesc.equalsIgnoreCase("desc") ? Sort.Direction.DESC:
Sort.Direction.ASC;
Sort sort = Sort.by(direction, propertySort);
Pageable pageable = PageRequest.of(pageNumber, pageSize, sort);
return lawCaseRepository.getUserLawCases(userId, pageable);
}
public Page<LawCase> getAllUserLawCases(Long userId, int pageSize, int pageNumber,
LawCaseType lawCaseType, String lawyer,
LawCasePriority lawCasePriority,
String propertySort, String ascDesc) {
Sort.Direction direction = ascDesc.equalsIgnoreCase("desc") ? Sort.Direction.DESC : Sort.Direction.ASC;
Sort sort = Sort.by(direction, propertySort);
Pageable pageable = PageRequest.of(pageNumber, pageSize, sort);
return lawCaseRepository.getUserLawCases(userId, lawCaseType, lawyer, lawCasePriority, pageable);
}
public void deleteById(Long id) {
lawCaseRepository.deleteById(id);
}
}
@@ -18,4 +18,5 @@ public class TariffConstants {
public static final Integer AUDIO_TOKEN_VALUE = 8; public static final Integer AUDIO_TOKEN_VALUE = 8;
public static final Integer PDF_TOKEN_VALUE = 6; public static final Integer PDF_TOKEN_VALUE = 6;
public static final Integer TOKEN_VALUE_FOR_SEARCH = 5; public static final Integer TOKEN_VALUE_FOR_SEARCH = 5;
public static final Integer LEGAL_COST = 100;
} }