-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper.js
61 lines (54 loc) · 2.16 KB
/
helper.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import { client } from "./index.js";
import { ObjectId } from "mongodb";
import bcrypt from "bcrypt";
// password generation
async function generatePassword(password) {
const NO_OF_ROUNDS = 10;
const salt = await bcrypt.genSalt(NO_OF_ROUNDS);
const hashedPassword = await bcrypt.hash(password, salt)
return hashedPassword;
}
//Users functions
async function getAllUsers() {
return await client.db("crm").collection("users").find({}).toArray()
}
async function deleteUser(data) {
return await client.db("crm").collection("users").deleteOne({ _id: ObjectId(data) })
}
async function updateUserById(id, role) {
return await client.db("crm").collection("users").updateOne({ _id: ObjectId(id) }, { $set: { role: role } })
}
// Leads functions
async function getAllLeads() {
return await client.db("crm").collection("leads").find({}).toArray();
}
async function createLeadsById(data) {
return await client.db("crm").collection("leads").insertOne(data);
}
async function deleteLead(data) {
return await client.db("crm").collection("leads").deleteOne({ _id: ObjectId(data) })
}
async function updateLeadsById(id, status) {
return await client.db("crm").collection("leads").updateOne({ _id: ObjectId(id) }, { $set: { status: status } })
}
//Requests functions
async function getAllRequests() {
return await client.db("crm").collection("requests").find({}).toArray()
}
async function createRequestById(data) {
return await client.db("crm").collection("requests").insertOne(data);
}
async function deleteRequest(data) {
return await client.db("crm").collection("requests").deleteOne({ _id: ObjectId(data) });
}
async function updateRequestsById(id, status) {
return await client.db("crm").collection("requests").updateOne({ _id: ObjectId(id) }, { $set: { status: status } })
}
// Admin functions
async function getAdmins() {
return await client.db("crm").collection("users").find({ role: { $in: ["Admin", "Manager"] } }).toArray()
}
export {
createLeadsById, generatePassword, getAllLeads, getAllUsers, deleteLead, updateLeadsById, deleteUser, updateUserById,
getAllRequests, createRequestById, deleteRequest, updateRequestsById, getAdmins
}