fix(backend): corrige persistência da região na criação de FOT e ordenação

- Corrige bug onde a região não era salva no banco durante a criação de FOT (campo faltante no service).
- Adiciona fallback para garantir região "SP" caso o header x-regiao esteja vazio.
- Altera ordenação da listagem para updated_at DESC (editados aparecem no topo).
This commit is contained in:
NANDO9322 2026-02-06 23:37:59 -03:00
parent 3b7bd6a181
commit 2fd1e2ece7
7 changed files with 24 additions and 8 deletions

View file

@ -2,6 +2,7 @@ package main
import ( import (
"context" "context"
"fmt"
"log" "log"
"photum-backend/docs" "photum-backend/docs"
@ -58,6 +59,7 @@ import (
// @in header // @in header
// @name Authorization // @name Authorization
func main() { func main() {
fmt.Println("=== PHOTUM BACKEND VERSION: 2.1 (UPDATED_AT SORT) ===")
cfg := config.LoadConfig() cfg := config.LoadConfig()
log.Printf("Loaded DSN: %s", cfg.DBDsn) log.Printf("Loaded DSN: %s", cfg.DBDsn)

View file

@ -46,6 +46,9 @@ func AuthMiddleware(cfg *config.Config) gin.HandlerFunc {
// Add Vary header to correctly handle browser caching based on region // Add Vary header to correctly handle browser caching based on region
c.Header("Vary", "x-regiao") c.Header("Vary", "x-regiao")
c.Header("Cache-Control", "no-cache, no-store, must-revalidate")
c.Header("Pragma", "no-cache")
c.Header("Expires", "0")
// Region Logic // Region Logic
requestedRegion := c.GetHeader("x-regiao") requestedRegion := c.GetHeader("x-regiao")

View file

@ -1,7 +1,6 @@
package cadastro_fot package cadastro_fot
import ( import (
"fmt"
"net/http" "net/http"
"photum-backend/internal/db/generated" "photum-backend/internal/db/generated"
@ -115,6 +114,12 @@ func (h *Handler) Create(c *gin.Context) {
} }
regiao := c.GetString("regiao") regiao := c.GetString("regiao")
if regiao == "" {
regiao = c.GetHeader("x-regiao")
}
if regiao == "" {
regiao = "SP"
}
res, err := h.service.Create(c.Request.Context(), req, regiao) res, err := h.service.Create(c.Request.Context(), req, regiao)
if err != nil { if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
@ -136,9 +141,14 @@ func (h *Handler) Create(c *gin.Context) {
func (h *Handler) List(c *gin.Context) { func (h *Handler) List(c *gin.Context) {
empresaID := c.Query("empresa_id") empresaID := c.Query("empresa_id")
regiao := c.GetString("regiao") regiao := c.GetString("regiao")
if regiao == "" {
regiao = c.GetHeader("x-regiao")
}
if regiao == "" {
regiao = "SP"
}
var response []CadastroFotResponse var response []CadastroFotResponse
fmt.Printf("[DEBUG] List FOT - Regiao: %s, EmpresaID: %s\n", regiao, empresaID)
if empresaID != "" { if empresaID != "" {
rows, err := h.service.ListByEmpresa(c.Request.Context(), empresaID, regiao) rows, err := h.service.ListByEmpresa(c.Request.Context(), empresaID, regiao)

View file

@ -49,6 +49,7 @@ func (s *Service) Create(ctx context.Context, input CreateInput, regiao string)
Observacoes: pgtype.Text{String: input.Observacoes, Valid: true}, Observacoes: pgtype.Text{String: input.Observacoes, Valid: true},
GastosCaptacao: toPgNumeric(input.GastosCaptacao), GastosCaptacao: toPgNumeric(input.GastosCaptacao),
PreVenda: pgtype.Bool{Bool: input.PreVenda, Valid: true}, PreVenda: pgtype.Bool{Bool: input.PreVenda, Valid: true},
Regiao: pgtype.Text{String: regiao, Valid: true},
}) })
if err != nil { if err != nil {
return nil, err return nil, err

View file

@ -262,7 +262,7 @@ LEFT JOIN empresas e ON c.empresa_id = e.id
LEFT JOIN cursos cur ON c.curso_id = cur.id LEFT JOIN cursos cur ON c.curso_id = cur.id
LEFT JOIN anos_formaturas a ON c.ano_formatura_id = a.id LEFT JOIN anos_formaturas a ON c.ano_formatura_id = a.id
WHERE c.regiao = $1 WHERE c.regiao = $1
ORDER BY c.fot DESC ORDER BY c.updated_at DESC
` `
type ListCadastroFotRow struct { type ListCadastroFotRow struct {
@ -334,7 +334,7 @@ LEFT JOIN empresas e ON c.empresa_id = e.id
LEFT JOIN cursos cur ON c.curso_id = cur.id LEFT JOIN cursos cur ON c.curso_id = cur.id
LEFT JOIN anos_formaturas a ON c.ano_formatura_id = a.id LEFT JOIN anos_formaturas a ON c.ano_formatura_id = a.id
WHERE c.empresa_id = $1 AND c.regiao = $2 WHERE c.empresa_id = $1 AND c.regiao = $2
ORDER BY c.fot DESC ORDER BY c.updated_at DESC
` `
type ListCadastroFotByEmpresaParams struct { type ListCadastroFotByEmpresaParams struct {

View file

@ -29,7 +29,7 @@ LEFT JOIN empresas e ON c.empresa_id = e.id
LEFT JOIN cursos cur ON c.curso_id = cur.id LEFT JOIN cursos cur ON c.curso_id = cur.id
LEFT JOIN anos_formaturas a ON c.ano_formatura_id = a.id LEFT JOIN anos_formaturas a ON c.ano_formatura_id = a.id
WHERE c.regiao = @regiao WHERE c.regiao = @regiao
ORDER BY c.fot DESC; ORDER BY c.updated_at DESC;
-- name: ListCadastroFotByEmpresa :many -- name: ListCadastroFotByEmpresa :many
SELECT SELECT
@ -42,7 +42,7 @@ LEFT JOIN empresas e ON c.empresa_id = e.id
LEFT JOIN cursos cur ON c.curso_id = cur.id LEFT JOIN cursos cur ON c.curso_id = cur.id
LEFT JOIN anos_formaturas a ON c.ano_formatura_id = a.id LEFT JOIN anos_formaturas a ON c.ano_formatura_id = a.id
WHERE c.empresa_id = $1 AND c.regiao = @regiao WHERE c.empresa_id = $1 AND c.regiao = @regiao
ORDER BY c.fot DESC; ORDER BY c.updated_at DESC;
-- name: GetCadastroFotByID :one -- name: GetCadastroFotByID :one
SELECT SELECT

View file

@ -329,9 +329,9 @@ export async function getAvailableCourses(): Promise<ApiResponse<Array<{ id: str
*/ */
export async function getCadastroFot(token: string, empresaId?: string): Promise<ApiResponse<any[]>> { export async function getCadastroFot(token: string, empresaId?: string): Promise<ApiResponse<any[]>> {
try { try {
let url = `${API_BASE_URL}/api/cadastro-fot`; let url = `${API_BASE_URL}/api/cadastro-fot?_t=${new Date().getTime()}`;
if (empresaId) { if (empresaId) {
url += `?empresa_id=${empresaId}`; url += `&empresa_id=${empresaId}`;
} }
const response = await fetch(url, { const response = await fetch(url, {