🚀 Auto-deploy: GPI atualizado em 03/04/2026 20:42:16

This commit is contained in:
2026-04-03 20:42:16 +00:00
parent e8ecac05d8
commit b4eee298a8

View File

@@ -9,10 +9,34 @@ interface AuthRequest extends Request {
export const getStockItems = async (req: AuthRequest, res: Response) => {
try {
const { data, error } = await supabase.from('stock_items').select('*');
if (error && error.code !== '42P01') throw error;
res.json(toCamelCase(data || []));
const { data: items, error: itemsError } = await supabase.from('stock_items').select('*');
if (itemsError && itemsError.code !== '42P01') throw itemsError;
if (!items || items.length === 0) return res.json([]);
// Get unique data sheet IDs
const dsIds = [...new Set(items.map(i => i.data_sheet_id).filter(Boolean))];
let dataSheets: any[] = [];
if (dsIds.length > 0) {
const { data: sheets, error: dsError } = await supabase
.from('technical_data_sheets')
.select('*')
.in('id', dsIds);
if (!dsError) dataSheets = sheets || [];
}
// Map data sheets to a lookup object
const dsMap = Object.fromEntries(dataSheets.map(ds => [ds.id, ds]));
// Merge and convert to camelCase
const enrichedItems = items.map(item => ({
...item,
data_sheet_id: dsMap[item.data_sheet_id] || item.data_sheet_id
}));
res.json(toCamelCase(enrichedItems));
} catch (error: unknown) {
console.error('Error fetching stock items:', error);
res.json([]);
}
};