Ver código fonte

create mapping api for sale person

AnilBwavhal 1 mês atrás
pai
commit
60c5d9b43c
3 arquivos alterados com 126 adições e 5 exclusões
  1. 118 5
      controllers/dvrReportController.js
  2. 6 0
      controllers/validation.js
  3. 2 0
      routes/route.js

+ 118 - 5
controllers/dvrReportController.js

@@ -1433,9 +1433,9 @@ module.exports.createDvrEntry = async (req, res) => {
             const date_of_visit = new Date(req.body.visit_date);
             //const date_of_visit=req.body.visit_date;
 
-            const query = "INSERT INTO dvr_report(date_of_visit,customer_name,contact_name,physical_meeting,message,created_by,created_on,stage_id,login_id,sale_person,delete_id,nextstep)" +
-                "VALUES(?,?,?,?,?,?,?,?,?,?,?,?)";
-            const values = [date_of_visit, req.body.customer_name, req.body.contact_name, req.body.physical_metting, req.body.message, req.body.created_by, createdate, req.body.stage_id, req.body.created_by, req.body.sale_person, 0,req.body.nextstep]
+            const query = "INSERT INTO dvr_report(date_of_visit,customer_name,contact_name,physical_meeting,message,created_by,created_on,stage_id,login_id,sale_person,delete_id,nextstep,second_contact)" +
+                "VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?)";
+            const values = [date_of_visit, req.body.customer_name, req.body.contact_name, req.body.physical_metting, req.body.message, req.body.created_by, createdate, req.body.stage_id, req.body.created_by, req.body.sale_person, 0,req.body.nextstep,req.body.second_contact]
 
             // console.log("query",query);
             //console.log("values",values);
@@ -1678,7 +1678,7 @@ module.exports.getlastestdvr = async (req, res) => {
 
         if (person_role == 'admin') {
             //admin show all data
-            const query = "SELECT d.dvr_id,d.date_of_visit,d.customer_name,d.contact_name,d.physical_meeting,d.message,s.stage_desc,d.stage_id FROM dvr_report d JOIN stage_master s ON d.stage_id=s.stage_id where  d.created_on between '" + startdate + "' and '" + enddate + "' and d.delete_id=0 order by d.date_of_visit desc";
+            const query = "SELECT d.dvr_id,d.date_of_visit,d.customer_name,d.contact_name,d.physical_meeting,d.message,s.stage_desc,d.stage_id,d.nextstep,d.second_contact FROM dvr_report d JOIN stage_master s ON d.stage_id=s.stage_id where  d.created_on between '" + startdate + "' and '" + enddate + "' and d.delete_id=0 order by d.date_of_visit desc";
             const queryresult = await db.executequery(query);
             console.log("query", query);
 
@@ -1715,7 +1715,7 @@ module.exports.getlastestdvr = async (req, res) => {
         } else {
             // other users 
             console.log("other users");
-            const query = "SELECT d.dvr_id,d.date_of_visit,d.customer_name,d.contact_name,d.physical_meeting,d.message,s.stage_desc,d.stage_id FROM dvr_report d JOIN stage_master s ON d.stage_id=s.stage_id where d.login_id='" + login_id + "' and d.created_on between '" + startdate + "' and '" + enddate + "' and d.delete_id=0 order by d.date_of_visit desc";
+            const query = "SELECT d.dvr_id,d.date_of_visit,d.customer_name,d.contact_name,d.physical_meeting,d.message,s.stage_desc,d.stage_id.d.nextstep,d.second_contact FROM dvr_report d JOIN stage_master s ON d.stage_id=s.stage_id where d.login_id='" + login_id + "' and d.created_on between '" + startdate + "' and '" + enddate + "' and d.delete_id=0 order by d.date_of_visit desc";
             const queryresult = await db.executequery(query);
             console.log("query", query);
 
@@ -2456,6 +2456,119 @@ module.exports.deletedvrEntry = async (req, res) => {
 
 
 
+// module.exports.mappingCustomerDetails = async (req, res) => {
+//     // Validate the request
+//     const validationResp = await validation.mappingCustomerDetailsCheck(req);
+    
+//     if (validationResp.status) {
+//         try {
+//             // Extract salesperson's name
+//             const sale_person = req.body.sale_person;
+//             const name = sale_person.split(" ");
+//             const first_name = name[0];
+//             const last_name = name[1];
+//             console.log("Extracted names:", first_name, last_name);
+
+//             // SQL query to get user_id from user_master
+//             const queryUserID = "SELECT user_id FROM user_master WHERE first_name='" + first_name + "' AND last_name='" + last_name + "'";
+//             console.log("queryUserID", queryUserID);
+//             const queryResultID = await db.executequery(queryUserID);
+//             console.log("User ID query result:", queryResultID);
+
+//             if (queryResultID.length > 0) {
+//                 res.send({
+//                     status: true,
+//                     data: {
+//                         user_id: queryResultID[0].user_id
+//                     },
+//                     error: null
+//                 });
+//             } else {
+//                 res.send({
+//                     status: false,
+//                     data: [],
+//                     errors: "No user found with the specified name."
+//                 });
+//             }
+//         } catch (err) {
+//             console.error("Error occurred:", err);
+//             res.send({
+//                 status: false,
+//                 data: [],
+//                 errors: "An error occurred while fetching the user ID."
+//             });
+//         }
+//     } else {
+//         res.send(validationResp);
+//     }
+// }
+
+module.exports.mappingCustomerDetails = async (req, res) => {
+    // Validate the request
+    console.log("sale_",req.query.sale_person)
+    const validationResp = await validation.mappingCustomerDetailsCheck(req);
+    
+    if (validationResp.status) {
+        try {
+            const sale_person = req.query.sale_person;
+            const name = sale_person.split(" ");
+            const first_name = name[0];
+            const last_name = name[1];
+            console.log("Extracted names:", first_name, last_name);
+
+            // SQL query to get user_id from user_master
+            const queryUserID = "SELECT user_id FROM user_master WHERE first_name='" + first_name + "' AND last_name='" + last_name + "'";
+            console.log("queryUserID", queryUserID);
+            const queryResultID = await db.executequery(queryUserID);
+            console.log("User ID query result:", queryResultID);
+
+            if (queryResultID.length > 0) {
+                const userId = queryResultID[0].user_id;
+
+                // SQL query to get customer details associated with the user_id
+                const queryCustomerDetails = `
+                    SELECT c.customer_id, c.customer_name, u.user_id 
+                    FROM customer_master c 
+                    JOIN customer_user_mapping u ON c.customer_id = u.customer_id 
+                    WHERE u.user_id = '${userId}'`;
+                console.log("queryCustomerDetails", queryCustomerDetails);
+                const customerDetailsResult = await db.executequery(queryCustomerDetails);
+                console.log("Customer details result:", customerDetailsResult);
+
+                if (customerDetailsResult.length > 0) {
+                    res.send({
+                        status: true,
+                        data: customerDetailsResult,
+                        error: null
+                    });
+                } else {
+                    res.send({
+                        status: false,
+                        data: [],
+                        errors: "No customer details found for the specified salesperson."
+                    });
+                }
+            } else {
+                res.send({
+                    status: false,
+                    data: [],
+                    errors: "No user found with the specified name."
+                });
+            }
+        } catch (err) {
+            console.error("Error occurred:", err);
+            res.send({
+                status: false,
+                data: [],
+                errors: "An error occurred while fetching the customer details."
+            });
+        }
+    } else {
+        res.send(validationResp);
+    }
+}
+
+
 function getLastWeeksDate() {
     const now = new Date();
     const format2 = "YYYY-MM-DD";

+ 6 - 0
controllers/validation.js

@@ -273,6 +273,12 @@ module.exports.updateCustomerDetailscheck = async (req, res) => {
     return { status: true };
 }
 
+module.exports.mappingCustomerDetailsCheck = async (req, res) => {
+    if (req.query.sale_person == "" || req.query.sale_person == undefined || req.query.sale_person == null) {
+        return { status: false, data: {}, errors: "Please enter sale person" }
+    } 
+    return { status: true };
+}
 function containsOnlyLetters(str) {
     //return /^[A-Za-z\s]+$/.test(str);
 

+ 2 - 0
routes/route.js

@@ -39,6 +39,8 @@ router.get("/api/getcustcontactdetails",auth,dvrController.getCustcontactDetails
 router.post("/api/createUpdateCustomerentry",auth,dvrController.createUpdateCustomerentry);
 
 router.post("/api/deletedvrEntry",auth,dvrController.deletedvrEntry);
+router.get("/api/mappingCustomerDetails",auth,dvrController.mappingCustomerDetails);
+