Refactor NoticeWorker to Enhance Tender Creation Logic

- Updated the ToTender method to accept a tender instance, allowing for direct population of tender fields from the notice entity.
- Simplified the tender creation and update process by checking if the tender ID is zero, streamlining the logic for handling tender entities.
- Removed the commented-out code related to AI translation, improving code clarity and maintainability.
- Enhanced error logging for tender creation and update failures, ensuring better visibility into potential issues during processing.
This commit is contained in:
n.nakhostin
2025-10-18 09:02:11 +03:30
parent 0747908873
commit 8e5308b70b
+66 -93
View File
@@ -2,7 +2,6 @@ package workers
import (
"context"
"fmt"
"tm/internal/notice"
"tm/internal/tender"
"tm/pkg/logger"
@@ -54,15 +53,12 @@ func (w *NoticeWorker) Run() {
"notice": n.ID.Hex(),
})
// exists, err := w.TenderRepo.GetByNoticePublicationID(context.Background(), n.NoticePublicationID)
// if err != nil {
// w.Logger.Error("Failed to get tender", map[string]interface{}{
// "error": err.Error(),
// })
// continue
// }
t, err := w.TenderRepo.GetByNoticePublicationID(context.Background(), n.NoticePublicationID)
if err == nil {
continue
}
t, err := w.ToTender(&n)
err = w.ToTender(&n, t)
if err != nil {
w.Logger.Error("Failed to create tender", map[string]interface{}{
"error": err.Error(),
@@ -70,34 +66,28 @@ func (w *NoticeWorker) Run() {
continue
}
err = w.TenderRepo.Create(context.Background(), t)
if err != nil {
w.Logger.Error("Failed to create tender", map[string]interface{}{
"error": err.Error(),
})
if t.ID.IsZero() {
err = w.TenderRepo.Create(context.Background(), t)
if err != nil {
w.Logger.Error("Failed to create tender", map[string]interface{}{
"error": err.Error(),
})
}
} else {
err = w.TenderRepo.Update(context.Background(), t)
if err != nil {
w.Logger.Error("Failed to update tender", map[string]interface{}{
"error": err.Error(),
})
}
}
}
skip += limit
}
}
func (w *NoticeWorker) translateAI(msg string) (string, error) {
builder := w.Ollama.NewChatBuilder().SetStream(false).SetMaxTokens(200000)
chat, err := builder.AddMessage(ollama.MessageRoleUser, fmt.Sprintf("Translate the following text into fluent, natural English while keeping the original meaning and tone. Do not summarize, shorten, or add extra explanations. Just provide the translation without other additional values. %s", msg)).Build()
if err != nil {
return "", err
}
resp, err := w.Ollama.Chat(context.Background(), chat)
if err != nil {
return "", err
}
return resp.Message.Content, nil
}
func (w *NoticeWorker) ToTender(n *notice.Notice) (*tender.Tender, error) {
func (w *NoticeWorker) ToTender(n *notice.Notice, t *tender.Tender) error {
// Buyer Organization
buyerOrg := &tender.Organization{}
if n.BuyerOrganization != nil {
@@ -234,67 +224,50 @@ func (w *NoticeWorker) ToTender(n *notice.Notice) (*tender.Tender, error) {
}
}
title, err := w.translateAI(n.Title)
if err != nil {
return nil, fmt.Errorf("failed to translate title: %w", err)
}
description, err := w.translateAI(n.Description)
if err != nil {
return nil, fmt.Errorf("failed to translate description: %w", err)
}
return &tender.Tender{
NoticePublicationID: n.NoticePublicationID,
Title: title,
Description: description,
ProcurementTypeCode: n.ProcurementTypeCode,
ProcedureCode: n.ProcedureCode,
EstimatedValue: n.EstimatedValue,
Currency: n.Currency,
TenderDeadline: n.TenderDeadline,
SubmissionURL: n.SubmissionURL,
BuyerOrganization: buyerOrg,
ReviewOrganization: reviewOrg,
Organizations: organizations,
SelectionCriteria: selectionCriteria,
Status: tender.TenderStatus(n.Status),
Source: tender.TenderSource(n.Source),
SourceFileURL: n.SourceFileURL,
SourceFileName: n.SourceFileName,
TenderID: n.TenderID,
ContractNoticeID: n.ContractNoticeID,
ContractFolderID: n.ContractFolderID,
NoticeTypeCode: n.NoticeTypeCode,
NoticeSubTypeCode: n.NoticeSubTypeCode,
NoticeLanguageCode: n.NoticeLanguageCode,
IssueDate: n.IssueDate,
IssueTime: n.IssueTime,
PublicationDate: n.PublicationDate,
SubmissionDeadline: n.SubmissionDeadline,
ApplicationDeadline: n.ApplicationDeadline,
GazetteID: n.GazetteID,
Duration: n.Duration,
DurationUnit: n.DurationUnit,
PlaceOfPerformance: n.PlaceOfPerformance,
CountryCode: n.CountryCode,
RegionCode: n.RegionCode,
CityName: n.CityName,
PostalCode: n.PostalCode,
DocumentURI: n.DocumentURI,
TenderURL: n.TenderURL,
MainClassification: n.MainClassification,
AdditionalClassifications: n.AdditionalClassifications,
OfficialLanguages: n.OfficialLanguages,
CancellationReason: n.CancellationReason,
CancellationDate: n.CancellationDate,
AwardDate: n.AwardDate,
AwardedValue: n.AwardedValue,
ContractNumber: n.ContractNumber,
SuspensionReason: n.SuspensionReason,
SuspensionDate: n.SuspensionDate,
WinningTenderer: winningTenderer,
Modifications: modifications,
AwardedEntities: awardedEntities,
}, nil
t.NoticePublicationID = n.NoticePublicationID
t.Title = n.Title
t.Description = n.Description
t.ProcurementTypeCode = n.ProcurementTypeCode
t.ProcedureCode = n.ProcedureCode
t.EstimatedValue = n.EstimatedValue
t.Currency = n.Currency
t.TenderDeadline = n.TenderDeadline
t.SubmissionURL = n.SubmissionURL
t.BuyerOrganization = buyerOrg
t.ReviewOrganization = reviewOrg
t.Organizations = organizations
t.SelectionCriteria = selectionCriteria
t.WinningTenderer = winningTenderer
t.Modifications = modifications
t.AwardedEntities = awardedEntities
t.NoticeTypeCode = n.NoticeTypeCode
t.NoticeSubTypeCode = n.NoticeSubTypeCode
t.NoticeLanguageCode = n.NoticeLanguageCode
t.IssueDate = n.IssueDate
t.IssueTime = n.IssueTime
t.PublicationDate = n.PublicationDate
t.SubmissionDeadline = n.SubmissionDeadline
t.ApplicationDeadline = n.ApplicationDeadline
t.GazetteID = n.GazetteID
t.PlaceOfPerformance = n.PlaceOfPerformance
t.CountryCode = n.CountryCode
t.RegionCode = n.RegionCode
t.CityName = n.CityName
t.PostalCode = n.PostalCode
t.DocumentURI = n.DocumentURI
t.TenderURL = n.TenderURL
t.MainClassification = n.MainClassification
t.AdditionalClassifications = n.AdditionalClassifications
t.OfficialLanguages = n.OfficialLanguages
t.CancellationReason = n.CancellationReason
t.CancellationDate = n.CancellationDate
t.AwardDate = n.AwardDate
t.AwardedValue = n.AwardedValue
t.ContractNumber = n.ContractNumber
t.SuspensionReason = n.SuspensionReason
t.SuspensionDate = n.SuspensionDate
t.WinningTenderer = winningTenderer
t.Modifications = modifications
t.AwardedEntities = awardedEntities
return nil
}