-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
198 lines (193 loc) · 5.82 KB
/
index.js
File metadata and controls
198 lines (193 loc) · 5.82 KB
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
const express = require("express");
const fs = require("fs");
const app = express();
app.set("view engine", "ejs");
const bcrypt = require("bcryptjs");
const jwt = require("jsonwebtoken");
const mongoose = require("mongoose");
const joi = require("joi");
const session = require("express-session");
const bodyParser = require("body-parser");
app.use(express.static(`${__dirname}/public/`));
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
const userModel = require("./models/data");
const invoiceModel = require("./models/invoicedata");
const { signupAuth, loginAuth, invoiceAuth, Access } = require("./functions");
//location of Authorisation
var urlencodedParser = bodyParser.urlencoded({ extended: false });
if (typeof localStorage === "undefined" || localStorage === null) {
var LocalStorage = require("node-localstorage").LocalStorage;
localStorage = new LocalStorage("./scratch");
}
app.use(
session({
secret: "InvoiceGenerator",
resave: false,
saveUninitialized: true,
cookie: { secure: true },
})
);
let port = process.env.port || 5000;
app.get("/signup", (req, res) => {
res.render("signup.ejs", { title: "My Invoice", msg: "", error: "" });
});
app.post("/signup", urlencodedParser, async (req, res, next) => {
try {
const value = await signupAuth.validateAsync(req.body);
var Password = value.Password;
var cryptPassword = bcrypt.hashSync(Password, 10);
var regData = new userModel({
email: value.Email,
password: cryptPassword,
createdAt: Date(),
});
regData.save((err, doc) => {
if (err) throw err;
res.render("signup.ejs", {
title: "My Invoice",
error: "",
msg: "User Registered Successfully",
});
});
} catch (err) {
console.log(err);
res.render("signup.ejs", { title: "My Invoice", error: err, msg: "" });
}
});
app.get("/", (req, res) => {
res.render("index.ejs", { title: "My Invoice", msg: "", error: "" });
});
app.post("/", async (req, res, next) => {
try {
const result = await loginAuth.validateAsync(req.body);
var getuser = userModel.findOne({ email: result.Email });
getuser.exec((error, data) => {
if (data === null) {
res.render("index.ejs", {
title: "Login",
msg: "",
error: "Invalid User Or Password !!",
});
} else {
if (error) throw new error();
var getId = data._id;
var getPass = data.password;
var user = data.email;
bcrypt.compare(result.Password, getPass, (err, doc) => {
if (err) throw err;
if (doc) {
var token = jwt.sign({ userID: getId }, "loginToken");
localStorage.setItem("UserToken", token);
localStorage.setItem("loginUser", result.Email);
req.session.Email = result.Email;
res.render("dashboard.ejs", {
title: "My Invoice App",
username: req.session.Email,
msg: "Welcome to Dashboard",
error: "",
});
} else
res.render("index.ejs", {
title: "My Invoice App",
msg: " ",
error: "Invalid Id or Password",
});
});
}
});
} catch (error) {
res.render("login", { title: "My Invoice App", msg: "", error: error });
}
});
app.get("/create", Access, (req, res, next) => {
res.render("invoice.ejs", { title: "My Invoice App", msg: "", error: "" });
});
app.post("/create", Access, async (req, res, next) => {
try {
const result = await invoiceAuth.validateAsync(req.body);
const { clientName } = req.body;
const { clientAddress } = req.body;
const { invoiceNumber } = req.body;
const { invoiceDate } = req.body;
const { subTotal } = req.body;
const { gst } = req.body;
const { totalAmount } = req.body;
const { advance } = req.body;
const { balancedue } = req.body;
let product = {};
const invoiceEntry = new invoiceModel({
clientName,
clientAddress,
invoiceNumber,
invoiceDate,
subTotal,
gst,
totalAmount,
advance,
balancedue,
product: {
itemName: req.body.itemName,
description: req.body.description,
itemPrice: req.body.itemPrice,
qty: req.body.qty,
price: req.body.Price,
},
});
if (product.length > 0) {
product.forEach((pdt) => {
product = pdt;
});
}
invoiceEntry.save((err, doc) => {
if (err) throw err;
res.render("invoice", {
title: "MyInvoice App",
msg: "Invoice generated successfully",
error: "",
});
});
} catch (error) {
res.render("invoice", { title: "My Invoice App", msg: "", error: error });
}
});
app.get("/invoicedetails", Access, (req, res, next) => {
const options = {
page: 1,
limit: 5,
};
invoiceModel.paginate({}, options).then((result) => {
res.render("invoiceList.ejs", {
title: "My Invoice App",
msg: "",
records: result.docs,
current: result.offset,
pages: Math.ceil(result.total / result.limit),
});
});
});
app.get("/delete/:id", Access, (req, res, next) => {
var id = req.params.id;
var del = invoiceModel.findByIdAndDelete(id);
del.exec(function (err, data) {
if (err) throw err;
res.redirect("/invoicedetails");
});
});
app.get("/invoice/:id", Access, (req, res, next) => {
const { id } = req.params;
const invoice = invoiceModel.findById(id);
invoice.exec((err, data) => {
if (err) throw err;
else {
res.render("finalInvoice.ejs", { title: "View bill", records: data });
}
});
});
app.get("/logout", (req, res) => {
req.session.destroy(function (err) {
if (err) throw err;
else res.render("logout.ejs", { title: "My Invoice App" });
});
});
app.listen(port, () => console.log("server running on " + port));