Rakshitjan commited on
Commit
a4ee724
·
verified ·
1 Parent(s): c3b011d

Upload 2 files

Browse files
Files changed (2) hide show
  1. index.js +78 -10
  2. models.js +5 -5
index.js CHANGED
@@ -1116,6 +1116,68 @@ app.put('/api/cases/:case_id', authenticateToken, requireAgentOrAdmin, async (re
1116
  }
1117
  });
1118
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1119
  // Update specific section of case
1120
  // PATCH endpoint removed as it handled legacy sections
1121
 
@@ -1464,20 +1526,26 @@ app.post('/api/decrypt', async (req, res) => {
1464
  // Helper function to process and save borrower data
1465
  // Helper function to process and save borrower data
1466
  async function processAndSaveBorrowerData(jsonData, appId) {
1467
- // No transformation needed, save directly to new fields
 
 
 
 
1468
 
1469
  // Create case data with your exact schema using the main Case model
1470
  const caseData = {
1471
  case_id: uuidv4(),
1472
- application_id: appId || jsonData.applicationId || jsonData.appIicationid, // Handle both spellings
1473
- case_applicant_name: jsonData.applicantDetail?.name || '',
1474
- case_applicant_contact: jsonData.applicantDetail?.mobile || '',
1475
- address: jsonData.residenceDetail ? `${jsonData.residenceDetail.address1}, ${jsonData.residenceDetail.cityId}` : '', // Simplified address for main field
1476
- case_type: "Business Loan", // Default
1477
- loan_amount: jsonData.incomeExpenditure?.yearlyIncome || 0,
 
 
1478
  priority: "MED",
1479
- assigned_agent: "",
1480
- status: "Pending",
1481
 
1482
  // Save the full detailed structures
1483
  applicantDetail: jsonData.applicantDetail,
@@ -1488,7 +1556,7 @@ async function processAndSaveBorrowerData(jsonData, appId) {
1488
  familyExpenses: jsonData.familyExpenses,
1489
  metaData: jsonData.metaData,
1490
 
1491
- remarks: `Application ID: ${appId || jsonData.applicationId || jsonData.appIicationid} - Auto imported`,
1492
  created_at: new Date(),
1493
  updated_at: new Date()
1494
  };
 
1116
  }
1117
  });
1118
 
1119
+ // Update document IDs in nested structures
1120
+ app.patch('/api/cases/:case_id/documents', authenticateToken, requireAgentOrAdmin, async (req, res) => {
1121
+ try {
1122
+ const caseDoc = await Case.findOne({ case_id: req.params.case_id });
1123
+
1124
+ if (!caseDoc) {
1125
+ return res.status(404).json({ error: 'Case not found' });
1126
+ }
1127
+
1128
+ if (req.user.role === 'agent' && caseDoc.assigned_agent !== req.user.userId) {
1129
+ return res.status(403).json({ error: 'Access denied to this case' });
1130
+ }
1131
+
1132
+ const updateData = { updated_at: new Date() };
1133
+
1134
+ // Update applicantDetail document IDs
1135
+ if (req.body.applicantDetail) {
1136
+ updateData.applicantDetail = {
1137
+ ...caseDoc.applicantDetail,
1138
+ ...req.body.applicantDetail
1139
+ };
1140
+ }
1141
+
1142
+ // Update residenceDetail document IDs
1143
+ if (req.body.residenceDetail) {
1144
+ updateData.residenceDetail = {
1145
+ ...caseDoc.residenceDetail,
1146
+ ...req.body.residenceDetail
1147
+ };
1148
+ }
1149
+
1150
+ // Update coApplicantDetail document IDs
1151
+ if (req.body.coApplicantDetail) {
1152
+ updateData.coApplicantDetail = {
1153
+ ...caseDoc.coApplicantDetail,
1154
+ ...req.body.coApplicantDetail
1155
+ };
1156
+ }
1157
+
1158
+ // Update businessDetail document IDs
1159
+ if (req.body.businessDetail) {
1160
+ updateData.businessDetail = {
1161
+ ...caseDoc.businessDetail,
1162
+ ...req.body.businessDetail
1163
+ };
1164
+ }
1165
+
1166
+ const updatedCase = await Case.findOneAndUpdate(
1167
+ { case_id: req.params.case_id },
1168
+ updateData,
1169
+ { new: true }
1170
+ );
1171
+
1172
+ res.json({
1173
+ message: 'Document IDs updated successfully',
1174
+ case: updatedCase
1175
+ });
1176
+ } catch (error) {
1177
+ res.status(400).json({ error: error.message });
1178
+ }
1179
+ });
1180
+
1181
  // Update specific section of case
1182
  // PATCH endpoint removed as it handled legacy sections
1183
 
 
1526
  // Helper function to process and save borrower data
1527
  // Helper function to process and save borrower data
1528
  async function processAndSaveBorrowerData(jsonData, appId) {
1529
+ // Extract data with proper fallbacks
1530
+ const applicantDetail = jsonData.applicantDetail || {};
1531
+ const residenceDetail = jsonData.residenceDetail || {};
1532
+ const businessDetail = jsonData.businessDetail || {};
1533
+ const incomeExpenditure = jsonData.incomeExpenditure || {};
1534
 
1535
  // Create case data with your exact schema using the main Case model
1536
  const caseData = {
1537
  case_id: uuidv4(),
1538
+ application_id: appId || jsonData.applicationId || jsonData.appIicationid || 'N/A',
1539
+ case_applicant_name: applicantDetail.name || 'N/A',
1540
+ case_applicant_contact: applicantDetail.mobile || 'N/A',
1541
+ address: residenceDetail.address1
1542
+ ? `${residenceDetail.address1}${residenceDetail.address2 ? ', ' + residenceDetail.address2 : ''}${residenceDetail.address3 ? ', ' + residenceDetail.address3 : ''}`
1543
+ : 'N/A',
1544
+ case_type: "Business Loan",
1545
+ loan_amount: incomeExpenditure.yearlyIncome || incomeExpenditure.totalIncome || 0,
1546
  priority: "MED",
1547
+ assigned_agent: null,
1548
+ status: "Unassigned",
1549
 
1550
  // Save the full detailed structures
1551
  applicantDetail: jsonData.applicantDetail,
 
1556
  familyExpenses: jsonData.familyExpenses,
1557
  metaData: jsonData.metaData,
1558
 
1559
+ remarks: `Application ID: ${appId || jsonData.applicationId || jsonData.appIicationid || 'N/A'} - Auto imported`,
1560
  created_at: new Date(),
1561
  updated_at: new Date()
1562
  };
models.js CHANGED
@@ -371,13 +371,13 @@ const agentSchema = new mongoose.Schema({
371
  // Case Schema with all sections
372
  const caseSchema = new mongoose.Schema({
373
  case_id: { type: String, required: true, unique: true },
374
- application_id: { type: String, required: true },
375
- case_applicant_name: { type: String, required: true },
376
- case_applicant_contact: { type: String, required: true },
377
- address: { type: String, required: true },
378
  case_type: {
379
  type: String,
380
- required: true
381
  },
382
  loan_amount: { type: Number, default: 0 },
383
  priority: {
 
371
  // Case Schema with all sections
372
  const caseSchema = new mongoose.Schema({
373
  case_id: { type: String, required: true, unique: true },
374
+ application_id: { type: String, default: 'N/A' },
375
+ case_applicant_name: { type: String, default: 'N/A' },
376
+ case_applicant_contact: { type: String, default: 'N/A' },
377
+ address: { type: String, default: 'N/A' },
378
  case_type: {
379
  type: String,
380
+ default: 'Business Loan'
381
  },
382
  loan_amount: { type: Number, default: 0 },
383
  priority: {