Add slug to cookie categories for stable consent identifiers

The category slug provides a stable, URL-safe key used as the
data-cookie-consent attribute value and consent data key, replacing
the fragile category name. This prevents breakage when categories
are renamed.

- Add slug column with unique-per-banner constraint and backfill migration
- Add Slug validator (lowercase alphanumeric + hyphens)
- Propagate slug through GraphQL schema, service layer, and snapshot
- Update console UI with slug field in create/edit forms
- Switch cookie-banner widget to use slug as consent data keys

Signed-off-by: Émile Ré <emile@getprobo.com>
This commit is contained in:
Émile Ré
2026-04-23 19:07:11 +04:00
parent bb39daebc5
commit 165b9ad9d3
19 changed files with 218 additions and 22 deletions

View File

@@ -18,15 +18,16 @@ import "go.probo.inc/probo/pkg/coredata"
var defaultCategories = []struct {
Name string
Slug string
Description string
Kind coredata.CookieCategoryKind
Rank int
}{
{"Necessary", "Essential cookies required for the website to function properly.", coredata.CookieCategoryKindNecessary, 0},
{"Analytics", "Cookies that help understand how visitors interact with the website.", coredata.CookieCategoryKindNormal, 1},
{"Advertising", "Cookies used to deliver relevant advertisements and track campaigns.", coredata.CookieCategoryKindNormal, 2},
{"Functional", "Cookies that enable enhanced functionality and personalization.", coredata.CookieCategoryKindNormal, 3},
{"Uncategorised", "Cookies that have not been assigned to a category yet.", coredata.CookieCategoryKindUncategorised, 4},
{"Necessary", "necessary", "Essential cookies required for the website to function properly.", coredata.CookieCategoryKindNecessary, 0},
{"Analytics", "analytics", "Cookies that help understand how visitors interact with the website.", coredata.CookieCategoryKindNormal, 1},
{"Advertising", "advertising", "Cookies used to deliver relevant advertisements and track campaigns.", coredata.CookieCategoryKindNormal, 2},
{"Functional", "functional", "Cookies that enable enhanced functionality and personalization.", coredata.CookieCategoryKindNormal, 3},
{"Uncategorised", "uncategorised", "Cookies that have not been assigned to a category yet.", coredata.CookieCategoryKindUncategorised, 4},
}
var defaultUIStringsByLanguage = map[string]map[string]string{

View File

@@ -26,6 +26,7 @@ var (
ErrNoPublishedVersion = errors.New("no published cookie banner version")
ErrNoDraftVersion = errors.New("no draft cookie banner version to publish")
ErrCannotDeleteSystemCategory = errors.New("cannot delete system cookie category")
ErrCategorySlugAlreadyExists = errors.New("a category with this slug already exists in this banner")
ErrOriginAlreadyInUse = errors.New("origin is already used by another active cookie banner")
ErrConsentNotFound = errors.New("consent record not found")
ErrCookieNotFound = errors.New("cookie not found")

View File

@@ -53,6 +53,7 @@ type (
CreateCookieCategoryRequest struct {
CookieBannerID gid.GID
Name string
Slug string
Description string
Rank int
}
@@ -70,6 +71,7 @@ type (
UpdateCookieCategoryRequest struct {
CookieCategoryID gid.GID
Name *string
Slug *string
Description *string
}
@@ -189,6 +191,7 @@ func (r *CreateCookieCategoryRequest) Validate() error {
v.Check(r.CookieBannerID, "cookie_banner_id", validator.Required(), validator.GID(coredata.CookieBannerEntityType))
v.Check(r.Name, "name", validator.Required(), validator.SafeTextNoNewLine(255))
v.Check(r.Slug, "slug", validator.Required(), validator.Slug(100))
v.Check(r.Description, "description", validator.Required(), validator.SafeText(1000))
v.Check(r.Rank, "rank", validator.Min(0))
@@ -200,6 +203,7 @@ func (r *UpdateCookieCategoryRequest) Validate() error {
v.Check(r.CookieCategoryID, "cookie_category_id", validator.Required(), validator.GID(coredata.CookieCategoryEntityType))
v.Check(r.Name, "name", validator.SafeTextNoNewLine(255))
v.Check(r.Slug, "slug", validator.Slug(100))
v.Check(r.Description, "description", validator.SafeText(1000))
return v.Error()
@@ -327,6 +331,7 @@ func buildSnapshot(
}
snapshotCategories[i] = coredata.CookieBannerVersionSnapshotCategory{
Name: c.Name,
Slug: c.Slug,
Description: c.Description,
Kind: c.Kind,
Cookies: cookies,
@@ -532,6 +537,7 @@ func (s *Service) CreateCookieBanner(
OrganizationID: banner.OrganizationID,
CookieBannerID: banner.ID,
Name: dc.Name,
Slug: dc.Slug,
Description: dc.Description,
Kind: dc.Kind,
Rank: dc.Rank,
@@ -925,6 +931,7 @@ func (s *Service) CreateCookieCategory(
OrganizationID: banner.OrganizationID,
CookieBannerID: req.CookieBannerID,
Name: req.Name,
Slug: req.Slug,
Description: req.Description,
Kind: coredata.CookieCategoryKindNormal,
Rank: req.Rank,
@@ -933,6 +940,9 @@ func (s *Service) CreateCookieCategory(
}
if err := category.Insert(ctx, tx, scope); err != nil {
if errors.Is(err, coredata.ErrResourceAlreadyExists) {
return ErrCategorySlugAlreadyExists
}
return fmt.Errorf("cannot insert cookie category: %w", err)
}
@@ -1274,6 +1284,9 @@ func (s *Service) UpdateCookieCategory(
if req.Name != nil {
category.Name = *req.Name
}
if req.Slug != nil {
category.Slug = *req.Slug
}
if req.Description != nil {
category.Description = *req.Description
}
@@ -1281,6 +1294,9 @@ func (s *Service) UpdateCookieCategory(
category.UpdatedAt = time.Now()
if err := category.Update(ctx, tx, scope); err != nil {
if errors.Is(err, coredata.ErrResourceAlreadyExists) {
return ErrCategorySlugAlreadyExists
}
return fmt.Errorf("cannot update cookie category: %w", err)
}