Skip to content

Commit

Permalink
fix(payments): fix connectors installation
Browse files Browse the repository at this point in the history
  • Loading branch information
paul-nicolas committed Sep 13, 2024
1 parent 6a11b77 commit d8a22b4
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 46 deletions.
29 changes: 2 additions & 27 deletions components/payments/internal/api/v3/handler_connectors_install.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package v3

import (
"encoding/json"
"errors"
"io"
"net/http"

Expand All @@ -11,18 +9,6 @@ import (
"github.com/formancehq/stack/libs/go-libs/api"
)

type connectorsInstallRequest struct {
Provider string `json:"provider"`
}

func (request connectorsInstallRequest) validate() error {
if request.Provider == "" {
return errors.New("provider is required")
}

return nil
}

func connectorsInstall(backend backend.Backend) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
ctx, span := otel.Tracer().Start(r.Context(), "v3_connectorsInstall")
Expand All @@ -35,20 +21,9 @@ func connectorsInstall(backend backend.Backend) http.HandlerFunc {
return
}

var request connectorsInstallRequest
if err := json.Unmarshal(config, &request); err != nil {
otel.RecordError(span, err)
api.BadRequest(w, ErrMissingOrInvalidBody, err)
return
}

if err := request.validate(); err != nil {
otel.RecordError(span, err)
api.BadRequest(w, ErrMissingOrInvalidBody, err)
return
}
provider := connector(r)

connectorID, err := backend.ConnectorsInstall(ctx, request.Provider, config)
connectorID, err := backend.ConnectorsInstall(ctx, provider, config)
if err != nil {
otel.RecordError(span, err)
handleServiceErrors(w, r, err)
Expand Down
6 changes: 5 additions & 1 deletion components/payments/internal/api/v3/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func newRouter(backend backend.Backend, a auth.Auth) *chi.Mux {
// Connectors
r.Route("/connectors", func(r chi.Router) {
r.Get("/", connectorsList(backend))
r.Post("/", connectorsInstall(backend))
r.Post("/install/{connector}", connectorsInstall(backend))

r.Get("/configs", connectorsConfigs(backend))

Expand All @@ -100,6 +100,10 @@ func newRouter(backend backend.Backend, a auth.Auth) *chi.Mux {
return r
}

func connector(r *http.Request) string {
return chi.URLParam(r, "connector")
}

func connectorID(r *http.Request) string {
return chi.URLParam(r, "connectorID")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

func (a Activities) StorageWebhooksConfigsStore(ctx context.Context, configs []models.WebhookConfig) error {
return a.StorageWebhooksConfigsStore(ctx, configs)
return a.storage.WebhooksConfigsUpsert(ctx, configs)
}

var StorageWebhooksConfigsStoreActivity = Activities{}.StorageWebhooksConfigsStore
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,20 @@ func (w Workflow) runInstallConnector(
return errors.Wrap(err, "failed to store tasks tree")
}

configs := make([]models.WebhookConfig, 0, len(installResponse.WebhooksConfigs))
for _, webhookConfig := range installResponse.WebhooksConfigs {
configs = append(configs, models.WebhookConfig{
Name: webhookConfig.Name,
ConnectorID: installConnector.ConnectorID,
URLPath: webhookConfig.URLPath,
})
}
// TODO(polo): store the capabilities
err = activities.StorageWebhooksConfigsStore(infiniteRetryContext(ctx), configs)
if err != nil {
return errors.Wrap(err, "failed to store webhooks configs")
if len(installResponse.WebhooksConfigs) > 0 {
configs := make([]models.WebhookConfig, 0, len(installResponse.WebhooksConfigs))
for _, webhookConfig := range installResponse.WebhooksConfigs {
configs = append(configs, models.WebhookConfig{
Name: webhookConfig.Name,
ConnectorID: installConnector.ConnectorID,
URLPath: webhookConfig.URLPath,
})
}
// TODO(polo): store the capabilities
err = activities.StorageWebhooksConfigsStore(infiniteRetryContext(ctx), configs)
if err != nil {
return errors.Wrap(err, "failed to store webhooks configs")
}
}

var config models.Config
Expand Down
34 changes: 34 additions & 0 deletions components/payments/internal/storage/migrations/0-init-schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -251,4 +251,38 @@ create table if not exists workflows_instances (
alter table workflows_instances
add constraint workflows_instances_connector_id_fk foreign key (connector_id)
references connectors (id)
on delete cascade;

-- Webhook configs
create table if not exists webhooks_configs (
-- Mandatory fields
name text not null,
connector_id varchar not null,
url_path text not null,

-- Primary key
primary key (name, connector_id)
);
alter table webhooks_configs
add constraint webhooks_configs_connector_id_fk foreign key (connector_id)
references connectors (id)
on delete cascade;

-- Webhooks
create table if not exists webhooks (
-- Mandatory fields
id text not null,
connector_id varchar not null,

-- Optional fields
headers json,
query_values json,
body bytea,

-- Primary key
primary key (id)
);
alter table webhooks
add constraint webhooks_connector_id_fk foreign key (connector_id)
references connectors (id)
on delete cascade;
3 changes: 0 additions & 3 deletions components/payments/internal/storage/payments.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ type payment struct {
Amount *big.Int `bun:"amount,type:numeric,notnull"`
Asset string `bun:"asset,type:text,notnull"`
Scheme models.PaymentScheme `bun:"scheme,type:text,notnull"`
Status models.PaymentStatus `bun:"status,type:text,notnull"`

// Optional fields
// c.f.: https://bun.uptrace.dev/guide/models.html#nulls
Expand Down Expand Up @@ -276,7 +275,6 @@ func fromPaymentModels(from models.Payment) payment {
Amount: from.Amount,
Asset: from.Asset,
Scheme: from.Scheme,
Status: from.Status,
SourceAccountID: from.SourceAccountID,
DestinationAccountID: from.DestinationAccountID,
Metadata: from.Metadata,
Expand All @@ -294,7 +292,6 @@ func toPaymentModels(payment payment) models.Payment {
Amount: payment.Amount,
Asset: payment.Asset,
Scheme: payment.Scheme,
Status: payment.Status,
SourceAccountID: payment.SourceAccountID,
DestinationAccountID: payment.DestinationAccountID,
Metadata: payment.Metadata,
Expand Down
4 changes: 2 additions & 2 deletions components/payments/internal/storage/webhooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ type webhook struct {
ConnectorID models.ConnectorID `bun:"connector_id,type:character varying,notnull"`

// Optional fields
Headers map[string][]string `bun:"headers,type:jsonb"`
QueryValues map[string][]string `bun:"query_values,type:jsonb"`
Headers map[string][]string `bun:"headers,type:json"`
QueryValues map[string][]string `bun:"query_values,type:json"`
Body []byte `bun:"payload,type:bytea,nullzero"`
}

Expand Down

0 comments on commit d8a22b4

Please sign in to comment.