Lint errors
Signed-off-by: Rodrigo Martins <rodrigo.sousa.577@gmail.com>
This commit is contained in:
1 parent
1e27604668
commit
ff59d8f090
1 file changed
+22
-64
@@ -28,27 +28,10 @@ router.get("/agendamentos", async (req, res) => {
|
|||||||
|
|
||||||
if (data) {
|
if (data) {
|
||||||
try {
|
try {
|
||||||
// Validar se o formato da data é válido
|
// Simplificar a validação - aceitar qualquer formato de data válido
|
||||||
if (typeof data !== 'string') {
|
const parsedDate = new Date(data as string);
|
||||||
return res.status(400).json({
|
|
||||||
error: "Formato de data inválido",
|
|
||||||
details: "O parâmetro 'data' deve ser uma string."
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
// Verificar se a data está no formato YYYY-MM-DD
|
|
||||||
const dateRegex = /^\d{4}-\d{2}-\d{2}$/;
|
|
||||||
if (!dateRegex.test(data)) {
|
|
||||||
return res.status(400).json({
|
|
||||||
error: "Formato de data inválido",
|
|
||||||
details: "A data deve estar no formato YYYY-MM-DD."
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
// Criar objeto de data com a data completa (início do dia)
|
|
||||||
const parsedDate = new Date(`${data}T00:00:00.000Z`);
|
|
||||||
|
|
||||||
// Verificar se a data é válida após a conversão
|
// Verificar se a data é válida
|
||||||
if (isNaN(parsedDate.getTime())) {
|
if (isNaN(parsedDate.getTime())) {
|
||||||
return res.status(400).json({
|
return res.status(400).json({
|
||||||
error: "Data inválida",
|
error: "Data inválida",
|
||||||
@@ -74,16 +57,15 @@ router.get("/agendamentos", async (req, res) => {
|
|||||||
};
|
};
|
||||||
} catch (parseError) {
|
} catch (parseError) {
|
||||||
console.error("Erro ao analisar data:", parseError);
|
console.error("Erro ao analisar data:", parseError);
|
||||||
return res.status(400).json({
|
// Continuar sem filtro de data em caso de erro, em vez de falhar
|
||||||
error: "Erro ao processar data",
|
console.log("Continuando sem filtro de data devido ao erro");
|
||||||
details: "O formato da data fornecida é inválido."
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log("Filtro de data:", JSON.stringify(dateFilter));
|
console.log("Filtro de data:", JSON.stringify(dateFilter));
|
||||||
|
|
||||||
const agendamentos = await prisma.agendamento.findMany({
|
// Usando o modelo AgendamentoAtendimento, que corresponde à tabela agendamentos_atendimento
|
||||||
|
const agendamentos = await prisma.agendamentoAtendimento.findMany({
|
||||||
where: dateFilter,
|
where: dateFilter,
|
||||||
include: {
|
include: {
|
||||||
cliente: {
|
cliente: {
|
||||||
@@ -100,31 +82,6 @@ router.get("/agendamentos", async (req, res) => {
|
|||||||
|
|
||||||
console.log(`Encontrados ${agendamentos.length} agendamentos`);
|
console.log(`Encontrados ${agendamentos.length} agendamentos`);
|
||||||
|
|
||||||
// Log para depuração
|
|
||||||
if (agendamentos.length > 0) {
|
|
||||||
console.log("Primeiro agendamento:", {
|
|
||||||
id: agendamentos[0].id,
|
|
||||||
data: agendamentos[0].data,
|
|
||||||
clienteNome: agendamentos[0].cliente?.nome
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
// Se não encontrou nenhum, vamos buscar todos os agendamentos sem filtro para depuração
|
|
||||||
const todosAgendamentos = await prisma.agendamento.count();
|
|
||||||
console.log(`Existem ${todosAgendamentos} agendamentos no total no banco de dados`);
|
|
||||||
|
|
||||||
if (todosAgendamentos > 0) {
|
|
||||||
const amostra = await prisma.agendamento.findMany({
|
|
||||||
take: 3,
|
|
||||||
include: { cliente: true }
|
|
||||||
});
|
|
||||||
console.log("Amostra de agendamentos no banco:", amostra.map(a => ({
|
|
||||||
id: a.id,
|
|
||||||
data: a.data,
|
|
||||||
clienteNome: a.cliente?.nome
|
|
||||||
})));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
res.json(agendamentos);
|
res.json(agendamentos);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Erro ao listar agendamentos:", error);
|
console.error("Erro ao listar agendamentos:", error);
|
||||||
@@ -151,14 +108,15 @@ router.post("/agendamentos", async (req, res) => {
|
|||||||
return res.status(404).json({ error: "Cliente não encontrado" });
|
return res.status(404).json({ error: "Cliente não encontrado" });
|
||||||
}
|
}
|
||||||
|
|
||||||
// Criar o agendamento
|
// Criar o agendamento - usando agendamentoAtendimento
|
||||||
const agendamento = await prisma.agendamento.create({
|
const agendamento = await prisma.agendamentoAtendimento.create({
|
||||||
data: {
|
data: {
|
||||||
clienteId,
|
clienteId,
|
||||||
data: new Date(data),
|
data: new Date(data),
|
||||||
observacoes,
|
observacoes,
|
||||||
tipo,
|
tipo,
|
||||||
status: "AGENDADO"
|
status: "AGENDADO",
|
||||||
|
usuarioId: req.user?.id // Adicionando usuarioId do middleware auth
|
||||||
},
|
},
|
||||||
include: {
|
include: {
|
||||||
cliente: {
|
cliente: {
|
||||||
@@ -179,7 +137,7 @@ router.post("/agendamentos", async (req, res) => {
|
|||||||
res.status(201).json(agendamento);
|
res.status(201).json(agendamento);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Erro ao criar agendamento:", error);
|
console.error("Erro ao criar agendamento:", error);
|
||||||
res.status(500).json({ error: "Erro ao criar agendamento" });
|
res.status(500).json({ error: "Erro ao criar agendamento", details: error.message });
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -200,8 +158,8 @@ router.put("/agendamentos/:id", async (req, res) => {
|
|||||||
|
|
||||||
console.log("Atualizando agendamento:", { id, clienteId, data, observacoes, tipo });
|
console.log("Atualizando agendamento:", { id, clienteId, data, observacoes, tipo });
|
||||||
|
|
||||||
// Verificar se o agendamento existe
|
// Verificar se o agendamento existe usando a tabela correta
|
||||||
const existingAgendamento = await prisma.agendamento.findUnique({
|
const existingAgendamento = await prisma.agendamentoAtendimento.findUnique({
|
||||||
where: { id }
|
where: { id }
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -209,8 +167,8 @@ router.put("/agendamentos/:id", async (req, res) => {
|
|||||||
return res.status(404).json({ error: "Agendamento não encontrado" });
|
return res.status(404).json({ error: "Agendamento não encontrado" });
|
||||||
}
|
}
|
||||||
|
|
||||||
// Atualizar o agendamento
|
// Atualizar o agendamento usando a tabela correta
|
||||||
const agendamento = await prisma.agendamento.update({
|
const agendamento = await prisma.agendamentoAtendimento.update({
|
||||||
where: { id },
|
where: { id },
|
||||||
data: {
|
data: {
|
||||||
clienteId,
|
clienteId,
|
||||||
@@ -237,7 +195,7 @@ router.put("/agendamentos/:id", async (req, res) => {
|
|||||||
res.json(agendamento);
|
res.json(agendamento);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Erro ao atualizar agendamento:", error);
|
console.error("Erro ao atualizar agendamento:", error);
|
||||||
res.status(500).json({ error: "Erro ao atualizar agendamento" });
|
res.status(500).json({ error: "Erro ao atualizar agendamento", details: error.message });
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -256,8 +214,8 @@ router.delete("/agendamentos/:id", async (req, res) => {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Verificar se o agendamento existe
|
// Verificar se o agendamento existe usando a tabela correta
|
||||||
const agendamento = await prisma.agendamento.findUnique({
|
const agendamento = await prisma.agendamentoAtendimento.findUnique({
|
||||||
where: { id },
|
where: { id },
|
||||||
include: { cliente: true }
|
include: { cliente: true }
|
||||||
});
|
});
|
||||||
@@ -273,8 +231,8 @@ router.delete("/agendamentos/:id", async (req, res) => {
|
|||||||
clienteNome: agendamento.cliente?.nome
|
clienteNome: agendamento.cliente?.nome
|
||||||
});
|
});
|
||||||
|
|
||||||
// Excluir o agendamento
|
// Excluir o agendamento usando a tabela correta
|
||||||
await prisma.agendamento.delete({
|
await prisma.agendamentoAtendimento.delete({
|
||||||
where: { id }
|
where: { id }
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -282,7 +240,7 @@ router.delete("/agendamentos/:id", async (req, res) => {
|
|||||||
res.status(200).json({ message: "Agendamento excluído com sucesso" });
|
res.status(200).json({ message: "Agendamento excluído com sucesso" });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Erro ao excluir agendamento:", error);
|
console.error("Erro ao excluir agendamento:", error);
|
||||||
res.status(500).json({ error: "Erro ao excluir agendamento" });
|
res.status(500).json({ error: "Erro ao excluir agendamento", details: error.message });
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user