fix: schema Supabase gpi, alinhamento de colunas createProject e permissoes superadmin dev
This commit is contained in:
@@ -9,9 +9,12 @@ if (!supabaseUrl || !supabaseServiceKey) {
|
||||
throw new Error('❌ SUPABASE_URL and SUPABASE_SERVICE_ROLE_KEY must be provided in environment variables');
|
||||
}
|
||||
|
||||
export const GPI_SCHEMA = 'public';
|
||||
export const GPI_SCHEMA = 'gpi';
|
||||
|
||||
export const supabase = createClient(supabaseUrl, supabaseServiceKey, {
|
||||
db: {
|
||||
schema: GPI_SCHEMA
|
||||
},
|
||||
auth: {
|
||||
autoRefreshToken: false,
|
||||
persistSession: false
|
||||
|
||||
@@ -13,9 +13,9 @@ export const createProject = async (req: AuthRequest, res: Response) => {
|
||||
const organizationId = req.appUser?.organizationId;
|
||||
const project = await projectService.createProject({ ...req.body, organizationId });
|
||||
res.status(201).json(toCamelCase(project));
|
||||
} catch (error: unknown) {
|
||||
} catch (error: any) {
|
||||
console.error('Error in createProject controller:', error);
|
||||
const message = error instanceof Error ? error.message : 'Unknown error';
|
||||
const message = error?.message || error?.details || (typeof error === 'string' ? error : JSON.stringify(error)) || 'Erro ao criar projeto';
|
||||
res.status(500).json({ error: message });
|
||||
}
|
||||
};
|
||||
@@ -27,9 +27,9 @@ export const getAllProjects = async (req: AuthRequest, res: Response) => {
|
||||
const { status } = req.query;
|
||||
const projects = await projectService.getAllProjects(organizationId, isGlobalAdmin, status as string);
|
||||
res.json(toCamelCase(projects));
|
||||
} catch (error: unknown) {
|
||||
} catch (error: any) {
|
||||
console.error('Error in getAllProjects controller:', error);
|
||||
const message = error instanceof Error ? error.message : JSON.stringify(error);
|
||||
const message = error?.message || error?.details || (typeof error === 'string' ? error : JSON.stringify(error));
|
||||
res.status(500).json({ error: message });
|
||||
}
|
||||
};
|
||||
@@ -40,8 +40,8 @@ export const archiveProject = async (req: AuthRequest, res: Response) => {
|
||||
const isGlobalAdmin = req.appUser?.email === 'admtracksteel@gmail.com';
|
||||
const project = await projectService.archiveProject(req.params.id as string, organizationId, isGlobalAdmin);
|
||||
res.json(toCamelCase(project));
|
||||
} catch (error: unknown) {
|
||||
const message = error instanceof Error ? error.message : 'Unknown error';
|
||||
} catch (error: any) {
|
||||
const message = error?.message || error?.details || 'Erro ao arquivar projeto';
|
||||
res.status(500).json({ error: message });
|
||||
}
|
||||
};
|
||||
@@ -51,8 +51,8 @@ export const getDashboardProjects = async (req: AuthRequest, res: Response) => {
|
||||
const organizationId = req.appUser?.organizationId;
|
||||
const projects = await projectService.getDashboardProjects(organizationId);
|
||||
res.json(toCamelCase(projects));
|
||||
} catch (error: unknown) {
|
||||
const message = error instanceof Error ? error.message : 'Unknown error';
|
||||
} catch (error: any) {
|
||||
const message = error?.message || error?.details || 'Erro ao obter projetos';
|
||||
res.status(500).json({ error: message });
|
||||
}
|
||||
};
|
||||
@@ -61,8 +61,8 @@ export const getProjectById = async (req: AuthRequest, res: Response) => {
|
||||
try {
|
||||
const project = await projectService.getProjectById(req.params.id as string);
|
||||
res.json(toCamelCase(project));
|
||||
} catch (error: unknown) {
|
||||
const message = error instanceof Error ? error.message : 'Unknown error';
|
||||
} catch (error: any) {
|
||||
const message = error?.message || error?.details || 'Projeto não encontrado';
|
||||
res.status(404).json({ error: message });
|
||||
}
|
||||
};
|
||||
@@ -83,8 +83,8 @@ export const updateProject = async (req: AuthRequest, res: Response) => {
|
||||
}
|
||||
|
||||
res.json(toCamelCase(project));
|
||||
} catch (error: unknown) {
|
||||
const message = error instanceof Error ? error.message : 'Unknown error';
|
||||
} catch (error: any) {
|
||||
const message = error?.message || error?.details || 'Erro ao atualizar projeto';
|
||||
res.status(500).json({ error: message });
|
||||
}
|
||||
};
|
||||
@@ -93,8 +93,8 @@ export const deleteProject = async (req: AuthRequest, res: Response) => {
|
||||
try {
|
||||
await projectService.deleteProject(req.params.id as string);
|
||||
res.status(204).send();
|
||||
} catch (error: unknown) {
|
||||
const message = error instanceof Error ? error.message : 'Unknown error';
|
||||
} catch (error: any) {
|
||||
const message = error?.message || error?.details || 'Erro ao excluir projeto';
|
||||
res.status(500).json({ error: message });
|
||||
}
|
||||
};
|
||||
@@ -208,7 +208,8 @@ export const login = async (req: Request, res: Response) => {
|
||||
user: {
|
||||
id: 'admin-user',
|
||||
role: 'admin',
|
||||
name: 'Administrador'
|
||||
name: 'Administrador (DEV)',
|
||||
email: 'admtracksteel@gmail.com'
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -23,7 +23,7 @@ export async function authenticateRequest(req: Request): Promise<IAppUser | null
|
||||
return {
|
||||
id: 'c0eebc99-9c0b-4ef8-bb6d-6bb9bd380a10',
|
||||
logtoId: 'admin',
|
||||
email: 'admin@gpi.app',
|
||||
email: 'admtracksteel@gmail.com',
|
||||
name: 'Administrador (Dev)',
|
||||
role: 'admin'
|
||||
};
|
||||
|
||||
@@ -59,22 +59,24 @@ export const formatProjectResponse = (project: any): any => {
|
||||
};
|
||||
|
||||
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 clientVal = data.client || data.clientName || '';
|
||||
const technicianVal = data.technician || data.responsibleEngineer || '';
|
||||
const environmentVal = data.environment || data.rigorLevel || '';
|
||||
const weightKgVal = data.weightKg !== undefined && data.weightKg !== null
|
||||
? Number(data.weightKg)
|
||||
: (data.weightTons !== undefined && data.weightTons !== null ? Number(data.weightTons) * 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,
|
||||
client: clientVal,
|
||||
technician: technicianVal,
|
||||
environment: environmentVal,
|
||||
organization_id: orgId,
|
||||
weight_tons: weightTons,
|
||||
weight_kg: weightKgVal,
|
||||
status: data.status || 'active',
|
||||
start_date: data.startDate ? new Date(data.startDate).toISOString() : null,
|
||||
end_date: data.endDate ? new Date(data.endDate).toISOString() : null
|
||||
start_date: data.startDate ? new Date(data.startDate).toISOString().split('T')[0] : null,
|
||||
end_date: data.endDate ? new Date(data.endDate).toISOString().split('T')[0] : null
|
||||
};
|
||||
|
||||
const { data: project, error } = await supabase
|
||||
@@ -167,22 +169,24 @@ export const updateProject = async (id: string, data: Partial<ProjectData> & { o
|
||||
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;
|
||||
updateData.client = data.client || data.clientName;
|
||||
}
|
||||
if (data.technician !== undefined || data.responsibleEngineer !== undefined) {
|
||||
updateData.responsible_engineer = data.responsibleEngineer || data.technician;
|
||||
updateData.technician = data.technician || data.responsibleEngineer;
|
||||
}
|
||||
if (data.environment !== undefined || data.rigorLevel !== undefined) {
|
||||
updateData.rigor_level = data.rigorLevel || data.environment;
|
||||
updateData.environment = data.environment || data.rigorLevel;
|
||||
}
|
||||
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().split('T')[0] : null;
|
||||
}
|
||||
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().split('T')[0] : null;
|
||||
}
|
||||
if (data.weightKg !== undefined || data.weightTons !== undefined) {
|
||||
updateData.weight_tons = data.weightTons !== undefined ? data.weightTons : (data.weightKg ? data.weightKg / 1000 : null);
|
||||
updateData.weight_kg = data.weightKg !== undefined && data.weightKg !== null
|
||||
? Number(data.weightKg)
|
||||
: (data.weightTons !== undefined && data.weightTons !== null ? Number(data.weightTons) * 1000 : null);
|
||||
}
|
||||
if (data.status !== undefined) {
|
||||
updateData.status = data.status;
|
||||
|
||||
Reference in New Issue
Block a user