Files
payments/internal/api/v3/handler_pools_create.go
Paul NicolasandGitHub 1c854a145c feat(pools): add possibility to create dynamic pool (#598)
* feat(pools): add dynamic pools creation

* fix(pools): fix missing json handling

* feat(openapi): add pool type enum
2025-11-24 12:50:07 +00:00

88 lines
2.4 KiB
Go

package v3
import (
"encoding/json"
"fmt"
"net/http"
"time"
"github.com/formancehq/go-libs/v3/api"
"github.com/formancehq/payments/internal/api/backend"
"github.com/formancehq/payments/internal/api/validation"
"github.com/formancehq/payments/internal/models"
"github.com/formancehq/payments/internal/otel"
"github.com/google/uuid"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace"
)
type CreatePoolRequest struct {
Name string `json:"name" validate:"required"`
Query map[string]any `json:"query" validate:"required_without=AccountIDs"`
AccountIDs []string `json:"accountIDs" validate:"required_without=Query,dive,accountID"`
}
func poolsCreate(backend backend.Backend, validator *validation.Validator) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
ctx, span := otel.Tracer().Start(r.Context(), "v3_poolsCreate")
defer span.End()
var CreatePoolRequest CreatePoolRequest
err := json.NewDecoder(r.Body).Decode(&CreatePoolRequest)
if err != nil {
otel.RecordError(span, err)
api.BadRequest(w, ErrMissingOrInvalidBody, err)
return
}
populateSpanFromCreatePoolRequest(span, CreatePoolRequest)
if _, err := validator.Validate(CreatePoolRequest); err != nil {
otel.RecordError(span, err)
api.BadRequest(w, ErrValidation, err)
return
}
pool := models.Pool{
ID: uuid.New(),
Name: CreatePoolRequest.Name,
CreatedAt: time.Now().UTC(),
}
if len(CreatePoolRequest.Query) > 0 {
pool.Type = models.POOL_TYPE_DYNAMIC
pool.Query = CreatePoolRequest.Query
} else {
pool.Type = models.POOL_TYPE_STATIC
accounts := make([]models.AccountID, len(CreatePoolRequest.AccountIDs))
for i, accountID := range CreatePoolRequest.AccountIDs {
aID, err := models.AccountIDFromString(accountID)
if err != nil {
otel.RecordError(span, err)
api.BadRequest(w, ErrValidation, err)
return
}
accounts[i] = aID
}
pool.PoolAccounts = accounts
}
err = backend.PoolsCreate(ctx, pool)
if err != nil {
otel.RecordError(span, err)
handleServiceErrors(w, r, err)
return
}
api.Created(w, pool.ID.String())
}
}
func populateSpanFromCreatePoolRequest(span trace.Span, req CreatePoolRequest) {
span.SetAttributes(attribute.String("name", req.Name))
for i, acc := range req.AccountIDs {
span.SetAttributes(attribute.String(fmt.Sprintf("accountIDs[%d]", i), acc))
}
}