diff --git a/hub-prime/src/main/java/org/techbd/controller/http/hub/prime/api/CsvController.java b/hub-prime/src/main/java/org/techbd/controller/http/hub/prime/api/CsvController.java index 473bd5a826..f5db8f9f11 100644 --- a/hub-prime/src/main/java/org/techbd/controller/http/hub/prime/api/CsvController.java +++ b/hub-prime/src/main/java/org/techbd/controller/http/hub/prime/api/CsvController.java @@ -4,7 +4,6 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.scheduling.annotation.Async; @@ -34,6 +33,24 @@ public CsvController(CsvService csvService) { this.csvService = csvService; } + private void validateFile(MultipartFile file) { + if (file == null || file.isEmpty() || file.getOriginalFilename() == null + || file.getOriginalFilename().trim().isEmpty()) { + throw new IllegalArgumentException("Validation Error: Uploaded file is missing or empty."); + } + + String originalFilename = file.getOriginalFilename(); + if (!originalFilename.toLowerCase().endsWith(".zip")) { + throw new IllegalArgumentException("Validation Error: Uploaded file must have a .zip extension."); + } + } + + private void validateTenantId(String tenantId) { + if (tenantId == null || tenantId.trim().isEmpty()) { + throw new IllegalArgumentException("Tenant ID must be provided."); + } + } + @PostMapping(value = { "/flatfile/csv/Bundle/$validate", "/flatfile/csv/Bundle/$validate/" }, consumes = MediaType.MULTIPART_FORM_DATA_VALUE) @ResponseBody @@ -46,34 +63,16 @@ public Object handleCsvUpload( HttpServletResponse response) throws Exception { - // Validate file presence - if (file == null || file.isEmpty() || file.getOriginalFilename() == null - || file.getOriginalFilename().trim().isEmpty()) { - log.error("CsvController: handleCsvUpload:: Uploaded file is missing or empty"); - return new ResponseEntity<>( - "{\"status\":\"Error\",\"message\":\"Validation Error: Uploaded file is missing or empty.\"}", - HttpStatus.BAD_REQUEST); - } - // Validate file extension - String originalFilename = file.getOriginalFilename(); - if (!originalFilename.toLowerCase().endsWith(".zip")) { - log.error("CsvController: handleCsvUpload:: Uploaded file is not a .zip file. Filename: " + originalFilename); - return new ResponseEntity<>( - "{\"status\":\"Error\",\"message\":\"Validation Error: Uploaded file must have a .zip extension.\"}", - HttpStatus.BAD_REQUEST); - } - if (tenantId == null || tenantId.trim().isEmpty()) { - log.error("CsvController: handleCsvUpload:: Tenant ID is missing or empty"); - throw new IllegalArgumentException("Tenant ID must be provided"); - } - return csvService.validateCsvFile(file, request, response, tenantId,origin,sftpSessionId); + validateFile(file); + validateTenantId(tenantId); + return csvService.validateCsvFile(file, request, response, tenantId, origin, sftpSessionId); } @PostMapping(value = { "/flatfile/csv/Bundle", "/flatfile/csv/Bundle/" }, consumes = { MediaType.MULTIPART_FORM_DATA_VALUE }) @ResponseBody @Async - public List handleCsvUploadAndConversion( + public ResponseEntity handleCsvUploadAndConversion( @Parameter(description = "ZIP file containing CSV data. Must not be null.", required = true) @RequestPart("file") @Nonnull MultipartFile file, @Parameter(description = "Parameter to specify the Tenant ID. This is a mandatory parameter.", required = true) @RequestHeader(value = Configuration.Servlet.HeaderName.Request.TENANT_ID, required = true) String tenantId, @Parameter(description = "Parameter to specify origin of the request.", required = false) @RequestParam(value = "origin", required = false,defaultValue = "HTTP") String origin, @@ -81,10 +80,11 @@ public List handleCsvUploadAndConversion( @Parameter(description = "Optional parameter to specify if transformed validation issue needs to be appended.", required = false) @RequestParam(value = "append-validation-issue", required = false, defaultValue = "true") boolean appendValidationIssue, HttpServletRequest request, HttpServletResponse response) throws Exception { - - if (tenantId == null || tenantId.trim().isEmpty()) { - throw new IllegalArgumentException("Tenant ID must be provided"); - } - return csvService.processZipFile(file, request, response, tenantId,origin,sftpSessionId,appendValidationIssue); + + validateFile(file); + validateTenantId(tenantId); + List processedFiles = csvService.processZipFile(file, request, response, tenantId, origin, sftpSessionId,appendValidationIssue); + return ResponseEntity.ok(processedFiles); + } }