target fix
This commit is contained in:
@@ -29,6 +29,7 @@ public class TargetingCampaignController {
|
|||||||
private final SocialMediaCredentialsService credentialsService;
|
private final SocialMediaCredentialsService credentialsService;
|
||||||
private final JwtService jwtService;
|
private final JwtService jwtService;
|
||||||
private final CampaignPredictionService predictionService;
|
private final CampaignPredictionService predictionService;
|
||||||
|
private final MarketingStrategyV3Service marketingStrategyV3Service;
|
||||||
|
|
||||||
private String resolveUserId(String userIdHeader, String authHeader) {
|
private String resolveUserId(String userIdHeader, String authHeader) {
|
||||||
if (StringUtils.hasText(userIdHeader)) {
|
if (StringUtils.hasText(userIdHeader)) {
|
||||||
@@ -288,6 +289,31 @@ public class TargetingCampaignController {
|
|||||||
return ResponseEntity.ok(campaignService.updateAudience(id, userId, audience));
|
return ResponseEntity.ok(campaignService.updateAudience(id, userId, audience));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ── Direct Facebook Publish ──────────────────────────────────────────────
|
||||||
|
|
||||||
|
@PostMapping("/strategy/{strategyId}/facebook/publish")
|
||||||
|
public ResponseEntity<?> publishFirstFacebookPostFromStrategy(
|
||||||
|
@RequestHeader(value = "X-User-Id", required = false) String userIdHeader,
|
||||||
|
@RequestHeader(value = "Authorization", required = false) String authHeader,
|
||||||
|
@PathVariable String strategyId) {
|
||||||
|
String userId = resolveUserId(userIdHeader, authHeader);
|
||||||
|
log.info("[Targeting] Direct Facebook publish for strategy {} requested by {}", strategyId, userId);
|
||||||
|
|
||||||
|
try {
|
||||||
|
String postId = marketingStrategyV3Service.publishFirstFacebookPost(strategyId);
|
||||||
|
return ResponseEntity.ok(Map.of(
|
||||||
|
"success", true,
|
||||||
|
"message", "Пост успешно опубликован в Facebook",
|
||||||
|
"postId", postId
|
||||||
|
));
|
||||||
|
} catch (IllegalArgumentException | IllegalStateException e) {
|
||||||
|
return ResponseEntity.badRequest().body(Map.of("success", false, "error", e.getMessage()));
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("Failed to publish post: ", e);
|
||||||
|
return ResponseEntity.internalServerError().body(Map.of("success", false, "error", "Не удалось опубликовать пост"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// ── AdSets ───────────────────────────────────────────────────────────────
|
// ── AdSets ───────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
@GetMapping("/campaigns/{id}/adsets")
|
@GetMapping("/campaigns/{id}/adsets")
|
||||||
|
|||||||
@@ -37,7 +37,6 @@ public class MarketingStrategyService {
|
|||||||
private final MarketingAnalysisRepository analysisRepository;
|
private final MarketingAnalysisRepository analysisRepository;
|
||||||
private final MarketingAnalysisV2Repository v2Repository;
|
private final MarketingAnalysisV2Repository v2Repository;
|
||||||
private final OpenAIAnalyticsService openAIAnalyticsService;
|
private final OpenAIAnalyticsService openAIAnalyticsService;
|
||||||
private final ImageGenerationService imageGenerationService;
|
|
||||||
private final MinIOService minIOService;
|
private final MinIOService minIOService;
|
||||||
private final PostingTaskService postingTaskService;
|
private final PostingTaskService postingTaskService;
|
||||||
private final ObjectMapper objectMapper = new ObjectMapper();
|
private final ObjectMapper objectMapper = new ObjectMapper();
|
||||||
|
|||||||
@@ -40,6 +40,7 @@ public class MarketingStrategyV3Service {
|
|||||||
private final GeminiVideoGenerationService geminiVideoService;
|
private final GeminiVideoGenerationService geminiVideoService;
|
||||||
private final PostingTaskService postingTaskService;
|
private final PostingTaskService postingTaskService;
|
||||||
private final MinIOService minIOService;
|
private final MinIOService minIOService;
|
||||||
|
private final FacebookPostingService facebookPostingService;
|
||||||
|
|
||||||
@Autowired @Lazy
|
@Autowired @Lazy
|
||||||
private MarketingStrategyV3Service self;
|
private MarketingStrategyV3Service self;
|
||||||
@@ -1185,4 +1186,64 @@ public class MarketingStrategyV3Service {
|
|||||||
repository.save(s);
|
repository.save(s);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// =====================================================================
|
||||||
|
// FACEBOOK DIRECT PUBLISH
|
||||||
|
// =====================================================================
|
||||||
|
|
||||||
|
public String publishFirstFacebookPost(String strategyId) {
|
||||||
|
Optional<MarketingStrategy> optStrategy = repository.findById(strategyId);
|
||||||
|
if (optStrategy.isEmpty()) {
|
||||||
|
throw new IllegalArgumentException("Strategy not found: " + strategyId);
|
||||||
|
}
|
||||||
|
|
||||||
|
MarketingStrategy strategy = optStrategy.get();
|
||||||
|
if (strategy.getPostCalendar() == null || strategy.getPostCalendar().isEmpty()) {
|
||||||
|
throw new IllegalStateException("Strategy has no posts in calendar");
|
||||||
|
}
|
||||||
|
|
||||||
|
Optional<MarketingStrategy.PostCalendarItem> fbPostOpt = strategy.getPostCalendar().stream()
|
||||||
|
.filter(item -> "facebook".equalsIgnoreCase(item.getPlatform()))
|
||||||
|
.findFirst();
|
||||||
|
|
||||||
|
if (fbPostOpt.isEmpty()) {
|
||||||
|
throw new IllegalStateException("No Facebook posts found in the strategy calendar");
|
||||||
|
}
|
||||||
|
|
||||||
|
MarketingStrategy.PostCalendarItem fbPost = fbPostOpt.get();
|
||||||
|
|
||||||
|
byte[] imageData = null;
|
||||||
|
if (fbPost.getImageFilename() != null && !fbPost.getImageFilename().isEmpty()) {
|
||||||
|
try {
|
||||||
|
java.io.InputStream imageStream = minIOService.downloadFile(fbPost.getImageFilename());
|
||||||
|
imageData = imageStream.readAllBytes();
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.warn("Failed to load image for post: {}", e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
String postId;
|
||||||
|
if (imageData != null && imageData.length > 0) {
|
||||||
|
postId = facebookPostingService.postToPageWithImage(fbPost.getPostText(), fbPost.getHashtags(), imageData);
|
||||||
|
} else {
|
||||||
|
postId = facebookPostingService.postToPage(fbPost.getPostText(), fbPost.getHashtags());
|
||||||
|
}
|
||||||
|
|
||||||
|
if (postId != null) {
|
||||||
|
try {
|
||||||
|
String reportText = "✅ НОВЫЙ ПОСТ ОПУБЛИКОВАН!\n\n" +
|
||||||
|
"📜 Текст поста:\n" + fbPost.getPostText() + "\n\n" +
|
||||||
|
"🔗 Ссылка: https://facebook.com/" + postId + "\n" +
|
||||||
|
"🕒 Время: " + LocalDateTime.now();
|
||||||
|
facebookPostingService.sendPrivateMessage("25769470256007187", reportText); // TEST_RECIPIENT_ID
|
||||||
|
if (imageData != null && imageData.length > 0) {
|
||||||
|
facebookPostingService.sendPrivateImageMessage("25769470256007187", imageData);
|
||||||
|
}
|
||||||
|
} catch (Exception msgEx) {
|
||||||
|
log.warn("Post published but failed to send private notification: {}", msgEx.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return postId;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -56,6 +56,9 @@ public class PostingTaskService {
|
|||||||
.toList();
|
.toList();
|
||||||
|
|
||||||
for (String platform : platforms) {
|
for (String platform : platforms) {
|
||||||
|
if ("facebook".equalsIgnoreCase(platform)) {
|
||||||
|
continue; // Игнорируем проверку БД, так как Facebook берет ключи из application.properties
|
||||||
|
}
|
||||||
if (!credentialsService.hasCredentials(strategy.getUserId(), platform)) {
|
if (!credentialsService.hasCredentials(strategy.getUserId(), platform)) {
|
||||||
throw new IllegalStateException(
|
throw new IllegalStateException(
|
||||||
"Credentials not found for platform: " + platform + ". Please configure credentials first.");
|
"Credentials not found for platform: " + platform + ". Please configure credentials first.");
|
||||||
@@ -126,9 +129,12 @@ public class PostingTaskService {
|
|||||||
taskRepository.save(task);
|
taskRepository.save(task);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
String credentials = credentialsService.getCredentials(task.getUserId(), task.getPlatform());
|
String credentials = null;
|
||||||
if (credentials == null || credentials.isEmpty()) {
|
if (!"facebook".equalsIgnoreCase(task.getPlatform())) {
|
||||||
throw new IllegalStateException("Credentials not found for platform: " + task.getPlatform());
|
credentials = credentialsService.getCredentials(task.getUserId(), task.getPlatform());
|
||||||
|
if (credentials == null || credentials.isEmpty()) {
|
||||||
|
throw new IllegalStateException("Credentials not found for platform: " + task.getPlatform());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
byte[] imageData = null;
|
byte[] imageData = null;
|
||||||
|
|||||||
Reference in New Issue
Block a user