-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
206 lines (172 loc) · 5.66 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
process.on('uncaughtException', function (err) {
console.error(err);
console.log("Node NOT Exiting...");
});
require('dotenv').config();
let port; // Declare port as a global variable
const express = require('express');
const { MongoClient, ServerApiVersion, ObjectId } = require('mongodb');
const path = require('path');
const { HttpsProxyAgent } = require('https-proxy-agent');
const app = express();
app.use(express.json());
app.use(express.static(__dirname));
// Get your QuotaGuard URL from the environment variable
const QGTunnel = process.env.QUOTAGUARDSTATIC_URL;
const agent = new HttpsProxyAgent(QGTunnel);
// Get the MongoDB URI from the environment variable
const uri = process.env.MONGODB_URI;
const client = new MongoClient(uri, {
useNewUrlParser: true,
useUnifiedTopology: true
});
async function startServer() {
try {
await client.connect();
console.log("Connected to MongoDB");
port = process.env.PORT || 3000; // Use the PORT environment variable, or 3000 if PORT is not set
app.listen(port, () => {
console.log('App is running on http://localhost:' + port);
});
} catch (err) {
console.error(err);
}
}
startServer();
// Helper function to get a collection
function getCollection(name) {
return client.db("CirculationApp").collection(name);
}
// Serve static files
app.use(express.static(path.join(__dirname, '.')));
// API endpoint for fetching all room data
app.get('/api/rooms', async (req, res) => {
try {
const rooms = await getCollection("spaces").find({}).toArray();
res.json(rooms);
} catch (err) {
console.error(err);
res.status(500).send('Internal server error');
}
});
// API endpoint for fetching room data by room number
app.get('/api/rooms/number/:room_number', async (req, res) => {
try {
const { room_number } = req.params;
const room = await getCollection("spaces").findOne({ room_number: room_number });
if (!room) {
res.status(404).send('Room not found');
return;
}
const studentIds = room.currentAssignees ? room.currentAssignees.map(id => new ObjectId(id)) : [];
const students = await getCollection("students").find({ _id: { $in: studentIds } }).toArray();
// Add students data to the room data
room.students = students;
res.json(room);
} catch (err) {
console.error(err);
res.status(500).send('Internal server error');
}
});
//API endpoint for updating room data
app.put('/api/rooms/number/:room_number', async (req, res) => {
try {
const { room_number } = req.params;
const { room_status, admin_notes, room_notes, currentAssignees, roomHistory } = req.body;
await getCollection("spaces").updateOne(
{ room_number: room_number },
{ $set: { room_status, admin_notes, room_notes, currentAssignees, roomHistory } }
);
res.sendStatus(200);
} catch (err) {
console.error(err);
res.status(500).send('Internal server error');
}
});
// Check if a string is a valid ObjectId
function isValidObjectId(id) {
return /^[0-9a-fA-F]{24}$/.test(id);
}
// API endpoint for fetching student data by IDs
app.get('/api/students/ids/:ids', async (req, res) => {
try {
const { ids } = req.params;
const studentIds = ids.split(',').map(id => parseInt(id)).filter(id => !isNaN(id));
const students = await getCollection("students").find({ pratt_id: { $in: studentIds } }).toArray();
res.json(students);
} catch (err) {
console.error(err);
res.status(500).send('Internal server error');
}
});
// API endpoint for adding a new student
app.post('/api/students', async (req, res) => {
const { first_name, last_name, pratt_id, email, legal_first_name, enrollment_status_current } = req.body;
const student = {
first_name,
last_name,
pratt_id,
email,
legal_first_name,
enrollment_status_current
};
try {
const result = await getCollection("students").insertOne(student);
res.sendStatus(200);
} catch (err) {
console.error(err);
res.status(500).send('Internal server error');
}
});
// API endpoint for adding a new room
app.post('/api/rooms', async (req, res) => {
const room = req.body;
try {
const result = await getCollection("spaces").insertOne(room);
res.sendStatus(200);
} catch (err) {
console.error(err);
res.status(500).send('Internal server error');
}
});
// API endpoint for searching students
app.get('/api/students/search', async (req, res) => {
const { type, term } = req.query;
try {
let query = {};
if (type === 'id') {
query.pratt_id = term;
} else if (type === 'email') {
query.email = term;
} else if (type === 'name') {
query.$or = [{ first_name: term }, { legal_first_name: term }, { last_name: term }];
}
query.enrollment_status_current = true;
const students = await getCollection("students").find(query).limit(25).toArray();
res.json(students);
} catch (err) {
console.error(err);
res.status(500).send('Internal server error');
}
});
// API endpoint for assigning a student to a room
app.put('/api/rooms/:room_id/assignees/:student_id', async (req, res) => {
const { room_id, student_id } = req.params;
try {
const result = await getCollection("spaces").updateOne({ _id: ObjectId(room_id) }, { $push: { currentAssignees: student_id } });
res.sendStatus(200);
} catch (err) {
console.error(err);
res.status(500).send('Internal server error');
}
});
// API endpoint for fetching room types
app.get('/api/room_types', async (req, res) => {
try {
const roomTypes = await getCollection("room_types").find({}).toArray();
res.json(roomTypes);
} catch (err) {
console.error(err);
res.status(500).send('Internal server error');
}
});