fix(auth): sobrescrever região do cliente pela região da franquia
Corrige o erro onde clientes de empresas MG ficavam vinculados ao estado de origem física (ex: SP) durante o registro via código. Agora, a região correta é obtida via GetEmpresaByIDGlobal e aplicada antes da inicialização das restrições de identidade.
This commit is contained in:
parent
dedff12559
commit
11fa5d6489
3 changed files with 32 additions and 0 deletions
|
|
@ -99,6 +99,19 @@ func (s *Service) Register(ctx context.Context, email, senha, role, nome, telefo
|
||||||
parsedEmpID, err := uuid.Parse(*empresaID)
|
parsedEmpID, err := uuid.Parse(*empresaID)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
empID = pgtype.UUID{Bytes: parsedEmpID, Valid: true}
|
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},
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -67,6 +67,22 @@ func (q *Queries) GetEmpresaByID(ctx context.Context, arg GetEmpresaByIDParams)
|
||||||
return i, err
|
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
|
const getEmpresaByNome = `-- name: GetEmpresaByNome :one
|
||||||
SELECT id, nome, criado_em, regiao FROM empresas WHERE nome = $1 AND regiao = $2
|
SELECT id, nome, criado_em, regiao FROM empresas WHERE nome = $1 AND regiao = $2
|
||||||
`
|
`
|
||||||
|
|
|
||||||
|
|
@ -7,6 +7,9 @@ SELECT * FROM empresas WHERE regiao = @regiao ORDER BY nome;
|
||||||
-- name: GetEmpresaByID :one
|
-- name: GetEmpresaByID :one
|
||||||
SELECT * FROM empresas WHERE id = $1 AND regiao = @regiao;
|
SELECT * FROM empresas WHERE id = $1 AND regiao = @regiao;
|
||||||
|
|
||||||
|
-- name: GetEmpresaByIDGlobal :one
|
||||||
|
SELECT * FROM empresas WHERE id = $1;
|
||||||
|
|
||||||
-- name: UpdateEmpresa :one
|
-- name: UpdateEmpresa :one
|
||||||
UPDATE empresas SET nome = $2 WHERE id = $1 AND regiao = @regiao RETURNING *;
|
UPDATE empresas SET nome = $2 WHERE id = $1 AND regiao = @regiao RETURNING *;
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue