tender duplication fix

This commit is contained in:
Mazyar
2026-05-12 17:02:39 +03:30
parent 29b4aa50bd
commit 5e8d4f67b2
7 changed files with 290 additions and 22 deletions
+31
View File
@@ -304,6 +304,14 @@ func (s *TEDScraper) findNoticeByContractNoticeID(ctx context.Context, contractN
return existing, nil
}
func isMongoDupKeyOnContractNoticeID(err error) bool {
if err == nil {
return false
}
msg := err.Error()
return strings.Contains(msg, "E11000") && strings.Contains(msg, "contract_notice_id")
}
func (s *TEDScraper) processXMLContent(ctx context.Context, content []byte, fileName string, result *FileProcessingResult) error {
s.logger.Debug("Processing XML content", map[string]interface{}{
"file_name": fileName,
@@ -382,6 +390,29 @@ func (s *TEDScraper) processXMLContent(ctx context.Context, content []byte, file
err = s.noticeRepo.Import(ctx, t)
if err != nil {
if isMongoDupKeyOnContractNoticeID(err) {
existingAfterRace, findErr := s.findNoticeByContractNoticeID(ctx, t.ContractNoticeID)
if findErr != nil {
return fmt.Errorf("import duplicate key, re-fetch notice: %w", findErr)
}
if existingAfterRace == nil {
return fmt.Errorf("failed to import notice: %w", err)
}
MergeNoticeUpdate(existingAfterRace, t)
if upErr := s.noticeRepo.Update(ctx, existingAfterRace); upErr != nil {
s.logger.Error("Failed to update notice after duplicate-key race", map[string]interface{}{
"contract_notice_id": t.ContractNoticeID,
"error": upErr.Error(),
})
return fmt.Errorf("failed to update notice after duplicate key: %w", upErr)
}
s.logger.Info("Notice merged after duplicate-key race (parallel ingest)", map[string]interface{}{
"contract_notice_id": t.ContractNoticeID,
"notice_doc_id": existingAfterRace.ID.Hex(),
})
result.SuccessCount++
return nil
}
s.logger.Error("Failed to import notice", map[string]interface{}{
"contract_notice_id": t.ContractNoticeID,
"error": err.Error(),