fix: Set IdempotencyKey before validation in CreateWithIdempotency

This commit is contained in:
Fimeg
2025-12-20 16:32:23 -05:00
parent d226536c76
commit b614cdb612

View File

@@ -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)