Skip to content

Commit

Permalink
Merge pull request #106 from isaqueveras/disable-user
Browse files Browse the repository at this point in the history
adding route to disable user
  • Loading branch information
isaqueveras committed Feb 11, 2023
2 parents 8476ddc + 625d548 commit 9fb2af8
Show file tree
Hide file tree
Showing 10 changed files with 121 additions and 4 deletions.
34 changes: 34 additions & 0 deletions internal/application/auth/user/business.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright (c) 2023 Isaque Veras
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file.

package user

import (
"context"

"github.com/google/uuid"
"github.com/isaqueveras/power-sso/internal/infrastructure/auth/user"
"github.com/isaqueveras/power-sso/pkg/database/postgres"
"github.com/isaqueveras/power-sso/pkg/oops"
)

// Disable is the business logic for disable user
func Disable(ctx context.Context, userUUID *uuid.UUID) (err error) {
var transaction *postgres.DBTransaction
if transaction, err = postgres.NewTransaction(ctx, false); err != nil {
return oops.Err(err)
}
defer transaction.Rollback()

repository := user.New(transaction)
if err = repository.DisableUser(userUUID); err != nil {
return oops.Err(err)
}

if err = transaction.Commit(); err != nil {
return oops.Err(err)
}

return
}
3 changes: 3 additions & 0 deletions internal/domain/auth/user/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@

package user

import "github.com/google/uuid"

// IUser define an interface for data layer access methods
type IUser interface {
GetUser(data *User) error
FindByEmailUserExists(email *string) (exists bool, err error)
DisableUser(userUUID *uuid.UUID) error
}
19 changes: 19 additions & 0 deletions internal/infrastructure/auth/user/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"database/sql"

"github.com/Masterminds/squirrel"
"github.com/google/uuid"

"github.com/isaqueveras/power-sso/internal/domain/auth/user"
"github.com/isaqueveras/power-sso/pkg/database/postgres"
Expand Down Expand Up @@ -78,3 +79,21 @@ func (pg *pgUser) getUser(data *user.User) (err error) {
}
return nil
}

// disableUser disable user in database
func (pg *pgUser) disableUser(userUUID *uuid.UUID) (err error) {
if err = pg.DB.Builder.
Update("users").
Set("is_active", false).
Set("updated_at", squirrel.Expr("NOW()")).
Where(squirrel.Eq{
"id": userUUID,
"is_active": true,
}).
Suffix("RETURNING id").
Scan(new(string)); err != nil {
return oops.Err(err)
}

return
}
6 changes: 6 additions & 0 deletions internal/infrastructure/auth/user/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package user

import (
"github.com/google/uuid"
"github.com/isaqueveras/power-sso/internal/domain/auth/user"
"github.com/isaqueveras/power-sso/pkg/database/postgres"
)
Expand All @@ -30,3 +31,8 @@ func (r *repository) FindByEmailUserExists(email *string) (bool, error) {
func (r *repository) GetUser(data *user.User) error {
return r.pg.getUser(data)
}

// DisableUser contains the flow for disable user
func (r *repository) DisableUser(userUUID *uuid.UUID) error {
return r.pg.disableUser(userUUID)
}
1 change: 1 addition & 0 deletions internal/interface/http/auth/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ func (a *authHandlerSuite) SetupSuite() {
config.LoadConfig("../../../../")

a.router = gin.New()
Router(a.router.Group("v1/auth"))
RouterAuthorization(a.router.Group("v1/auth"))
}

Expand Down
9 changes: 7 additions & 2 deletions internal/interface/http/auth/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,21 @@

package auth

import "github.com/gin-gonic/gin"
import (
"github.com/gin-gonic/gin"
"github.com/isaqueveras/power-sso/internal/interface/http/auth/user"
)

// Router is the router for the auth module.
func Router(r *gin.RouterGroup) {
r.POST("activation/:token", activation)
r.POST("register", register)
r.POST("login", login)
}

// RouterAuthorization is the router for the auth module.
func RouterAuthorization(r *gin.RouterGroup) {
r.POST("register", register)
r.DELETE("logout", logout)

user.RouterWithUUID(r.Group("user/:user_uuid"))
}
39 changes: 39 additions & 0 deletions internal/interface/http/auth/user/handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright (c) 2023 Isaque Veras
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file.

package user

import (
"net/http"

"github.com/gin-gonic/gin"
"github.com/google/uuid"
"github.com/isaqueveras/power-sso/internal/application/auth/user"
"github.com/isaqueveras/power-sso/internal/utils"
"github.com/isaqueveras/power-sso/pkg/oops"
)

// disable godoc
// @Summary Disable user
// @Description Route to disable a user
// @Tags Http/Auth/User
// @Param user_uuid path string true "UUID of the user"
// @Accept json
// @Produce json
// @Success 201 {object} utils.NoContent{}
// @Router /v1/auth/user/{user_uuid}/disable [put]
func disable(ctx *gin.Context) {
userUUID, err := uuid.Parse(ctx.Param("user_uuid"))
if err != nil {
oops.Handling(ctx, err)
return
}

if err = user.Disable(ctx, &userUUID); err != nil {
oops.Handling(ctx, err)
return
}

ctx.JSON(http.StatusCreated, utils.NoContent{})
}
12 changes: 12 additions & 0 deletions internal/interface/http/auth/user/router.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Copyright (c) 2023 Isaque Veras
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file.

package user

import "github.com/gin-gonic/gin"

// RouterWithUUID is the router for the user module.
func RouterWithUUID(r *gin.RouterGroup) {
r.PUT("disable", disable)
}
File renamed without changes.
2 changes: 0 additions & 2 deletions pkg/oops/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,6 @@ func handling(rawError error) error {

case strconv.ErrSyntax:
message, code = i18n.Value("errors.handling.error.strconv_err_syntax"), defaultCode+3
default:
message, code = err.Error(), defaultCode+4
}
case nil:
return nil
Expand Down

1 comment on commit 9fb2af8

@vercel
Copy link

@vercel vercel bot commented on 9fb2af8 Feb 11, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

power-sso – ./

power-sso.vercel.app
power-sso-isaqueveras.vercel.app
power-sso-git-main-isaqueveras.vercel.app

Please sign in to comment.