Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add logic for checking whether connector is installed #95

Merged
merged 1 commit into from
Feb 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 38 additions & 14 deletions internal/app/api/connector.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package api

import (
"context"
"encoding/json"
"errors"
"net/http"
"time"

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)

Expand Down Expand Up @@ -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)

Expand All @@ -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
}
20 changes: 18 additions & 2 deletions internal/app/api/stripe.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand All @@ -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)

Expand Down