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

@@ -574,6 +574,7 @@ func (s *Service) CreateCookieBanner(
Kind: dc.Kind,
Rank: dc.Rank,
GCMConsentTypes: gcmConsentTypes,
PostHogConsent: dc.PostHogConsent,
CreatedAt: now,
UpdatedAt: now,
}
@@ -1350,6 +1351,15 @@ func (s *Service) UpdateCookieCategory(
category.GCMConsentTypes = *req.GCMConsentTypes
}
if req.PostHogConsent != nil {
if *req.PostHogConsent && category.Kind != coredata.CookieCategoryKindNormal {
return ErrPostHogConsentKindInvalid
}
if *req.PostHogConsent {
var categories coredata.CookieCategories
if err := categories.ClearPostHogConsentByBannerID(ctx, tx, scope, category.CookieBannerID); err != nil {
return fmt.Errorf("cannot clear posthog consent: %w", err)
}
}
category.PostHogConsent = *req.PostHogConsent
}
@@ -2091,13 +2101,13 @@ func (s *Service) ReportDetectedCookies(
UpdatedAt: now,
}
if err := cookie.Insert(ctx, tx, scope); err != nil {
if errors.Is(err, coredata.ErrResourceAlreadyExists) {
continue
}
ok, err := cookie.InsertIfNotExists(ctx, tx, scope)
if err != nil {
return fmt.Errorf("cannot insert detected cookie: %w", err)
}
inserted++
if ok {
inserted++
}
}
if inserted > 0 {