Dealer_Onboarding_Backend/controllers/applicationController.js

136 lines
3.4 KiB
JavaScript

const { Application } = require('../models');
const { v4: uuidv4 } = require('uuid');
exports.submitApplication = async (req, res) => {
try {
const {
applicantName, email, phone, businessType, locationType,
preferredLocation, city, state, experienceYears, investmentCapacity
} = req.body;
const applicationId = `APP-${new Date().getFullYear()}-${uuidv4().substring(0, 6).toUpperCase()}`;
const application = await Application.create({
applicationId,
applicantName,
email,
phone,
businessType,
preferredLocation,
city,
state,
experienceYears,
investmentCapacity,
currentStage: 'DD',
overallStatus: 'Pending',
progressPercentage: 0
});
res.status(201).json({
success: true,
message: 'Application submitted successfully',
applicationId: application.applicationId
});
} catch (error) {
console.error('Submit application error:', error);
res.status(500).json({ success: false, message: 'Error submitting application' });
}
};
exports.getApplications = async (req, res) => {
try {
const applications = await Application.findAll({
order: [['createdAt', 'DESC']]
});
res.json({ success: true, applications });
} catch (error) {
console.error('Get applications error:', error);
res.status(500).json({ success: false, message: 'Error fetching applications' });
}
};
exports.getApplicationById = async (req, res) => {
try {
const { id } = req.params;
const application = await Application.findOne({
where: {
[Op.or]: [
{ id },
{ applicationId: id }
]
}
});
if (!application) {
return res.status(404).json({ success: false, message: 'Application not found' });
}
res.json({ success: true, application });
} catch (error) {
console.error('Get application error:', error);
res.status(500).json({ success: false, message: 'Error fetching application' });
}
};
exports.takeAction = async (req, res) => {
try {
const { id } = req.params;
const { action, comments, rating } = req.body;
const application = await Application.findOne({
where: {
[Op.or]: [
{ id },
{ applicationId: id }
]
}
});
if (!application) {
return res.status(404).json({ success: false, message: 'Application not found' });
}
await application.update({
overallStatus: action,
updatedAt: new Date()
});
res.json({ success: true, message: 'Action taken successfully' });
} catch (error) {
console.error('Take action error:', error);
res.status(500).json({ success: false, message: 'Error processing action' });
}
};
exports.uploadDocuments = async (req, res) => {
try {
const { id } = req.params;
const { documents } = req.body;
const application = await Application.findOne({
where: {
[Op.or]: [
{ id },
{ applicationId: id }
]
}
});
if (!application) {
return res.status(404).json({ success: false, message: 'Application not found' });
}
await application.update({
documents: documents,
updatedAt: new Date()
});
res.json({ success: true, message: 'Documents uploaded successfully' });
} catch (error) {
console.error('Upload documents error:', error);
res.status(500).json({ success: false, message: 'Error uploading documents' });
}
};