Skip to content

Commit f9f8142

Browse files
Implementation: Form Submission(without s3) - Plantation
1 parent 3cb63d7 commit f9f8142

File tree

2 files changed

+149
-0
lines changed

2 files changed

+149
-0
lines changed
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
//const mysql = require("mysql2/promise");
2+
const db = require("../configs/mysql_config");
3+
const asyncHandler = require("../middlewares/asyncHandler");
4+
5+
const postPlantationformData_basicdetails_sql = `INSERT INTO forms (
6+
farmer_name, age, mobile, district, block, panchayat, hamlet,
7+
id_type, id_number, gender, spouse, type_of_households, h_members,
8+
hh_occupation, special_catog, caste, house_owner, type_of_house,
9+
drinking_water, potability, domestic_water, toilet_avail, toilet_cond,
10+
household_education, user_id, created_at, lat, lon, status, form_type
11+
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`;
12+
13+
const postPlantationformData_plantation_details_sql = `INSERT INTO plantation_details (
14+
form_id, ownership, well_irrigation, area_irrigated, irrigated_lands,
15+
patta, total_area, taluk, firka, revenue, crop_season, livestocks,
16+
sf_number, soil_type, land_to_benefit, date_of_ins,
17+
area_benefited_by_proposal, any_other_works, p_contribution, f_contribution,
18+
total_est, field_insp, date_of_app, plantaions
19+
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`;
20+
21+
const postPlantationformData_bankdetails_sql = `INSERT INTO bank_details (
22+
form_id, account_holder_name, account_number, bank_name,
23+
branch, ifsc_code, farmer_ack
24+
) VALUES (?, ?, ?, ?, ?, ?, ?)`;
25+
26+
const postLandformData_files_sql = `INSERT INTO files (
27+
form_id, identity, geotag, patta, fmb, photo, passbook
28+
) VALUES (?, ?, ?, ?, ?, ?, ?)`;
29+
30+
exports.postPlantationformData = asyncHandler(async (req, res) => {
31+
const connection = await db.getConnection();
32+
await connection.beginTransaction(); // 🚀 Start transaction
33+
34+
try {
35+
const plantationformData = req.body;
36+
const date = new Date();
37+
const today = new Date(date.getFullYear(), date.getMonth(), date.getDate()).toLocaleDateString('en-CA');
38+
console.log("Received landform data:", plantationformData);
39+
40+
const safe = (value) => value === undefined ? null : value;
41+
42+
// Insert basic details
43+
const [formResult] = await connection.execute(postPlantationformData_basicdetails_sql, [
44+
safe(plantationformData.basicDetails.name),
45+
safe(plantationformData.basicDetails.age),
46+
safe(plantationformData.basicDetails.mobile),
47+
safe(plantationformData.basicDetails.district),
48+
safe(plantationformData.basicDetails.block),
49+
safe(plantationformData.basicDetails.panchayat),
50+
safe(plantationformData.basicDetails.hamlet),
51+
safe(plantationformData.basicDetails.idCardType),
52+
safe(plantationformData.basicDetails.idCardNumber),
53+
safe(plantationformData.basicDetails.gender),
54+
safe(plantationformData.basicDetails.fatherSpouse),
55+
safe(plantationformData.basicDetails.householdType),
56+
safe(plantationformData.basicDetails.hhcombined),
57+
safe(plantationformData.basicDetails.occupationCombined),
58+
safe(plantationformData.basicDetails.specialCategoryNumber),
59+
safe(plantationformData.basicDetails.caste),
60+
safe(plantationformData.basicDetails.houseOwnership),
61+
safe(plantationformData.basicDetails.houseType),
62+
safe(plantationformData.basicDetails.drinkingWaterCombined),
63+
safe(plantationformData.basicDetails.potabilityCombined),
64+
safe(plantationformData.basicDetails.domesticWaterCombined),
65+
safe(plantationformData.basicDetails.toiletAvailability),
66+
safe(plantationformData.basicDetails.toiletCondition),
67+
safe(plantationformData.basicDetails.education),
68+
safe(plantationformData.user_id),
69+
safe(today),
70+
safe(plantationformData.landDevelopment.latitude),
71+
safe(plantationformData.landDevelopment.longitude),
72+
1,
73+
3
74+
]);
75+
76+
const form_id = formResult.insertId;
77+
console.log("Form ID:", form_id);
78+
79+
//console.log(plantationformData.landDevelopment.workType);
80+
81+
// Insert plantation details
82+
await connection.execute(postPlantationformData_plantation_details_sql, [
83+
safe(form_id),
84+
safe(plantationformData.landOwnership.landOwnershipType),
85+
safe(plantationformData.landOwnership.hasWell),
86+
safe(plantationformData.landOwnership.areaIrrigated),
87+
safe(plantationformData.landOwnership.irrigatedLandCombined),
88+
safe(plantationformData.landOwnership.pattaNumber),
89+
safe(plantationformData.landOwnership.totalArea),
90+
safe(plantationformData.landOwnership.taluk),
91+
safe(plantationformData.landOwnership.firka),
92+
safe(plantationformData.landOwnership.revenueVillage),
93+
safe(plantationformData.landOwnership.cropSeason),
94+
safe(plantationformData.landOwnership.livestockCombined),
95+
safe(plantationformData.landDevelopment.sfNumber),
96+
safe(plantationformData.landDevelopment.soilTypeCombined),
97+
safe(plantationformData.landDevelopment.landBenefit),
98+
safe(today),
99+
safe(plantationformData.landDevelopment.proposalArea),
100+
safe(plantationformData.landDevelopment.otherWorks),
101+
safe(plantationformData.landDevelopment.pradanContribution),
102+
safe(plantationformData.landDevelopment.farmerContribution),
103+
safe(plantationformData.landDevelopment.totalEstimate),
104+
safe(today),
105+
safe(today),
106+
safe(plantationformData.landDevelopment.workType2)
107+
]);
108+
109+
//console.log("form id:", form_id);
110+
111+
// Insert bank details
112+
await connection.execute(postPlantationformData_bankdetails_sql, [
113+
safe(form_id),
114+
safe(plantationformData.bankDetails.accountHolderName),
115+
safe(plantationformData.bankDetails.accountNumber),
116+
safe(plantationformData.bankDetails.bankName),
117+
safe(plantationformData.bankDetails.branch),
118+
safe(plantationformData.bankDetails.ifscCode),
119+
safe(plantationformData.bankDetails.farmerAgreed),
120+
]);
121+
122+
// Optional: Insert files if present
123+
// if (plantationformData.files) {
124+
// await connection.execute(postLandformData_files_sql, [
125+
// safe(form_id),
126+
// safe(plantationformData.files.identity),
127+
// safe(plantationformData.files.geotag),
128+
// safe(plantationformData.files.patta),
129+
// safe(plantationformData.files.fmb),
130+
// safe(plantationformData.files.photo),
131+
// safe(plantationformData.files.passbook),
132+
// ]);
133+
// }
134+
135+
// Commit transaction
136+
await connection.commit();
137+
res.status(201).json({ message: "Form data submitted successfully", form_id });
138+
139+
} catch (error) {
140+
await connection.rollback();
141+
console.error("Error during transaction:", error);
142+
res.status(500).json({ error: "Failed to submit form data" });
143+
} finally {
144+
connection.release(); // Always release
145+
}
146+
});
147+

routes/formData.route.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ const express = require("express");
22
const router = express.Router();
33
const {postLandformData} = require("../controllers/landformData.controller");
44
const {postPondformData} = require("../controllers/pondformData.controller");
5+
const {postPlantationformData} = require("../controllers/plantationformData.controller");
56

67
router.route('/postLandformData').post(postLandformData);
78
router.route('/postPondformData').post(postPondformData);
9+
router.route('/postPlantationformData').post(postPlantationformData);
810

911
module.exports = router;

0 commit comments

Comments
 (0)