Add deduplication and normalization for tender recommendations
continuous-integration/drone/push Build is passing
continuous-integration/drone/push Build is passing
- Introduced a new function `dedupeRecommendationResponsesByTenderID` to remove duplicate tender recommendations based on their IDs, ensuring unique entries in the recommendation list. - Updated the `mergeRecommendedTenders` function to utilize the new deduplication logic, streamlining the merging process of recommendation lists. - Enhanced the `fetchAIRecommendations` method to apply deduplication on the fetched recommendations, improving data integrity. - Added unit tests for the new deduplication function to validate its behavior and ensure correct handling of various input scenarios. This update enhances the recommendation handling by ensuring that duplicate tender entries are effectively managed, improving the overall quality of the recommendations provided to users.
This commit is contained in:
@@ -94,6 +94,25 @@ func addTenderIDsToSet(set map[string]struct{}, ids []string) {
|
||||
}
|
||||
}
|
||||
|
||||
// markResolvedRecommendedTenderSeen records a resolved tender and reports whether it was already seen.
|
||||
// Recommendations may reference the same tender via different AI refs (procedure ref vs Mongo ID,
|
||||
// or multiple notice publication IDs for one merged tender).
|
||||
func markResolvedRecommendedTenderSeen(seen map[string]struct{}, tender Tender) bool {
|
||||
if seen == nil {
|
||||
return false
|
||||
}
|
||||
|
||||
tenderID := strings.TrimSpace(tender.GetID())
|
||||
if tenderID == "" {
|
||||
return true
|
||||
}
|
||||
if _, exists := seen[tenderID]; exists {
|
||||
return true
|
||||
}
|
||||
seen[tenderID] = struct{}{}
|
||||
return false
|
||||
}
|
||||
|
||||
func isRecommendedTenderExcluded(tender Tender, tenderRef string, excluded map[string]struct{}) bool {
|
||||
if len(excluded) == 0 {
|
||||
return false
|
||||
|
||||
@@ -126,3 +126,24 @@ func TestIsRecommendedTenderExcludedForAllCompanies(t *testing.T) {
|
||||
t.Fatalf("expected tender to be hidden when all companies rejected it")
|
||||
}
|
||||
}
|
||||
|
||||
func TestMarkResolvedRecommendedTenderSeen(t *testing.T) {
|
||||
sample := Tender{}
|
||||
sample.ID, _ = bson.ObjectIDFromHex("507f1f77bcf86cd799439011")
|
||||
|
||||
seen := make(map[string]struct{})
|
||||
first := markResolvedRecommendedTenderSeen(seen, sample)
|
||||
second := markResolvedRecommendedTenderSeen(seen, sample)
|
||||
if first {
|
||||
t.Fatalf("first sighting should not be skipped, id=%q seen=%v", sample.GetID(), seen)
|
||||
}
|
||||
if !second {
|
||||
t.Fatalf("duplicate sighting should be skipped, id=%q seen=%v", sample.GetID(), seen)
|
||||
}
|
||||
|
||||
otherRefSameTender := Tender{}
|
||||
otherRefSameTender.ID = sample.ID
|
||||
if !markResolvedRecommendedTenderSeen(seen, otherRefSameTender) {
|
||||
t.Fatalf("duplicate tender via different ref should be skipped")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1002,6 +1002,7 @@ func (s *tenderService) Recommend(ctx context.Context, form *SearchForm, paginat
|
||||
tenderRef string
|
||||
}
|
||||
ordered := make([]rankedRecommendedTender, 0, len(recommendations))
|
||||
seenTenderIDs := make(map[string]struct{}, len(recommendations))
|
||||
for _, rec := range recommendations {
|
||||
tenderRef := strings.TrimSpace(rec.TenderID)
|
||||
tender, ok := tenderByRef[tenderRef]
|
||||
@@ -1021,6 +1022,9 @@ func (s *tenderService) Recommend(ctx context.Context, form *SearchForm, paginat
|
||||
if len(form.Status) > 0 && !statusAllowed(tender.Status, form.Status) {
|
||||
continue
|
||||
}
|
||||
if markResolvedRecommendedTenderSeen(seenTenderIDs, tender) {
|
||||
continue
|
||||
}
|
||||
|
||||
ordered = append(ordered, rankedRecommendedTender{
|
||||
tender: tender,
|
||||
|
||||
Reference in New Issue
Block a user