Procházet zdrojové kódy

fix: move setup condition check to helper

ntorga před 4 měsíci
rodič
revize
1e66b13fc3

+ 24 - 0
src/presentation/ui/presenter/helper/shouldEnableInitialSetup.go

@@ -0,0 +1,24 @@
+package presenterHelper
+
+import (
+	"github.com/goinfinite/os/src/domain/dto"
+	"github.com/goinfinite/os/src/presentation/service"
+)
+
+func ShouldEnableInitialSetup(accountService *service.AccountService) bool {
+	accountsServiceResponse := accountService.Read(map[string]interface{}{})
+	if accountsServiceResponse.Status != service.Success {
+		return false
+	}
+
+	accountsReadResponse, assertOk := accountsServiceResponse.Body.(dto.ReadAccountsResponse)
+	if !assertOk {
+		return false
+	}
+
+	if len(accountsReadResponse.Accounts) > 0 {
+		return false
+	}
+
+	return true
+}

+ 2 - 20
src/presentation/ui/presenter/login.go

@@ -3,13 +3,13 @@ package presenter
 import (
 	"net/http"
 
-	"github.com/goinfinite/os/src/domain/dto"
 	"github.com/goinfinite/os/src/domain/useCase"
 	"github.com/goinfinite/os/src/domain/valueObject"
 	infraEnvs "github.com/goinfinite/os/src/infra/envs"
 	internalDbInfra "github.com/goinfinite/os/src/infra/internalDatabase"
 	"github.com/goinfinite/os/src/presentation/service"
 	"github.com/goinfinite/os/src/presentation/ui/layout"
+	presenterHelper "github.com/goinfinite/os/src/presentation/ui/presenter/helper"
 	"github.com/labstack/echo/v4"
 )
 
@@ -26,26 +26,8 @@ func NewLoginPresenter(
 	}
 }
 
-func (presenter *LoginPresenter) shouldRedirectToSetupPage() bool {
-	responseOutput := presenter.accountService.Read(map[string]interface{}{})
-	if responseOutput.Status != service.Success {
-		return false
-	}
-
-	typedOutputBody, assertOk := responseOutput.Body.(dto.ReadAccountsResponse)
-	if !assertOk {
-		return false
-	}
-
-	if len(typedOutputBody.Accounts) > 0 {
-		return false
-	}
-
-	return true
-}
-
 func (presenter *LoginPresenter) Handler(c echo.Context) error {
-	if presenter.shouldRedirectToSetupPage() {
+	if presenterHelper.ShouldEnableInitialSetup(presenter.accountService) {
 		return c.Redirect(http.StatusFound, "/setup/")
 	}
 

+ 2 - 12
src/presentation/ui/presenter/setup.go

@@ -3,10 +3,10 @@ package presenter
 import (
 	"net/http"
 
-	"github.com/goinfinite/os/src/domain/dto"
 	internalDbInfra "github.com/goinfinite/os/src/infra/internalDatabase"
 	"github.com/goinfinite/os/src/presentation/service"
 	"github.com/goinfinite/os/src/presentation/ui/layout"
+	presenterHelper "github.com/goinfinite/os/src/presentation/ui/presenter/helper"
 	"github.com/labstack/echo/v4"
 )
 
@@ -24,17 +24,7 @@ func NewSetupPresenter(
 }
 
 func (presenter *SetupPresenter) Handler(c echo.Context) error {
-	responseOutput := presenter.accountService.Read(map[string]interface{}{})
-	if responseOutput.Status != service.Success {
-		return nil
-	}
-
-	typedOutputBody, assertOk := responseOutput.Body.(dto.ReadAccountsResponse)
-	if !assertOk {
-		return nil
-	}
-
-	if len(typedOutputBody.Accounts) > 0 {
+	if !presenterHelper.ShouldEnableInitialSetup(presenter.accountService) {
 		return c.Redirect(http.StatusFound, "/login/")
 	}