From b614cdb612dd8db8d558cc706731580a87e0545b Mon Sep 17 00:00:00 2001 From: Fimeg Date: Sat, 20 Dec 2025 16:32:23 -0500 Subject: [PATCH] fix: Set IdempotencyKey before validation in CreateWithIdempotency --- aggregator-server/internal/command/factory.go | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/aggregator-server/internal/command/factory.go b/aggregator-server/internal/command/factory.go index 13b1b3a..1734403 100644 --- a/aggregator-server/internal/command/factory.go +++ b/aggregator-server/internal/command/factory.go @@ -57,11 +57,21 @@ func (f *Factory) CreateWithIdempotency(agentID uuid.UUID, commandType string, p if err != nil { // If no existing command found, proceed with creation if err.Error() == "sql: no rows in result set" || err.Error() == "command not found" { - cmd, createErr := f.Create(agentID, commandType, params) - if createErr != nil { - return nil, createErr + cmd := &models.AgentCommand{ + ID: uuid.New(), + AgentID: agentID, + CommandType: commandType, + Status: "pending", + Source: determineSource(commandType), + IdempotencyKey: &idempotencyKey, + Params: params, + CreatedAt: time.Now(), + UpdatedAt: time.Now(), + } + + if err := f.validator.Validate(cmd); err != nil { + return nil, fmt.Errorf("command validation failed: %w", err) } - cmd.IdempotencyKey = &idempotencyKey return cmd, nil } return nil, fmt.Errorf("failed to check idempotency: %w", err)