-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
381 lines (300 loc) · 12.5 KB
/
index.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
import express from "express";
import { MongoClient } from "mongodb";
import dotenv from "dotenv";
import bcrypt from "bcrypt";
import jwt from "jsonwebtoken";
import cors from "cors";
import {Check_auth} from "./middleware/auth.js"
import nodemailer from 'nodemailer';
import {EmployeeRoutes} from "./Routes/EmployeeRoutes/EmployeeRoutes.js";
import {TeamLeadRoutes} from "./Routes/TeamLeadRoutes/TeamLeadRoutes.js";
import {AdminRoutes} from "./Routes/AdminRoutes/AdminRoutes.js";
dotenv.config();
const app = express();
const port = process.env.PORT;
const MONGO_URL = process.env.MONGO_URL;
async function createConnection() {
const client = new MongoClient(MONGO_URL);
await client.connect();
console.log("mongo is connected");
return client;
}
export const client = await createConnection();
app.use(express.json()); //middleWare
app.use(cors());
// //password generation
// async function genPassword(password) {
// const salt = await bcrypt.genSalt(10);
// const hashedPassword = await bcrypt.hash(password, salt);
// return hashedPassword;
// }
//login
app.post("/login", async (req, res) => {
try{
const loginUser = req.body;
// console.log(loginUser);
const existingUser = await client.db("CRM")
.collection("accounts")
.findOne({ email: loginUser.email });
if (!existingUser) {
// console.log("not existing user");
return res.status(404).send({message:"invalid credentials"})
}
const role = existingUser.role;
const password = existingUser.password;
const passwordMatch = await bcrypt.compare(loginUser.password, password);
if (!passwordMatch) {
return res.status(406).send({message:"invalid credentials"});
}
// function randomStr(){
// const arr = "123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
// let ans = '';
// for (var i = 10; i > 0; i--) {
// ans +=
// arr[Math.floor(Math.random() * arr.length)];
// }
// return ans;
// }
// const encryption = randomStr();
if (role === "admin") {
const token = jwt.sign({ id: existingUser._id }, process.env.SECRET_ADMIN_KEY);
return res.send({ "role": role,name:existingUser.userName, "token": token });
}
else if(role === "team Lead") {
const token = jwt.sign({ id: existingUser._id }, process.env.SECRET_MANAGER_KEY);
return res.send({ "role": role,name:existingUser.userName, "token": token });
}
else if(role === "employee") {
const token = jwt.sign({ id: existingUser._id }, process.env.SECRET_EMPLOYEE_KEY);
return res.send({ "role": role,name:existingUser.userName, "token": token });
}else{
return res.status(404).send("user role is not defined");
}
}catch(err){
console.log("login route",err.message);
return res.status(500).send({message:"server error,try again later"});
}
});
//loggedInCheck
app.post("/loggedInCheck",Check_auth,async (req,res)=>{
try{
const {loggedInUser} = req.params;
if(loggedInUser){
return res.send({name:loggedInUser.firstName,role:loggedInUser.role});
}
}catch(err){
console.log("loggedInCheck",err.message);
return res.status(500).send({message:"server error"});
}
});
// sign-up
app.post("/signup", async (req, res) => {
try{
const {firstName,lastName,email,company,password,confirm_password,terms_and_conditions} = req.body;
const regex = {name:"^[a-zA-Z0-9 ]{2,}$",
password:"^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[!@#$%^&*])[a-zA-Z0-9!@#$%^&*]{8,}$",
// userName:"^[a-zA-Z0-9@#]{4,16}$",
company:`^[a-zA-Z0-9.'",@ ()-_]{1,}$`,
email:"^[a-z0-9._%+-]+@[a-z0-9.-]+.[a-z]{2,4}$",
};
if(!new RegExp(regex.name).test(firstName)){
return res.status(404).send({message:"Valid First Name is required",field:"firstName"});
}
if(!new RegExp(regex.name).test(lastName)){
return res.status(404).send({message:"Valid Last Name is required",field:"lastName"});
}
if(!new RegExp(regex.email).test(email)){
return res.status(404).send({message:"Valid Email is required",field:"email"});
}
if(!new RegExp(regex.company).test(company)){
return res.status(404).send({message:"Company Name is required",field:"company"});
}
if(!new RegExp(regex.password).test(password)){
return res.status(404).send({message:"Valid Password is required",field:"password"});
}
if(!new RegExp(regex.password).test(confirm_password)){
return res.status(404).send({message:"Passwords Mis-Matched",field:"confirm_password"});
}
if(terms_and_conditions !== true ){
return res.status(404).send({message:"You should Agree to our Terms and Conditions",field:"terms_and_conditions"});
}
const newUser = {firstName,lastName,email,company,password,terms_and_conditions};
//--- check for existing mail -------------------------
const existingMail = await client.db("CRM")
.collection("accounts")
.findOne({ email: newUser.email});
if(existingMail){
return res.status(400).send({message:"email already exists",field:"email"});
}
//-------------------------------------------------------
//--- check for existing company -------------------------
const existingCompany = await client.db("CRM")
.collection("accounts")
.findOne({ company: newUser.company});
if(existingCompany){
return res.status(400).send({message:"Only your company's ADMIN can make you as ADMIN",field:"company"});
}
//-------------------------------------------------------
//--- hashing password --------------------------------
newUser.password = await genPassword(newUser.password);
//---------------------------------------------------------
//--- createUserName --------------------------------------
const userName = await createUserName(newUser);
//---------------------------------------------------------
if(userName){
const joinedDate = new Date(Date.now()).toDateString().split(" ").slice(1,4).join("/");
//--- adding new user -------------------------------------
const result = await client.db("CRM")
.collection("accounts")
.insertOne({...newUser,role:"admin",userName,joinedDate,notes:""});
if(result){
return res.send(result);
}else{
return res.status(500).send({message:"server error,please try again later"});
}
//----------------------------------------------------------
}else{
return res.status(500).send({message:"server error,please try again later"});
}
}catch(err){
console.log("signup error",err.message);
return res.status(500).send({message:"server error,try again later"});
}
})
// forgot password
app.post("/forgotPassword", async (req, res) => {
const {role,userName,email} = req.body;
// if(new_password){
// async function genPassword(password) {
// const salt = await bcrypt.genSalt(10);
// const hashedPassword = await bcrypt.hash(password, salt);
// return hashedPassword;
// }
// const password = await genPassword(new_password);
// const passwordUpdate = await client.db("userDB")
// .collection("employees")
// .updateOne({email:user_email},{$set:{password:password}});
// if(passwordUpdate){
// // console.log("password updated");
// return res.send(passwordUpdate);
// }
// // console.log("password couldnt update");
// return res.status(400).send("couldnt update");
// }
let existingUser;
if(role === "admin"){
existingUser = await client.db("CRM")
.collection("admins")
.findOne({ userName:userName,email:email });
}
if(role === "manager"){
existingUser = await client.db("CRM")
.collection("managers")
.findOne({ userName:userName,email:email });
}
if(role === "employee"){
existingUser = await client.db("CRM")
.collection("employees")
.findOne({ userName:userName,email:email });
}
if(!existingUser){
return res.status(400).send({email:email ,userName:userName});
}else{
await otpMailer(email,userName,res);
}
});
// password - reset have to work
app.post("/passwordReset",async(req,res)=>{
})
app.use("/employee",EmployeeRoutes);
app.use("/teamLead",TeamLeadRoutes);
app.use("/admin",AdminRoutes);
app.listen(port, console.log("server started at port " + port));
async function otpMailer(email,userName, res) {
const transport = nodemailer.createTransport({
service: 'gmail',
auth: {
type: 'OAuth2',
user: process.env.MAIL_ID,
pass: process.env.MAIL_PASS,
clientId: process.env.CLIENT_ID,
clientSecret: process.env.CLIENT_SECRET,
refreshToken: process.env.TOKEN_URI
}
});
const passwordResetPage = `https://localhost:3000/password-reset/`
var mailOptions = {
from: '"RV`s CRM TEAM" <[email protected]>',
to: email,
subject: 'OTP FOR MAIL CONFIRMATION',
html: `<b>Dear CRM User</b>,<br/><br/> The password reset link is given below.<br/><b>${passwordResetPage}</b><br/>
<p>PLEASE KEEP YOUR PASSWORD SAFE AND SECURE !!!</p><br/>
<img style="width:200px;height:200px;object-fit:contain;" src="https://www.puffin.com/imgs/img_secure_security.gif"></img>`
};
transport.sendMail(mailOptions, async (error) => {
if (error) {
return res.status(400).send({email,userName});
}
return res.send({email,userName});
});
}
export async function genPassword(password) {
const salt = await bcrypt.genSalt(10);
const hashedPassword = await bcrypt.hash(password, salt);
return hashedPassword;
};
// console.log(Date.now());
// console.log(Date.now().toString().slice(7,13));
export async function createUserName(newUser){
let userName = `${newUser.firstName[0]}${newUser.lastName[0]}-${Date.now().toString().slice(7,13)}`;
const isUserExist = await client.db("CRM")
.collection("accounts")
.findOne({userName});
if(isUserExist){
userName = await createUserName(newUser);
}else{
return userName;
}
}
// console.log(Date.parse(new Date(Date.now())),Date.now(),new Date("2022-02-06"));
// console.log(new Date().toDateString().split(" "));
// export function giveDate(date) {
// let ActualDate = new Date(date).toDateString().split(" ");
// ActualDate.shift();
// ActualDate.pop();
// return ActualDate.reverse().join(" ");
// }
// console.log(Date.parse(new Date("2022-05-16")));
// console.log(1000*60*60*24);
// console.log(Date.parse(new Date("2022-05-15")));
// console.log(new Date("2022-05-15"));
// console.log();
// console.log(new Date(new Date()- (-5.5*(1000*60*60))) - (new Date("2022-05-15")));
// console.log(Date.parse(new Date("2022-05-16")) - (new Date()- (-5.5*(1000*60*60))));
// console.log(new Date());
// console.log(giveDate("2022-05-15"));
// export function giveDate(date,withYear) {
// let ActualDate = new Date(date).toDateString().split(" ");
// ActualDate.shift();
// if(withYear === "withYear"){
// return ActualDate.join("/");
// }
// ActualDate.pop();
// return ActualDate.reverse().join(" ");
// }
// console.log(giveDate("2022-05-15T18:30:00.000Z"));
// export function dateColor(date){
// let difference = new Date(date) - new Date();
// if(difference <= (1000*60*60*24)){
// return "error"
// }
// else if (difference <= (1000*60*60*24*3)){
// return "warning";
// }
// else{
// return "primary";
// }
// }
// console.log(giveDate(new Date(),"withYear"));
// console.log(new Date("2022-05-15T18:30:00.000Z"), new Date());
// console.log(dateColor("2022-05-15T18:30:00.000Z"));