This repository has been archived by the owner on Jan 27, 2021. It is now read-only.
/
my-requests.js
171 lines (146 loc) · 4.77 KB
/
my-requests.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
const express = require('express');
const router = express.Router();
const admin = require('../config/firebase-config-admin');
const firebase = require('../config/firebase-config-client');
const database = admin.firestore();
/* Check if donations are expired */
router.use(async (req, res, next) => {
try {
await database
.collection('donations')
.where('expired', '==', false)
.get()
.then(snapshot => {
snapshot.docs.map(document => {
let donation = document.data();
let donation_id = document.id;
let currentDate = new Date(Date.now());
let expiration_date = donation.expiration_date.toDate();
if (expiration_date < currentDate) {
database
.collection('donations')
.doc(donation_id)
.update({ expired: true });
}
});
});
} catch (error) {
console.log(error);
}
console.log('Expired donation check complete.');
next();
});
/* GET my-requests page */
router.get('/', (req, res) => {
let unsubscribe = firebase.auth().onAuthStateChanged(async user => {
if (!user) {
res.redirect('/');
}
let donations;
await database
.collection('donations')
.where('donatee', '==', user.uid)
.orderBy('request_accepted', 'desc') // if a donation request has been accepted, show it first...
.orderBy('expiration_date')
.get()
.then(snapshot => {
donations = snapshot.docs.map(document => {
let donation = document.data();
donation.id = document.id;
let dietary_restrictions = '';
if (donation.halal) dietary_restrictions += 'Halal ';
if (donation.kosher) dietary_restrictions += 'Kosher ';
if (donation.pescatarian) dietary_restrictions += 'Pescatarian ';
if (donation.vegan) dietary_restrictions += 'Vegan ';
if (donation.vegetarian) dietary_restrictions += 'Vegetarian';
donation.dietary_restrictions = dietary_restrictions;
let date_added = donation.date_added;
let pickup_date = donation.pickup_date;
let expiration_date = donation.expiration_date;
// Firestore timestamp -> JavaScript date object
date_added = date_added.toDate();
pickup_date = pickup_date.toDate();
expiration_date = expiration_date.toDate();
// JavaScript date objects -> DOM strings
date_added = date_added.toLocaleString();
pickup_date = pickup_date.toLocaleString();
expiration_date = expiration_date.toLocaleString();
donation.date_added = date_added;
donation.pickup_date = pickup_date;
donation.expiration_date = expiration_date;
return donation;
});
});
res.render('my-requests', { user: user, donations });
});
unsubscribe();
});
/* POST my-requests page, fulfilling a request, redirecting to my-requests */
router.post('/fulfill', (req, res) => {
let unsubscribe = firebase.auth().onAuthStateChanged(async user => {
if (!user) {
res.redirect('/');
} else {
try {
let donation_id = req.body.donation_id;
await database
.collection('donations')
.doc(donation_id)
.update({
fulfilled_donatee: true,
});
} catch (error) {
res.render('index', { user: user, errorMessage: error });
}
res.redirect('/my-requests');
}
});
unsubscribe();
});
/* POST my-requests page, hiding a request, redirecting to my-requests */
router.post('/hide', (req, res) => {
let unsubscribe = firebase.auth().onAuthStateChanged(async user => {
if (!user) {
res.redirect('/');
} else {
try {
let donation_id = req.body.donation_id;
await database
.collection('donations')
.doc(donation_id)
.update({
hidden_donatee: true,
});
} catch (error) {
res.render('index', { user: user, errorMessage: error });
}
res.redirect('/my-requests');
}
});
unsubscribe();
});
/* POST my-requests page, cancelling a request, redirecting to my-requests */
router.post('/cancel', (req, res) => {
let unsubscribe = firebase.auth().onAuthStateChanged(async user => {
if (!user) {
res.redirect('/');
} else {
try {
let donation_id = req.body.donation_id;
await database
.collection('donations')
.doc(donation_id)
.update({
donatee: null,
requested: false,
request_accepted: false,
});
} catch (error) {
res.render('index', { user: user, errorMessage: error });
}
res.redirect('/my-requests');
}
});
unsubscribe();
});
module.exports = router;