🚀 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 });
|
||||
res.status(201).json(toCamelCase(project));
|
||||
} catch (error: unknown) {
|
||||
console.error('Error in createProject controller:', error);
|
||||
const message = error instanceof Error ? error.message : 'Unknown error';
|
||||
res.status(500).json({ error: message });
|
||||
}
|
||||
|
||||
@@ -2,33 +2,92 @@ import { supabase } from '../config/supabase.js';
|
||||
|
||||
interface ProjectData {
|
||||
name: string;
|
||||
client: string;
|
||||
client?: string;
|
||||
clientName?: string;
|
||||
startDate?: string | Date;
|
||||
endDate?: string | Date;
|
||||
technician?: string;
|
||||
responsibleEngineer?: string;
|
||||
environment?: string;
|
||||
rigorLevel?: string;
|
||||
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 }) => {
|
||||
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
|
||||
.from('projects')
|
||||
.insert({
|
||||
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'
|
||||
})
|
||||
.insert(insertPayload)
|
||||
.select()
|
||||
.single();
|
||||
|
||||
if (error) throw error;
|
||||
return project;
|
||||
if (error) {
|
||||
console.error('Supabase project insert error:', error);
|
||||
throw error;
|
||||
}
|
||||
return formatProjectResponse(project);
|
||||
};
|
||||
|
||||
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;
|
||||
|
||||
if (error) {
|
||||
console.log('Error fetching projects:', error);
|
||||
console.error('Error fetching projects:', error);
|
||||
return [];
|
||||
}
|
||||
|
||||
return projects || [];
|
||||
return formatProjectResponse(projects || []);
|
||||
} catch (error) {
|
||||
console.error('Error getting projects:', error);
|
||||
return [];
|
||||
@@ -62,7 +121,7 @@ export const getDashboardProjects = async (organizationId?: string) => {
|
||||
.select('*, painting_schemes(*)');
|
||||
|
||||
if (error) return [];
|
||||
return projects || [];
|
||||
return formatProjectResponse(projects || []);
|
||||
} catch (error) {
|
||||
console.error('Error getting dashboard projects:', error);
|
||||
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 (!isGlobalAdmin && project.organization_id !== organizationId) {
|
||||
if (!isGlobalAdmin && project.organization_id && organizationId && project.organization_id !== organizationId) {
|
||||
throw new Error('Acesso negado');
|
||||
}
|
||||
|
||||
@@ -90,7 +149,7 @@ export const archiveProject = async (id: string, organizationId?: string, isGlob
|
||||
.single();
|
||||
|
||||
if (error) throw error;
|
||||
return updated;
|
||||
return formatProjectResponse(updated);
|
||||
};
|
||||
|
||||
export const getProjectById = async (id: string) => {
|
||||
@@ -101,24 +160,32 @@ export const getProjectById = async (id: string) => {
|
||||
.single();
|
||||
|
||||
if (error) throw error;
|
||||
return data;
|
||||
return formatProjectResponse(data);
|
||||
};
|
||||
|
||||
export const updateProject = async (id: string, data: Partial<ProjectData> & { organizationId?: string }) => {
|
||||
const updateData: any = { ...data };
|
||||
delete updateData.organizationId;
|
||||
|
||||
const updateData: any = {};
|
||||
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) {
|
||||
updateData.start_date = data.startDate ? new Date(data.startDate).toISOString() : null;
|
||||
delete updateData.startDate;
|
||||
}
|
||||
if (data.endDate !== undefined) {
|
||||
updateData.end_date = data.endDate ? new Date(data.endDate).toISOString() : null;
|
||||
delete updateData.endDate;
|
||||
}
|
||||
if (data.weightKg !== undefined) {
|
||||
updateData.weight_kg = data.weightKg;
|
||||
delete updateData.weightKg;
|
||||
if (data.weightKg !== undefined || data.weightTons !== undefined) {
|
||||
updateData.weight_tons = data.weightTons !== undefined ? data.weightTons : (data.weightKg ? data.weightKg / 1000 : null);
|
||||
}
|
||||
if (data.status !== undefined) {
|
||||
updateData.status = data.status;
|
||||
}
|
||||
|
||||
const { data: updated, error } = await supabase
|
||||
@@ -129,7 +196,7 @@ export const updateProject = async (id: string, data: Partial<ProjectData> & { o
|
||||
.single();
|
||||
|
||||
if (error) throw error;
|
||||
return updated;
|
||||
return formatProjectResponse(updated);
|
||||
};
|
||||
|
||||
export const deleteProject = async (id: string) => {
|
||||
@@ -157,7 +224,7 @@ export const getProjectStats = async (projectId: string) => {
|
||||
]);
|
||||
|
||||
return {
|
||||
project,
|
||||
project: formatProjectResponse(project),
|
||||
schemes: schemesRes.data || [],
|
||||
inspections: inspectionsRes.data || [],
|
||||
parts: partsRes.data || []
|
||||
|
||||
Reference in New Issue
Block a user