🚀 Auto-deploy: GPI atualizado em 27/08/2026 21:31:28
This commit is contained in:
@@ -14,6 +14,7 @@ export const createProject = async (req: AuthRequest, res: Response) => {
|
|||||||
const project = await projectService.createProject({ ...req.body, organizationId });
|
const project = await projectService.createProject({ ...req.body, organizationId });
|
||||||
res.status(201).json(toCamelCase(project));
|
res.status(201).json(toCamelCase(project));
|
||||||
} catch (error: unknown) {
|
} catch (error: unknown) {
|
||||||
|
console.error('Error in createProject controller:', error);
|
||||||
const message = error instanceof Error ? error.message : 'Unknown error';
|
const message = error instanceof Error ? error.message : 'Unknown error';
|
||||||
res.status(500).json({ error: message });
|
res.status(500).json({ error: message });
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,33 +2,92 @@ import { supabase } from '../config/supabase.js';
|
|||||||
|
|
||||||
interface ProjectData {
|
interface ProjectData {
|
||||||
name: string;
|
name: string;
|
||||||
client: string;
|
client?: string;
|
||||||
|
clientName?: string;
|
||||||
startDate?: string | Date;
|
startDate?: string | Date;
|
||||||
endDate?: string | Date;
|
endDate?: string | Date;
|
||||||
technician?: string;
|
technician?: string;
|
||||||
|
responsibleEngineer?: string;
|
||||||
environment?: string;
|
environment?: string;
|
||||||
|
rigorLevel?: string;
|
||||||
weightKg?: number;
|
weightKg?: number;
|
||||||
|
weightTons?: number;
|
||||||
|
status?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const formatProjectResponse = (project: any): any => {
|
||||||
|
if (!project) return project;
|
||||||
|
if (Array.isArray(project)) {
|
||||||
|
return project.map(p => formatProjectResponse(p));
|
||||||
|
}
|
||||||
|
const clientVal = project.client_name || project.clientName || project.client || '';
|
||||||
|
const technicianVal = project.responsible_engineer || project.responsibleEngineer || project.technician || '';
|
||||||
|
const environmentVal = project.rigor_level || project.rigorLevel || project.environment || '';
|
||||||
|
|
||||||
|
let weightKgVal: number | null = null;
|
||||||
|
if (project.weight_kg !== undefined && project.weight_kg !== null) {
|
||||||
|
weightKgVal = Number(project.weight_kg);
|
||||||
|
} else if (project.weightKg !== undefined && project.weightKg !== null) {
|
||||||
|
weightKgVal = Number(project.weightKg);
|
||||||
|
} else if (project.weight_tons !== undefined && project.weight_tons !== null) {
|
||||||
|
weightKgVal = Number(project.weight_tons) * 1000;
|
||||||
|
} else if (project.weightTons !== undefined && project.weightTons !== null) {
|
||||||
|
weightKgVal = Number(project.weightTons) * 1000;
|
||||||
|
}
|
||||||
|
|
||||||
|
let weightTonsVal: number | null = null;
|
||||||
|
if (weightKgVal !== null) {
|
||||||
|
weightTonsVal = weightKgVal / 1000;
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
...project,
|
||||||
|
client: clientVal,
|
||||||
|
client_name: clientVal,
|
||||||
|
clientName: clientVal,
|
||||||
|
technician: technicianVal,
|
||||||
|
responsible_engineer: technicianVal,
|
||||||
|
responsibleEngineer: technicianVal,
|
||||||
|
environment: environmentVal,
|
||||||
|
rigor_level: environmentVal,
|
||||||
|
rigorLevel: environmentVal,
|
||||||
|
weight_kg: weightKgVal,
|
||||||
|
weightKg: weightKgVal,
|
||||||
|
weight_tons: weightTonsVal,
|
||||||
|
weightTons: weightTonsVal
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
export const createProject = async (data: ProjectData & { organizationId?: string }) => {
|
export const createProject = async (data: ProjectData & { organizationId?: string }) => {
|
||||||
|
const clientName = data.clientName || data.client || '';
|
||||||
|
const responsibleEngineer = data.responsibleEngineer || data.technician || '';
|
||||||
|
const rigorLevel = data.rigorLevel || data.environment || '';
|
||||||
|
const weightTons = data.weightTons !== undefined ? data.weightTons : (data.weightKg ? data.weightKg / 1000 : null);
|
||||||
|
const orgId = data.organizationId || 'e47e6210-4879-4e5b-bf21-9285d2713123';
|
||||||
|
|
||||||
|
const insertPayload: any = {
|
||||||
|
name: data.name,
|
||||||
|
client_name: clientName,
|
||||||
|
responsible_engineer: responsibleEngineer,
|
||||||
|
rigor_level: rigorLevel,
|
||||||
|
organization_id: orgId,
|
||||||
|
weight_tons: weightTons,
|
||||||
|
status: data.status || 'active',
|
||||||
|
start_date: data.startDate ? new Date(data.startDate).toISOString() : null,
|
||||||
|
end_date: data.endDate ? new Date(data.endDate).toISOString() : null
|
||||||
|
};
|
||||||
|
|
||||||
const { data: project, error } = await supabase
|
const { data: project, error } = await supabase
|
||||||
.from('projects')
|
.from('projects')
|
||||||
.insert({
|
.insert(insertPayload)
|
||||||
name: data.name,
|
|
||||||
client: data.client,
|
|
||||||
start_date: data.startDate ? new Date(data.startDate).toISOString() : null,
|
|
||||||
end_date: data.endDate ? new Date(data.endDate).toISOString() : null,
|
|
||||||
technician: data.technician,
|
|
||||||
environment: data.environment,
|
|
||||||
organization_id: data.organizationId,
|
|
||||||
weight_kg: data.weightKg,
|
|
||||||
status: 'active'
|
|
||||||
})
|
|
||||||
.select()
|
.select()
|
||||||
.single();
|
.single();
|
||||||
|
|
||||||
if (error) throw error;
|
if (error) {
|
||||||
return project;
|
console.error('Supabase project insert error:', error);
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
return formatProjectResponse(project);
|
||||||
};
|
};
|
||||||
|
|
||||||
export const getAllProjects = async (organizationId?: string, isGlobalAdmin?: boolean, status?: string) => {
|
export const getAllProjects = async (organizationId?: string, isGlobalAdmin?: boolean, status?: string) => {
|
||||||
@@ -44,11 +103,11 @@ export const getAllProjects = async (organizationId?: string, isGlobalAdmin?: bo
|
|||||||
const { data: projects, error } = await query;
|
const { data: projects, error } = await query;
|
||||||
|
|
||||||
if (error) {
|
if (error) {
|
||||||
console.log('Error fetching projects:', error);
|
console.error('Error fetching projects:', error);
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
return projects || [];
|
return formatProjectResponse(projects || []);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error getting projects:', error);
|
console.error('Error getting projects:', error);
|
||||||
return [];
|
return [];
|
||||||
@@ -62,7 +121,7 @@ export const getDashboardProjects = async (organizationId?: string) => {
|
|||||||
.select('*, painting_schemes(*)');
|
.select('*, painting_schemes(*)');
|
||||||
|
|
||||||
if (error) return [];
|
if (error) return [];
|
||||||
return projects || [];
|
return formatProjectResponse(projects || []);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error getting dashboard projects:', error);
|
console.error('Error getting dashboard projects:', error);
|
||||||
return [];
|
return [];
|
||||||
@@ -78,7 +137,7 @@ export const archiveProject = async (id: string, organizationId?: string, isGlob
|
|||||||
|
|
||||||
if (fetchError || !project) throw new Error('Projeto não encontrado');
|
if (fetchError || !project) throw new Error('Projeto não encontrado');
|
||||||
|
|
||||||
if (!isGlobalAdmin && project.organization_id !== organizationId) {
|
if (!isGlobalAdmin && project.organization_id && organizationId && project.organization_id !== organizationId) {
|
||||||
throw new Error('Acesso negado');
|
throw new Error('Acesso negado');
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -90,7 +149,7 @@ export const archiveProject = async (id: string, organizationId?: string, isGlob
|
|||||||
.single();
|
.single();
|
||||||
|
|
||||||
if (error) throw error;
|
if (error) throw error;
|
||||||
return updated;
|
return formatProjectResponse(updated);
|
||||||
};
|
};
|
||||||
|
|
||||||
export const getProjectById = async (id: string) => {
|
export const getProjectById = async (id: string) => {
|
||||||
@@ -101,26 +160,34 @@ export const getProjectById = async (id: string) => {
|
|||||||
.single();
|
.single();
|
||||||
|
|
||||||
if (error) throw error;
|
if (error) throw error;
|
||||||
return data;
|
return formatProjectResponse(data);
|
||||||
};
|
};
|
||||||
|
|
||||||
export const updateProject = async (id: string, data: Partial<ProjectData> & { organizationId?: string }) => {
|
export const updateProject = async (id: string, data: Partial<ProjectData> & { organizationId?: string }) => {
|
||||||
const updateData: any = { ...data };
|
const updateData: any = {};
|
||||||
delete updateData.organizationId;
|
if (data.name !== undefined) updateData.name = data.name;
|
||||||
|
if (data.client !== undefined || data.clientName !== undefined) {
|
||||||
|
updateData.client_name = data.clientName || data.client;
|
||||||
|
}
|
||||||
|
if (data.technician !== undefined || data.responsibleEngineer !== undefined) {
|
||||||
|
updateData.responsible_engineer = data.responsibleEngineer || data.technician;
|
||||||
|
}
|
||||||
|
if (data.environment !== undefined || data.rigorLevel !== undefined) {
|
||||||
|
updateData.rigor_level = data.rigorLevel || data.environment;
|
||||||
|
}
|
||||||
if (data.startDate !== undefined) {
|
if (data.startDate !== undefined) {
|
||||||
updateData.start_date = data.startDate ? new Date(data.startDate).toISOString() : null;
|
updateData.start_date = data.startDate ? new Date(data.startDate).toISOString() : null;
|
||||||
delete updateData.startDate;
|
|
||||||
}
|
}
|
||||||
if (data.endDate !== undefined) {
|
if (data.endDate !== undefined) {
|
||||||
updateData.end_date = data.endDate ? new Date(data.endDate).toISOString() : null;
|
updateData.end_date = data.endDate ? new Date(data.endDate).toISOString() : null;
|
||||||
delete updateData.endDate;
|
|
||||||
}
|
}
|
||||||
if (data.weightKg !== undefined) {
|
if (data.weightKg !== undefined || data.weightTons !== undefined) {
|
||||||
updateData.weight_kg = data.weightKg;
|
updateData.weight_tons = data.weightTons !== undefined ? data.weightTons : (data.weightKg ? data.weightKg / 1000 : null);
|
||||||
delete updateData.weightKg;
|
|
||||||
}
|
}
|
||||||
|
if (data.status !== undefined) {
|
||||||
|
updateData.status = data.status;
|
||||||
|
}
|
||||||
|
|
||||||
const { data: updated, error } = await supabase
|
const { data: updated, error } = await supabase
|
||||||
.from('projects')
|
.from('projects')
|
||||||
.update(updateData)
|
.update(updateData)
|
||||||
@@ -129,7 +196,7 @@ export const updateProject = async (id: string, data: Partial<ProjectData> & { o
|
|||||||
.single();
|
.single();
|
||||||
|
|
||||||
if (error) throw error;
|
if (error) throw error;
|
||||||
return updated;
|
return formatProjectResponse(updated);
|
||||||
};
|
};
|
||||||
|
|
||||||
export const deleteProject = async (id: string) => {
|
export const deleteProject = async (id: string) => {
|
||||||
@@ -157,7 +224,7 @@ export const getProjectStats = async (projectId: string) => {
|
|||||||
]);
|
]);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
project,
|
project: formatProjectResponse(project),
|
||||||
schemes: schemesRes.data || [],
|
schemes: schemesRes.data || [],
|
||||||
inspections: inspectionsRes.data || [],
|
inspections: inspectionsRes.data || [],
|
||||||
parts: partsRes.data || []
|
parts: partsRes.data || []
|
||||||
|
|||||||
Reference in New Issue
Block a user