diff --git a/pkg/coredata/cookie_banner.go b/pkg/coredata/cookie_banner.go index 26c6b1e87..7652e8a22 100644 --- a/pkg/coredata/cookie_banner.go +++ b/pkg/coredata/cookie_banner.go @@ -170,6 +170,7 @@ func (b *CookieBanners) LoadByOrganizationID( scope Scoper, organizationID gid.GID, cursor *page.Cursor[CookieBannerOrderField], + filter *CookieBannerFilter, ) error { q := ` SELECT @@ -189,12 +190,14 @@ WHERE %s AND organization_id = @organization_id AND %s + AND %s ` - q = fmt.Sprintf(q, scope.SQLFragment(), cursor.SQLFragment()) + q = fmt.Sprintf(q, scope.SQLFragment(), filter.SQLFragment(), cursor.SQLFragment()) args := pgx.StrictNamedArgs{"organization_id": organizationID} maps.Copy(args, scope.SQLArguments()) + maps.Copy(args, filter.SQLArguments()) maps.Copy(args, cursor.SQLArguments()) rows, err := conn.Query(ctx, q, args) @@ -217,6 +220,7 @@ func (b *CookieBanners) CountByOrganizationID( conn pg.Querier, scope Scoper, organizationID gid.GID, + filter *CookieBannerFilter, ) (int, error) { q := ` SELECT @@ -226,12 +230,14 @@ FROM WHERE %s AND organization_id = @organization_id + AND %s ` - q = fmt.Sprintf(q, scope.SQLFragment()) + q = fmt.Sprintf(q, scope.SQLFragment(), filter.SQLFragment()) args := pgx.StrictNamedArgs{"organization_id": organizationID} maps.Copy(args, scope.SQLArguments()) + maps.Copy(args, filter.SQLArguments()) row := conn.QueryRow(ctx, q, args) diff --git a/pkg/coredata/cookie_banner_filter.go b/pkg/coredata/cookie_banner_filter.go new file mode 100644 index 000000000..efd87f5a5 --- /dev/null +++ b/pkg/coredata/cookie_banner_filter.go @@ -0,0 +1,50 @@ +// Copyright (c) 2025-2026 Probo Inc . +// +// 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 coredata + +import "github.com/jackc/pgx/v5" + +type CookieBannerFilter struct { + state *CookieBannerState +} + +func NewCookieBannerFilter(state *CookieBannerState) *CookieBannerFilter { + return &CookieBannerFilter{ + state: state, + } +} + +func (f *CookieBannerFilter) SQLFragment() string { + return ` +( + CASE + WHEN @filter_state::text IS NOT NULL THEN + state = @filter_state::cookie_banner_state + ELSE TRUE + END +)` +} + +func (f *CookieBannerFilter) SQLArguments() pgx.StrictNamedArgs { + args := pgx.StrictNamedArgs{ + "filter_state": nil, + } + + if f.state != nil { + args["filter_state"] = string(*f.state) + } + + return args +}