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:
parent
3b7bd6a181
commit
2fd1e2ece7
7 changed files with 24 additions and 8 deletions
|
|
@ -2,6 +2,7 @@ package main
|
|||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
"photum-backend/docs"
|
||||
|
|
@ -58,6 +59,7 @@ import (
|
|||
// @in header
|
||||
// @name Authorization
|
||||
func main() {
|
||||
fmt.Println("=== PHOTUM BACKEND VERSION: 2.1 (UPDATED_AT SORT) ===")
|
||||
cfg := config.LoadConfig()
|
||||
log.Printf("Loaded DSN: %s", cfg.DBDsn)
|
||||
|
||||
|
|
|
|||
|
|
@ -46,6 +46,9 @@ func AuthMiddleware(cfg *config.Config) gin.HandlerFunc {
|
|||
|
||||
// Add Vary header to correctly handle browser caching based on region
|
||||
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
|
||||
requestedRegion := c.GetHeader("x-regiao")
|
||||
|
|
|
|||
|
|
@ -1,7 +1,6 @@
|
|||
package cadastro_fot
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"photum-backend/internal/db/generated"
|
||||
|
|
@ -115,6 +114,12 @@ func (h *Handler) Create(c *gin.Context) {
|
|||
}
|
||||
|
||||
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)
|
||||
if err != nil {
|
||||
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) {
|
||||
empresaID := c.Query("empresa_id")
|
||||
regiao := c.GetString("regiao")
|
||||
if regiao == "" {
|
||||
regiao = c.GetHeader("x-regiao")
|
||||
}
|
||||
if regiao == "" {
|
||||
regiao = "SP"
|
||||
}
|
||||
|
||||
var response []CadastroFotResponse
|
||||
fmt.Printf("[DEBUG] List FOT - Regiao: %s, EmpresaID: %s\n", regiao, empresaID)
|
||||
|
||||
if empresaID != "" {
|
||||
rows, err := h.service.ListByEmpresa(c.Request.Context(), empresaID, regiao)
|
||||
|
|
|
|||
|
|
@ -49,6 +49,7 @@ func (s *Service) Create(ctx context.Context, input CreateInput, regiao string)
|
|||
Observacoes: pgtype.Text{String: input.Observacoes, Valid: true},
|
||||
GastosCaptacao: toPgNumeric(input.GastosCaptacao),
|
||||
PreVenda: pgtype.Bool{Bool: input.PreVenda, Valid: true},
|
||||
Regiao: pgtype.Text{String: regiao, Valid: true},
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
|
|
|||
|
|
@ -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 anos_formaturas a ON c.ano_formatura_id = a.id
|
||||
WHERE c.regiao = $1
|
||||
ORDER BY c.fot DESC
|
||||
ORDER BY c.updated_at DESC
|
||||
`
|
||||
|
||||
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 anos_formaturas a ON c.ano_formatura_id = a.id
|
||||
WHERE c.empresa_id = $1 AND c.regiao = $2
|
||||
ORDER BY c.fot DESC
|
||||
ORDER BY c.updated_at DESC
|
||||
`
|
||||
|
||||
type ListCadastroFotByEmpresaParams struct {
|
||||
|
|
|
|||
|
|
@ -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 anos_formaturas a ON c.ano_formatura_id = a.id
|
||||
WHERE c.regiao = @regiao
|
||||
ORDER BY c.fot DESC;
|
||||
ORDER BY c.updated_at DESC;
|
||||
|
||||
-- name: ListCadastroFotByEmpresa :many
|
||||
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 anos_formaturas a ON c.ano_formatura_id = a.id
|
||||
WHERE c.empresa_id = $1 AND c.regiao = @regiao
|
||||
ORDER BY c.fot DESC;
|
||||
ORDER BY c.updated_at DESC;
|
||||
|
||||
-- name: GetCadastroFotByID :one
|
||||
SELECT
|
||||
|
|
|
|||
|
|
@ -329,9 +329,9 @@ export async function getAvailableCourses(): Promise<ApiResponse<Array<{ id: str
|
|||
*/
|
||||
export async function getCadastroFot(token: string, empresaId?: string): Promise<ApiResponse<any[]>> {
|
||||
try {
|
||||
let url = `${API_BASE_URL}/api/cadastro-fot`;
|
||||
let url = `${API_BASE_URL}/api/cadastro-fot?_t=${new Date().getTime()}`;
|
||||
if (empresaId) {
|
||||
url += `?empresa_id=${empresaId}`;
|
||||
url += `&empresa_id=${empresaId}`;
|
||||
}
|
||||
|
||||
const response = await fetch(url, {
|
||||
|
|
|
|||
Loading…
Reference in a new issue