Merge pull request #53 from rede5/cadastro-cliente-mg-indo-como-sp-11346545121

fix(auth): sobrescrever região do cliente pela região da franquia
This commit is contained in:
Andre F. Rodrigues 2026-02-23 19:18:58 -03:00 committed by GitHub
commit a516308017
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 32 additions and 0 deletions

View file

@ -99,6 +99,19 @@ func (s *Service) Register(ctx context.Context, email, senha, role, nome, telefo
parsedEmpID, err := uuid.Parse(*empresaID)
if err == nil {
empID = pgtype.UUID{Bytes: parsedEmpID, Valid: true}
// Override region with Company region to ensure client is bound to the correct franchise
empresa, errEmp := s.queries.GetEmpresaByIDGlobal(ctx, empID)
if errEmp == nil && empresa.Regiao.Valid && empresa.Regiao.String != "" {
fmt.Printf("[DEBUG] Overriding client region from '%s' to company region '%s'\n", regiao, empresa.Regiao.String)
regiao = empresa.Regiao.String
// We also need to update the User regions binding in the DB since it might have been set to physical address state above
_ = s.queries.UpdateUsuarioRegions(ctx, generated.UpdateUsuarioRegionsParams{
ID: pgtype.UUID{Bytes: user.ID.Bytes, Valid: true},
RegioesPermitidas: []string{regiao},
})
}
}
}

View file

@ -67,6 +67,22 @@ func (q *Queries) GetEmpresaByID(ctx context.Context, arg GetEmpresaByIDParams)
return i, err
}
const getEmpresaByIDGlobal = `-- name: GetEmpresaByIDGlobal :one
SELECT id, nome, criado_em, regiao FROM empresas WHERE id = $1
`
func (q *Queries) GetEmpresaByIDGlobal(ctx context.Context, id pgtype.UUID) (Empresa, error) {
row := q.db.QueryRow(ctx, getEmpresaByIDGlobal, id)
var i Empresa
err := row.Scan(
&i.ID,
&i.Nome,
&i.CriadoEm,
&i.Regiao,
)
return i, err
}
const getEmpresaByNome = `-- name: GetEmpresaByNome :one
SELECT id, nome, criado_em, regiao FROM empresas WHERE nome = $1 AND regiao = $2
`

View file

@ -7,6 +7,9 @@ SELECT * FROM empresas WHERE regiao = @regiao ORDER BY nome;
-- name: GetEmpresaByID :one
SELECT * FROM empresas WHERE id = $1 AND regiao = @regiao;
-- name: GetEmpresaByIDGlobal :one
SELECT * FROM empresas WHERE id = $1;
-- name: UpdateEmpresa :one
UPDATE empresas SET nome = $2 WHERE id = $1 AND regiao = @regiao RETURNING *;