-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalander.js
212 lines (191 loc) · 5.75 KB
/
calander.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
import { google } from "googleapis";
import fs from "fs";
import path from "path";
import {
parse,
addHours,
isValid,
startOfHour,
addDays,
setHours,
} from "date-fns";
const SERVICE_ACCOUNT_PATH = "auth.json";
const TIME_ZONE = "America/New_York";
const APPOINTMENT_DURATION_HOURS = 1;
const CALENDAR_ID = "primary";
const CHECK_DAYS_AHEAD = 7;
const BUSINESS_START_HOUR = 9; // Business hours start at 9 AM
const BUSINESS_END_HOUR = 17; // Business hours end at 5 PM
// Load service account credentials
const serviceAccountCredentials = JSON.parse(
fs.readFileSync(path.join(SERVICE_ACCOUNT_PATH), "utf8")
);
// Google Calendar API client setup
const auth = new google.auth.JWT(
serviceAccountCredentials.client_email,
null,
serviceAccountCredentials.private_key,
[
"https://www.googleapis.com/auth/calendar",
"https://www.googleapis.com/auth/calendar.events",
"https://www.googleapis.com/auth/admin.directory.resource.calendar",
"https://www.googleapis.com/auth/cloud-platform",
],
"111370419970452146902"
);
const calendar = google.calendar({ version: "v3", auth });
export const checkDateTimeAvailability = async (
date,
numberOfDays = CHECK_DAYS_AHEAD
) => {
let availableSlots = [];
let currentDate = startOfHour(new Date(date)); // Start from the beginning of the hour of the provided date
for (let day = 0; day < numberOfDays; day++) {
for (let hour = BUSINESS_START_HOUR; hour < BUSINESS_END_HOUR; hour++) {
let startDateTime = setHours(addDays(currentDate, day), hour);
let endDateTime = addHours(startDateTime, APPOINTMENT_DURATION_HOURS);
try {
const response = await calendar.freebusy.query({
requestBody: {
timeMin: startDateTime.toISOString(),
timeMax: endDateTime.toISOString(),
items: [{ id: CALENDAR_ID }],
timeZone: TIME_ZONE,
},
});
if (response.data.calendars[CALENDAR_ID].busy.length === 0) {
availableSlots.push(startDateTime);
}
} catch (error) {
console.error("Error checking availability:", error);
throw new Error("Failed to check availability");
}
}
}
return availableSlots;
};
// Function to create a calendar event with a limit on recursion for finding available slots
export const createAppointment = async (
dateTime,
summary,
description,
email,
attempt = 0,
maxAttempts = 10 // You can adjust the maxAttempts based on your requirement
) => {
const isAvailable = await checkDateTimeAvailability(dateTime);
if (!isAvailable && attempt < maxAttempts) {
console.log(
"Time slot not available, looking for the next available slot..."
);
const nextAttempt = addHours(dateTime, APPOINTMENT_DURATION_HOURS);
return createAppointment(
nextAttempt,
summary,
description,
email,
attempt + 1
);
} else if (attempt >= maxAttempts) {
throw new Error("No available slots found within the attempt limit.");
}
const event = {
summary,
description,
start: { dateTime: dateTime.toISOString(), timeZone: TIME_ZONE },
end: {
dateTime: addHours(dateTime, APPOINTMENT_DURATION_HOURS).toISOString(),
timeZone: TIME_ZONE,
},
attendees: [{ email }],
};
try {
const { data } = await calendar.events.insert({
calendarId: CALENDAR_ID,
resource: event,
});
return data.htmlLink;
} catch (error) {
console.error("Error booking appointment:", error);
throw new Error("Failed to book appointment");
}
};
// Function to setup a meeting
export const setupMeeting = async (date, time, summary, description, email) => {
const dateTime = parseDateTime(date, time);
try {
const bookingLink = await createAppointment(
dateTime,
summary,
description,
email
);
console.log(
`Meeting successfully scheduled, you can join using this link: ${bookingLink}`
);
return bookingLink;
} catch (error) {
console.error("Failed to schedule the meeting:", error);
}
};
const parseDateTime = (date, time) => {
const dateTimeFormats = [
"dd/MM/yyyy HH:mm",
"dd/MM/yyyy hh:mm a",
"dd-MM-yyyy HH:mm",
"dd-MM-yyyy hh:mm a",
"yyyy-MM-dd'T'HH:mm:ssX",
];
for (const formatString of dateTimeFormats) {
const parsedDate = parse(`${date} ${time}`, formatString, new Date());
if (isValid(parsedDate)) {
return parsedDate;
}
}
throw new Error("Invalid date-time value");
};
// Function to send meeting details to user's email
export const sendMeetingDetails = async (
meetingLink,
email,
meetingDetails
) => {
const nodemailer = require("nodemailer");
// Create a transport object
const transporter = nodemailer.createTransport({
host: "smtp.gmail.com",
port: 587,
secure: false, // or 'STARTTLS'
auth: {
user: "[email protected]",
pass: "Gil@sapir2309",
},
});
// Define the email message
const mailOptions = {
from: "[email protected]",
to: email,
subject: "Meeting Details",
html: `
<p>Dear Participant,</p>
<p>We are pleased to invite you to the following meeting:</p>
<p><strong>Meeting Details:</strong></p>
<ul>
<li><strong>Date:</strong> ${meetingDetails.date}</li>
<li><strong>Time:</strong> ${meetingDetails.time}</li>
<li><strong>Agenda:</strong> ${meetingDetails.agenda}</li>
</ul>
<p>You can join the meeting using this link: <a href="${meetingLink}">${meetingLink}</a></p>
<p>We look forward to your participation.</p>
`,
};
// Send the email
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
return console.log(error);
}
console.log("Email sent: " + info.response);
return info.response;
});
};