corrigido endpoints

This commit is contained in:
2026-04-02 13:33:30 +00:00
parent 44aac9ac2d
commit 9860583e81
7 changed files with 289 additions and 239 deletions

View File

@@ -9,10 +9,8 @@ interface AuthRequest extends Request {
export const createProject = async (req: AuthRequest, res: Response) => {
try {
console.log('Backend creating project. Body:', req.body);
const organizationId = req.appUser?.organizationId;
const project = await projectService.createProject({ ...req.body, organizationId });
console.log('Project created successfully:', project._id);
res.status(201).json(project);
} catch (error: unknown) {
const message = error instanceof Error ? error.message : 'Unknown error';
@@ -25,10 +23,14 @@ export const getAllProjects = async (req: AuthRequest, res: Response) => {
const organizationId = req.appUser?.organizationId;
const isGlobalAdmin = req.appUser?.email === 'admtracksteel@gmail.com';
const { status } = req.query;
console.log('getAllProjects controller:', { organizationId, isGlobalAdmin, status });
const projects = await projectService.getAllProjects(organizationId, isGlobalAdmin, status as string);
console.log('getAllProjects result:', projects?.length);
res.json(projects);
} catch (error: unknown) {
const message = error instanceof Error ? error.message : 'Unknown error';
console.error('Error in getAllProjects controller:', error);
const message = error instanceof Error ? error.message : JSON.stringify(error);
console.log('Sending error response:', message);
res.status(500).json({ error: message });
}
};
@@ -58,9 +60,7 @@ export const getDashboardProjects = async (req: AuthRequest, res: Response) => {
export const getProjectById = async (req: AuthRequest, res: Response) => {
try {
const organizationId = req.appUser?.organizationId;
const isGlobalAdmin = req.appUser?.email === 'admtracksteel@gmail.com';
const project = await projectService.getProjectById(req.params.id as string, organizationId, isGlobalAdmin);
const project = await projectService.getProjectById(req.params.id as string);
res.json(project);
} catch (error: unknown) {
const message = error instanceof Error ? error.message : 'Unknown error';
@@ -71,19 +71,15 @@ export const getProjectById = async (req: AuthRequest, res: Response) => {
export const updateProject = async (req: AuthRequest, res: Response) => {
try {
const organizationId = req.appUser?.organizationId;
const isGlobalAdmin = req.appUser?.email === 'admtracksteel@gmail.com';
const project = await projectService.updateProject(req.params.id as string, req.body, organizationId, isGlobalAdmin);
const project = await projectService.updateProject(req.params.id as string, req.body);
// Notificação se Peso mudar (Exemplo simplificado, idealmente compararíamos com valor anterior)
// Como o update retorna o objeto atualizado, podemos assumir que se o body tem weightKg, houve intenção de mudar.
// Para ser mais preciso, deveríamos buscar o antigo antes, mas para MVP vamos notificar se houver o campo no body.
if (req.body.weightKg !== undefined && organizationId) {
if (req.body.weightKg !== undefined && organizationId && project) {
await notificationService.create({
organizationId,
title: 'Atualização de Obra',
message: `O peso da obra "${project.name}" foi atualizado para ${project.weightKg}kg.`,
message: `O peso da obra "${project.name}" foi atualizado para ${project.weight_kg}kg.`,
type: 'info',
metadata: { projectId: project._id, triggerType: 'project_update' }
metadata: { projectId: project.id, triggerType: 'project_update' }
});
}
@@ -96,12 +92,10 @@ export const updateProject = async (req: AuthRequest, res: Response) => {
export const deleteProject = async (req: AuthRequest, res: Response) => {
try {
const organizationId = req.appUser?.organizationId;
const isGlobalAdmin = req.appUser?.email === 'admtracksteel@gmail.com';
await projectService.deleteProject(req.params.id as string, organizationId, isGlobalAdmin);
await projectService.deleteProject(req.params.id as string);
res.status(204).send();
} catch (error: unknown) {
const message = error instanceof Error ? error.message : 'Unknown error';
res.status(500).json({ error: message });
}
};
};