fix
This commit is contained in:
@@ -38,6 +38,7 @@ public class MarketingAnalysisV3Controller {
|
|||||||
private final MinIOService minIOService;
|
private final MinIOService minIOService;
|
||||||
private final JwtService jwtService;
|
private final JwtService jwtService;
|
||||||
|
|
||||||
|
// ЕДИНЫЙ МЕТОД ДЛЯ ИЗВЛЕЧЕНИЯ USER ID ИЗ JWT (КАК В СТАРОМ КОНТРОЛЛЕРЕ)
|
||||||
private String extractUserIdFromHeader(String authHeader) {
|
private String extractUserIdFromHeader(String authHeader) {
|
||||||
if (authHeader == null || authHeader.isEmpty()) {
|
if (authHeader == null || authHeader.isEmpty()) {
|
||||||
return null;
|
return null;
|
||||||
@@ -45,7 +46,7 @@ public class MarketingAnalysisV3Controller {
|
|||||||
try {
|
try {
|
||||||
return jwtService.extractUserIdFromHeader(authHeader);
|
return jwtService.extractUserIdFromHeader(authHeader);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error("Error extracting userId: {}", e.getMessage());
|
log.error("Error extracting userId from JWT: {}", e.getMessage());
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -153,31 +154,50 @@ public class MarketingAnalysisV3Controller {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* ГЕНЕРАЦИЯ СТРАТЕГИИ: Принимает настройки и файл логотипа (опционально)
|
* ГЕНЕРАЦИЯ СТРАТЕГИИ: Принимает настройки и файл логотипа (опционально)
|
||||||
|
* ИСПОЛЬЗУЕТ JWT ДЛЯ АВТОРИЗАЦИИ (БЕЗ РУЧНОГО USER-ID)
|
||||||
*/
|
*/
|
||||||
@PostMapping(value = "/{analysisId}/strategy/generate", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
|
@PostMapping(value = "/{analysisId}/strategy/generate", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
|
||||||
public ResponseEntity<MarketingStrategy> generateStrategy(
|
public ResponseEntity<?> generateStrategy(
|
||||||
|
@RequestHeader(value = "Authorization", required = false) String authHeader,
|
||||||
@PathVariable String analysisId,
|
@PathVariable String analysisId,
|
||||||
// Данные стратегии принимаем как отдельную часть формы (в Postman это будет текст/JSON)
|
@RequestPart("request") @Valid MarketingStrategyRequest request,
|
||||||
@RequestPart("request") MarketingStrategyRequest request,
|
@RequestPart(value = "logo", required = false) MultipartFile logoFile
|
||||||
// Файл принимаем как MultipartFile
|
) {
|
||||||
@RequestPart(value = "logo", required = false) MultipartFile logoFile,
|
// Достаем ID пользователя из JWT токена
|
||||||
@RequestHeader("User-Id") String userId) {
|
String userId = extractUserIdFromHeader(authHeader);
|
||||||
|
if (userId == null) return unauthorizedResponse();
|
||||||
|
|
||||||
String uploadedLogoFilename = null;
|
try {
|
||||||
|
// Проверяем доступ к анализу
|
||||||
|
Optional<MarketingAnalysisV3Document> analysisOpt = analysisService.getAnalysisById(analysisId);
|
||||||
|
if (analysisOpt.isEmpty()) return notFoundResponse("Анализ не найден");
|
||||||
|
if (!analysisOpt.get().getUserId().equals(userId)) return forbiddenResponse();
|
||||||
|
|
||||||
// Если файл прислали, сохраняем его в MinIO
|
String logoFilename = null;
|
||||||
if (logoFile != null && !logoFile.isEmpty()) {
|
|
||||||
try {
|
// Если юзер прикрепил логотип, сохраняем его в MinIO
|
||||||
uploadedLogoFilename = "logo_" + System.currentTimeMillis() + "_" + logoFile.getOriginalFilename();
|
if (logoFile != null && !logoFile.isEmpty()) {
|
||||||
minIOService.uploadFile(uploadedLogoFilename, logoFile.getBytes(), logoFile.getContentType());
|
String originalExt = logoFile.getOriginalFilename() != null && logoFile.getOriginalFilename().contains(".") ?
|
||||||
} catch (Exception e) {
|
logoFile.getOriginalFilename().substring(logoFile.getOriginalFilename().lastIndexOf(".")) : ".png";
|
||||||
return ResponseEntity.internalServerError().build();
|
logoFilename = "logo_" + UUID.randomUUID() + originalExt;
|
||||||
|
minIOService.uploadFile(logoFilename, logoFile.getBytes(), logoFile.getContentType());
|
||||||
|
log.info("Логотип клиента успешно загружен в MinIO: {}", logoFilename);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// Передаем имя файла дальше в твой сервис
|
// Запускаем полную генерацию, передавая userId из токена
|
||||||
MarketingStrategy strategy = strategyService.generateStrategy(analysisId, request, userId, uploadedLogoFilename);
|
MarketingStrategy strategy = strategyService.generateStrategy(analysisId, request, userId, logoFilename);
|
||||||
return ResponseEntity.ok(strategy);
|
|
||||||
|
Map<String, String> responseData = Map.of(
|
||||||
|
"strategyId", strategy.getId(),
|
||||||
|
"status", strategy.getStatus(),
|
||||||
|
"message", "Генерация стратегии и медиафайлов успешно запущена"
|
||||||
|
);
|
||||||
|
return ResponseEntity.accepted().body(ApiResponse.success("Запущено", responseData));
|
||||||
|
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("Failed to generate strategy for analysis: {}", analysisId, e);
|
||||||
|
return internalErrorResponse(e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -192,7 +212,6 @@ public class MarketingAnalysisV3Controller {
|
|||||||
if (userId == null) return unauthorizedResponse();
|
if (userId == null) return unauthorizedResponse();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Предполагается, что в MarketingStrategyV3Service есть метод getStrategyById
|
|
||||||
Optional<MarketingStrategy> strategyOpt = strategyService.getStrategyById(strategyId);
|
Optional<MarketingStrategy> strategyOpt = strategyService.getStrategyById(strategyId);
|
||||||
if (strategyOpt.isEmpty()) return notFoundResponse("Стратегия не найдена");
|
if (strategyOpt.isEmpty()) return notFoundResponse("Стратегия не найдена");
|
||||||
|
|
||||||
@@ -207,7 +226,7 @@ public class MarketingAnalysisV3Controller {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ==========================================
|
// ==========================================
|
||||||
// БЛОК 3: ОБРАБОТКА ОШИБОК
|
// БЛОК 3: ОБРАБОТКА ОШИБОК И ОТВЕТЫ
|
||||||
// ==========================================
|
// ==========================================
|
||||||
|
|
||||||
@ExceptionHandler(MethodArgumentNotValidException.class)
|
@ExceptionHandler(MethodArgumentNotValidException.class)
|
||||||
@@ -222,12 +241,12 @@ public class MarketingAnalysisV3Controller {
|
|||||||
|
|
||||||
private ResponseEntity<ApiResponse<Object>> unauthorizedResponse() {
|
private ResponseEntity<ApiResponse<Object>> unauthorizedResponse() {
|
||||||
return ResponseEntity.status(HttpStatus.UNAUTHORIZED)
|
return ResponseEntity.status(HttpStatus.UNAUTHORIZED)
|
||||||
.body(ApiResponse.error("Не авторизован", new ErrorResponse("UNAUTHORIZED", "Требуется авторизация")));
|
.body(ApiResponse.error("Не авторизован", new ErrorResponse("UNAUTHORIZED", "Требуется авторизация. Предоставьте валидный JWT токен.")));
|
||||||
}
|
}
|
||||||
|
|
||||||
private ResponseEntity<ApiResponse<Object>> forbiddenResponse() {
|
private ResponseEntity<ApiResponse<Object>> forbiddenResponse() {
|
||||||
return ResponseEntity.status(HttpStatus.FORBIDDEN)
|
return ResponseEntity.status(HttpStatus.FORBIDDEN)
|
||||||
.body(ApiResponse.error("Доступ запрещен", new ErrorResponse("FORBIDDEN", "Нет доступа")));
|
.body(ApiResponse.error("Доступ запрещен", new ErrorResponse("FORBIDDEN", "У вас нет прав для доступа к этому ресурсу.")));
|
||||||
}
|
}
|
||||||
|
|
||||||
private ResponseEntity<ApiResponse<Object>> notFoundResponse(String message) {
|
private ResponseEntity<ApiResponse<Object>> notFoundResponse(String message) {
|
||||||
|
|||||||
Reference in New Issue
Block a user