diff --git a/src/server/index.ts b/src/server/index.ts index 0845680..4f6eda9 100644 --- a/src/server/index.ts +++ b/src/server/index.ts @@ -1227,26 +1227,54 @@ app.get("/api/atendimentos/:id", async (req, res) => { }); // Rotas de Agendamento -app.get("/api/agendamentos", async (req, res) => { +app.get("/api/atendimentos/agendamentos", async (req, res) => { try { const { data } = req.query; - const where = data - ? { - data: { - gte: new Date(data as string), - lt: new Date( - new Date(data as string).setDate( - new Date(data as string).getDate() + 1, - ), - ), - }, + console.log("Requisição de agendamentos recebida, data:", data); + + let where = {}; + + if (data) { + try { + // Criar uma data a partir do parâmetro recebido + const parsedDate = new Date(data as string); + + if (!isNaN(parsedDate.getTime())) { + // Se a data for válida, criar o filtro para a semana + const weekStart = startOfWeek(parsedDate, { locale: ptBR }); + const weekEnd = endOfWeek(parsedDate, { locale: ptBR }); + + console.log("Período de busca:", { + weekStart: weekStart.toISOString(), + weekEnd: weekEnd.toISOString() + }); + + where = { + data: { + gte: weekStart, + lte: weekEnd, + }, + }; + } else { + console.log("Data inválida fornecida, buscando todos os agendamentos"); } - : {}; + } catch (error) { + console.error("Erro ao processar data:", error); + // Continuar sem filtro em caso de erro + } + } + console.log("Filtro final:", where); + const agendamentos = await prisma.agendamentoAtendimento.findMany({ where, include: { - cliente: true, + cliente: { + select: { + id: true, + nome: true, + }, + }, usuario: { select: { id: true, @@ -1265,6 +1293,8 @@ app.get("/api/agendamentos", async (req, res) => { data: "asc", }, }); + + console.log(`Encontrados ${agendamentos.length} agendamentos`); res.json(agendamentos); } catch (error) { console.error("Erro ao buscar agendamentos:", error);