Add coredata batch loaders for tracker third parties

Surface the third-party links carried by tracker patterns (org-scoped
ThirdParty via third_party_id, or global CommonThirdParty via
common_tracker_pattern_id) requires three new batch loaders and two
filter dimensions, all kept inside their owning entity tables to honour
the no-cross-entity-JOIN rule.

  * CommonTrackerPatterns gains LoadByIDs and the ID-only
    LoadIDsByCommonThirdPartyID helper, which lets callers translate a
    common third party into a set of common_tracker_pattern_id values
    without ever JOINing against tracker_patterns.

  * CommonThirdParties gains LoadByIDs.

  * TrackerPatterns gains LoadDistinctThirdPartyIDsByCookieBannerID and
    LoadDistinctCommonTrackerPatternIDsByCookieBannerID, used by the
    upcoming CookieBanner.linkedThirdParties resolver to enumerate the
    third parties referenced in a banner.

  * TrackerPatternFilter gains thirdPartyID and commonTrackerPatternIDs
    filter dimensions; the GraphQL layer will dispatch a single
    thirdPartyId argument to the right one based on the GID entity-type
    prefix.

Service-layer wrappers (cookiebanner.GetCommonTrackerPatternsByIDs,
cookiebanner.LoadCommonTrackerPatternIDsByCommonThirdPartyID,
cookiebanner.LoadDistinctThirdPartyIDsByCookieBannerID,
cookiebanner.LoadDistinctCommonTrackerPatternIDsByCookieBannerID, and
thirdparty.GetCommonThirdPartiesByIDs) expose the new loaders to the
console resolvers and dataloaders that follow.

Signed-off-by: Émile Ré <emile@probo.com>
This commit is contained in:
Émile Ré
2026-05-27 18:40:58 +02:00
parent 36bd377d47
commit cdd7eb1171
6 changed files with 408 additions and 17 deletions

View File

@@ -864,6 +864,82 @@ WHERE
return count, nil
}
// LoadDistinctThirdPartyIDsByCookieBannerID returns the distinct non-null
// `third_party_id` values referenced by tracker patterns of the given
// banner. Callers feed it to ThirdParty.GetByIDs to power per-banner
// pickers without crossing the entity boundary.
func (tps *TrackerPatterns) LoadDistinctThirdPartyIDsByCookieBannerID(
ctx context.Context,
conn pg.Querier,
scope Scoper,
cookieBannerID gid.GID,
) ([]gid.GID, error) {
q := `
SELECT DISTINCT third_party_id
FROM tracker_patterns
WHERE
%s
AND cookie_banner_id = @cookie_banner_id
AND third_party_id IS NOT NULL
`
q = fmt.Sprintf(q, scope.SQLFragment())
args := pgx.StrictNamedArgs{"cookie_banner_id": cookieBannerID}
maps.Copy(args, scope.SQLArguments())
rows, err := conn.Query(ctx, q, args)
if err != nil {
return nil, fmt.Errorf("cannot query distinct third party ids: %w", err)
}
ids, err := pgx.CollectRows(rows, pgx.RowTo[gid.GID])
if err != nil {
return nil, fmt.Errorf("cannot collect distinct third party ids: %w", err)
}
return ids, nil
}
// LoadDistinctCommonTrackerPatternIDsByCookieBannerID returns the
// distinct non-null `common_tracker_pattern_id` values referenced by
// tracker patterns of the given banner. Callers chain this with
// CommonTrackerPatterns.LoadByIDs and CommonThirdParties.LoadByIDs to
// resolve the linked common third parties without JOINs.
func (tps *TrackerPatterns) LoadDistinctCommonTrackerPatternIDsByCookieBannerID(
ctx context.Context,
conn pg.Querier,
scope Scoper,
cookieBannerID gid.GID,
) ([]gid.GID, error) {
q := `
SELECT DISTINCT common_tracker_pattern_id
FROM tracker_patterns
WHERE
%s
AND cookie_banner_id = @cookie_banner_id
AND common_tracker_pattern_id IS NOT NULL
AND third_party_id IS NULL
`
q = fmt.Sprintf(q, scope.SQLFragment())
args := pgx.StrictNamedArgs{"cookie_banner_id": cookieBannerID}
maps.Copy(args, scope.SQLArguments())
rows, err := conn.Query(ctx, q, args)
if err != nil {
return nil, fmt.Errorf("cannot query distinct common tracker pattern ids: %w", err)
}
ids, err := pgx.CollectRows(rows, pgx.RowTo[gid.GID])
if err != nil {
return nil, fmt.Errorf("cannot collect distinct common tracker pattern ids: %w", err)
}
return ids, nil
}
func (tps *TrackerPatterns) UpdateLastMatchedAt(
ctx context.Context,
tx pg.Tx,