-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
383 lines (359 loc) · 10.5 KB
/
app.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
382
383
let express = require("express"),
bodyParser = require("body-parser"),
mongoose = require("mongoose"),
path = require("path"),
multer = require("multer"),
busboyBodyParser = require("busboy-body-parser"),
passport = require("passport"),
passportLocal = require("passport-local"),
passportLocalMongoose = require("passport-local-mongoose"),
flash = require("connect-flash"),
auth = require("./routes/auth"),
clients = require("./routes/clients"),
Order = require("./models/Order"),
Client = require("./models/Client"),
Technician = require("./models/Technician"),
User = require("./models/User"),
moment = require("moment"),
nodemailer = require("nodemailer"),
xoauth2 = require('xoauth2')
techs = require("./routes/techs"),
middleware = require("./middleware"),
app = express();
const storage = multer.diskStorage({
destination: "./public/uploads",
filename: function(req, file, cb) {
cb(
null,
file.fieldname + "-" + Date.now() + path.extname(file.originalname)
);
}
});
// Init Upload
const upload = multer({
storage: storage
}).single("pdfFile");
app.set("view engine", "ejs");
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static("public"));
app.use(busboyBodyParser());
// Database Connect
var url = process.env.DATABASEURL || "mongodb://localhost/better_work_order";
mongoose.connect(url);
// Authentication
app.use(
require("express-session")({
secret: "Muhammad Amir",
resave: false,
saveUninitialized: false
})
);
app.use(passport.initialize());
app.use(passport.session());
passport.use(new passportLocal(User.authenticate()));
passport.serializeUser(User.serializeUser());
passport.deserializeUser(User.deserializeUser());
app.use(flash());
app.use(function(req, res, next) {
res.locals.error = req.flash("error");
res.locals.success = req.flash("success");
next();
});
// Routes
app.use(auth);
app.use(clients);
app.use(techs);
app.get("/orders", middleware.isLoggedIn, (req, res) => {
Order.find({}, (err, orders) => {
if (err) {
alert("Could not load orders");
}
if (req.isAuthenticated()) {
res.render("orders/orders", {
orders: orders,
user: true
});
} else {
res.render("orders/orders", {
orders: orders,
user: false
});
}
});
});
app.get("/orders/new", middleware.isLoggedIn, (req, res) => {
Client.find({}, (err, clients) => {
if (err) {
console.log("Clients not found");
}
Technician.find({}, (err, techs) => {
if (err) {
console.log("Technicians not found");
}
if (req.isAuthenticated()) {
res.render("orders/new", {
clients: clients,
technicians: techs,
user: true
});
} else {
res.render("orders/new", {
clients: clients,
technicians: techs,
user: false
});
}
});
});
});
let currentOrder;
let currentClient;
let currentTech;
app.post("/orders/new", middleware.isLoggedIn, (req, res) => {
const _MS_PER_DAY = 1000 * 60;
let start = new Date(req.body.start);
let stop = new Date(req.body.stop);
var hours = Math.abs(stop - start) / 36e5;
let hoursMinStr = hours.toString();
let minutes =
parseFloat(
"0." +
hoursMinStr.substring(hoursMinStr.indexOf(".") + 1, hoursMinStr.length)
) * 60;
let orderObj = {
title: req.body.title,
start: req.body.start,
stop: req.body.stop,
Services: req.body.services,
Recommendations: req.body.recommendations,
Notes: req.body.notes,
PartsAndMaterials: req.body.partsMaterials,
totalHours: Math.round(hours),
totalMinutes: Math.round(minutes),
dateNow: moment().format("MMMM Do YYYY, h:mm:ss a")
};
if (req.body.clientCheck === "on") {
Client.find({ _id: req.body.clientSelect }, (err, client) => {
if (err) {
console.log("client err");
console.log(err);
}
currentClient = client;
if (req.body.techCheck === "on") {
Technician.find({ _id: req.body.techSelect }, (err2, tech) => {
if (err2) {
console.log("tech err");
console.log(err2);
}
currentTech = tech;
Order.create(orderObj, (err3, order) => {
if (err3) {
console.log("Not Created");
console.log(err3);
}
currentOrder = order;
res.redirect("/review");
});
});
} else {
Technician.create(
{
company: req.body.techCompany,
email: req.body.techEmail,
contactPerson: req.body.techContactPerson,
phone: req.body.techPhone,
address: req.body.techAddress
},
(err, tech) => {
if (err) {
console.log("error line 130");
}
currentTech = tech;
Order.create(orderObj, (err3, order) => {
if (err3) {
console.log("Not Created");
console.log(err3);
}
currentOrder = order;
res.redirect("/review");
});
}
);
}
});
} else {
Client.create(
{
company: req.body.clientCompany,
email: req.body.clientEmail,
contactPerson: req.body.clientContactPerson,
phone: req.body.clientPhone,
address: req.body.clientAddress
},
(err, client) => {
if (err) {
console.log("error line 130");
}
currentClient = client;
if (req.body.techCheck === "on") {
Technician.find({ _id: req.body.techSelect }, (err2, tech) => {
if (err2) {
console.log("tech err");
console.log(err2);
}
currentTech = tech;
Order.create(orderObj, (err3, order) => {
if (err3) {
console.log("Not Created");
console.log(err3);
}
currentOrder = order;
res.redirect("/review");
});
});
} else {
Technician.create(
{
company: req.body.techCompany,
email: req.body.techEmail,
contactPerson: req.body.techContactPerson,
phone: req.body.techPhone,
address: req.body.techAddress
},
(err, tech) => {
if (err) {
console.log("error line 130");
}
currentTech = tech;
Order.create(orderObj, (err3, order) => {
if (err3) {
console.log("Not Created");
console.log(err3);
}
currentOrder = order;
res.redirect("/review");
});
}
);
}
}
);
}
});
app.post("/orders/delete", (req, res) => {
Order.findByIdAndRemove({_id: req.body.id}, (err) => {
if (err){console.log(err)};
})
})
app.get("/review", middleware.isLoggedIn, (req, res) => {
let currentClient1;
if (!currentClient[0]) {
currentClient1 = currentClient;
currentClient = currentClient1;
} else {
currentClient1 = currentClient[0];
currentClient = currentClient1;
}
let currentTech1;
if (!currentTech[0]) {
currentTech1 = currentTech;
currentTech = currentTech1;
} else {
currentTech1 = currentTech[0];
currentTech = currentTech1;
}
if (req.isAuthenticated()) {
res.render("reviewData", {
currentOrder: currentOrder,
currentClient: currentClient,
currentTech: currentTech,
user: true
});
} else {
res.render("reviewData", {
currentOrder: currentOrder,
currentClient: currentClient,
currentTech: currentTech,
user: false
});
}
});
app.get("/sendEmail", middleware.isLoggedIn, (req, res) => {
if (req.isAuthenticated()) {
res.render("sendEmail", {
user: true
});
} else {
res.render("sendEmail", {
user: false
});
}
});
app.post("/sendEmail", middleware.isLoggedIn, (req, res) => {
let receiveList = [];
receiveList.push(currentClient.email);
let email2 = req.body.techCheck === "on" ? currentTech.email : "";
if (email2 !== "") {
receiveList.push(email2);
}
if (req.body.anotherEmail) {
receiveList.push(req.body.anotherEmail);
}
let receivers = "";
receiveList.forEach(receiver => {
receivers = receivers + receiver + ",";
});
receivers = receivers.substring(0, receivers.length - 1);
upload(req, res, err => {
if (err) {
res, render("/sendEmail", { currentOrder: currentOrder });
} else {
const output = `
<p>App Name: Work Order</p>
<p>Attached you will find the data collected by Form from your users.</p>
`;
// create reusable transporter object using the default SMTP transport
let transporter = nodemailer.createTransport({
service: "gmail",
auth: {
type: 'OAuth2',
user: "[email protected]",
clientId: '794334359277-k2e4j0lg9d0pk43e76qj6ubkra0pvmrv.apps.googleusercontent.com',
clientSecret: 'ki4rlkGvSzhNa28iYWNZ15Bt',
refreshToken: '1/Kfp1gn_Pn0RHTCdLom9T63ThbzK_RTZjBjSqYiEBOonMMhWH5aVmbil7QLXt2Pmw'
},
// tls: {
// rejectUnauthorized: false
// }
});
// setup email data with unicode symbols
let mailOptions = {
from: 'Work Order <[email protected]>', // sender address
to: receivers, // list of receivers
subject: "Work Order Contact", // Subject line
text: "Hello world?", // plain text body
html: output, // html body
attachments: [
{
// utf-8 string as an attachment
filename: `${req.body.title}.pdf`,
content: req.files.pdfFile.data
}
]
};
// send mail with defined transport object
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
return console.log(error);
}
console.log("Message sent: %s", info.messageId);
console.log("Preview URL: %s", nodemailer.getTestMessageUrl(info));
res.redirect("/");
});
}
});
});
const PORT = process.env.PORT || 8888;
app.listen(PORT, function() {
console.log("Listening");
});