Skip to content

Commit

Permalink
Merge pull request #19 from victorts1991/develop
Browse files Browse the repository at this point in the history
add /lgpd/clientes/delete endpoint
  • Loading branch information
victorts1991 authored Jun 20, 2024
2 parents c0c4fa3 + e97f7fb commit 8394d7a
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 12 deletions.
39 changes: 37 additions & 2 deletions internal/adapters/http/handlers/cliente.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,16 @@ import (
type Cliente struct {
cadastraClienteUC usecase.CadastrarClienteUseCase
pegaClienteUC usecase.PesquisarCliente
lgpd usecase.LGPD
validator util.Validator
tokenJwt auth.Token
}

func NewCliente(cadastraClienteUC usecase.CadastrarClienteUseCase, pegaClientePorCPFUC usecase.PesquisarCliente, validator util.Validator, tokenJwt auth.Token) *Cliente {
func NewCliente(cadastraClienteUC usecase.CadastrarClienteUseCase, pegaClientePorCPFUC usecase.PesquisarCliente, lgpd usecase.LGPD, validator util.Validator, tokenJwt auth.Token) *Cliente {
return &Cliente{
cadastraClienteUC: cadastraClienteUC,
pegaClienteUC: pegaClientePorCPFUC,
lgpd: lgpd,
validator: validator,
tokenJwt: tokenJwt,
}
Expand All @@ -32,6 +34,7 @@ func (h *Cliente) RegistraRotasCliente(server *echo.Echo) {
server.POST("/cliente", h.cadastra)
server.GET("/clientes/:cpf", h.pegaPorCpf, h.tokenJwt.VerifyToken)
server.GET("/internal/clientes/:id", h.pegaPorID)
server.DELETE("/lgpd/clientes/delete", h.anonimizarClientLGPD)
}

// cadastra godoc
Expand Down Expand Up @@ -75,8 +78,9 @@ func (h *Cliente) cadastra(ctx echo.Context) error {
// @Router /clientes/{cpf} [get]
func (h *Cliente) pegaPorCpf(ctx echo.Context) error {
cpf := ctx.Param("cpf")

c := &domain.Cliente{
Cpf: cpf,
Cpf: &cpf,
}

if err := c.ValidateCPF(); err != nil {
Expand Down Expand Up @@ -133,3 +137,34 @@ func (h *Cliente) validateClienteBody(c *domain.Cliente) error {

return nil
}

// delete godoc
// @Summary deleta um cliente
// @Tags Cliente
// @Accept json
// @Produce json
// @Param pedido body domain.LGPDClienteRequest true "anonimiza os dados do cliente"
// @Success 200 {object} domain.Cliente
// @Router /lgpd/clientes/delete [delete]
func (h *Cliente) anonimizarClientLGPD(ctx echo.Context) error {
var (
cliente domain.Cliente
err error
)

if err = ctx.Bind(&cliente); err != nil {
return serverErr.HandleError(ctx, serverErr.BadRequest.New(err.Error()))
}

if err := cliente.ValidateCPF(); err != nil {
return serverErr.HandleError(ctx, serverErr.BadRequest.New(err.Error()))
}

err = h.lgpd.Anonimizar(ctx.Request().Context(), &cliente)
if err != nil {
return serverErr.HandleError(ctx, errorx.Cast(err))
}

return ctx.NoContent(http.StatusOK)

}
2 changes: 1 addition & 1 deletion internal/adapters/http/handlers/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (h *Login) RegistraRotasLogin(server *echo.Echo) {
func (h *Login) login(ctx echo.Context) error {
cpf := ctx.Param("cpf")
c := &domain.Cliente{
Cpf: cpf,
Cpf: &cpf,
}

if err := c.ValidateCPF(); err != nil {
Expand Down
30 changes: 29 additions & 1 deletion internal/adapters/repository/cliente.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"fiap-tech-challenge-api/internal/core/domain"
db "github.com/rhuandantas/fiap-tech-challenge-commons/pkg/db/mysql"
_error "github.com/rhuandantas/fiap-tech-challenge-commons/pkg/errors"

"github.com/joomcode/errorx"
"xorm.io/xorm"
)

Expand All @@ -19,6 +19,8 @@ type ClienteRepo interface {
Insere(ctx context.Context, cliente *domain.Cliente) (*domain.Cliente, error)
PesquisaPorCPF(ctx context.Context, cliente *domain.Cliente) (*domain.Cliente, error)
PesquisaPorId(ctx context.Context, id int64) (*domain.Cliente, error)
Anonimizar(ctx context.Context, cliente *domain.Cliente) error

}

func NewClienteRepo(connector db.DBConnector) ClienteRepo {
Expand Down Expand Up @@ -76,3 +78,29 @@ func (r *cliente) PesquisaPorId(ctx context.Context, id int64) (*domain.Cliente,

return &c, nil
}

func (r *cliente) Anonimizar(ctx context.Context, cliente *domain.Cliente) error {

newCliente := domain.Cliente{
Cpf: nil,
Nome: nil,
Email: nil,
Telefone: nil,
}

found, err := r.session.Context(ctx).Get(&cliente)
if err != nil {
return err
}

if !found {
return _error.NotFound.New("cliente não encontrado")
}

affected, err := r.session.Context(ctx).ID(&cliente.Id).Update(newCliente)
if err != nil {
return errorx.InternalError.New(err.Error())
}
_ = affected
return nil
}
28 changes: 21 additions & 7 deletions internal/core/domain/cliente.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,22 @@ type ClienteRequest struct {
Telefone string `json:"telefone"`
}

type LGPDClienteRequest struct {
Cpf string `json:"cpf" validate:"required" xorm:"unique"`
}

type Cliente struct {
Id int64 `json:"id" xorm:"pk autoincr 'cliente_id'"`
Cpf string `json:"cpf" validate:"required" xorm:"unique"`
Nome string `json:"nome" validate:"required"`
Email string `json:"email" validate:"email"`
Telefone string `json:"telefone"`
Cpf *string `json:"cpf" xorm:"null unique"`
Nome *string `json:"nome" xorm:"null"`
Email *string `json:"email" xorm:"null"`
Telefone *string `json:"telefone" xorm:"null"`
CreatedAt time.Time `json:"created_at" xorm:"created"`
UpdatedAt time.Time `json:"updated_at" xorm:"updated"`
}

func (c *Cliente) ValidateCPF() error {
if !brdoc.IsCPF(c.Cpf) {
if !brdoc.IsCPF(DerefString(c.Cpf)) {
return errors.New(commons.CpfInvalido)
}

Expand All @@ -37,13 +41,23 @@ func (c *Cliente) ValidateCPF() error {
return nil
}

func DerefString(s *string) string {
if s != nil {
return *s
}

return ""
}


func (c *Cliente) limpaCaracteresEspeciais() {
buf := bytes.NewBufferString("")
for _, r := range c.Cpf {
for _, r := range DerefString(c.Cpf) {
if unicode.IsDigit(r) {
buf.WriteRune(r)
}
}

c.Cpf = buf.String()
varAux := string(buf.String())
c.Cpf = &varAux
}
26 changes: 26 additions & 0 deletions internal/core/usecase/lgpd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package usecase

import (
"context"
"fiap-tech-challenge-api/internal/adapters/repository"
"fiap-tech-challenge-api/internal/core/domain"
)

type LGPD interface {
Anonimizar(ctx context.Context, cliente *domain.Cliente) error
}

type anonimizarClienteUC struct {
clienteRepo repository.ClienteRepo
}

func NewLGPD(clienteRepo repository.ClienteRepo) LGPD {
return &anonimizarClienteUC{
clienteRepo: clienteRepo,
}
}


func (uc *anonimizarClienteUC) Anonimizar(ctx context.Context, cliente *domain.Cliente) error {
return uc.clienteRepo.Anonimizar(ctx, cliente)
}
1 change: 1 addition & 0 deletions wire.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ func InitializeWebServer() (*http.Server, error) {
repository.NewProdutoRepo,
auth.NewJwtToken,
usecase.NewCadastraCliente,
usecase.NewLGPD,
usecase.NewPesquisarCliente,
usecase.NewCadastraProduto,
usecase.NewPegaProdutoPorCategoria,
Expand Down
3 changes: 2 additions & 1 deletion wire_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 8394d7a

Please sign in to comment.