Move common third-party resolver to thirdparty pkg
resolveOrCreateCommonThirdParty lived as a package-level helper in the tracker mapping worker, but the common pattern enrichment worker now reuses it. Homing shared catalog logic in a mapping-named file made the enrichment worker quietly depend on the mapping worker's file, and it is not a mapping concern. Move it to pkg/thirdparty as exported ResolveOrCreateCommonThirdParty, decoupled from cookiebanner's TrackerMappingAgentResult (it now takes a name and category) to avoid an import cycle. It stays a transaction- scoped free function so both workers compose it into their own tx for atomicity rather than receiving a service that owns its own connection. Relocate the catalog dedup DB test alongside it. Signed-off-by: Émile Ré <emile@probo.com>
This commit is contained in:
94
pkg/thirdparty/resolver.go
vendored
Normal file
94
pkg/thirdparty/resolver.go
vendored
Normal file
@@ -0,0 +1,94 @@
|
||||
// Copyright (c) 2026 Probo Inc <hello@getprobo.com>.
|
||||
//
|
||||
// Permission to use, copy, modify, and/or distribute this software for any
|
||||
// purpose with or without fee is hereby granted, provided that the above
|
||||
// copyright notice and this permission notice appear in all copies.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
||||
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
||||
// AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
||||
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
||||
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
||||
// OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
||||
// PERFORMANCE OF THIS SOFTWARE.
|
||||
|
||||
package thirdparty
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"go.gearno.de/kit/log"
|
||||
"go.gearno.de/kit/pg"
|
||||
"go.probo.inc/probo/pkg/coredata"
|
||||
"go.probo.inc/probo/pkg/gid"
|
||||
"go.probo.inc/probo/pkg/slug"
|
||||
)
|
||||
|
||||
// ResolveOrCreateCommonThirdParty links a named vendor to the global
|
||||
// catalog, creating a row when none matches. Dedup is deterministic:
|
||||
// exact name, then slug, before insert. Callers run inside their own
|
||||
// transaction and pass the logger explicitly, so it is shared by the
|
||||
// tracker mapping worker (which supplies observed domains) and the
|
||||
// common pattern enrichment worker (which has none).
|
||||
func ResolveOrCreateCommonThirdParty(
|
||||
ctx context.Context,
|
||||
tx pg.Tx,
|
||||
logger *log.Logger,
|
||||
name string,
|
||||
category coredata.ThirdPartyCategory,
|
||||
domains []string,
|
||||
) (*gid.GID, error) {
|
||||
var party coredata.CommonThirdParty
|
||||
if err := party.LoadByName(ctx, tx, name); err == nil {
|
||||
return &party.ID, nil
|
||||
}
|
||||
|
||||
partySlug := slug.Make(name)
|
||||
if partySlug == "" {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
if err := party.LoadBySlug(ctx, tx, partySlug); err == nil {
|
||||
return &party.ID, nil
|
||||
}
|
||||
|
||||
now := time.Now()
|
||||
party = coredata.CommonThirdParty{
|
||||
ID: gid.New(gid.NilTenant, coredata.CommonThirdPartyEntityType),
|
||||
Name: name,
|
||||
Slug: partySlug,
|
||||
Category: category,
|
||||
Certifications: []string{},
|
||||
CreatedAt: now,
|
||||
UpdatedAt: now,
|
||||
}
|
||||
|
||||
if err := party.Insert(ctx, tx); err != nil {
|
||||
return nil, fmt.Errorf("cannot create common third party: %w", err)
|
||||
}
|
||||
|
||||
for _, domain := range domains {
|
||||
domainRecord := coredata.CommonThirdPartyDomain{
|
||||
ID: gid.New(gid.NilTenant, coredata.CommonThirdPartyDomainEntityType),
|
||||
CommonThirdPartyID: party.ID,
|
||||
Domain: domain,
|
||||
CreatedAt: now,
|
||||
UpdatedAt: now,
|
||||
}
|
||||
|
||||
if _, err := domainRecord.Upsert(ctx, tx); err != nil {
|
||||
return nil, fmt.Errorf("cannot create common third party domain: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
logger.InfoCtx(
|
||||
ctx,
|
||||
"created common third party from agent identification",
|
||||
log.String("name", name),
|
||||
log.String("category", category.String()),
|
||||
)
|
||||
|
||||
return &party.ID, nil
|
||||
}
|
||||
Reference in New Issue
Block a user