-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
337 lines (287 loc) · 9.34 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
const express = require('express');
const app = express();
const pgp = require('pg-promise')();
const db = pgp(process.env.DATABASE_URL || 'postgres://taylorhalsted@localhost:5432/pfaff');
const mustache = require('mustache-express');
const methodOverride = require('method-override');
const bdPars = require('body-parser');
const fetch = require('node-fetch');
const session = require('express-session');
const bcrypt = require('bcryptjs');
const moment = require('moment')
app.engine('html',mustache());
app.set('view engine','html');
app.set('views',__dirname+'/views');
app.use(express.static(__dirname+'/public'));
app.use(methodOverride('_method'));
app.use(bdPars.urlencoded({ extended: false}));
app.use(bdPars.json());
app.use(session({
secret: 'ForeverPuff',
resave: false,
saveUninitialized: true,
cookie: { secure: false }
}))
app.get('/', function(req,res) {
res.render('index')
})
app.get('/login', function(req,res) {
res.render('login')
})
app.post('/login', function(req,res) {
var data = req.body;
db.one(
"SELECT * FROM users WHERE email = $1",
[data.email]
).catch(function(){
res.render('woops');
}).then(function(user){
bcrypt.compare(data.password, user.password_digest, function(err, cmp){
if(cmp) {
req.session.user = user;
res.redirect('/account');
} else {
res.render('woops');
}
})
})
})
app.get('/signup', function(req,res) {
res.render('signup')
})
app.post('/signup', function(req,res) {
var data = req.body;
bcrypt.hash(data.password, 10, function(err, hash){
db.none(
"INSERT INTO users(email, password_digest) VALUES($1, $2)", [data.email, hash]
).catch(function(){
res.render('woops');
}).then(function(){
req.session.user = data;
res.redirect('/account')
})
})
})
app.get('/account', function(req,res) {
var logged_in;
var email;
if(req.session.user){
logged_in = true;
email = req.session.user.email;
}
var data = {
"logged_in": logged_in,
"email": email
}
var json_data = {};
db.any("SELECT * FROM interactions JOIN contacts ON interactions.contact_id = contacts.cont_id JOIN companies ON contacts.company_id = companies.comp_id WHERE companies.user_email = $1 AND (interactions.next_date - CURRENT_DATE) >= 0 AND (interactions.next_date - CURRENT_DATE) < 7 ORDER BY interactions.next_date ASC",[data.email])
.then(function(cData) {
if(cData.length === 0) {
res.redirect('/companies')
} else {
cData.forEach(function(x,y) {
cData[y].interaction_date = moment(x.interaction_date).format("MMMM Do, YYYY | dddd")
cData[y].next_date = moment(x.next_date).format("MMMM Do, YYYY | dddd")
})
var days = []
for(var i = 0; i < 7; i++) {
var date = moment().add(i, 'days').format("MMMM Do, YYYY | dddd");
var holder = [];
cData.forEach(function(z) {
if(z.next_date === date) {
holder.push(z)
}
})
days.push({
day: date,
interactions: holder
})
}
var ans = days.filter(function(a) {
if(a.interactions.length != 0) {
return a;
}
})
json_data.days = ans;
res.render('account', json_data)
}
})
})
app.get('/companies', function(req,res) {
var logged_in;
var email;
if(req.session.user){
logged_in = true;
email = req.session.user.email;
}
var data = {
"logged_in": logged_in,
"email": email
}
db.any("SELECT * FROM companies WHERE user_email = $1 ORDER BY comp_id DESC",[data.email])
.then(function(cData) {
var json_data = {};
if(cData.length != 0) {
json_data.companies = cData;
} else {
json_data.empty = {hustle: "No companies yet, time to hustle."}
}
res.render('companies', json_data)
})
})
app.post('/companies', function(req,res) {
var logged_in;
var email;
if(req.session.user){
logged_in = true;
email = req.session.user.email;
}
var data = {
"logged_in": logged_in,
"email": email
}
var input = req.body;
var address = '';
if (input.url.includes('http')) {
address = input.url;
} else {
address = 'http://'+input.url;
}
db.none("INSERT INTO companies(name, industry, description, url, user_email) VALUES($1, $2, $3, $4, $5)",[input.company, input.industry, input.desc, address, data.email])
.then(function(){
res.redirect('/companies')
})
})
app.put('/companies/:id', function(req,res) {
var logged_in;
var email;
var id = req.params.id;
if(req.session.user){
logged_in = true;
email = req.session.user.email;
}
var data = {
"logged_in": logged_in,
"email": email
}
var input = req.body;
var address = '';
if (input.url.includes('http')) {
address = input.url;
} else {
address = 'http://'+input.url;
}
db.none("UPDATE companies SET name = $1, industry = $2, description = $3, url = $4 WHERE comp_id = $5",[input.company, input.industry, input.desc, address, id])
.then(function(){
res.redirect('/companies')
})
})
app.delete('/companies/:id', function(req,res) {
var id = req.params.id;
db.none("DELETE FROM companies WHERE comp_id = $1",[id])
.then(function(){
res.redirect('/companies')
})
})
app.get('/contacts/:id', function(req,res) {
var id = req.params.id;
var json_data = {};
db.any("SELECT * FROM companies LEFT JOIN contacts ON companies.comp_id = contacts.company_id WHERE companies.comp_id = $1 ORDER BY contacts.cont_id DESC",[id])
.then(function(cData) {
var query = cData[0].name.replace(/ /g,'+');
var search = 'https://api.cognitive.microsoft.com/bing/v5.0/news/search?q='+query+'+business+news&mkt=en-us&category=business';
json_data.company = cData[0].name;
json_data.url = cData[0].url;
json_data.cid = cData[0].comp_id;
if(cData[0].company_id != null) {
json_data.contacts = cData;
} else {
json_data.empty = {hustle: "No contacts yet, time to hustle."}
}
fetch(search, {
method: 'GET',
headers: {'ocp-apim-subscription-key': process.env.API_KEY}
})
.then(function(back) {
return back.json();
}).then(function(json) {
json_data.news = json;
res.render('contacts',json_data)
})
})
})
app.post('/contacts/:id', function(req,res) {
var id = req.params.id;
var input = req.body;
db.none("INSERT INTO contacts(contact_name, company_id, title, email, phone, found_through, note, date_created) VALUES($1, $2, $3, $4, $5, $6, $7, $8)",[input.contact, id, input.title, input.email, input.phone, input.found_through, input.note, moment().format("MMM Do YYYY")])
.then(function(){
res.redirect('/contacts/'+id)
})
})
app.put('/contacts/:id/:cid', function(req,res) {
var id = req.params.id;
var cid = req.params.cid;
var input = req.body;
db.none("UPDATE contacts SET contact_name = $1, title = $2, email = $3, phone = $4, found_through = $5, note = $6 WHERE cont_id = $7",[input.contact, input.title, input.email, input.phone, input.found_through, input.note, id])
.then(function(){
res.redirect('/contacts/'+cid)
})
})
app.delete('/contacts/:id/:cid', function(req,res) {
var id = req.params.id;
var cid = req.params.cid;
db.none("DELETE FROM contacts WHERE cont_id = $1",[id])
.then(function(){
res.redirect('/contacts/'+cid)
})
})
app.get('/interactions/:id', function(req,res) {
var id = req.params.id;
db.any("SELECT * FROM contacts LEFT JOIN interactions ON contacts.cont_id = interactions.contact_id WHERE contacts.cont_id = $1 ORDER BY interactions.interaction_date DESC",[id])
.then(function(cData) {
var json_data = {};
json_data.person = cData[0].contact_name;
json_data.cid = cData[0].cont_id;
if(cData[0].contact_id != null) {
cData.forEach(function(x,y) {
cData[y].interaction_dateShort = moment(x.interaction_date).format("MM/DD/YY")
cData[y].interaction_date = moment(x.interaction_date).format("MMMM Do, YYYY | dddd")
cData[y].next_dateShort = moment(x.next_date).format("MM/DD/YY")
cData[y].next_date = moment(x.next_date).format("MMMM Do, YYYY | dddd")
})
json_data.interactions = cData;
} else {
json_data.empty = {hustle: "No interactions yet, time to hustle."}
}
res.render('interactions', json_data)
})
})
app.post('/interactions/:id', function(req,res) {
var id = req.params.id;
var input = req.body;
db.none("INSERT INTO interactions(contact_id, interaction_date, type, next_step, next_date, notes) VALUES($1, $2, $3, $4, $5, $6)",[id, input.int_date, input.type, input.next, input.next_date, input.note])
.then(function(){
res.redirect('/interactions/'+id)
})
})
app.put('/interactions/:id/:cid', function(req,res) {
var id = req.params.id;
var cid = req.params.cid;
var input = req.body;
db.none("UPDATE interactions SET interaction_date = $1, type = $2, notes = $3, next_step = $4, next_date = $5 WHERE int_id = $6",[input.int_date, input.type, input.note, input.next, input.next_date, id])
.then(function(){
res.redirect('/interactions/'+cid)
})
})
app.delete('/interactions/:id/:cid', function(req,res) {
var id = req.params.id;
var cid = req.params.cid;
db.none("DELETE FROM interactions WHERE int_id = $1",[id])
.then(function(){
res.redirect('/interactions/'+cid)
})
})
const port = process.env.PORT || 3000;
app.listen(port, function(){
console.log('Server alive on port 3000!');
});