Merge branch 'main' into feature/zoom-integration
# Conflicts: # lib/calendarClient.ts # lib/emails/new-event.ts # pages/[user]/book.tsx # pages/api/availability/[user].ts # pages/api/book/[user].ts # pages/integrations/index.tsx
This commit is contained in:
commit
ebc42f0c96
25 files changed
+752
-250
No files matched your search
+188
-127
@@ -74,6 +74,13 @@ interface CalendarEvent {
|
||||
attendees: Person[];
|
||||
};
|
||||
|
||||
interface IntegrationCalendar {
|
||||
integration: string;
|
||||
primary: boolean;
|
||||
externalId: string;
|
||||
name: string;
|
||||
}
|
||||
|
||||
interface CalendarApiAdapter {
|
||||
createEvent(event: CalendarEvent): Promise<any>;
|
||||
|
||||
@@ -81,7 +88,9 @@ interface CalendarApiAdapter {
|
||||
|
||||
deleteEvent(uid: String);
|
||||
|
||||
getAvailability(dateFrom, dateTo): Promise<any>;
|
||||
getAvailability(dateFrom, dateTo, selectedCalendars: IntegrationCalendar[]): Promise<any>;
|
||||
|
||||
listCalendars(): Promise<IntegrationCalendar[]>;
|
||||
}
|
||||
|
||||
const MicrosoftOffice365Calendar = (credential): CalendarApiAdapter => {
|
||||
@@ -120,98 +129,127 @@ const MicrosoftOffice365Calendar = (credential): CalendarApiAdapter => {
|
||||
}
|
||||
};
|
||||
|
||||
return {
|
||||
getAvailability: (dateFrom, dateTo) => {
|
||||
const payload = {
|
||||
schedules: [credential.key.email],
|
||||
startTime: {
|
||||
dateTime: dateFrom,
|
||||
timeZone: 'UTC',
|
||||
},
|
||||
endTime: {
|
||||
dateTime: dateTo,
|
||||
timeZone: 'UTC',
|
||||
},
|
||||
availabilityViewInterval: 60
|
||||
};
|
||||
const integrationType = "office365_calendar";
|
||||
|
||||
return auth.getToken().then(
|
||||
(accessToken) => fetch('https://graph.microsoft.com/v1.0/me/calendar/getSchedule', {
|
||||
method: 'post',
|
||||
headers: {
|
||||
'Authorization': 'Bearer ' + accessToken,
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify(payload)
|
||||
})
|
||||
.then(handleErrorsJson)
|
||||
.then(responseBody => {
|
||||
return responseBody.value[0].scheduleItems.map((evt) => ({
|
||||
start: evt.start.dateTime + 'Z',
|
||||
end: evt.end.dateTime + 'Z'
|
||||
}))
|
||||
})
|
||||
).catch((err) => {
|
||||
console.log(err);
|
||||
});
|
||||
},
|
||||
createEvent: (event: CalendarEvent) => auth.getToken().then(accessToken => fetch('https://graph.microsoft.com/v1.0/me/calendar/events', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Authorization': 'Bearer ' + accessToken,
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify(translateEvent(event))
|
||||
}).then(handleErrorsJson).then((responseBody) => ({
|
||||
...responseBody,
|
||||
disableConfirmationEmail: true,
|
||||
}))),
|
||||
deleteEvent: (uid: String) => auth.getToken().then(accessToken => fetch('https://graph.microsoft.com/v1.0/me/calendar/events/' + uid, {
|
||||
method: 'DELETE',
|
||||
headers: {
|
||||
'Authorization': 'Bearer ' + accessToken
|
||||
}
|
||||
}).then(handleErrorsRaw)),
|
||||
updateEvent: (uid: String, event: CalendarEvent) => auth.getToken().then(accessToken => fetch('https://graph.microsoft.com/v1.0/me/calendar/events/' + uid, {
|
||||
method: 'PATCH',
|
||||
headers: {
|
||||
'Authorization': 'Bearer ' + accessToken,
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify(translateEvent(event))
|
||||
}).then(handleErrorsRaw)),
|
||||
}
|
||||
function listCalendars(): Promise<IntegrationCalendar[]> {
|
||||
return auth.getToken().then(accessToken => fetch('https://graph.microsoft.com/v1.0/me/calendars', {
|
||||
method: 'get',
|
||||
headers: {
|
||||
'Authorization': 'Bearer ' + accessToken,
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
}).then(handleErrorsJson)
|
||||
.then(responseBody => {
|
||||
return responseBody.value.map(cal => {
|
||||
const calendar: IntegrationCalendar = {
|
||||
externalId: cal.id, integration: integrationType, name: cal.name, primary: cal.isDefaultCalendar
|
||||
}
|
||||
return calendar;
|
||||
});
|
||||
})
|
||||
)
|
||||
}
|
||||
|
||||
return {
|
||||
getAvailability: (dateFrom, dateTo, selectedCalendars) => {
|
||||
const filter = "?$filter=start/dateTime ge '" + dateFrom + "' and end/dateTime le '" + dateTo + "'"
|
||||
return auth.getToken().then(
|
||||
(accessToken) => {
|
||||
const selectedCalendarIds = selectedCalendars.filter(e => e.integration === integrationType).map(e => e.externalId);
|
||||
if (selectedCalendarIds.length == 0 && selectedCalendars.length > 0){
|
||||
// Only calendars of other integrations selected
|
||||
return Promise.resolve([]);
|
||||
}
|
||||
|
||||
return (selectedCalendarIds.length == 0
|
||||
? listCalendars().then(cals => cals.map(e => e.externalId))
|
||||
: Promise.resolve(selectedCalendarIds).then(x => x)).then((ids: string[]) => {
|
||||
const urls = ids.map(calendarId => 'https://graph.microsoft.com/v1.0/me/calendars/' + calendarId + '/events' + filter)
|
||||
return Promise.all(urls.map(url => fetch(url, {
|
||||
method: 'get',
|
||||
headers: {
|
||||
'Authorization': 'Bearer ' + accessToken,
|
||||
'Prefer': 'outlook.timezone="Etc/GMT"'
|
||||
}
|
||||
})
|
||||
.then(handleErrorsJson)
|
||||
.then(responseBody => responseBody.value.map((evt) => ({
|
||||
start: evt.start.dateTime + 'Z',
|
||||
end: evt.end.dateTime + 'Z'
|
||||
}))
|
||||
))).then(results => results.reduce((acc, events) => acc.concat(events), []))
|
||||
})
|
||||
}
|
||||
).catch((err) => {
|
||||
console.log(err);
|
||||
});
|
||||
},
|
||||
createEvent: (event: CalendarEvent) => auth.getToken().then(accessToken => fetch('https://graph.microsoft.com/v1.0/me/calendar/events', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Authorization': 'Bearer ' + accessToken,
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify(translateEvent(event))
|
||||
}).then(handleErrorsJson).then((responseBody) => ({
|
||||
...responseBody,
|
||||
disableConfirmationEmail: true,
|
||||
}))),
|
||||
deleteEvent: (uid: String) => auth.getToken().then(accessToken => fetch('https://graph.microsoft.com/v1.0/me/calendar/events/' + uid, {
|
||||
method: 'DELETE',
|
||||
headers: {
|
||||
'Authorization': 'Bearer ' + accessToken
|
||||
}
|
||||
}).then(handleErrorsRaw)),
|
||||
updateEvent: (uid: String, event: CalendarEvent) => auth.getToken().then(accessToken => fetch('https://graph.microsoft.com/v1.0/me/calendar/events/' + uid, {
|
||||
method: 'PATCH',
|
||||
headers: {
|
||||
'Authorization': 'Bearer ' + accessToken,
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify(translateEvent(event))
|
||||
}).then(handleErrorsRaw)),
|
||||
listCalendars
|
||||
}
|
||||
};
|
||||
|
||||
const GoogleCalendar = (credential): CalendarApiAdapter => {
|
||||
const myGoogleAuth = googleAuth();
|
||||
myGoogleAuth.setCredentials(credential.key);
|
||||
return {
|
||||
getAvailability: (dateFrom, dateTo) => new Promise((resolve, reject) => {
|
||||
const calendar = google.calendar({version: 'v3', auth: myGoogleAuth});
|
||||
calendar.calendarList
|
||||
.list()
|
||||
.then(cals => {
|
||||
calendar.freebusy.query({
|
||||
requestBody: {
|
||||
timeMin: dateFrom,
|
||||
timeMax: dateTo,
|
||||
items: cals.data.items
|
||||
}
|
||||
}, (err, apires) => {
|
||||
if (err) {
|
||||
reject(err);
|
||||
}
|
||||
resolve(
|
||||
Object.values(apires.data.calendars).flatMap(
|
||||
(item) => item["busy"]
|
||||
)
|
||||
)
|
||||
});
|
||||
})
|
||||
.catch((err) => {
|
||||
reject(err);
|
||||
});
|
||||
const myGoogleAuth = googleAuth();
|
||||
myGoogleAuth.setCredentials(credential.key);
|
||||
const integrationType = "google_calendar";
|
||||
|
||||
return {
|
||||
getAvailability: (dateFrom, dateTo, selectedCalendars) => new Promise((resolve, reject) => {
|
||||
const calendar = google.calendar({version: 'v3', auth: myGoogleAuth});
|
||||
calendar.calendarList
|
||||
.list()
|
||||
.then(cals => {
|
||||
const filteredItems = cals.data.items.filter(i => selectedCalendars.findIndex(e => e.externalId === i.id) > -1)
|
||||
if (filteredItems.length == 0 && selectedCalendars.length > 0){
|
||||
// Only calendars of other integrations selected
|
||||
resolve([]);
|
||||
}
|
||||
calendar.freebusy.query({
|
||||
requestBody: {
|
||||
timeMin: dateFrom,
|
||||
timeMax: dateTo,
|
||||
items: filteredItems.length > 0 ? filteredItems : cals.data.items
|
||||
}
|
||||
}, (err, apires) => {
|
||||
if (err) {
|
||||
reject(err);
|
||||
}
|
||||
|
||||
resolve(
|
||||
Object.values(apires.data.calendars).flatMap(
|
||||
(item) => item["busy"]
|
||||
)
|
||||
)
|
||||
});
|
||||
})
|
||||
.catch((err) => {
|
||||
reject(err);
|
||||
});
|
||||
|
||||
}),
|
||||
createEvent: (event: CalendarEvent) => new Promise((resolve, reject) => {
|
||||
@@ -277,39 +315,55 @@ const GoogleCalendar = (credential): CalendarApiAdapter => {
|
||||
payload['location'] = event.location;
|
||||
}
|
||||
|
||||
const calendar = google.calendar({version: 'v3', auth: myGoogleAuth});
|
||||
calendar.events.update({
|
||||
auth: myGoogleAuth,
|
||||
calendarId: 'primary',
|
||||
eventId: uid,
|
||||
sendNotifications: true,
|
||||
sendUpdates: 'all',
|
||||
resource: payload
|
||||
}, function (err, event) {
|
||||
if (err) {
|
||||
console.log('There was an error contacting the Calendar service: ' + err);
|
||||
return reject(err);
|
||||
}
|
||||
return resolve(event.data);
|
||||
});
|
||||
}),
|
||||
deleteEvent: (uid: String) => new Promise((resolve, reject) => {
|
||||
const calendar = google.calendar({version: 'v3', auth: myGoogleAuth});
|
||||
calendar.events.delete({
|
||||
auth: myGoogleAuth,
|
||||
calendarId: 'primary',
|
||||
eventId: uid,
|
||||
sendNotifications: true,
|
||||
sendUpdates: 'all',
|
||||
}, function (err, event) {
|
||||
if (err) {
|
||||
console.log('There was an error contacting the Calendar service: ' + err);
|
||||
return reject(err);
|
||||
}
|
||||
return resolve(event.data);
|
||||
});
|
||||
})
|
||||
};
|
||||
const calendar = google.calendar({version: 'v3', auth: myGoogleAuth});
|
||||
calendar.events.update({
|
||||
auth: myGoogleAuth,
|
||||
calendarId: 'primary',
|
||||
eventId: uid,
|
||||
sendNotifications: true,
|
||||
sendUpdates: 'all',
|
||||
resource: payload
|
||||
}, function (err, event) {
|
||||
if (err) {
|
||||
console.log('There was an error contacting the Calendar service: ' + err);
|
||||
return reject(err);
|
||||
}
|
||||
return resolve(event.data);
|
||||
});
|
||||
}),
|
||||
deleteEvent: (uid: String) => new Promise( (resolve, reject) => {
|
||||
const calendar = google.calendar({version: 'v3', auth: myGoogleAuth});
|
||||
calendar.events.delete({
|
||||
auth: myGoogleAuth,
|
||||
calendarId: 'primary',
|
||||
eventId: uid,
|
||||
sendNotifications: true,
|
||||
sendUpdates: 'all',
|
||||
}, function (err, event) {
|
||||
if (err) {
|
||||
console.log('There was an error contacting the Calendar service: ' + err);
|
||||
return reject(err);
|
||||
}
|
||||
return resolve(event.data);
|
||||
});
|
||||
}),
|
||||
listCalendars: () => new Promise((resolve, reject) => {
|
||||
const calendar = google.calendar({version: 'v3', auth: myGoogleAuth});
|
||||
calendar.calendarList
|
||||
.list()
|
||||
.then(cals => {
|
||||
resolve(cals.data.items.map(cal => {
|
||||
const calendar: IntegrationCalendar = {
|
||||
externalId: cal.id, integration: integrationType, name: cal.summary, primary: cal.primary
|
||||
}
|
||||
return calendar;
|
||||
}))
|
||||
})
|
||||
.catch((err) => {
|
||||
reject(err);
|
||||
});
|
||||
})
|
||||
};
|
||||
};
|
||||
|
||||
// factory
|
||||
@@ -324,11 +378,18 @@ const calendars = (withCredentials): CalendarApiAdapter[] => withCredentials.map
|
||||
}
|
||||
}).filter(Boolean);
|
||||
|
||||
|
||||
const getBusyCalendarTimes = (withCredentials, dateFrom, dateTo) => Promise.all(
|
||||
calendars(withCredentials).map(c => c.getAvailability(dateFrom, dateTo))
|
||||
const getBusyCalendarTimes = (withCredentials, dateFrom, dateTo, selectedCalendars) => Promise.all(
|
||||
calendars(withCredentials).map(c => c.getAvailability(dateFrom, dateTo, selectedCalendars))
|
||||
).then(
|
||||
(results) => results.reduce((acc, availability) => acc.concat(availability), [])
|
||||
(results) => {
|
||||
return results.reduce((acc, availability) => acc.concat(availability), [])
|
||||
}
|
||||
);
|
||||
|
||||
const listCalendars = (withCredentials) => Promise.all(
|
||||
calendars(withCredentials).map(c => c.listCalendars())
|
||||
).then(
|
||||
(results) => results.reduce((acc, calendars) => acc.concat(calendars), [])
|
||||
);
|
||||
|
||||
const createEvent = async (credential, calEvent: CalendarEvent): Promise<any> => {
|
||||
@@ -377,4 +438,4 @@ const deleteEvent = (credential, uid: String): Promise<any> => {
|
||||
return Promise.resolve({});
|
||||
};
|
||||
|
||||
export {getBusyCalendarTimes, createEvent, updateEvent, deleteEvent, CalendarEvent};
|
||||
export {getBusyCalendarTimes, createEvent, updateEvent, deleteEvent, CalendarEvent, listCalendars, IntegrationCalendar};
|
||||
@@ -9,14 +9,14 @@ export default class EventOrganizerMail extends EventMail {
|
||||
*/
|
||||
protected getiCalEventAsString(): string {
|
||||
const icsEvent = createEvent({
|
||||
start: dayjs(this.calEvent.startTime).utc().toArray().slice(0, 6),
|
||||
start: dayjs(this.calEvent.startTime).utc().toArray().slice(0, 6).map((v, i) => i === 1 ? v + 1 : v),
|
||||
startInputType: 'utc',
|
||||
productId: 'calendso/ics',
|
||||
title: `${this.calEvent.type} with ${this.calEvent.attendees[0].name}`,
|
||||
description: this.calEvent.description + this.stripHtml(this.getAdditionalBody()) + this.stripHtml(this.getAdditionalFooter()),
|
||||
duration: {minutes: dayjs(this.calEvent.endTime).diff(dayjs(this.calEvent.startTime), 'minute')},
|
||||
organizer: {name: this.calEvent.organizer.name, email: this.calEvent.organizer.email},
|
||||
attendees: this.calEvent.attendees.map((attendee: any) => ({name: attendee.name, email: attendee.email})),
|
||||
description: this.calEvent.description,
|
||||
duration: { minutes: dayjs(this.calEvent.endTime).diff(dayjs(this.calEvent.startTime), 'minute') },
|
||||
organizer: { name: this.calEvent.organizer.name, email: this.calEvent.organizer.email },
|
||||
attendees: this.calEvent.attendees.map( (attendee: any) => ({ name: attendee.name, email: attendee.email }) ),
|
||||
status: "CONFIRMED",
|
||||
});
|
||||
if (icsEvent.error) {
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
export function getEventName(name: string, eventTitle: string, eventNameTemplate?: string) {
|
||||
return eventNameTemplate
|
||||
? eventNameTemplate.replace("{USER}", name)
|
||||
: eventTitle + ' with ' + name
|
||||
}
|
||||
Reference in new issue
Block a user