Files
probo/pkg/coredata/cookie_banner.go
Émile Ré d189913ba3 Add i18n data structures for cookie banner
Add cookie_banner_translations table to store per-language
translations as JSONB, and a default_language column on
cookie_banners. Extend the version snapshot types to carry
translated UI strings and category content per language.

Signed-off-by: Émile Ré <emile@getprobo.com>
2026-04-24 12:18:08 +04:00

504 lines
10 KiB
Go

// Copyright (c) 2025-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 (
"context"
"errors"
"fmt"
"maps"
"time"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgconn"
"go.gearno.de/kit/pg"
"go.probo.inc/probo/pkg/gid"
"go.probo.inc/probo/pkg/page"
)
type (
CookieBanner struct {
ID gid.GID `db:"id"`
OrganizationID gid.GID `db:"organization_id"`
Name string `db:"name"`
Origin string `db:"origin"`
State CookieBannerState `db:"state"`
PrivacyPolicyURL string `db:"privacy_policy_url"`
ConsentExpiryDays int `db:"consent_expiry_days"`
ConsentMode CookieConsentMode `db:"consent_mode"`
ShowBranding bool `db:"show_branding"`
DefaultLanguage string `db:"default_language"`
CreatedAt time.Time `db:"created_at"`
UpdatedAt time.Time `db:"updated_at"`
}
CookieBanners []*CookieBanner
)
func (b *CookieBanner) CursorKey(field CookieBannerOrderField) page.CursorKey {
switch field {
case CookieBannerOrderFieldCreatedAt:
return page.NewCursorKey(b.ID, b.CreatedAt)
}
panic(fmt.Sprintf("unsupported order by: %s", field))
}
func (b *CookieBanner) AuthorizationAttributes(ctx context.Context, conn pg.Querier) (map[string]string, error) {
q := `SELECT organization_id FROM cookie_banners WHERE id = $1 LIMIT 1;`
var organizationID gid.GID
if err := conn.QueryRow(ctx, q, b.ID).Scan(&organizationID); err != nil {
if errors.Is(err, pgx.ErrNoRows) {
return nil, ErrResourceNotFound
}
return nil, fmt.Errorf("cannot query cookie banner authorization attributes: %w", err)
}
return map[string]string{"organization_id": organizationID.String()}, nil
}
func (b *CookieBanner) LoadByID(
ctx context.Context,
conn pg.Querier,
scope Scoper,
bannerID gid.GID,
) error {
q := `
SELECT
id,
organization_id,
name,
origin,
state,
privacy_policy_url,
consent_expiry_days,
consent_mode,
show_branding,
default_language,
created_at,
updated_at
FROM
cookie_banners
WHERE
%s
AND id = @banner_id
LIMIT 1;
`
q = fmt.Sprintf(q, scope.SQLFragment())
args := pgx.StrictNamedArgs{"banner_id": bannerID}
maps.Copy(args, scope.SQLArguments())
rows, err := conn.Query(ctx, q, args)
if err != nil {
return fmt.Errorf("cannot query cookie banners: %w", err)
}
banner, err := pgx.CollectExactlyOneRow(rows, pgx.RowToStructByName[CookieBanner])
if err != nil {
if errors.Is(err, pgx.ErrNoRows) {
return ErrResourceNotFound
}
return fmt.Errorf("cannot collect cookie banner: %w", err)
}
*b = banner
return nil
}
func (b *CookieBanner) LoadActiveByID(
ctx context.Context,
conn pg.Querier,
bannerID gid.GID,
) error {
q := `
SELECT
id,
organization_id,
name,
origin,
state,
privacy_policy_url,
consent_expiry_days,
consent_mode,
show_branding,
default_language,
created_at,
updated_at
FROM
cookie_banners
WHERE
id = @banner_id
AND state = @state
LIMIT 1;
`
args := pgx.StrictNamedArgs{
"banner_id": bannerID,
"state": CookieBannerStateActive,
}
rows, err := conn.Query(ctx, q, args)
if err != nil {
return fmt.Errorf("cannot query cookie banners: %w", err)
}
banner, err := pgx.CollectExactlyOneRow(rows, pgx.RowToStructByName[CookieBanner])
if err != nil {
if errors.Is(err, pgx.ErrNoRows) {
return ErrResourceNotFound
}
return fmt.Errorf("cannot collect cookie banner: %w", err)
}
*b = banner
return nil
}
func (b *CookieBanner) LoadActiveByOrigin(
ctx context.Context,
conn pg.Querier,
scope Scoper,
origin string,
) error {
q := `
SELECT
id,
organization_id,
name,
origin,
state,
privacy_policy_url,
consent_expiry_days,
consent_mode,
show_branding,
default_language,
created_at,
updated_at
FROM
cookie_banners
WHERE
%s
AND origin = @origin
AND state = @state
LIMIT 1;
`
q = fmt.Sprintf(q, scope.SQLFragment())
args := pgx.StrictNamedArgs{
"origin": origin,
"state": CookieBannerStateActive,
}
maps.Copy(args, scope.SQLArguments())
rows, err := conn.Query(ctx, q, args)
if err != nil {
return fmt.Errorf("cannot query cookie banners: %w", err)
}
banner, err := pgx.CollectExactlyOneRow(rows, pgx.RowToStructByName[CookieBanner])
if err != nil {
if errors.Is(err, pgx.ErrNoRows) {
return ErrResourceNotFound
}
return fmt.Errorf("cannot collect cookie banner: %w", err)
}
*b = banner
return nil
}
func (b *CookieBanners) LoadByOrganizationID(
ctx context.Context,
conn pg.Querier,
scope Scoper,
organizationID gid.GID,
cursor *page.Cursor[CookieBannerOrderField],
filter *CookieBannerFilter,
) error {
q := `
SELECT
id,
organization_id,
name,
origin,
state,
privacy_policy_url,
consent_expiry_days,
consent_mode,
show_branding,
default_language,
created_at,
updated_at
FROM
cookie_banners
WHERE
%s
AND organization_id = @organization_id
AND %s
AND %s
`
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)
if err != nil {
return fmt.Errorf("cannot query cookie banners: %w", err)
}
banners, err := pgx.CollectRows(rows, pgx.RowToAddrOfStructByName[CookieBanner])
if err != nil {
return fmt.Errorf("cannot collect cookie banners: %w", err)
}
*b = banners
return nil
}
func (b *CookieBanners) CountByOrganizationID(
ctx context.Context,
conn pg.Querier,
scope Scoper,
organizationID gid.GID,
filter *CookieBannerFilter,
) (int, error) {
q := `
SELECT
COUNT(id)
FROM
cookie_banners
WHERE
%s
AND organization_id = @organization_id
AND %s
`
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)
var count int
if err := row.Scan(&count); err != nil {
return 0, fmt.Errorf("cannot scan count: %w", err)
}
return count, nil
}
func (b *CookieBanner) Insert(
ctx context.Context,
tx pg.Tx,
scope Scoper,
) error {
q := `
INSERT INTO cookie_banners (
id,
tenant_id,
organization_id,
name,
origin,
state,
privacy_policy_url,
consent_expiry_days,
consent_mode,
show_branding,
default_language,
created_at,
updated_at
) VALUES (
@id,
@tenant_id,
@organization_id,
@name,
@origin,
@state,
@privacy_policy_url,
@consent_expiry_days,
@consent_mode,
@show_branding,
@default_language,
@created_at,
@updated_at
)
`
args := pgx.StrictNamedArgs{
"id": b.ID,
"tenant_id": scope.GetTenantID(),
"organization_id": b.OrganizationID,
"name": b.Name,
"origin": b.Origin,
"state": b.State,
"privacy_policy_url": b.PrivacyPolicyURL,
"consent_expiry_days": b.ConsentExpiryDays,
"consent_mode": b.ConsentMode,
"show_branding": b.ShowBranding,
"default_language": b.DefaultLanguage,
"created_at": b.CreatedAt,
"updated_at": b.UpdatedAt,
}
_, err := tx.Exec(ctx, q, args)
if err != nil {
if pgErr, ok := errors.AsType[*pgconn.PgError](err); ok {
if pgErr.Code == "23505" && pgErr.ConstraintName == "idx_cookie_banners_unique_active_origin" {
return ErrResourceAlreadyExists
}
}
return fmt.Errorf("cannot insert cookie banner: %w", err)
}
return nil
}
func (b *CookieBanner) Update(
ctx context.Context,
tx pg.Tx,
scope Scoper,
) error {
q := `
UPDATE cookie_banners
SET
name = @name,
origin = @origin,
state = @state,
privacy_policy_url = @privacy_policy_url,
consent_expiry_days = @consent_expiry_days,
consent_mode = @consent_mode,
show_branding = @show_branding,
default_language = @default_language,
updated_at = @updated_at
WHERE
%s
AND id = @id
`
q = fmt.Sprintf(q, scope.SQLFragment())
args := pgx.StrictNamedArgs{
"id": b.ID,
"name": b.Name,
"origin": b.Origin,
"state": b.State,
"privacy_policy_url": b.PrivacyPolicyURL,
"consent_expiry_days": b.ConsentExpiryDays,
"consent_mode": b.ConsentMode,
"show_branding": b.ShowBranding,
"default_language": b.DefaultLanguage,
"updated_at": b.UpdatedAt,
}
maps.Copy(args, scope.SQLArguments())
result, err := tx.Exec(ctx, q, args)
if err != nil {
if pgErr, ok := errors.AsType[*pgconn.PgError](err); ok {
if pgErr.Code == "23505" && pgErr.ConstraintName == "idx_cookie_banners_unique_active_origin" {
return ErrResourceAlreadyExists
}
}
return fmt.Errorf("cannot update cookie banner: %w", err)
}
if result.RowsAffected() == 0 {
return ErrResourceNotFound
}
return nil
}
func (b *CookieBanner) UpdateShowBranding(
ctx context.Context,
tx pg.Tx,
scope Scoper,
show bool,
) error {
q := `
UPDATE cookie_banners
SET
show_branding = @show_branding,
updated_at = @updated_at
WHERE
%s
AND id = @id
`
q = fmt.Sprintf(q, scope.SQLFragment())
args := pgx.StrictNamedArgs{
"id": b.ID,
"show_branding": show,
"updated_at": time.Now(),
}
maps.Copy(args, scope.SQLArguments())
result, err := tx.Exec(ctx, q, args)
if err != nil {
return fmt.Errorf("cannot update cookie banner show_branding: %w", err)
}
if result.RowsAffected() == 0 {
return ErrResourceNotFound
}
b.ShowBranding = show
return nil
}
func (b *CookieBanner) Delete(
ctx context.Context,
tx pg.Tx,
scope Scoper,
) error {
q := `
DELETE FROM cookie_banners
WHERE
%s
AND id = @id
`
q = fmt.Sprintf(q, scope.SQLFragment())
args := pgx.StrictNamedArgs{"id": b.ID}
maps.Copy(args, scope.SQLArguments())
_, err := tx.Exec(ctx, q, args)
if err != nil {
return fmt.Errorf("cannot delete cookie banner: %w", err)
}
return nil
}