vvet_backend_new/controllers/vvetController.js

106 lines
4.8 KiB
JavaScript
Raw Normal View History

2025-05-17 00:11:16 +05:30
const { donorPool, fundraisingPool } = require('../db');
// Helper: get filename from uploaded file or null
const getFileName = (files, field) => files && files[field] && files[field][0] ? files[field][0].filename : null;
// Donor submission
exports.submitDonor = (req, res) => {
const files = req.files || {};
const {
name, email, age, dob, phone, pan_number,
bank_account_holder_name, bank_name, bank_account_number, ifsc_code,
donation_amount, payment
} = req.body;
if (!name || !donation_amount || !payment) {
return res.status(400).json({ error: 'Missing required fields' });
}
const pan_document = getFileName(files, 'pan_document');
const transaction_screenshot = getFileName(files, 'transaction_screenshot');
const sql = `
INSERT INTO donors (
name, email, age, dob, phone, pan_number, pan_document,
bank_account_holder_name, bank_name, bank_account_number, ifsc_code,
donation_amount, payment, transaction_screenshot
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
`;
const values = [
name, email || null, age || null, dob || null, phone || null, pan_number || null, pan_document,
bank_account_holder_name || null, bank_name || null, bank_account_number || null, ifsc_code || null,
donation_amount, payment, transaction_screenshot
];
donorPool.query(sql, values, (err, results) => {
if (err) {
console.error('Donor Insert Error:', err);
return res.status(500).json({ error: 'Failed to insert donor data' });
}
res.json({ message: 'Donor data submitted', id: results.insertId });
});
};
// Fundraising submission
exports.submitFundraisings = (req, res) => {
const files = req.files || {};
const {
name, email, age, dob, phone, pan_number,
category, patient_name, cause, hospital_name, medical_location, attender_name, medical_phone,
medical_amount, student_name, standard, parent_name, parent_phone, education_amount,
disaster_name, disaster_place, disaster_phone, disaster_amount,
general_name, general_place, general_phone, general_amount,
other_reason, bank_account_holder_name, bank_name, bank_account_number, ifsc_code,
fundraising_amount, payment_method
} = req.body;
if (!name || !category || !fundraising_amount) {
return res.status(400).json({ error: 'Missing required fields' });
}
// Files
const pan_document = getFileName(files, 'pan_document');
const fee_document = getFileName(files, 'fee_document');
const medical_certificate = getFileName(files, 'medical_certificate');
const medical_document = getFileName(files, 'medical_document');
const disaster_document = getFileName(files, 'disaster_document');
const general_document = getFileName(files, 'general_document');
const other_document = getFileName(files, 'other_document');
const transaction_screenshot = getFileName(files, 'transaction_screenshot');
const sql = `
INSERT INTO fundraisings (
name, email, age, dob, phone, pan_number, pan_document,
category, patient_name, cause, hospital_name, medical_location, attender_name, medical_phone,
medical_amount, medical_certificate, medical_document,
student_name, standard, parent_name, parent_phone, education_amount, fee_document,
disaster_name, disaster_place, disaster_phone, disaster_amount, disaster_document,
general_name, general_place, general_phone, general_amount, general_document,
other_reason, other_document,
bank_account_holder_name, bank_name, bank_account_number, ifsc_code,
fundraising_amount, transaction_screenshot
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
`;
const values = [
name, email || null, age || null, dob || null, phone || null, pan_number || null, pan_document,
category, patient_name || null, cause || null, hospital_name || null, medical_location || null, attender_name || null, medical_phone || null,
medical_amount || null, medical_certificate, medical_document,
student_name || null, standard || null, parent_name || null, parent_phone || null, education_amount || null, fee_document,
disaster_name || null, disaster_place || null, disaster_phone || null, disaster_amount || null, disaster_document,
general_name || null, general_place || null, general_phone || null, general_amount || null, general_document,
other_reason || null, other_document,
bank_account_holder_name || null, bank_name || null, bank_account_number || null, ifsc_code || null,
fundraising_amount, transaction_screenshot
];
fundraisingPool.query(sql, values, (err, results) => {
if (err) {
console.error('Fundraising Insert Error:', err);
return res.status(500).json({ error: 'Failed to insert fundraising data' });
}
res.json({ message: 'Fundraising data submitted', id: results.insertId });
});
};