overalSummary nil response
This commit is contained in:
@@ -68,6 +68,7 @@ type SearchResponse struct {
|
|||||||
type TenderResponse struct {
|
type TenderResponse struct {
|
||||||
ID string `json:"id"`
|
ID string `json:"id"`
|
||||||
NoticePublicationID string `json:"notice_publication_id"`
|
NoticePublicationID string `json:"notice_publication_id"`
|
||||||
|
RelatedNoticePublicationIDs []string `json:"related_notice_publication_ids,omitempty"`
|
||||||
NoticeTypeCode string `json:"notice_type_code"`
|
NoticeTypeCode string `json:"notice_type_code"`
|
||||||
Title string `json:"title"`
|
Title string `json:"title"`
|
||||||
Description string `json:"description"`
|
Description string `json:"description"`
|
||||||
@@ -137,6 +138,7 @@ func (t *Tender) ToResponseWithLanguage(language string) *TenderResponse {
|
|||||||
response := &TenderResponse{
|
response := &TenderResponse{
|
||||||
ID: t.ID.Hex(),
|
ID: t.ID.Hex(),
|
||||||
NoticePublicationID: t.NoticePublicationID,
|
NoticePublicationID: t.NoticePublicationID,
|
||||||
|
RelatedNoticePublicationIDs: t.RelatedNoticePublicationIDs,
|
||||||
NoticeTypeCode: t.NoticeTypeCode,
|
NoticeTypeCode: t.NoticeTypeCode,
|
||||||
Title: title,
|
Title: title,
|
||||||
Description: description,
|
Description: description,
|
||||||
@@ -180,7 +182,7 @@ type AITranslateResponse struct {
|
|||||||
type AISummaryResponse struct {
|
type AISummaryResponse struct {
|
||||||
NoticeID string `json:"notice_id"`
|
NoticeID string `json:"notice_id"`
|
||||||
OverallSummary string `json:"overall_summary"`
|
OverallSummary string `json:"overall_summary"`
|
||||||
Source string `json:"source"` // "storage" or "on_demand"
|
Source string `json:"source"` // storage | pending | unavailable
|
||||||
}
|
}
|
||||||
|
|
||||||
// AISummarizeResponse represents the response for triggering on-demand AI summarization
|
// AISummarizeResponse represents the response for triggering on-demand AI summarization
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import (
|
|||||||
"strconv"
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
"tm/pkg/logger"
|
"tm/pkg/logger"
|
||||||
|
orm "tm/pkg/mongo"
|
||||||
"tm/pkg/response"
|
"tm/pkg/response"
|
||||||
|
|
||||||
"github.com/asaskevich/govalidator"
|
"github.com/asaskevich/govalidator"
|
||||||
@@ -477,11 +478,14 @@ func (h *TenderHandler) GetAISummary(c echo.Context) error {
|
|||||||
|
|
||||||
summary, err := h.service.GetAISummary(c.Request().Context(), id)
|
summary, err := h.service.GetAISummary(c.Request().Context(), id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
if errors.Is(err, orm.ErrDocumentNotFound) {
|
||||||
|
return response.NotFound(c, "Tender not found")
|
||||||
|
}
|
||||||
h.logger.Error("Failed to get AI summary", map[string]interface{}{
|
h.logger.Error("Failed to get AI summary", map[string]interface{}{
|
||||||
"tender_id": id,
|
"tender_id": id,
|
||||||
"error": err.Error(),
|
"error": err.Error(),
|
||||||
})
|
})
|
||||||
return response.NotFound(c, err.Error())
|
return response.InternalServerError(c, "Failed to retrieve AI summary")
|
||||||
}
|
}
|
||||||
|
|
||||||
return response.Success(c, summary, "AI summary retrieved successfully")
|
return response.Success(c, summary, "AI summary retrieved successfully")
|
||||||
@@ -574,11 +578,14 @@ func (h *TenderHandler) GetPublicAISummary(c echo.Context) error {
|
|||||||
|
|
||||||
summary, err := h.service.GetAISummary(c.Request().Context(), id)
|
summary, err := h.service.GetAISummary(c.Request().Context(), id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
if errors.Is(err, orm.ErrDocumentNotFound) {
|
||||||
|
return response.NotFound(c, "Tender not found")
|
||||||
|
}
|
||||||
h.logger.Error("Failed to get AI summary", map[string]interface{}{
|
h.logger.Error("Failed to get AI summary", map[string]interface{}{
|
||||||
"tender_id": id,
|
"tender_id": id,
|
||||||
"error": err.Error(),
|
"error": err.Error(),
|
||||||
})
|
})
|
||||||
return response.NotFound(c, err.Error())
|
return response.InternalServerError(c, "Failed to retrieve AI summary")
|
||||||
}
|
}
|
||||||
|
|
||||||
return response.Success(c, summary, "AI summary retrieved successfully")
|
return response.Success(c, summary, "AI summary retrieved successfully")
|
||||||
|
|||||||
+18
-14
@@ -827,14 +827,14 @@ func (s *tenderService) CleanupOldData(ctx context.Context, olderThan time.Durat
|
|||||||
}
|
}
|
||||||
|
|
||||||
// GetAISummary retrieves the AI-generated summary for a tender.
|
// GetAISummary retrieves the AI-generated summary for a tender.
|
||||||
// It first tries to fetch from MinIO storage (async pipeline result).
|
// It loads from MinIO storage (async pipeline result). When no summary exists yet,
|
||||||
// If not found or not ready, it returns an appropriate error.
|
// the pipeline is still running, or storage is unavailable, it returns 200-compatible
|
||||||
|
// data with an empty overall_summary and a Source hint instead of an error.
|
||||||
func (s *tenderService) GetAISummary(ctx context.Context, id string) (*AISummaryResponse, error) {
|
func (s *tenderService) GetAISummary(ctx context.Context, id string) (*AISummaryResponse, error) {
|
||||||
if id == "" {
|
if id == "" {
|
||||||
return nil, fmt.Errorf("tender ID cannot be empty")
|
return nil, fmt.Errorf("tender ID cannot be empty")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get tender to find the notice_publication_id
|
|
||||||
t, err := s.repository.GetByID(ctx, id)
|
t, err := s.repository.GetByID(ctx, id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
s.logger.Error("Failed to get tender for AI summary", map[string]interface{}{
|
s.logger.Error("Failed to get tender for AI summary", map[string]interface{}{
|
||||||
@@ -844,20 +844,24 @@ func (s *tenderService) GetAISummary(ctx context.Context, id string) (*AISummary
|
|||||||
return nil, fmt.Errorf("failed to get tender: %w", err)
|
return nil, fmt.Errorf("failed to get tender: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if t == nil {
|
noticeID := strings.TrimSpace(t.NoticePublicationID)
|
||||||
return nil, fmt.Errorf("tender not found")
|
emptyResp := func(source string) *AISummaryResponse {
|
||||||
|
return &AISummaryResponse{
|
||||||
|
NoticeID: noticeID,
|
||||||
|
OverallSummary: "",
|
||||||
|
Source: source,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
noticeID := t.NoticePublicationID
|
if noticeID == "" || strings.TrimSpace(t.ContractFolderID) == "" {
|
||||||
if noticeID == "" {
|
return emptyResp("unavailable"), nil
|
||||||
return nil, fmt.Errorf("tender has no notice publication ID")
|
|
||||||
}
|
|
||||||
if t.ContractFolderID == "" {
|
|
||||||
return nil, fmt.Errorf("tender has no contract folder id")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if s.aiSummarizerStorage == nil {
|
if s.aiSummarizerStorage == nil {
|
||||||
return nil, fmt.Errorf("AI summarizer storage is not configured")
|
s.logger.Debug("AI summarizer storage not configured; returning empty AI summary", map[string]interface{}{
|
||||||
|
"tender_id": id,
|
||||||
|
})
|
||||||
|
return emptyResp("unavailable"), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
summary, usedNotice, err := s.lookupSummaryForTender(ctx, t)
|
summary, usedNotice, err := s.lookupSummaryForTender(ctx, t)
|
||||||
@@ -877,10 +881,10 @@ func (s *tenderService) GetAISummary(ctx context.Context, id string) (*AISummary
|
|||||||
})
|
})
|
||||||
|
|
||||||
if errors.Is(err, ai_summarizer.ErrSummaryNotReady) {
|
if errors.Is(err, ai_summarizer.ErrSummaryNotReady) {
|
||||||
return nil, fmt.Errorf("AI summary is not ready yet, the pipeline is still processing")
|
return emptyResp("pending"), nil
|
||||||
}
|
}
|
||||||
if errors.Is(err, ai_summarizer.ErrObjectNotFound) || errors.Is(err, ai_summarizer.ErrNoOverallSummary) {
|
if errors.Is(err, ai_summarizer.ErrObjectNotFound) || errors.Is(err, ai_summarizer.ErrNoOverallSummary) {
|
||||||
return nil, fmt.Errorf("AI summary not found — this tender has not been processed by the AI pipeline yet")
|
return emptyResp("unavailable"), nil
|
||||||
}
|
}
|
||||||
return nil, fmt.Errorf("failed to get AI summary: %w", err)
|
return nil, fmt.Errorf("failed to get AI summary: %w", err)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user