8000 contacat dash + service req · vishal-coder/CRM_backend@44a5bb9 · GitHub
[go: up one dir, main page]

Skip to content

Commit 44a5bb9

Browse files
committed
contacat dash + service req
1 parent d9d03c7 commit 44a5bb9

File tree

11 files changed

+330
-1
lines changed

11 files changed

+330
-1
lines changed

controllers/ContactController.js

Lines changed: 61 additions & 0 deletions
B41A
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import {
2+
fetchContacts,
3+
getManagerContacts,
4+
insertContact,
5+
} from "../models/ContactModel.js";
6+
7+
export const createContact = 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+
Payment: "Pending",
20+
createdOn: new Date(),
21+
};
22+
console.log("inside createContact--", data);
23+
const response = await insertContact(data);
24+
console.log("createContact response is---", response);
25+
res.status(200).send({
26+
message: "Contact Created Successfully",
27+
success: true,
28+
users: response,
29+
});
30+
} catch (error) {
31+
return res.send({
32+
message: "Something went wrong....Please try again later",
33+
success: false,
34+
});
35+
}
36+
};
37+
38+
export const getContacts = async (req, res) => {
39+
try {
40+
const { username, userType } = req.body;
41+
42+
console.log("inside getLeads-----", username, userType);
43+
let response = null;
44+
if (userType === "Manager") {
45+
response = await getManagerContacts(username);
46+
} else {
47+
response = await fetchContacts(username, userType);
48+
}
49+
console.log("getContact response is---", response);
50+
res.status(200).send({
51+
message: "Contact fetched Successfully",
52+
success: true,
53+
contacts: response,
54+
});
55+
} catch (error) {
56+
return res.send({
57+
message: "Something went wrong....Please try again later",
58+
success: false,
59+
});
60+
}
61+
};

controllers/LeadController.js

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1+
import { insertContact } from "../models/ContactModel.js";
12
import {
23
deleteLeadById,
34
fetchLeads,
5+
getLeadById,
46
getManagerLeads,
57
insertLead,
8+
updateLeadById,
69
} from "../models/LeadModel.js";
710

811
export const createLead = async (req, res) => {
@@ -52,7 +55,7 @@ export const getLeads = async (req, res) => {
5255
}
5356
console.log("getLeads response is---", response);
5457
res.status(200).send({
55-
message: "Lead Created Successfully",
58+
message: "Lead fetched Successfully",
5659
success: true,
5760
leads: response,
5861
});
@@ -85,3 +88,43 @@ export const deleteLead = async (req, res) => {
8588
});
8689
}
8790
};
91+
92+
export const MarkLeadAsContact = async (req, res) => {
93+
try {
94+
const { id } = req.body;
95+
96+
console.log("inside MarkLeadAsContact----------------", id);
97+
98+
const response = await updateLeadById(id);
99+
100+
console.log("response of update lead is", response);
101+
const lead = await getLeadById(id);
102+
console.log("lead", lead);
103+
const contact = {
104+
firstname: lead.firstname,
105+
lastname: lead.lastname,
106+
phone: lead.phone,
107+
email: lead.email,
108+
category: lead.category,
109+
createdBy: lead.createdBy,
110+
category: "Contact",
111+
status: "Pending Payment",
112+
createdOn: new Date(),
113+
};
114+
console.log("contact", contact);
115+
const contactResult = await insertContact(contact);
116+
117+
console.log("MarkLeadAsContact contactResult is---", contactResult);
118+
119+
res.status(200).send({
120+
message: "Lead updated Successfully",
121+
success: true,
122+
leads: response,
123+
});
124+
} catch (error) {
125+
return res.send({
126+
message: "Something went wrong....Please try again later",
127+
success: false,
128+
});
129+
}
130+
};

controllers/ServiceController.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { insertServiceRequest } from "../models/ServiceModel.js";
2+
3+
export const createServiceRequest = async (req, res) => {
4+
try {
5+
const { email, description, createdBy, priority } = req.body;
6+
const data = {
7+
email: email,
8+
description: description,
9+
createdBy: createdBy,
10+
priority: priority,
11+
createdOn: new Date(),
12+
};
13+
console.log("inside createServiceRequest--", data);
14+
const response = await insertServiceRequest(data);
15+
console.log("createServiceRequest response is---", response);
16+
res.status(200).send({
17+
message: "Service Request Created Successfully",
18+
success: true,
19+
users: response,
20+
});
21+
} catch (error) {
22+
return res.send({
23+
message: "Something went wrong....Please try again later",
24+
success: false,
25+
});
26+
}
27+
};

index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { userRouter } from "./routes/User.js";
66
import http from "http";
77
import { leadRouter } from "./routes/LeadRoute.js";
88
import { contactRouter } from "./routes/ContactRoute.js";
9+
import { serviceRouter } from "./routes/ServiceRoutes.js";
910

1011
const corsOptions = {
1112
origin: "*",
@@ -44,3 +45,4 @@ app.use("/auth", userRouter);
4445
app.use("/lead", leadRouter);
4546

4647
app.use("/contact", contactRouter);
48+
app.use("/service", serviceRouter);

models/ContactModel.js

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

models/LeadModel.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,20 @@ export function deleteLeadById(id) {
5959
.collection("leads")
6060
.deleteOne({ _id: ObjectId(id) });
6161
}
62+
63+
export function updateLeadById(id) {
64+
return client
65+
.db("CRM")
66+
.collection("leads")
67+
.updateOne(
68+
{ _id: ObjectId(id) },
69+
{ $set: { status: "Marked As Contact" } }
70+
);
71+
}
72+
73+
export function getLeadById(id) {
74+
return client
75+
.db("CRM")
76+
.collection("leads")
77+
.findOne({ _id: ObjectId(id) });
78+
}

models/ServiceModel.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import { client } from "../index.js";
2+
3+
export function insertServiceRequest(data) {
4+
console.log("indide create insertServiceRequest");
5+
return client.db("CRM").collection("servicereqs").insertOne(data);
6+
}
7+
8+
export async function fetchServiceRequest(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("servicereq")
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 getManagerServiceRequest(username) {
31+
console.log("inside manager get leads");
32+
return client
33+
.db("CRM")
34+
.collection("users")
35+
.aggregate([
36+
{
37+
$lookup: {
38+
from: "servicereq",
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+
}
54+
55+
export function updateServiceRequestById(id) {
56+
return client
57+
.db("CRM")
58+
.collection("servicereq")
59+
.updateOne(
60+
{ _id: ObjectId(id) },
61+
{ $set: { status: "Marked As Contact" } }
62+
);
63+
}

routes/ContactRoute.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import express from "express";
2+
import {
3+
createContact,
4+
getContacts,
5+
} from "../controllers/ContactController.js";
6+
7+
import { ContactValidation } from "../validations/ContactValidation.js";
8+
9+
const router = express.Router();
10+
router.get("/", (req, res) => {
11+
res.send("Contact route working");
12+
});
13+
14+
router.post("/create", ContactValidation(), createContact);
15+
router.post("/getAll", getContacts);
16+
17+
export const contactRouter = router;

routes/LeadRoute.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
createLead,
44
deleteLead,
55
getLeads,
6+
MarkLeadAsContact,
67
} from "../controllers/LeadController.js";
78

89
import { leadValidation } from "../validations/LeadValidation.js";
@@ -15,5 +16,6 @@ router.get("/", (req, res) => {
1516
router.post("/create", leadValidation(), createLead);
1617
router.post("/getAll", getLeads);
1718
router.delete("/delete", deleteLead);
19+
router.post("/markAsContact", MarkLeadAsContact);
1820

1921
export const leadRouter = router;

routes/ServiceRoutes.js

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

validations/ContactValidation.js

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

0 commit comments

Comments
 (0)
0