-
Notifications
You must be signed in to change notification settings - Fork 10
/
app.js
184 lines (154 loc) · 4.57 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
const express = require("express");
const { google } = require("googleapis");
const moment = require("moment");
const mongoose = require("mongoose");
const {
getOAuth2Client,
getGoogleAuthURL,
getGoogleUser,
} = require("./config/google-auth");
const { getFreeIntervals } = require("./utils/intervals");
const User = require("./models/user.model");
const Meeting = require("./models/meeting.model");
require("dotenv").config();
require("./config/mongoose");
const calendar = google.calendar("v3");
const app = express();
app.set("view engine", "ejs");
app.use(express.static(__dirname + "/public"));
app.use(express.json());
const PORT = process.env.PORT;
app.get("/", (req, res) => {
res.render("index");
});
app.get("/auth/google", (req, res) => {
res.redirect(getGoogleAuthURL());
});
app.get("/auth/google/callback", async (req, res) => {
try {
const googleUser = await getGoogleUser(req.query);
const { id, email, name } = googleUser.data;
let user = await User.findOne({ email });
if (!user) {
user = new User({
_id: new mongoose.Types.ObjectId(),
googleId: id,
name,
email,
refresh_token: googleUser.refresh_token,
});
await user.save();
}
res.render("auth", { url: `http://localhost:5001/calendar/${user._id}` });
} catch (err) {
res.status(500).json({ error: err.message });
}
});
app.get("/calendar/:userId", async (req, res) => {
try {
const user = await User.findById(req.params.userId);
const calendarId = user.email;
let selectedDate = req.query.selectedDate;
if (!selectedDate || selectedDate === "null") {
selectedDate = new Date();
} else {
selectedDate = new Date(selectedDate);
}
const data = await calendar.freebusy.query({
auth: getOAuth2Client(user.refresh_token),
requestBody: {
timeMin: moment(selectedDate).startOf("day").format(),
timeMax: moment(selectedDate).endOf("day").format(),
items: [
{
id: calendarId,
},
],
timeZone: "IST",
},
});
const busyIntervals = data.data.calendars[calendarId].busy;
const freeIntervals = getFreeIntervals(selectedDate, busyIntervals);
res.render("calendar", {
freeIntervals: freeIntervals,
busyIntervals: busyIntervals,
selectedDate: selectedDate,
});
} catch (err) {
res.status(500).json({ error: err.message });
}
});
app.get("/meeting/:userId", async (req, res) => {
try {
const user = await User.findById(req.params.userId);
const startTime = new Date(
req.query.selectedInterval.slice(0, -5).concat("+0530")
);
const endTime = moment(startTime).clone().add(30, "m");
const data = await calendar.events.insert({
auth: getOAuth2Client(user.refresh_token),
calendarId: user.email,
requestBody: {
start: {
dateTime: startTime,
},
end: {
dateTime: endTime,
},
attendees: [
{
email: "[email protected]",
displayName: "Test Customer",
},
],
description: "Test sales event",
summary: "Sales Meeting",
},
});
const meetingData = {
_id: mongoose.Types.ObjectId(),
...data.data,
};
const meeting = new Meeting(meetingData);
await meeting.save();
await User.findByIdAndUpdate(req.params.userId, {
$push: { meetings: meetingData._id },
});
res.render("meeting", {
organizer: user.name,
meetingTime: startTime.toString(),
});
} catch (err) {
res.status(500).json({ error: err.message });
}
});
app.get("/admin/meetings", async (req, res) => {
try {
const userId = req.query.userId;
let meetingsData = [];
let userData;
if (userId) {
const populatedUser = await User.findById(userId)
.populate("meetings")
.exec();
userData = {
email: populatedUser.email,
name: populatedUser.name,
};
meetingsData = populatedUser.meetings.map((meeting) => ({
start: new Date(meeting.start.dateTime).toLocaleString(),
end: new Date(meeting.end.dateTime).toLocaleString(),
attendee: meeting.attendees[0].email,
}));
}
const users = (await User.find()).map((user) => ({
name: user.name,
email: user.email,
id: user._id,
}));
return res.render("all-meetings", { users, meetingsData, userData });
} catch (err) {
res.status(500).json({ error: err.message });
}
});
app.listen(PORT, () => console.log(`SERVER RUNNING AT PORT ${PORT}`));