Remove dead code from cookiebanner and coredata packages

Signed-off-by: Émile Ré <emile@getprobo.com>
This commit is contained in:
Émile Ré
2026-04-30 11:28:20 +04:00
parent 6482361352
commit e5e1119883
4 changed files with 3 additions and 729 deletions

View File

@@ -29,11 +29,9 @@ var (
ErrCategorySlugAlreadyExists = errors.New("a category with this slug already exists in this banner")
ErrOriginAlreadyInUse = errors.New("origin is already used by another active cookie banner")
ErrConsentNotFound = errors.New("consent record not found")
ErrCookieNotFound = errors.New("cookie not found")
ErrCookieNameAlreadyExists = errors.New("a cookie with this name already exists in this banner")
ErrCategoriesBannerMismatch = errors.New("source and target categories belong to different banners")
ErrSameCategoryMove = errors.New("source and target cookie categories must be different")
ErrPostHogConsentKindInvalid = errors.New("PostHog consent can only be enabled on normal categories")
ErrCookieNotFound = errors.New("cookie not found")
ErrCategoriesBannerMismatch = errors.New("source and target categories belong to different banners")
ErrPostHogConsentKindInvalid = errors.New("PostHog consent can only be enabled on normal categories")
ErrCookiePatternNotFound = errors.New("cookie pattern not found")
ErrPatternAlreadyExists = errors.New("a pattern with this name already exists in this banner")
ErrSamePatternCategoryMove = errors.New("source and target cookie categories must be different")

View File

@@ -78,30 +78,11 @@ type (
PostHogConsent *bool
}
CreateCookieRequest struct {
CookieCategoryID gid.GID
Name string
MaxAgeSeconds *int
Description string
}
UpdateCookieRequest struct {
CookieID gid.GID
Name *string
MaxAgeSeconds **int
Description *string
}
ReorderCookieCategoryRequest struct {
CookieCategoryID gid.GID
Rank int
}
MoveCookieToCategoryRequest struct {
CookieID gid.GID
TargetCookieCategoryID gid.GID
}
CreateCookiePatternRequest struct {
CookieCategoryID gid.GID
Pattern string
@@ -239,26 +220,6 @@ func (r *UpdateCookieCategoryRequest) Validate() error {
return v.Error()
}
func (r *CreateCookieRequest) Validate() error {
v := validator.New()
v.Check(r.CookieCategoryID, "cookie_category_id", validator.Required(), validator.GID(coredata.CookieCategoryEntityType))
v.Check(r.Name, "name", validator.Required(), validator.SafeTextNoNewLine(255))
v.Check(r.Description, "description", validator.SafeText(1000))
return v.Error()
}
func (r *UpdateCookieRequest) Validate() error {
v := validator.New()
v.Check(r.CookieID, "cookie_id", validator.Required(), validator.GID(coredata.CookieEntityType))
v.Check(r.Name, "name", validator.SafeTextNoNewLine(255))
v.Check(r.Description, "description", validator.SafeText(1000))
return v.Error()
}
func (r *ReorderCookieCategoryRequest) Validate() error {
v := validator.New()
@@ -308,15 +269,6 @@ func (r *MoveCookiePatternToCategoryRequest) Validate() error {
return v.Error()
}
func (r *MoveCookieToCategoryRequest) Validate() error {
v := validator.New()
v.Check(r.CookieID, "cookie_id", validator.Required(), validator.GID(coredata.CookieEntityType))
v.Check(r.TargetCookieCategoryID, "target_cookie_category_id", validator.Required(), validator.GID(coredata.CookieCategoryEntityType))
return v.Error()
}
func (r *CreateCookieConsentRecordRequest) Validate() error {
v := validator.New()
@@ -1257,112 +1209,6 @@ func (s *Service) CountCookieCategoriesForBanner(
return count, nil
}
func (s *Service) CreateCookie(
ctx context.Context,
scope coredata.Scoper,
req CreateCookieRequest,
) (*coredata.Cookie, error) {
if err := req.Validate(); err != nil {
return nil, fmt.Errorf("invalid request: %w", err)
}
var cookie *coredata.Cookie
err := s.pg.WithTx(
ctx,
func(ctx context.Context, tx pg.Tx) error {
var category coredata.CookieCategory
if err := category.LoadByID(ctx, tx, scope, req.CookieCategoryID); err != nil {
if errors.Is(err, coredata.ErrResourceNotFound) {
return ErrCategoryNotFound
}
return fmt.Errorf("cannot load cookie category: %w", err)
}
now := time.Now()
pattern := &coredata.CookiePattern{
ID: gid.New(scope.GetTenantID(), coredata.CookiePatternEntityType),
OrganizationID: category.OrganizationID,
CookieBannerID: category.CookieBannerID,
CookieCategoryID: category.ID,
Pattern: req.Name,
MatchType: coredata.CookiePatternMatchTypeExact,
DisplayName: req.Name,
MaxAgeSeconds: req.MaxAgeSeconds,
Description: req.Description,
Source: coredata.CookieSourceScript,
CreatedAt: now,
UpdatedAt: now,
}
if err := pattern.Insert(ctx, tx, scope); err != nil {
if errors.Is(err, coredata.ErrResourceAlreadyExists) {
return ErrCookieNameAlreadyExists
}
return fmt.Errorf("cannot insert cookie pattern: %w", err)
}
cookie = &coredata.Cookie{
ID: gid.New(scope.GetTenantID(), coredata.CookieEntityType),
OrganizationID: category.OrganizationID,
CookieBannerID: category.CookieBannerID,
CookiePatternID: pattern.ID,
Name: req.Name,
MaxAgeSeconds: req.MaxAgeSeconds,
Source: coredata.CookieSourceScript,
CreatedAt: now,
UpdatedAt: now,
}
if err := cookie.Insert(ctx, tx, scope); err != nil {
if errors.Is(err, coredata.ErrResourceAlreadyExists) {
return ErrCookieNameAlreadyExists
}
return fmt.Errorf("cannot insert cookie: %w", err)
}
if _, err := s.ensureDraftVersionForBanner(ctx, tx, scope, category.CookieBannerID); err != nil {
return fmt.Errorf("cannot ensure draft version: %w", err)
}
return nil
},
)
if err != nil {
return nil, err
}
return cookie, nil
}
func (s *Service) GetCookie(
ctx context.Context,
scope coredata.Scoper,
cookieID gid.GID,
) (*coredata.Cookie, error) {
var cookie coredata.Cookie
err := s.pg.WithConn(
ctx,
func(ctx context.Context, conn pg.Querier) error {
if err := cookie.LoadByID(ctx, conn, scope, cookieID); err != nil {
if errors.Is(err, coredata.ErrResourceNotFound) {
return ErrCookieNotFound
}
return fmt.Errorf("cannot load cookie: %w", err)
}
return nil
},
)
if err != nil {
return nil, err
}
return &cookie, nil
}
func (s *Service) GetCookiePattern(
ctx context.Context,
scope coredata.Scoper,
@@ -1678,133 +1524,6 @@ func (s *Service) CountCookiesForPattern(
return count, nil
}
func (s *Service) UpdateCookie(
ctx context.Context,
scope coredata.Scoper,
req UpdateCookieRequest,
) (*coredata.Cookie, error) {
if err := req.Validate(); err != nil {
return nil, fmt.Errorf("invalid request: %w", err)
}
var cookie coredata.Cookie
err := s.pg.WithTx(
ctx,
func(ctx context.Context, tx pg.Tx) error {
if err := cookie.LoadByID(ctx, tx, scope, req.CookieID); err != nil {
if errors.Is(err, coredata.ErrResourceNotFound) {
return ErrCookieNotFound
}
return fmt.Errorf("cannot load cookie: %w", err)
}
if req.MaxAgeSeconds != nil {
cookie.MaxAgeSeconds = *req.MaxAgeSeconds
}
cookie.UpdatedAt = time.Now()
if err := cookie.Update(ctx, tx, scope); err != nil {
return fmt.Errorf("cannot update cookie: %w", err)
}
if _, err := s.ensureDraftVersionForBanner(ctx, tx, scope, cookie.CookieBannerID); err != nil {
return fmt.Errorf("cannot ensure draft version: %w", err)
}
return nil
},
)
if err != nil {
return nil, err
}
return &cookie, nil
}
func (s *Service) DeleteCookie(
ctx context.Context,
scope coredata.Scoper,
cookieID gid.GID,
) error {
return s.pg.WithTx(
ctx,
func(ctx context.Context, tx pg.Tx) error {
var cookie coredata.Cookie
if err := cookie.LoadByID(ctx, tx, scope, cookieID); err != nil {
if errors.Is(err, coredata.ErrResourceNotFound) {
return ErrCookieNotFound
}
return fmt.Errorf("cannot load cookie: %w", err)
}
if err := cookie.Delete(ctx, tx, scope); err != nil {
return fmt.Errorf("cannot delete cookie: %w", err)
}
if _, err := s.ensureDraftVersionForBanner(ctx, tx, scope, cookie.CookieBannerID); err != nil {
return fmt.Errorf("cannot ensure draft version: %w", err)
}
return nil
},
)
}
func (s *Service) ListCookiesForCategory(
ctx context.Context,
scope coredata.Scoper,
categoryID gid.GID,
cursor *page.Cursor[coredata.CookieOrderField],
) (coredata.Cookies, error) {
var cookies coredata.Cookies
err := s.pg.WithConn(
ctx,
func(ctx context.Context, conn pg.Querier) error {
if err := cookies.LoadByCookieCategoryIDViaPattern(ctx, conn, scope, categoryID, cursor); err != nil {
return fmt.Errorf("cannot list cookies: %w", err)
}
return nil
},
)
if err != nil {
return nil, err
}
return cookies, nil
}
func (s *Service) CountCookiesForCategory(
ctx context.Context,
scope coredata.Scoper,
categoryID gid.GID,
) (int, error) {
var count int
err := s.pg.WithConn(
ctx,
func(ctx context.Context, conn pg.Querier) error {
var cookies coredata.Cookies
var err error
count, err = cookies.CountByCookieCategoryIDViaPattern(ctx, conn, scope, categoryID)
if err != nil {
return fmt.Errorf("cannot count cookies: %w", err)
}
return nil
},
)
if err != nil {
return 0, err
}
return count, nil
}
func (s *Service) UpdateCookieCategory(
ctx context.Context,
scope coredata.Scoper,
@@ -1874,83 +1593,6 @@ func (s *Service) UpdateCookieCategory(
return &category, nil
}
type MoveCookieToCategoryResult struct {
Cookie *coredata.Cookie
Banner *coredata.CookieBanner
}
func (s *Service) MoveCookieToCategory(
ctx context.Context,
scope coredata.Scoper,
req MoveCookieToCategoryRequest,
) (*MoveCookieToCategoryResult, error) {
if err := req.Validate(); err != nil {
return nil, fmt.Errorf("invalid request: %w", err)
}
var result MoveCookieToCategoryResult
err := s.pg.WithTx(
ctx,
func(ctx context.Context, tx pg.Tx) error {
var cookie coredata.Cookie
if err := cookie.LoadByID(ctx, tx, scope, req.CookieID); err != nil {
if errors.Is(err, coredata.ErrResourceNotFound) {
return ErrCookieNotFound
}
return fmt.Errorf("cannot load cookie: %w", err)
}
var target coredata.CookieCategory
if err := target.LoadByID(ctx, tx, scope, req.TargetCookieCategoryID); err != nil {
if errors.Is(err, coredata.ErrResourceNotFound) {
return ErrCategoryNotFound
}
return fmt.Errorf("cannot load target cookie category: %w", err)
}
var pattern coredata.CookiePattern
if err := pattern.LoadByID(ctx, tx, scope, cookie.CookiePatternID); err != nil {
return fmt.Errorf("cannot load cookie pattern: %w", err)
}
if pattern.CookieCategoryID == target.ID {
return ErrSameCategoryMove
}
if cookie.CookieBannerID != target.CookieBannerID {
return ErrCategoriesBannerMismatch
}
pattern.CookieCategoryID = target.ID
pattern.UpdatedAt = time.Now()
if err := pattern.Update(ctx, tx, scope); err != nil {
return fmt.Errorf("cannot update cookie pattern: %w", err)
}
var banner coredata.CookieBanner
if err := banner.LoadByID(ctx, tx, scope, cookie.CookieBannerID); err != nil {
return fmt.Errorf("cannot load cookie banner: %w", err)
}
if _, err := s.ensureDraftVersionForBanner(ctx, tx, scope, cookie.CookieBannerID); err != nil {
return fmt.Errorf("cannot ensure draft version: %w", err)
}
result.Cookie = &cookie
result.Banner = &banner
return nil
},
)
if err != nil {
return nil, err
}
return &result, nil
}
func (s *Service) ReorderCookieCategory(
ctx context.Context,
scope coredata.Scoper,

View File

@@ -25,7 +25,6 @@ import (
"github.com/jackc/pgx/v5/pgconn"
"go.gearno.de/kit/pg"
"go.probo.inc/probo/pkg/gid"
"go.probo.inc/probo/pkg/page"
)
type (
@@ -44,125 +43,6 @@ type (
Cookies []*Cookie
)
func (c *Cookie) CursorKey(field CookieOrderField) page.CursorKey {
switch field {
case CookieOrderFieldCreatedAt:
return page.NewCursorKey(c.ID, c.CreatedAt)
}
panic(fmt.Sprintf("unsupported order by: %s", field))
}
func (c *Cookie) AuthorizationAttributes(ctx context.Context, conn pg.Querier) (map[string]string, error) {
q := `SELECT organization_id FROM cookies WHERE id = $1 LIMIT 1;`
var organizationID gid.GID
if err := conn.QueryRow(ctx, q, c.ID).Scan(&organizationID); err != nil {
if errors.Is(err, pgx.ErrNoRows) {
return nil, ErrResourceNotFound
}
return nil, fmt.Errorf("cannot query cookie authorization attributes: %w", err)
}
return map[string]string{"organization_id": organizationID.String()}, nil
}
func (c *Cookie) LoadByID(
ctx context.Context,
conn pg.Querier,
scope Scoper,
cookieID gid.GID,
) error {
q := `
SELECT
id,
organization_id,
cookie_banner_id,
cookie_pattern_id,
name,
max_age_seconds,
source,
created_at,
updated_at
FROM
cookies
WHERE
%s
AND id = @cookie_id
LIMIT 1;
`
q = fmt.Sprintf(q, scope.SQLFragment())
args := pgx.StrictNamedArgs{"cookie_id": cookieID}
maps.Copy(args, scope.SQLArguments())
rows, err := conn.Query(ctx, q, args)
if err != nil {
return fmt.Errorf("cannot query cookies: %w", err)
}
cookie, err := pgx.CollectExactlyOneRow(rows, pgx.RowToStructByName[Cookie])
if err != nil {
if errors.Is(err, pgx.ErrNoRows) {
return ErrResourceNotFound
}
return fmt.Errorf("cannot collect cookie: %w", err)
}
*c = cookie
return nil
}
func (c *Cookies) LoadByCookiePatternID(
ctx context.Context,
conn pg.Querier,
scope Scoper,
cookiePatternID gid.GID,
cursor *page.Cursor[CookieOrderField],
) error {
q := `
SELECT
id,
organization_id,
cookie_banner_id,
cookie_pattern_id,
name,
max_age_seconds,
source,
created_at,
updated_at
FROM
cookies
WHERE
%s
AND cookie_pattern_id = @cookie_pattern_id
AND %s
`
q = fmt.Sprintf(q, scope.SQLFragment(), cursor.SQLFragment())
args := pgx.StrictNamedArgs{"cookie_pattern_id": cookiePatternID}
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 cookies: %w", err)
}
cookies, err := pgx.CollectRows(rows, pgx.RowToAddrOfStructByName[Cookie])
if err != nil {
return fmt.Errorf("cannot collect cookies: %w", err)
}
*c = cookies
return nil
}
func (c *Cookies) CountByCookiePatternID(
ctx context.Context,
conn pg.Querier,
@@ -194,134 +74,6 @@ WHERE
return count, nil
}
func (c *Cookies) LoadByCookieCategoryIDViaPattern(
ctx context.Context,
conn pg.Querier,
scope Scoper,
cookieCategoryID gid.GID,
cursor *page.Cursor[CookieOrderField],
) error {
q := `
SELECT
id,
organization_id,
cookie_banner_id,
cookie_pattern_id,
name,
max_age_seconds,
source,
created_at,
updated_at
FROM
cookies
WHERE
%s
AND cookie_pattern_id IN (
SELECT id FROM cookie_patterns WHERE cookie_category_id = @cookie_category_id
)
AND %s
`
q = fmt.Sprintf(q, scope.SQLFragment(), cursor.SQLFragment())
args := pgx.StrictNamedArgs{"cookie_category_id": cookieCategoryID}
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 cookies: %w", err)
}
cookies, err := pgx.CollectRows(rows, pgx.RowToAddrOfStructByName[Cookie])
if err != nil {
return fmt.Errorf("cannot collect cookies: %w", err)
}
*c = cookies
return nil
}
func (c *Cookies) CountByCookieCategoryIDViaPattern(
ctx context.Context,
conn pg.Querier,
scope Scoper,
cookieCategoryID gid.GID,
) (int, error) {
q := `
SELECT
COUNT(id)
FROM
cookies
WHERE
%s
AND cookie_pattern_id IN (
SELECT id FROM cookie_patterns WHERE cookie_category_id = @cookie_category_id
)
`
q = fmt.Sprintf(q, scope.SQLFragment())
args := pgx.StrictNamedArgs{"cookie_category_id": cookieCategoryID}
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 *Cookies) LoadAllByCookieBannerID(
ctx context.Context,
conn pg.Querier,
scope Scoper,
cookieBannerID gid.GID,
) error {
q := `
SELECT
id,
organization_id,
cookie_banner_id,
cookie_pattern_id,
name,
max_age_seconds,
source,
created_at,
updated_at
FROM
cookies
WHERE
%s
AND cookie_banner_id = @cookie_banner_id
ORDER BY
created_at ASC, id ASC;
`
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 fmt.Errorf("cannot query cookies: %w", err)
}
cookies, err := pgx.CollectRows(rows, pgx.RowToAddrOfStructByName[Cookie])
if err != nil {
return fmt.Errorf("cannot collect cookies: %w", err)
}
*c = cookies
return nil
}
func (c *Cookie) Insert(
ctx context.Context,
tx pg.Tx,
@@ -435,69 +187,6 @@ ON CONFLICT (cookie_banner_id, name) DO UPDATE
return result.RowsAffected() > 0, nil
}
func (c *Cookie) Update(
ctx context.Context,
tx pg.Tx,
scope Scoper,
) error {
q := `
UPDATE cookies
SET
cookie_pattern_id = @cookie_pattern_id,
max_age_seconds = @max_age_seconds,
updated_at = @updated_at
WHERE
%s
AND id = @id
`
q = fmt.Sprintf(q, scope.SQLFragment())
args := pgx.StrictNamedArgs{
"id": c.ID,
"cookie_pattern_id": c.CookiePatternID,
"max_age_seconds": c.MaxAgeSeconds,
"updated_at": c.UpdatedAt,
}
maps.Copy(args, scope.SQLArguments())
result, err := tx.Exec(ctx, q, args)
if err != nil {
return fmt.Errorf("cannot update cookie: %w", err)
}
if result.RowsAffected() == 0 {
return ErrResourceNotFound
}
return nil
}
func (c *Cookie) Delete(
ctx context.Context,
tx pg.Tx,
scope Scoper,
) error {
q := `
DELETE FROM cookies
WHERE
%s
AND id = @id
`
q = fmt.Sprintf(q, scope.SQLFragment())
args := pgx.StrictNamedArgs{"id": c.ID}
maps.Copy(args, scope.SQLArguments())
_, err := tx.Exec(ctx, q, args)
if err != nil {
return fmt.Errorf("cannot delete cookie: %w", err)
}
return nil
}
func (c *Cookies) RelinkByCookiePatternID(
ctx context.Context,
tx pg.Tx,

View File

@@ -1,55 +0,0 @@
// 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 "fmt"
type CookieOrderField string
const (
CookieOrderFieldCreatedAt CookieOrderField = "CREATED_AT"
)
func (p CookieOrderField) Column() string {
switch p {
case CookieOrderFieldCreatedAt:
return "created_at"
}
panic(fmt.Sprintf("unsupported order by: %s", p))
}
func (p CookieOrderField) IsValid() bool {
switch p {
case CookieOrderFieldCreatedAt:
return true
}
return false
}
func (p CookieOrderField) String() string {
return string(p)
}
func (p *CookieOrderField) UnmarshalText(text []byte) error {
*p = CookieOrderField(text)
if !p.IsValid() {
return fmt.Errorf("%s is not a valid CookieOrderField", string(text))
}
return nil
}
func (p CookieOrderField) MarshalText() ([]byte, error) {
return []byte(p.String()), nil
}