8000 lead delete · vishal-coder/CRM_backend@d9d03c7 · GitHub
[go: up one dir, main page]

Skip to content

Commit d9d03c7

Browse files
committed
lead delete
1 parent 917b626 commit d9d03c7

File tree

4 files changed

+62
-1
lines changed

4 files changed

+62
-1
lines changed

controllers/LeadController.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {
2+
deleteLeadById,
23
fetchLeads,
34
getManagerLeads,
45
insertLead,
@@ -62,3 +63,25 @@ export const getLeads = async (req, res) => {
6263
});
6364
}
6465
};
66+
67+
export const deleteLead = async (req, res) => {
68+
try {
69+
const { id } = req.body;
70+
71+
console.log("inside deleteLead----------------", id);
72+
73+
const response = await deleteLeadById(id);
74+
75+
console.log("deleteLead response is---", response);
76+
res.status(200).send({
77+
message: "Lead deleted Successfully",
78+
success: true,
79+
leads: response,
80+
});
81+
} catch (error) {
82+
return res.send({
83+
message: "Something went wrong....Please try again later",
84+
success: false,
85+
});
86+
}
87+
};

models/LeadModel.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { ObjectId } from "mongodb";
12
import { client } from "../index.js";
23

34
export function insertLead(data) {
@@ -51,3 +52,10 @@ export function getManagerLeads(username) {
5152
])
5253
.toArray();
5354
}
55+
56+
export function deleteLeadById(id) {
57+
return client
58+
.db("CRM")
59+
.collection("leads")
60+
.deleteOne({ _id: ObjectId(id) });
61+
}

routes/LeadRoute.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import express from "express";
2-
import { createLead, getLeads } from "../controllers/LeadController.js";
2+
import {
3+
createLead,
4+
deleteLead,
5+
getLeads,
6+
} from "../controllers/LeadController.js";
37

48
import { leadValidation } from "../validations/LeadValidation.js";
59

@@ -10,5 +14,6 @@ router.get("/", (req, res) => {
1014

1115
router.post("/create", leadValidation(), createLead);
1216
router.post("/getAll", getLeads);
17+
router.delete("/delete", deleteLead);
1318

1419
export const leadRouter = router;

validations/LeadValidation.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { body, check, validationResult } from "express-validator";
2+
import express from "express";
3+
4+
export function leadValidation() {
5+
return [
6+
check("firstname").notEmpty(),
7+
check("lastname").notEmpty(),
8+
check("phone").notEmpty(),
9+
check("source").notEmpty(),
10+
check("category").notEmpty(),
11+
check("createdBy").notEmpty(),
12+
check("email", "Please provide a valid email").isEmail(),
13+
(req, res, next) => {
14+
const errors = validationResult(req);
15+
16+
if (errors.isEmpty()) {
17+
return next();
18+
}
19+
console.log(errors);
20+
return res.status(422).json({
21+
errors: errors.msg,
22+
});
23+
},
24+
];
25+
}

0 commit comments

Comments
 (0)
0