Constrain PostHog consent to one normal category per banner

Add a partial unique index ensuring only one category per banner can
have posthog_consent enabled. Default it to the analytics category on
banner creation, clear the previous mapping before setting a new one,
and restrict the toggle to NORMAL categories in both the service layer
and the console UI.

Signed-off-by: Émile Ré <emile@getprobo.com>
This commit is contained in:
Émile Ré
2026-04-24 16:35:39 +04:00
parent 7f1dffad80
commit 9fbb716b00
9 changed files with 190 additions and 27 deletions

View File

@@ -297,6 +297,59 @@ INSERT INTO cookies (
return nil
}
func (c *Cookie) InsertIfNotExists(
ctx context.Context,
tx pg.Tx,
scope Scoper,
) (bool, error) {
q := `
INSERT INTO cookies (
id,
tenant_id,
organization_id,
cookie_banner_id,
cookie_category_id,
name,
duration,
description,
created_at,
updated_at
) VALUES (
@id,
@tenant_id,
@organization_id,
@cookie_banner_id,
@cookie_category_id,
@name,
@duration,
@description,
@created_at,
@updated_at
)
ON CONFLICT (cookie_banner_id, name) DO NOTHING
`
args := pgx.StrictNamedArgs{
"id": c.ID,
"tenant_id": scope.GetTenantID(),
"organization_id": c.OrganizationID,
"cookie_banner_id": c.CookieBannerID,
"cookie_category_id": c.CookieCategoryID,
"name": c.Name,
"duration": c.Duration,
"description": c.Description,
"created_at": c.CreatedAt,
"updated_at": c.UpdatedAt,
}
result, err := tx.Exec(ctx, q, args)
if err != nil {
return false, fmt.Errorf("cannot insert cookie: %w", err)
}
return result.RowsAffected() > 0, nil
}
func (c *Cookie) Update(
ctx context.Context,
tx pg.Tx,