This commit is contained in:
@@ -50,6 +50,8 @@ public enum MessageCode {
|
|||||||
USER_LIMIT_IS_OVER(2, "Over user limits"),
|
USER_LIMIT_IS_OVER(2, "Over user limits"),
|
||||||
ERROR_TARIFF_INFO(2, "Erorr with tariff info"),
|
ERROR_TARIFF_INFO(2, "Erorr with tariff info"),
|
||||||
FILE_FOR_SEARCH_NOT_VALID(2, "File for search unsupported"),
|
FILE_FOR_SEARCH_NOT_VALID(2, "File for search unsupported"),
|
||||||
|
NOT_VALID_FILE_TYPE_OR_COUNT_FILE(2, "Cost for file type not found, count is negative or files count" +
|
||||||
|
"more than max valid"),
|
||||||
USER_NOT_ACTIVE(2, "User not active");
|
USER_NOT_ACTIVE(2, "User not active");
|
||||||
|
|
||||||
private final Integer code;
|
private final Integer code;
|
||||||
|
|||||||
@@ -11,6 +11,12 @@ public class CostResponse {
|
|||||||
|
|
||||||
private Boolean success;
|
private Boolean success;
|
||||||
|
|
||||||
|
@JsonProperty("max_files_for_check")
|
||||||
|
private Integer maxFilesForCheck;
|
||||||
|
|
||||||
|
@JsonProperty("count_file")
|
||||||
|
private Integer countFile;
|
||||||
|
|
||||||
@JsonProperty("tokens_count")
|
@JsonProperty("tokens_count")
|
||||||
private int needTokensCount;
|
private int needTokensCount;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -27,6 +27,8 @@ public class CostHandler implements RequestHandler {
|
|||||||
|
|
||||||
private final ObjectMapper objectMapper;
|
private final ObjectMapper objectMapper;
|
||||||
|
|
||||||
|
private final Integer MAX_FILES_FOR_CHECK = 1000;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public BaseResponse handle(BaseRequest request) throws Exception {
|
public BaseResponse handle(BaseRequest request) throws Exception {
|
||||||
CostRequest costRequest = objectMapper.convertValue(request.getMessageBody(), CostRequest.class);
|
CostRequest costRequest = objectMapper.convertValue(request.getMessageBody(), CostRequest.class);
|
||||||
@@ -46,6 +48,14 @@ public class CostHandler implements RequestHandler {
|
|||||||
private BaseResponse handleFileProtectCost(CostRequest costRequest, BaseRequest request) {
|
private BaseResponse handleFileProtectCost(CostRequest costRequest, BaseRequest request) {
|
||||||
int cost = costService.protectFileCost(costRequest.getFileType());
|
int cost = costService.protectFileCost(costRequest.getFileType());
|
||||||
|
|
||||||
|
if (cost == 0) {
|
||||||
|
return new BaseResponse(request.getMsgId(), MessageCode.NOT_VALID_FILE_TYPE_OR_COUNT_FILE.getCode(),
|
||||||
|
MessageCode.NOT_VALID_FILE_TYPE_OR_COUNT_FILE.getDescription(),
|
||||||
|
CostResponse.builder()
|
||||||
|
.cost(cost)
|
||||||
|
.build());
|
||||||
|
}
|
||||||
|
|
||||||
return new BaseResponse(request.getMsgId(), MessageCode.SUCCESS.getCode(),
|
return new BaseResponse(request.getMsgId(), MessageCode.SUCCESS.getCode(),
|
||||||
MessageCode.SUCCESS.getDescription(),
|
MessageCode.SUCCESS.getDescription(),
|
||||||
CostResponse.builder()
|
CostResponse.builder()
|
||||||
@@ -70,6 +80,17 @@ public class CostHandler implements RequestHandler {
|
|||||||
|
|
||||||
int cost = costService.protectFileCost(costRequest.getFileType());
|
int cost = costService.protectFileCost(costRequest.getFileType());
|
||||||
int count = costRequest.getCountFilesForProtect();
|
int count = costRequest.getCountFilesForProtect();
|
||||||
|
|
||||||
|
if (cost == 0 || count <= 0 || count > MAX_FILES_FOR_CHECK) {
|
||||||
|
return new BaseResponse(request.getMsgId(), MessageCode.NOT_VALID_FILE_TYPE_OR_COUNT_FILE.getCode(),
|
||||||
|
MessageCode.NOT_VALID_FILE_TYPE_OR_COUNT_FILE.getDescription(),
|
||||||
|
CostResponse.builder()
|
||||||
|
.cost(cost)
|
||||||
|
.countFile(count)
|
||||||
|
.maxFilesForCheck(MAX_FILES_FOR_CHECK)
|
||||||
|
.build());
|
||||||
|
}
|
||||||
|
|
||||||
int totalCost = count * cost;
|
int totalCost = count * cost;
|
||||||
boolean success = totalCost <= tokens;
|
boolean success = totalCost <= tokens;
|
||||||
int needToken = success ? 0: tokens - totalCost;
|
int needToken = success ? 0: tokens - totalCost;
|
||||||
|
|||||||
@@ -22,8 +22,8 @@ public class CostService {
|
|||||||
return TariffConstants.AUDIO_TOKEN_VALUE;
|
return TariffConstants.AUDIO_TOKEN_VALUE;
|
||||||
case "document":
|
case "document":
|
||||||
return TariffConstants.PDF_TOKEN_VALUE;
|
return TariffConstants.PDF_TOKEN_VALUE;
|
||||||
|
default:
|
||||||
|
return cost;
|
||||||
}
|
}
|
||||||
|
|
||||||
return cost;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user