diff --git a/internal/app/api/connector.go b/internal/app/api/connector.go index 7cbf60e2..5ef622dd 100644 --- a/internal/app/api/connector.go +++ b/internal/app/api/connector.go @@ -1,9 +1,7 @@ package api import ( - "context" "encoding/json" - "errors" "net/http" "time" @@ -51,6 +49,10 @@ func handleError(w http.ResponseWriter, r *http.Request, err error) { func readConfig[Config models.ConnectorConfigObject](connectorManager *integration.ConnectorManager[Config], ) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { + if connectorNotInstalled(connectorManager, w, r) { + return + } + config, err := connectorManager.ReadConfig(r.Context()) if err != nil { handleError(w, r, err) @@ -81,6 +83,10 @@ type listTasksResponseElement struct { func listTasks[Config models.ConnectorConfigObject](connectorManager *integration.ConnectorManager[Config], ) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { + if connectorNotInstalled(connectorManager, w, r) { + return + } + pageSize, err := pageSizeQueryParam(r) if err != nil { handleValidationError(w, r, err) @@ -134,6 +140,10 @@ func listTasks[Config models.ConnectorConfigObject](connectorManager *integratio func readTask[Config models.ConnectorConfigObject](connectorManager *integration.ConnectorManager[Config], ) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { + if connectorNotInstalled(connectorManager, w, r) { + return + } + taskID, err := uuid.Parse(mux.Vars(r)["taskID"]) if err != nil { handleErrorBadRequest(w, r, err) @@ -171,6 +181,10 @@ func readTask[Config models.ConnectorConfigObject](connectorManager *integration func uninstall[Config models.ConnectorConfigObject](connectorManager *integration.ConnectorManager[Config], ) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { + if connectorNotInstalled(connectorManager, w, r) { + return + } + err := connectorManager.Uninstall(r.Context()) if err != nil { handleError(w, r, err) @@ -185,7 +199,7 @@ func uninstall[Config models.ConnectorConfigObject](connectorManager *integratio func install[Config models.ConnectorConfigObject](connectorManager *integration.ConnectorManager[Config], ) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { - installed, err := connectorManager.IsInstalled(context.Background()) + installed, err := connectorManager.IsInstalled(r.Context()) if err != nil { handleError(w, r, err) @@ -222,20 +236,11 @@ func install[Config models.ConnectorConfigObject](connectorManager *integration. func reset[Config models.ConnectorConfigObject](connectorManager *integration.ConnectorManager[Config], ) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { - installed, err := connectorManager.IsInstalled(context.Background()) - if err != nil { - handleError(w, r, err) - + if connectorNotInstalled(connectorManager, w, r) { return } - if !installed { - handleError(w, r, errors.New("connector not installed")) - - return - } - - err = connectorManager.Reset(r.Context()) + err := connectorManager.Reset(r.Context()) if err != nil { handleError(w, r, err) @@ -245,3 +250,22 @@ func reset[Config models.ConnectorConfigObject](connectorManager *integration.Co w.WriteHeader(http.StatusNoContent) } } + +func connectorNotInstalled[Config models.ConnectorConfigObject](connectorManager *integration.ConnectorManager[Config], + w http.ResponseWriter, r *http.Request, +) bool { + installed, err := connectorManager.IsInstalled(r.Context()) + if err != nil { + handleError(w, r, err) + + return true + } + + if !installed { + handleErrorBadRequest(w, r, integration.ErrNotInstalled) + + return true + } + + return false +} diff --git a/internal/app/api/stripe.go b/internal/app/api/stripe.go index 1f17272b..e0507a7a 100644 --- a/internal/app/api/stripe.go +++ b/internal/app/api/stripe.go @@ -5,6 +5,8 @@ import ( "encoding/json" "net/http" + "github.com/formancehq/payments/internal/app/integration" + "github.com/formancehq/go-libs/api" "github.com/formancehq/payments/internal/app/models" @@ -49,13 +51,27 @@ func (req *stripeTransferRequest) validate() error { type stripeTransfersRepository interface { GetConfig(ctx context.Context, connectorName models.ConnectorProvider, cfg any) error + IsInstalled(ctx context.Context, provider models.ConnectorProvider) (bool, error) } func handleStripeTransfers(repo stripeTransfersRepository) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { + installed, err := repo.IsInstalled(r.Context(), stripeConnector.Name) + if err != nil { + handleError(w, r, err) + + return + } + + if !installed { + handleErrorBadRequest(w, r, integration.ErrNotInstalled) + + return + } + var cfg stripeConnector.Config - if err := repo.GetConfig(r.Context(), stripeConnector.Name, &cfg); err != nil { + if err = repo.GetConfig(r.Context(), stripeConnector.Name, &cfg); err != nil { handleError(w, r, err) return @@ -65,7 +81,7 @@ func handleStripeTransfers(repo stripeTransfersRepository) http.HandlerFunc { var transferRequest stripeTransferRequest - err := json.NewDecoder(r.Body).Decode(&transferRequest) + err = json.NewDecoder(r.Body).Decode(&transferRequest) if err != nil { handleError(w, r, err)