-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCustomContentWebhookService.java
More file actions
200 lines (163 loc) · 9.75 KB
/
CustomContentWebhookService.java
File metadata and controls
200 lines (163 loc) · 9.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package com.linglevel.api.content.custom.service;
import com.linglevel.api.content.custom.dto.*;
import com.linglevel.api.content.custom.entity.ContentRequest;
import com.linglevel.api.content.custom.entity.ContentRequestStatus;
import com.linglevel.api.content.custom.entity.CustomContent;
import com.linglevel.api.content.custom.exception.CustomContentErrorCode;
import com.linglevel.api.content.custom.exception.CustomContentException;
import com.linglevel.api.content.custom.repository.ContentRequestRepository;
import com.linglevel.api.content.custom.repository.CustomContentRepository;
import com.linglevel.api.s3.service.S3AiService;
import com.linglevel.api.s3.service.S3TransferService;
import com.linglevel.api.s3.service.S3UrlService;
import com.linglevel.api.s3.strategy.CustomContentPathStrategy;
import com.linglevel.api.user.ticket.service.TicketService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import java.time.Instant;
@Service
@RequiredArgsConstructor
@Slf4j
public class CustomContentWebhookService {
private final ContentRequestRepository contentRequestRepository;
private final CustomContentRepository customContentRepository;
private final UserCustomContentService userCustomContentService;
private final CustomContentImportService customContentImportService;
private final CustomContentReadingTimeService customContentReadingTimeService;
private final S3AiService s3AiService;
private final S3TransferService s3TransferService;
private final S3UrlService s3UrlService;
private final CustomContentPathStrategy pathStrategy;
private final CustomContentNotificationService notificationService;
private final TicketService ticketService;
@Transactional
public CustomContentCompletedResponse handleContentCompleted(CustomContentCompletedRequest request) {
log.info("Handling content completion for request: {}", request.getRequestId());
try {
// 1. Get ContentRequest and AiResultDto
ContentRequest contentRequest = contentRequestRepository.findById(request.getRequestId())
.orElseThrow(() -> new CustomContentException(CustomContentErrorCode.CONTENT_REQUEST_NOT_FOUND));
AiResultDto aiResult = s3AiService.downloadJsonFile(
request.getRequestId(),
AiResultDto.class,
pathStrategy
);
// 2. Create the main CustomContent entity
CustomContent savedContent = customContentImportService.createCustomContent(contentRequest, aiResult);
// 3. Create UserCustomContent mapping for this user
userCustomContentService.createMapping(contentRequest, savedContent);
// 4. Transfer S3 images from AI temp location to static location
transferS3ImagesAndUpdateCoverUrl(request.getRequestId(), savedContent, aiResult);
// Save updated content with permanent cover image URL
savedContent = customContentRepository.save(savedContent);
// 5. Create all associated chunks with permanent image URLs
customContentImportService.createCustomContentChunks(savedContent, aiResult);
// 6. Calculate and update reading time
customContentReadingTimeService.updateReadingTime(savedContent.getId());
// 7. Update ContentRequest status
contentRequest.setResultCustomContentId(savedContent.getId());
contentRequest.setStatus(ContentRequestStatus.COMPLETED);
contentRequest.setCompletedAt(Instant.now());
contentRequestRepository.save(contentRequest);
// 8. Send notification
notificationService.sendContentCompletedNotification(
contentRequest.getUserId(),
request.getRequestId(),
aiResult.getTitle(),
savedContent.getId()
);
log.info("Successfully processed AI result for request: {}", request.getRequestId());
return CustomContentCompletedResponse.builder()
.requestId(request.getRequestId())
.status("completed")
.build();
} catch (Exception e) {
log.error("Failed to process content completion for request: {}. Error: {}", request.getRequestId(), e.getMessage());
// Handle failure case with proper exception handling
handleContentProcessingFailure(request.getRequestId(), e);
throw new CustomContentException(CustomContentErrorCode.AI_RESULT_PROCESSING_FAILED, e.getMessage());
}
}
@Transactional
public void handleContentFailed(CustomContentFailedRequest request) {
log.info("Handling content failure for request: {}", request.getRequestId());
try {
ContentRequest contentRequest = contentRequestRepository.findById(request.getRequestId())
.orElseThrow(() -> new CustomContentException(CustomContentErrorCode.CONTENT_REQUEST_NOT_FOUND));
contentRequest.setStatus(ContentRequestStatus.FAILED);
contentRequest.setErrorMessage(request.getErrorMessage());
contentRequestRepository.save(contentRequest);
// 티켓 복원 (1개 환불)
try {
ticketService.grantTicket(contentRequest.getUserId(), 1, "Content creation failed - refund");
log.info("Ticket refunded for failed request: {}", request.getRequestId());
} catch (Exception ticketE) {
log.error("Failed to refund ticket for request: {}. Error: {}", request.getRequestId(), ticketE.getMessage());
}
String titleForNotification = StringUtils.hasText(contentRequest.getTitle())
? contentRequest.getTitle()
: "Untitled Content";
notificationService.sendContentFailedNotification(
contentRequest.getUserId(),
request.getRequestId(),
titleForNotification,
request.getErrorMessage()
);
log.info("Updated request status to FAILED for request: {}", request.getRequestId());
} catch (Exception e) {
log.error("Failed to handle content failure for request: {}. Error: {}", request.getRequestId(), e.getMessage());
throw new CustomContentException(CustomContentErrorCode.WEBHOOK_PROCESSING_FAILED, e.getMessage());
}
}
@Transactional
public void handleContentProgress(CustomContentProgressRequest request) {
log.info("Handling content progress for request: {} - {}%", request.getRequestId(), request.getProgress());
try {
ContentRequest contentRequest = contentRequestRepository.findById(request.getRequestId())
.orElseThrow(() -> new CustomContentException(CustomContentErrorCode.CONTENT_REQUEST_NOT_FOUND));
contentRequest.setStatus(ContentRequestStatus.PROCESSING);
contentRequest.setProgress(request.getProgress());
contentRequestRepository.save(contentRequest);
log.info("Updated progress to {}% for request: {}", request.getProgress(), request.getRequestId());
} catch (Exception e) {
log.error("Failed to handle content progress for request: {}. Error: {}", request.getRequestId(), e.getMessage());
throw new CustomContentException(CustomContentErrorCode.WEBHOOK_PROCESSING_FAILED, e.getMessage());
}
}
private void transferS3ImagesAndUpdateCoverUrl(String requestId, CustomContent customContent, AiResultDto aiResult) {
try {
s3TransferService.transferImagesFromAiToStatic(requestId, customContent.getId(), pathStrategy);
if (StringUtils.hasText(aiResult.getCoverImageUrl())) {
String permanentCoverImageUrl = s3UrlService.getCoverImageUrl(customContent.getId(), pathStrategy);
customContent.setCoverImageUrl(permanentCoverImageUrl);
}
} catch (Exception e) {
log.error("Failed to transfer S3 images for content: {}. Error: {}", customContent.getId(), e.getMessage());
// Don't fail the entire process for S3 transfer issues
}
}
private void handleContentProcessingFailure(String requestId, Exception originalException) {
try {
ContentRequest contentRequest = contentRequestRepository.findById(requestId)
.orElse(null);
if (contentRequest != null) {
contentRequest.setStatus(ContentRequestStatus.FAILED);
contentRequest.setErrorMessage("AI 결과 처리 실패: " + originalException.getMessage());
contentRequestRepository.save(contentRequest);
// 티켓 복원 (1개 환불)
try {
ticketService.grantTicket(contentRequest.getUserId(), 1, "Content processing failed - refund");
log.info("Ticket refunded for processing failure: {}", requestId);
} catch (Exception ticketE) {
log.error("Failed to refund ticket for processing failure: {}. Error: {}", requestId, ticketE.getMessage());
}
}
} catch (Exception e) {
log.error("Failed to update content request status to FAILED for request: {}. Error: {}", requestId, e.getMessage());
// Don't throw exception here to preserve original exception
}
}
}