Unify cookie category queries with coredata filter

Replace duplicated LoadConsentCategoriesByCookieBannerID,
CountConsentCategoriesByCookieBannerID, and
LoadAllConsentCategoriesByCookieBannerID with a single
CookieCategoryFilter in pkg/coredata. The filter uses the
standard CASE WHEN idiom to optionally exclude a kind,
eliminating branching in the service layer.

Signed-off-by: Émile Ré <emile@probo.com>
This commit is contained in:
Émile Ré
2026-05-20 11:57:30 +04:00
parent 883031830f
commit be9b43e98f
6 changed files with 128 additions and 170 deletions

View File

@@ -202,6 +202,7 @@ func (c *CookieCategories) LoadByCookieBannerID(
scope Scoper,
cookieBannerID gid.GID,
cursor *page.Cursor[CookieCategoryOrderField],
filter *CookieCategoryFilter,
) error {
q := `
SELECT
@@ -223,12 +224,14 @@ WHERE
%s
AND cookie_banner_id = @cookie_banner_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{"cookie_banner_id": cookieBannerID}
maps.Copy(args, scope.SQLArguments())
maps.Copy(args, filter.SQLArguments())
maps.Copy(args, cursor.SQLArguments())
rows, err := conn.Query(ctx, q, args)
@@ -251,6 +254,7 @@ func (c *CookieCategories) CountByCookieBannerID(
conn pg.Querier,
scope Scoper,
cookieBannerID gid.GID,
filter *CookieCategoryFilter,
) (int, error) {
q := `
SELECT
@@ -260,101 +264,14 @@ FROM
WHERE
%s
AND cookie_banner_id = @cookie_banner_id
`
q = fmt.Sprintf(q, scope.SQLFragment())
args := pgx.StrictNamedArgs{"cookie_banner_id": cookieBannerID}
maps.Copy(args, scope.SQLArguments())
row := conn.QueryRow(ctx, q, args)
var count int
if err := row.Scan(&count); err != nil {
return 0, fmt.Errorf("cannot scan count: %w", err)
}
return count, nil
}
func (c *CookieCategories) LoadConsentCategoriesByCookieBannerID(
ctx context.Context,
conn pg.Querier,
scope Scoper,
cookieBannerID gid.GID,
cursor *page.Cursor[CookieCategoryOrderField],
) error {
q := `
SELECT
id,
organization_id,
cookie_banner_id,
name,
slug,
description,
kind,
rank,
gcm_consent_types,
posthog_consent,
created_at,
updated_at
FROM
cookie_categories
WHERE
%s
AND cookie_banner_id = @cookie_banner_id
AND kind != @excluded_kind
AND %s
`
q = fmt.Sprintf(q, scope.SQLFragment(), cursor.SQLFragment())
q = fmt.Sprintf(q, scope.SQLFragment(), filter.SQLFragment())
args := pgx.StrictNamedArgs{
"cookie_banner_id": cookieBannerID,
"excluded_kind": CookieCategoryKindUncategorised,
}
maps.Copy(args, scope.SQLArguments())
maps.Copy(args, cursor.SQLArguments())
rows, err := conn.Query(ctx, q, args)
if err != nil {
return fmt.Errorf("cannot query consent cookie categories: %w", err)
}
categories, err := pgx.CollectRows(rows, pgx.RowToAddrOfStructByName[CookieCategory])
if err != nil {
return fmt.Errorf("cannot collect consent cookie categories: %w", err)
}
*c = categories
return nil
}
func (c *CookieCategories) CountConsentCategoriesByCookieBannerID(
ctx context.Context,
conn pg.Querier,
scope Scoper,
cookieBannerID gid.GID,
) (int, error) {
q := `
SELECT
COUNT(id)
FROM
cookie_categories
WHERE
%s
AND cookie_banner_id = @cookie_banner_id
AND kind != @excluded_kind
`
q = fmt.Sprintf(q, scope.SQLFragment())
args := pgx.StrictNamedArgs{
"cookie_banner_id": cookieBannerID,
"excluded_kind": CookieCategoryKindUncategorised,
}
args := pgx.StrictNamedArgs{"cookie_banner_id": cookieBannerID}
maps.Copy(args, scope.SQLArguments())
maps.Copy(args, filter.SQLArguments())
row := conn.QueryRow(ctx, q, args)
@@ -371,6 +288,7 @@ func (c *CookieCategories) LoadAllByCookieBannerID(
conn pg.Querier,
scope Scoper,
cookieBannerID gid.GID,
filter *CookieCategoryFilter,
) error {
q := `
SELECT
@@ -391,14 +309,16 @@ FROM
WHERE
%s
AND cookie_banner_id = @cookie_banner_id
AND %s
ORDER BY
rank ASC, id ASC;
`
q = fmt.Sprintf(q, scope.SQLFragment())
q = fmt.Sprintf(q, scope.SQLFragment(), filter.SQLFragment())
args := pgx.StrictNamedArgs{"cookie_banner_id": cookieBannerID}
maps.Copy(args, scope.SQLArguments())
maps.Copy(args, filter.SQLArguments())
rows, err := conn.Query(ctx, q, args)
if err != nil {
@@ -415,61 +335,6 @@ ORDER BY
return nil
}
// LoadAllConsentCategoriesByCookieBannerID loads all categories except
// UNCATEGORISED, which is an admin-side inbox never shown to visitors.
func (c *CookieCategories) LoadAllConsentCategoriesByCookieBannerID(
ctx context.Context,
conn pg.Querier,
scope Scoper,
cookieBannerID gid.GID,
) error {
q := `
SELECT
id,
organization_id,
cookie_banner_id,
name,
slug,
description,
kind,
rank,
gcm_consent_types,
posthog_consent,
created_at,
updated_at
FROM
cookie_categories
WHERE
%s
AND cookie_banner_id = @cookie_banner_id
AND kind != @excluded_kind
ORDER BY
rank ASC, id ASC;
`
q = fmt.Sprintf(q, scope.SQLFragment())
args := pgx.StrictNamedArgs{
"cookie_banner_id": cookieBannerID,
"excluded_kind": CookieCategoryKindUncategorised,
}
maps.Copy(args, scope.SQLArguments())
rows, err := conn.Query(ctx, q, args)
if err != nil {
return fmt.Errorf("cannot query consent cookie categories: %w", err)
}
categories, err := pgx.CollectRows(rows, pgx.RowToAddrOfStructByName[CookieCategory])
if err != nil {
return fmt.Errorf("cannot collect consent cookie categories: %w", err)
}
*c = categories
return nil
}
func (c *CookieCategory) Insert(
ctx context.Context,
tx pg.Tx,

View File

@@ -0,0 +1,63 @@
// 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 coredata
import (
"github.com/jackc/pgx/v5"
)
type CookieCategoryFilter struct {
excludeKind *CookieCategoryKind
}
func NewCookieCategoryFilter(excludeKind *CookieCategoryKind) *CookieCategoryFilter {
return &CookieCategoryFilter{excludeKind: excludeKind}
}
func (f *CookieCategoryFilter) SQLFragment() string {
if f == nil {
return "TRUE"
}
return `(
CASE
WHEN @has_exclude_kind_filter::boolean = false THEN TRUE
WHEN @has_exclude_kind_filter::boolean = true THEN
kind != @filter_exclude_kind::cookie_category_kind
ELSE TRUE
END
)`
}
func (f *CookieCategoryFilter) SQLArguments() pgx.StrictNamedArgs {
if f == nil {
return pgx.StrictNamedArgs{
"has_exclude_kind_filter": false,
"filter_exclude_kind": nil,
}
}
args := pgx.StrictNamedArgs{
"has_exclude_kind_filter": false,
"filter_exclude_kind": nil,
}
if f.excludeKind != nil {
args["has_exclude_kind_filter"] = true
args["filter_exclude_kind"] = string(*f.excludeKind)
}
return args
}