Filter uncategorised category at the SQL level
Instead of loading all categories and filtering out UNCATEGORISED in Go, add LoadConsentCategoriesByCookieBannerID which excludes it in the query. This avoids fetching data we immediately discard and makes the intent explicit at each call site. Signed-off-by: Émile Ré <emile@getprobo.com>
This commit is contained in:
@@ -323,6 +323,61 @@ ORDER BY
|
||||
return nil
|
||||
}
|
||||
|
||||
// LoadConsentCategoriesByCookieBannerID loads all categories except
|
||||
// UNCATEGORISED, which is an admin-side inbox never shown to visitors.
|
||||
func (c *CookieCategories) LoadConsentCategoriesByCookieBannerID(
|
||||
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,
|
||||
|
||||
Reference in New Issue
Block a user