// Copyright (c) 2026 Probo Inc . // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal // in the Software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // SOFTWARE. package coredata import ( "context" "encoding/json" "errors" "fmt" "maps" "time" "github.com/jackc/pgx/v5" "go.gearno.de/kit/pg" "go.probo.inc/probo/pkg/gid" "go.probo.inc/probo/pkg/iam/policy" "go.probo.inc/probo/pkg/page" ) type ( CookieBannerVersionSnapshot struct { PrivacyPolicyURL *string `json:"privacy_policy_url,omitempty"` CookiePolicyURL string `json:"cookie_policy_url"` ConsentExpiryDays int `json:"consent_expiry_days"` DefaultLanguage string `json:"default_language"` Categories []CookieBannerVersionSnapshotCategory `json:"categories"` } CookieBannerVersionSnapshotTranslation struct { UI map[string]string `json:"ui"` Categories []CookieBannerVersionSnapshotCategoryTranslation `json:"categories"` } CookieBannerVersionSnapshotCategoryTranslation struct { Name string `json:"name"` Description string `json:"description"` } CookieBannerVersionSnapshotCategory struct { Name string `json:"name"` Slug string `json:"slug"` Description string `json:"description"` Kind CookieCategoryKind `json:"kind"` Cookies CookieItems `json:"cookies"` GCMConsentTypes []string `json:"gcm_consent_types"` PostHogConsent bool `json:"posthog_consent"` } CookieBannerVersion struct { ID gid.GID `db:"id"` OrganizationID gid.GID `db:"organization_id"` CookieBannerID gid.GID `db:"cookie_banner_id"` Version int `db:"version"` State CookieBannerVersionState `db:"state"` Snapshot json.RawMessage `db:"snapshot"` CreatedAt time.Time `db:"created_at"` UpdatedAt time.Time `db:"updated_at"` } CookieBannerVersions []*CookieBannerVersion ) func (v *CookieBannerVersion) CursorKey(field CookieBannerVersionOrderField) page.CursorKey { switch field { case CookieBannerVersionOrderFieldCreatedAt: return page.NewCursorKey(v.ID, v.CreatedAt) } panic(fmt.Sprintf("unsupported order by: %s", field)) } func (v *CookieBannerVersion) AuthorizationAttributes( ctx context.Context, conn pg.Querier, resourceIDs []gid.GID, ) (policy.AttributesByID, error) { q := `SELECT id, organization_id FROM cookie_banner_versions WHERE id = ANY(@resource_ids::text[])` args := pgx.StrictNamedArgs{ "resource_ids": resourceIDs, } rows, err := conn.Query(ctx, q, args) if err != nil { return nil, fmt.Errorf("cannot query authorization attributes: %w", err) } defer rows.Close() attrsByID := make(policy.AttributesByID) for rows.Next() { var id, organizationID gid.GID if err := rows.Scan(&id, &organizationID); err != nil { return nil, fmt.Errorf("cannot scan authorization attributes: %w", err) } attrsByID[id] = policy.Attributes{ "organization_id": organizationID.String(), } } if err := rows.Err(); err != nil { return nil, fmt.Errorf("cannot iterate authorization attributes: %w", err) } return attrsByID, nil } func (v *CookieBannerVersion) GetSnapshot() (CookieBannerVersionSnapshot, error) { var snapshot CookieBannerVersionSnapshot if err := json.Unmarshal(v.Snapshot, &snapshot); err != nil { return snapshot, fmt.Errorf("cannot unmarshal cookie banner version snapshot: %w", err) } // Snapshots created before tracker types were captured only ever held // cookie-type trackers, so their cookie items carry an empty tracker // type. Backfill them as cookies so downstream consumers (policy // generation, GraphQL, served banner config) see a valid type. for i := range snapshot.Categories { for j := range snapshot.Categories[i].Cookies { if snapshot.Categories[i].Cookies[j].TrackerType == "" { snapshot.Categories[i].Cookies[j].TrackerType = TrackerTypeCookie } } } return snapshot, nil } func (v *CookieBannerVersion) SetSnapshot(snapshot CookieBannerVersionSnapshot) error { data, err := json.Marshal(snapshot) if err != nil { return fmt.Errorf("cannot marshal cookie banner version snapshot: %w", err) } v.Snapshot = data return nil } func (v *CookieBannerVersion) LoadByID( ctx context.Context, conn pg.Querier, scope Scoper, versionID gid.GID, ) error { q := ` SELECT id, organization_id, cookie_banner_id, version, state, snapshot, created_at, updated_at FROM cookie_banner_versions WHERE %s AND id = @version_id LIMIT 1; ` q = fmt.Sprintf(q, scope.SQLFragment()) args := pgx.StrictNamedArgs{"version_id": versionID} maps.Copy(args, scope.SQLArguments()) rows, err := conn.Query(ctx, q, args) if err != nil { return fmt.Errorf("cannot query cookie banner versions: %w", err) } version, err := pgx.CollectExactlyOneRow(rows, pgx.RowToStructByName[CookieBannerVersion]) if err != nil { if errors.Is(err, pgx.ErrNoRows) { return ErrResourceNotFound } return fmt.Errorf("cannot collect cookie banner version: %w", err) } *v = version return nil } func (v *CookieBannerVersions) LoadByCookieBannerID( ctx context.Context, conn pg.Querier, scope Scoper, cookieBannerID gid.GID, cursor *page.Cursor[CookieBannerVersionOrderField], ) error { q := ` SELECT id, organization_id, cookie_banner_id, version, state, snapshot, created_at, updated_at FROM cookie_banner_versions WHERE %s AND cookie_banner_id = @cookie_banner_id AND %s ` q = fmt.Sprintf(q, scope.SQLFragment(), cursor.SQLFragment()) args := pgx.StrictNamedArgs{"cookie_banner_id": cookieBannerID} 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 cookie banner versions: %w", err) } versions, err := pgx.CollectRows(rows, pgx.RowToAddrOfStructByName[CookieBannerVersion]) if err != nil { return fmt.Errorf("cannot collect cookie banner versions: %w", err) } *v = versions return nil } func (v *CookieBannerVersions) CountByCookieBannerID( ctx context.Context, conn pg.Querier, scope Scoper, cookieBannerID gid.GID, ) (int, error) { q := ` SELECT COUNT(id) FROM cookie_banner_versions 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 (v *CookieBannerVersion) LoadByCookieBannerIDAndVersion( ctx context.Context, conn pg.Querier, scope Scoper, cookieBannerID gid.GID, version int, ) error { q := ` SELECT id, organization_id, cookie_banner_id, version, state, snapshot, created_at, updated_at FROM cookie_banner_versions WHERE %s AND cookie_banner_id = @cookie_banner_id AND version = @version LIMIT 1; ` q = fmt.Sprintf(q, scope.SQLFragment()) args := pgx.StrictNamedArgs{ "cookie_banner_id": cookieBannerID, "version": version, } maps.Copy(args, scope.SQLArguments()) rows, err := conn.Query(ctx, q, args) if err != nil { return fmt.Errorf("cannot query cookie banner versions: %w", err) } ver, err := pgx.CollectExactlyOneRow(rows, pgx.RowToStructByName[CookieBannerVersion]) if err != nil { if errors.Is(err, pgx.ErrNoRows) { return ErrResourceNotFound } return fmt.Errorf("cannot collect cookie banner version: %w", err) } *v = ver return nil } func (v *CookieBannerVersion) LoadLatestByCookieBannerID( ctx context.Context, conn pg.Querier, scope Scoper, cookieBannerID gid.GID, ) error { q := ` SELECT id, organization_id, cookie_banner_id, version, state, snapshot, created_at, updated_at FROM cookie_banner_versions WHERE %s AND cookie_banner_id = @cookie_banner_id ORDER BY version DESC LIMIT 1; ` 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 cookie banner versions: %w", err) } ver, err := pgx.CollectExactlyOneRow(rows, pgx.RowToStructByName[CookieBannerVersion]) if err != nil { if errors.Is(err, pgx.ErrNoRows) { return ErrResourceNotFound } return fmt.Errorf("cannot collect cookie banner version: %w", err) } *v = ver return nil } func (v *CookieBannerVersion) LoadNextVersion( ctx context.Context, conn pg.Querier, scope Scoper, cookieBannerID gid.GID, ) (int, error) { q := ` SELECT COALESCE(MAX(version), 0) + 1 FROM cookie_banner_versions 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 nextVersion int if err := row.Scan(&nextVersion); err != nil { return 0, fmt.Errorf("cannot scan next version: %w", err) } return nextVersion, nil } func (v *CookieBannerVersion) Insert( ctx context.Context, tx pg.Tx, scope Scoper, ) error { q := ` INSERT INTO cookie_banner_versions ( id, tenant_id, organization_id, cookie_banner_id, version, state, snapshot, created_at, updated_at ) VALUES ( @id, @tenant_id, @organization_id, @cookie_banner_id, @version, @state, @snapshot, @created_at, @updated_at ) ` args := pgx.StrictNamedArgs{ "id": v.ID, "tenant_id": scope.GetTenantID(), "organization_id": v.OrganizationID, "cookie_banner_id": v.CookieBannerID, "version": v.Version, "state": v.State, "snapshot": v.Snapshot, "created_at": v.CreatedAt, "updated_at": v.UpdatedAt, } _, err := tx.Exec(ctx, q, args) if err != nil { return fmt.Errorf("cannot insert cookie banner version: %w", err) } return nil } func (v *CookieBannerVersion) Update( ctx context.Context, tx pg.Tx, scope Scoper, ) error { q := ` UPDATE cookie_banner_versions SET state = @state, snapshot = @snapshot, updated_at = @updated_at WHERE %s AND id = @id ` q = fmt.Sprintf(q, scope.SQLFragment()) args := pgx.StrictNamedArgs{ "id": v.ID, "state": v.State, "snapshot": v.Snapshot, "updated_at": v.UpdatedAt, } maps.Copy(args, scope.SQLArguments()) result, err := tx.Exec(ctx, q, args) if err != nil { return fmt.Errorf("cannot update cookie banner version: %w", err) } if result.RowsAffected() == 0 { return ErrResourceNotFound } return nil } func (v *CookieBannerVersion) LoadLatestPublishedByCookieBannerID( ctx context.Context, conn pg.Querier, scope Scoper, cookieBannerID gid.GID, ) error { q := ` SELECT id, organization_id, cookie_banner_id, version, state, snapshot, created_at, updated_at FROM cookie_banner_versions WHERE %s AND cookie_banner_id = @cookie_banner_id AND state = @state ORDER BY version DESC LIMIT 1; ` q = fmt.Sprintf(q, scope.SQLFragment()) args := pgx.StrictNamedArgs{ "cookie_banner_id": cookieBannerID, "state": CookieBannerVersionStatePublished, } maps.Copy(args, scope.SQLArguments()) rows, err := conn.Query(ctx, q, args) if err != nil { return fmt.Errorf("cannot query cookie banner versions: %w", err) } ver, err := pgx.CollectExactlyOneRow(rows, pgx.RowToStructByName[CookieBannerVersion]) if err != nil { if errors.Is(err, pgx.ErrNoRows) { return ErrResourceNotFound } return fmt.Errorf("cannot collect cookie banner version: %w", err) } *v = ver return nil }