From d6c5740f9cedf14c1eb42a0407d641e315da9c67 Mon Sep 17 00:00:00 2001 From: Hein Date: Sun, 20 Sep 2026 16:12:44 +0200 Subject: [PATCH] fix(handler): add operation type to hook context --- pkg/resolvespec/handler.go | 215 +++++++++++++++++++----------------- pkg/restheadspec/handler.go | 8 ++ 2 files changed, 121 insertions(+), 102 deletions(-) diff --git a/pkg/resolvespec/handler.go b/pkg/resolvespec/handler.go index 6d98569..4211fe0 100644 --- a/pkg/resolvespec/handler.go +++ b/pkg/resolvespec/handler.go @@ -306,15 +306,16 @@ func (h *Handler) handleRead(ctx context.Context, w common.ResponseWriter, id st txErr := h.db.RunInTransaction(ctx, func(tx common.Database) error { hookCtx := &HookContext{ - Context: ctx, - Handler: h, - Schema: schema, - Entity: entity, - Model: model, - Options: options, - ID: id, - Writer: w, - Tx: tx, + Context: ctx, + Handler: h, + Schema: schema, + Entity: entity, + Model: model, + Operation: "read", + Options: options, + ID: id, + Writer: w, + Tx: tx, } if err := h.hooks.ExecuteBeforeOp(BeforeRead, hookCtx); err != nil { statusCode, errCode, errMsg = http.StatusInternalServerError, "hook_error", "BeforeRead hook failed" @@ -722,15 +723,16 @@ func (h *Handler) handleCreate(ctx context.Context, w common.ResponseWriter, dat var nestedResult *common.ProcessResult err := h.db.RunInTransaction(ctx, func(tx common.Database) error { hookCtx := &HookContext{ - Context: ctx, - Handler: h, - Schema: schema, - Entity: entity, - Model: model, - Options: options, - Data: v, - Writer: w, - Tx: tx, + Context: ctx, + Handler: h, + Schema: schema, + Entity: entity, + Model: model, + Operation: "create", + Options: options, + Data: v, + Writer: w, + Tx: tx, } if err := h.hooks.ExecuteBeforeOp(BeforeCreate, hookCtx); err != nil { return fmt.Errorf("BeforeCreate hook failed: %w", err) @@ -769,15 +771,16 @@ func (h *Handler) handleCreate(ctx context.Context, w common.ResponseWriter, dat var responseData interface{} = v err := h.db.RunInTransaction(ctx, func(tx common.Database) error { hookCtx := &HookContext{ - Context: ctx, - Handler: h, - Schema: schema, - Entity: entity, - Model: model, - Options: options, - Data: v, - Writer: w, - Tx: tx, + Context: ctx, + Handler: h, + Schema: schema, + Entity: entity, + Model: model, + Operation: "create", + Options: options, + Data: v, + Writer: w, + Tx: tx, } if err := h.hooks.ExecuteBeforeOp(BeforeCreate, hookCtx); err != nil { return fmt.Errorf("BeforeCreate hook failed: %w", err) @@ -851,15 +854,16 @@ func (h *Handler) handleCreate(ctx context.Context, w common.ResponseWriter, dat for _, item := range v { hookCtx := &HookContext{ - Context: ctx, - Handler: h, - Schema: schema, - Entity: entity, - Model: model, - Options: options, - Data: item, - Writer: w, - Tx: tx, + Context: ctx, + Handler: h, + Schema: schema, + Entity: entity, + Model: model, + Operation: "create", + Options: options, + Data: item, + Writer: w, + Tx: tx, } if err := h.hooks.ExecuteBeforeOp(BeforeCreate, hookCtx); err != nil { return fmt.Errorf("BeforeCreate hook failed: %w", err) @@ -898,15 +902,16 @@ func (h *Handler) handleCreate(ctx context.Context, w common.ResponseWriter, dat err := h.db.RunInTransaction(ctx, func(tx common.Database) error { for _, item := range v { hookCtx := &HookContext{ - Context: ctx, - Handler: h, - Schema: schema, - Entity: entity, - Model: model, - Options: options, - Data: item, - Writer: w, - Tx: tx, + Context: ctx, + Handler: h, + Schema: schema, + Entity: entity, + Model: model, + Operation: "create", + Options: options, + Data: item, + Writer: w, + Tx: tx, } if err := h.hooks.ExecuteBeforeOp(BeforeCreate, hookCtx); err != nil { return fmt.Errorf("BeforeCreate hook failed: %w", err) @@ -982,15 +987,16 @@ func (h *Handler) handleCreate(ctx context.Context, w common.ResponseWriter, dat for _, item := range v { if itemMap, ok := item.(map[string]interface{}); ok { hookCtx := &HookContext{ - Context: ctx, - Handler: h, - Schema: schema, - Entity: entity, - Model: model, - Options: options, - Data: itemMap, - Writer: w, - Tx: tx, + Context: ctx, + Handler: h, + Schema: schema, + Entity: entity, + Model: model, + Operation: "create", + Options: options, + Data: itemMap, + Writer: w, + Tx: tx, } if err := h.hooks.ExecuteBeforeOp(BeforeCreate, hookCtx); err != nil { return fmt.Errorf("BeforeCreate hook failed: %w", err) @@ -1035,15 +1041,16 @@ func (h *Handler) handleCreate(ctx context.Context, w common.ResponseWriter, dat } hookCtx := &HookContext{ - Context: ctx, - Handler: h, - Schema: schema, - Entity: entity, - Model: model, - Options: options, - Data: itemMap, - Writer: w, - Tx: tx, + Context: ctx, + Handler: h, + Schema: schema, + Entity: entity, + Model: model, + Operation: "create", + Options: options, + Data: itemMap, + Writer: w, + Tx: tx, } if err := h.hooks.ExecuteBeforeOp(BeforeCreate, hookCtx); err != nil { return fmt.Errorf("BeforeCreate hook failed: %w", err) @@ -1166,16 +1173,17 @@ func (h *Handler) handleUpdate(ctx context.Context, w common.ResponseWriter, url // they must run before the existence-check select so that select is // also subject to RLS on this connection/transaction. hookCtx := &HookContext{ - Context: ctx, - Handler: h, - Schema: schema, - Entity: entity, - Model: model, - Options: options, - ID: urlID, - Data: updates, - Writer: w, - Tx: tx, + Context: ctx, + Handler: h, + Schema: schema, + Entity: entity, + Model: model, + Operation: "update", + Options: options, + ID: urlID, + Data: updates, + Writer: w, + Tx: tx, } if err := h.hooks.ExecuteBeforeOp(BeforeUpdate, hookCtx); err != nil { @@ -1387,16 +1395,17 @@ func (h *Handler) handleUpdate(ctx context.Context, w common.ResponseWriter, url // Execute BeforeUpdate hooks inside transaction hookCtx := &HookContext{ - Context: ctx, - Handler: h, - Schema: schema, - Entity: entity, - Model: model, - Options: options, - ID: itemIDStr, - Data: item, - Writer: w, - Tx: tx, + Context: ctx, + Handler: h, + Schema: schema, + Entity: entity, + Model: model, + Operation: "update", + Options: options, + ID: itemIDStr, + Data: item, + Writer: w, + Tx: tx, } if err := h.hooks.ExecuteBeforeOp(BeforeUpdate, hookCtx); err != nil { @@ -1543,16 +1552,17 @@ func (h *Handler) handleUpdate(ctx context.Context, w common.ResponseWriter, url // Execute BeforeUpdate hooks inside transaction hookCtx := &HookContext{ - Context: ctx, - Handler: h, - Schema: schema, - Entity: entity, - Model: model, - Options: options, - ID: itemIDStr, - Data: itemMap, - Writer: w, - Tx: tx, + Context: ctx, + Handler: h, + Schema: schema, + Entity: entity, + Model: model, + Operation: "update", + Options: options, + ID: itemIDStr, + Data: itemMap, + Writer: w, + Tx: tx, } if err := h.hooks.ExecuteBeforeOp(BeforeUpdate, hookCtx); err != nil { @@ -1648,15 +1658,16 @@ func (h *Handler) handleDelete(ctx context.Context, w common.ResponseWriter, id // Execute BeforeDelete hooks (covers model-rule checks before any deletion) hookCtx := &HookContext{ - Context: ctx, - Handler: h, - Schema: schema, - Entity: entity, - Model: model, - ID: id, - Data: data, - Writer: w, - Tx: h.db, + Context: ctx, + Handler: h, + Schema: schema, + Entity: entity, + Model: model, + Operation: "delete", + ID: id, + Data: data, + Writer: w, + Tx: h.db, } if err := h.hooks.ExecuteBeforeOp(BeforeDelete, hookCtx); err != nil { logger.Error("BeforeDelete hook failed: %v", err) diff --git a/pkg/restheadspec/handler.go b/pkg/restheadspec/handler.go index 01fbe6b..620e546 100644 --- a/pkg/restheadspec/handler.go +++ b/pkg/restheadspec/handler.go @@ -379,6 +379,7 @@ func (h *Handler) handleRead(ctx context.Context, w common.ResponseWriter, id st Entity: entity, TableName: tableName, Model: model, + Operation: "read", Options: options, ID: id, Writer: w, @@ -1236,6 +1237,7 @@ func (h *Handler) handleCreate(ctx context.Context, w common.ResponseWriter, dat Entity: entity, TableName: tableName, Model: model, + Operation: "create", Options: options, Data: data, Writer: w, @@ -1335,6 +1337,7 @@ func (h *Handler) handleCreate(ctx context.Context, w common.ResponseWriter, dat Entity: entity, TableName: tableName, Model: model, + Operation: "create", Options: options, Data: modelValue, Writer: w, @@ -1489,6 +1492,7 @@ func (h *Handler) handleUpdate(ctx context.Context, w common.ResponseWriter, id TableName: tableName, Tx: tx, Model: model, + Operation: "update", Options: options, ID: id, Data: dataMap, @@ -1686,6 +1690,7 @@ func (h *Handler) handleDelete(ctx context.Context, w common.ResponseWriter, id Entity: entity, TableName: tableName, Model: model, + Operation: "delete", ID: itemID, Writer: w, Tx: tx, @@ -1760,6 +1765,7 @@ func (h *Handler) handleDelete(ctx context.Context, w common.ResponseWriter, id Entity: entity, TableName: tableName, Model: model, + Operation: "delete", ID: itemIDStr, Writer: w, Tx: tx, @@ -1818,6 +1824,7 @@ func (h *Handler) handleDelete(ctx context.Context, w common.ResponseWriter, id Entity: entity, TableName: tableName, Model: model, + Operation: "delete", ID: itemIDStr, Writer: w, Tx: tx, @@ -1902,6 +1909,7 @@ func (h *Handler) handleDelete(ctx context.Context, w common.ResponseWriter, id Entity: entity, TableName: tableName, Model: model, + Operation: "delete", ID: id, Writer: w, Tx: h.db,