8000 lead part completed except delete · vishal-coder/CRM_backend@917b626 · GitHub
[go: up one dir, main page]

Skip to content

Commit 917b626

Browse files
committed
lead part completed except delete
1 parent 8ee6a21 commit 917b626

File tree

7 files changed

+161
-0
lines changed

7 files changed

+161
-0
lines changed

controllers/LeadController.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import {
2+
fetchLeads,
3+
getManagerLeads,
4+
insertLead,
5+
} from "../models/LeadModel.js";
6+
7+
export const createLead = async (req, res) => {
8+
try {
9+
const { firstname, lastname, phone, email, category, createdBy, source } =
10+
req.body;
11+
const data = {
12+
firstname: firstname,
13+
lastname: lastname,
14+
phone: phone,
15+
email: email,
16+
category: category,
17+
createdBy: createdBy,
18+
source: source,
19+
createdOn: new Date(),
20+
};
21+
console.log("inside createLead--", data);
22+
const response = await insertLead(data);
23+
console.log("createLead response is---", response);
24+
res.status(200).send({
25+
message: "Lead Created Successfully",
26+
success: true,
27+
users: response,
28+
});
29+
} catch (error) {
30+
return res.send({
31+
message: "Something went wrong....Please try again later",
32+
success: false,
33+
});
34+
}
35+
};
36+
37+
export const getLeads = async (req, res) => {
38+
try {
39+
const { username, userType } = req.body;
40+
41+
console.log(
42+
"inside getLeads----------------------------------------------------------",
43+
username,
44+
userType
45+
);
46+
let response = null;
47+
if (userType === "Manager") {
48+
response = await getManagerLeads(username);
49+
} else {
50+
response = await fetchLeads(username, userType);
51+
}
52+
console.log("getLeads response is---", response);
53+
res.status(200).send({
54+
message: "Lead Created Successfully",
55+
success: true,
56+
leads: response,
57+
});
58+
} catch (error) {
59+
return res.send({
60+
message: "Something went wrong....Please try again later",
61+
success: false,
62+
});
63+
}
64+
};

controllers/UserController.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import Crypto from "crypto";
33
import jwt from "jsonwebtoken";
44
import {
55
activatateUser,
6+
deleteUserByUsername,
67
fetchAllUsers,
78
getDBUserByEmail,
89
insertAccountConfirmationCode,
@@ -229,3 +230,21 @@ export const getAllUsers = async (req, res) => {
229230
});
230231
}
231232
};
233+
export const deleteUser = async (req, res) => {
234+
try {
235+
const { username } = req.body;
236+
console.log("inside deleteUser", username);
237+
const response = await deleteUserByUsername({ username: username });
238+
console.log("deleteUser is---", response);
239+
res.status(200).send({
240+
message: "User Deleted Successfully",
241+
success: true,
242+
users: response,
243+
});
244+
} catch (error) {
245+
return res.send({
246+
message: "Something went wrong....Please try again later",
247+
success: false,
248+
});
249+
}
250+
};

index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import express from "express";
44
import { MongoClient } from "mongodb";
55
import { userRouter } from "./routes/User.js";
66
import http from "http";
7+
import { leadRouter } from "./routes/LeadRoute.js";
8+
import { contactRouter } from "./routes/ContactRoute.js";
79

810
const corsOptions = {
911
origin: "*",
@@ -39,3 +41,6 @@ app.get("/", (req, res) => {
3941
});
4042

4143
app.use("/auth", userRouter);
44+
app.use("/lead", leadRouter);
45+
46+
app.use("/contact", contactRouter);

models/AuthModel.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,3 +95,7 @@ export async function fetchAllUsers(username, userType) {
9595
.toArray();
9696
}
9797
}
98+
99+
export function deleteUserByUsername(data) {
100+
return client.db("CRM").collection("users").deleteOne(data);
101+
}

models/LeadModel.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import { client } from "../index.js";
2+
3+
export function insertLead(data) {
4+
console.log("indide create lead model");
5+
return client.db("CRM").collection("leads").insertOne(data);
6+
}
7+
8+
export async function fetchLeads(username, userType) {
9+
let query = {};
10+
if (userType === "Employee") {
11+
query = { createdBy: username };
12+
} else if (userType === "Manager") {
13+
query = { $or: [{ parent: username }, { createdBy: username }] };
14+
}
15+
return client
16+
.db("CRM")
17+
.collection("leads")
18+
.find(query, {
19+
firstname: 1,
20+
lastname: 1,
21+
phone: 1,
22+
email: 1,
23+
parent: 1,
24+
source: 1,
25+
createdOn: 1,
26+
})
27+
.toArray();
28+
}
29+
30+
export function getManagerLeads(username) {
31+
console.log("inside manager get leads");
32+
return client
33+
.db("CRM")
34+
.collection("users")
35+
.aggregate([
36+
{
37+
$lookup: {
38+
from: "leads",
39+
localField: "username",
40+
foreignField: "createdBy",
41+
as: "leads",
42+
},
43+
},
44+
{
45+
$match: {
46+
$or: [{ parent: username }, { username: username }],
47+
},
48+
},
49+
{ $project: { _id: 0, leads: 1 } },
50+
{ $unwind: { path: "$leads" } },
51+
])
52+
.toArray();
53+
}

routes/LeadRoute.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import express from "express";
2+
import { createLead, getLeads } from "../controllers/LeadController.js";
3+
4+
import { leadValidation } from "../validations/LeadValidation.js";
5+
6+
const router = express.Router();
7+
router.get("/", (req, res) => {
8+
res.send("lead route working");
9+
});
10+
11+
router.post("/create", leadValidation(), createLead);
12+
router.post("/getAll", getLeads);
13+
14+
export const leadRouter = router;

routes/User.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
signup,
99
verifyEmail,
1010
getAllUsers,
11+
deleteUser,
1112
} from "../controllers/UserController.js";
1213
import {
1314
forgotPasswordValidation,
@@ -30,5 +31,6 @@ router.post("/resetPassword", restPasswordValidation(), resetpassword);
3031
router.post("/verifyToken", verifyToken);
3132
router.post("/logoutUser", logoutUser);
3233
router.post("/allUsers", getAllUsers);
34+
router.delete("/delete", deleteUser);
3335

3436
export const userRouter = router;

0 commit comments

Comments
 (0)
0