Add reorderCookieCategory mutation
Category reordering previously required two separate updateCookieCategory calls to swap ranks, which was not atomic. Replace with a single reorderCookieCategory mutation that shifts all affected ranks in one SQL statement, and remove the rank field from UpdateCookieCategoryInput. Signed-off-by: Émile Ré <emile@getprobo.com>
This commit is contained in:
@@ -410,7 +410,6 @@ func (r *mutationResolver) UpdateCookieCategory(ctx context.Context, input types
|
||||
CookieCategoryID: input.CookieCategoryID,
|
||||
Name: input.Name,
|
||||
Description: input.Description,
|
||||
Rank: input.Rank,
|
||||
Cookies: cookies,
|
||||
},
|
||||
)
|
||||
@@ -482,6 +481,38 @@ func (r *mutationResolver) DeleteCookieCategory(ctx context.Context, input types
|
||||
}, nil
|
||||
}
|
||||
|
||||
// ReorderCookieCategory is the resolver for the reorderCookieCategory field.
|
||||
func (r *mutationResolver) ReorderCookieCategory(ctx context.Context, input types.ReorderCookieCategoryInput) (*types.ReorderCookieCategoryPayload, error) {
|
||||
if err := r.authorize(ctx, input.CookieCategoryID, probo.ActionCookieCategoryUpdate); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
scope := coredata.NewScopeFromObjectID(input.CookieCategoryID)
|
||||
|
||||
banner, err := r.cookieBanner.ReorderCookieCategory(
|
||||
ctx,
|
||||
scope,
|
||||
cookiebanner.ReorderCookieCategoryRequest{
|
||||
CookieCategoryID: input.CookieCategoryID,
|
||||
Rank: input.Rank,
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
if errors.Is(err, cookiebanner.ErrCategoryNotFound) {
|
||||
return nil, gqlutils.NotFound(ctx, err)
|
||||
}
|
||||
if validationErrors, ok := errors.AsType[validator.ValidationErrors](err); ok {
|
||||
return nil, gqlutils.InvalidValidationErrors(ctx, validationErrors)
|
||||
}
|
||||
r.logger.ErrorCtx(ctx, "cannot reorder cookie category", log.Error(err))
|
||||
return nil, gqlutils.Internal(ctx)
|
||||
}
|
||||
|
||||
return &types.ReorderCookieCategoryPayload{
|
||||
CookieBanner: types.NewCookieBanner(banner),
|
||||
}, nil
|
||||
}
|
||||
|
||||
// CookieBanner returns schema.CookieBannerResolver implementation.
|
||||
func (r *Resolver) CookieBanner() schema.CookieBannerResolver { return &cookieBannerResolver{r} }
|
||||
|
||||
|
||||
@@ -169,6 +169,9 @@ extend type Mutation {
|
||||
deleteCookieCategory(
|
||||
input: DeleteCookieCategoryInput!
|
||||
): DeleteCookieCategoryPayload!
|
||||
reorderCookieCategory(
|
||||
input: ReorderCookieCategoryInput!
|
||||
): ReorderCookieCategoryPayload!
|
||||
}
|
||||
|
||||
input CreateCookieBannerInput {
|
||||
@@ -218,7 +221,6 @@ input UpdateCookieCategoryInput {
|
||||
cookieCategoryId: ID!
|
||||
name: String
|
||||
description: String
|
||||
rank: Int
|
||||
cookies: [CookieItemInput!]
|
||||
}
|
||||
|
||||
@@ -226,6 +228,11 @@ input DeleteCookieCategoryInput {
|
||||
cookieCategoryId: ID!
|
||||
}
|
||||
|
||||
input ReorderCookieCategoryInput {
|
||||
cookieCategoryId: ID!
|
||||
rank: Int!
|
||||
}
|
||||
|
||||
input CookieItemInput {
|
||||
name: String!
|
||||
duration: String!
|
||||
@@ -271,3 +278,7 @@ type DeleteCookieCategoryPayload {
|
||||
deletedCookieCategoryId: ID!
|
||||
cookieBanner: CookieBanner!
|
||||
}
|
||||
|
||||
type ReorderCookieCategoryPayload {
|
||||
cookieBanner: CookieBanner!
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user