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:
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user