Table Importing Added, UI updates
This commit is contained in:
@@ -84,6 +84,44 @@ router.patch('/:id', async (req, res) => {
|
||||
}
|
||||
});
|
||||
|
||||
// POST /api/chemicals/import
|
||||
router.post('/import', async (req, res) => {
|
||||
const rows: Partial<Record<string, unknown>>[] = req.body.rows;
|
||||
if (!Array.isArray(rows) || rows.length === 0) {
|
||||
return res.status(400).json({ error: 'No rows provided' });
|
||||
}
|
||||
if (rows.length > 1000) {
|
||||
return res.status(400).json({ error: 'Maximum 1000 rows per import' });
|
||||
}
|
||||
|
||||
const skipFields = new Set(['id', 'userId', 'createdAt', 'updatedAt', 'dateEntered', 'lastChanged']);
|
||||
let imported = 0;
|
||||
const errors: string[] = [];
|
||||
|
||||
for (let i = 0; i < rows.length; i++) {
|
||||
const b = rows[i];
|
||||
const fields = Object.keys(b).filter(k => !skipFields.has(k) && b[k] !== '' && b[k] != null);
|
||||
if (fields.length === 0) continue;
|
||||
|
||||
const snakeCols = fields.map(camelToSnake);
|
||||
const allCols = ['user_id', ...snakeCols];
|
||||
const placeholders = allCols.map((_, idx) => `$${idx + 1}`).join(', ');
|
||||
const values = [req.user!.id, ...fields.map(f => b[f] || null)];
|
||||
|
||||
try {
|
||||
await pool.query(
|
||||
`INSERT INTO chemicals (${allCols.join(', ')}) VALUES (${placeholders})`,
|
||||
values
|
||||
);
|
||||
imported++;
|
||||
} catch (err) {
|
||||
errors.push(`Row ${i + 1}: ${(err as Error).message}`);
|
||||
}
|
||||
}
|
||||
|
||||
res.json({ imported, errors });
|
||||
});
|
||||
|
||||
// DELETE /api/chemicals/:id
|
||||
router.delete('/:id', async (req, res) => {
|
||||
try {
|
||||
|
||||
Reference in New Issue
Block a user