async function prepareAndInsertExpenses(values, response) {
try {
const insertQuery = 'INSERT INTO "expenses" (n_plan, type_work_id, printer_id, material_id, performer_id, format_id, n_sheets, n_page, n_ruined) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)';
const result = await pool.query(insertQuery, values);
console.log('Data successfully inserted');
response.json({ success: true, message: 'Данные успешно вставлены' });
return true;
} catch (err) {
console.error('Error inserting data:', err.stack);
response.status(500).json({ success: false, error: 'Произошла ошибка при вставке данных' });
return false;
}
}
async function processExpenses(response) {
for (let i = 0; i = expenses_without_first.length; i += 8) {
const selection = expenses_without_first.slice(i, i + 8);
console.log("expensesChunk", expensesChunk)
const values = [
work_id_array[i / 8],
selection[0],
selection[1],
selection[2],
selection[3],
selection[4],
selection[5],
selection[6],
selection[7]
];
console.log("processExpenses.values", values);
const insertionResult = await prepareAndInsertExpenses(values, response);
if (!insertionResult) {
return response.status(500).json({ success: false, error: 'Произошла ошибка при вставке данных' });
}
}
response.json({ success: true, message: 'Данные успешно вставлены' });
}
processExpenses(res);