Updated to latest
This commit is contained in:
commit
88fc22ddd4
36 files changed
+1340
-405
No files matched your search
+189
-164
@@ -1,69 +1,77 @@
|
||||
import EventOrganizerMail from "./emails/EventOrganizerMail";
|
||||
import EventAttendeeMail from "./emails/EventAttendeeMail";
|
||||
import {v5 as uuidv5} from 'uuid';
|
||||
import short from 'short-uuid';
|
||||
import EventOrganizerRescheduledMail from "./emails/EventOrganizerRescheduledMail";
|
||||
import EventAttendeeRescheduledMail from "./emails/EventAttendeeRescheduledMail";
|
||||
|
||||
const translator = short();
|
||||
|
||||
const {google} = require('googleapis');
|
||||
import createNewEventEmail from "./emails/new-event";
|
||||
|
||||
const googleAuth = () => {
|
||||
const {client_secret, client_id, redirect_uris} = JSON.parse(process.env.GOOGLE_API_CREDENTIALS).web;
|
||||
return new google.auth.OAuth2(client_id, client_secret, redirect_uris[0]);
|
||||
const {client_secret, client_id, redirect_uris} = JSON.parse(process.env.GOOGLE_API_CREDENTIALS).web;
|
||||
return new google.auth.OAuth2(client_id, client_secret, redirect_uris[0]);
|
||||
};
|
||||
|
||||
function handleErrorsJson(response) {
|
||||
if (!response.ok) {
|
||||
response.json().then(console.log);
|
||||
throw Error(response.statusText);
|
||||
}
|
||||
return response.json();
|
||||
if (!response.ok) {
|
||||
response.json().then(console.log);
|
||||
throw Error(response.statusText);
|
||||
}
|
||||
return response.json();
|
||||
}
|
||||
|
||||
function handleErrorsRaw(response) {
|
||||
if (!response.ok) {
|
||||
response.text().then(console.log);
|
||||
throw Error(response.statusText);
|
||||
}
|
||||
return response.text();
|
||||
if (!response.ok) {
|
||||
response.text().then(console.log);
|
||||
throw Error(response.statusText);
|
||||
}
|
||||
return response.text();
|
||||
}
|
||||
|
||||
const o365Auth = (credential) => {
|
||||
|
||||
const isExpired = (expiryDate) => expiryDate < +(new Date());
|
||||
const isExpired = (expiryDate) => expiryDate < +(new Date());
|
||||
|
||||
const refreshAccessToken = (refreshToken) => fetch('https://login.microsoftonline.com/common/oauth2/v2.0/token', {
|
||||
method: 'POST',
|
||||
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
|
||||
body: new URLSearchParams({
|
||||
'scope': 'User.Read Calendars.Read Calendars.ReadWrite',
|
||||
'client_id': process.env.MS_GRAPH_CLIENT_ID,
|
||||
'refresh_token': refreshToken,
|
||||
'grant_type': 'refresh_token',
|
||||
'client_secret': process.env.MS_GRAPH_CLIENT_SECRET,
|
||||
})
|
||||
const refreshAccessToken = (refreshToken) => fetch('https://login.microsoftonline.com/common/oauth2/v2.0/token', {
|
||||
method: 'POST',
|
||||
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
|
||||
body: new URLSearchParams({
|
||||
'scope': 'User.Read Calendars.Read Calendars.ReadWrite',
|
||||
'client_id': process.env.MS_GRAPH_CLIENT_ID,
|
||||
'refresh_token': refreshToken,
|
||||
'grant_type': 'refresh_token',
|
||||
'client_secret': process.env.MS_GRAPH_CLIENT_SECRET,
|
||||
})
|
||||
})
|
||||
.then(handleErrorsJson)
|
||||
.then((responseBody) => {
|
||||
credential.key.access_token = responseBody.access_token;
|
||||
credential.key.expiry_date = Math.round((+(new Date()) / 1000) + responseBody.expires_in);
|
||||
return credential.key.access_token;
|
||||
})
|
||||
.then(handleErrorsJson)
|
||||
.then((responseBody) => {
|
||||
credential.key.access_token = responseBody.access_token;
|
||||
credential.key.expiry_date = Math.round((+(new Date()) / 1000) + responseBody.expires_in);
|
||||
return credential.key.access_token;
|
||||
})
|
||||
|
||||
return {
|
||||
getToken: () => !isExpired(credential.key.expiry_date) ? Promise.resolve(credential.key.access_token) : refreshAccessToken(credential.key.refresh_token)
|
||||
};
|
||||
return {
|
||||
getToken: () => !isExpired(credential.key.expiry_date) ? Promise.resolve(credential.key.access_token) : refreshAccessToken(credential.key.refresh_token)
|
||||
};
|
||||
};
|
||||
|
||||
interface Person {
|
||||
name?: string,
|
||||
email: string,
|
||||
timeZone: string
|
||||
name?: string,
|
||||
email: string,
|
||||
timeZone: string
|
||||
}
|
||||
|
||||
interface CalendarEvent {
|
||||
type: string;
|
||||
title: string;
|
||||
startTime: string;
|
||||
endTime: string;
|
||||
description?: string;
|
||||
location?: string;
|
||||
organizer: Person;
|
||||
attendees: Person[];
|
||||
type: string;
|
||||
title: string;
|
||||
startTime: string;
|
||||
endTime: string;
|
||||
description?: string;
|
||||
location?: string;
|
||||
organizer: Person;
|
||||
attendees: Person[];
|
||||
};
|
||||
|
||||
interface IntegrationCalendar {
|
||||
@@ -74,11 +82,11 @@ interface IntegrationCalendar {
|
||||
}
|
||||
|
||||
interface CalendarApiAdapter {
|
||||
createEvent(event: CalendarEvent): Promise<any>;
|
||||
createEvent(event: CalendarEvent): Promise<any>;
|
||||
|
||||
updateEvent(uid: String, event: CalendarEvent);
|
||||
updateEvent(uid: String, event: CalendarEvent);
|
||||
|
||||
deleteEvent(uid: String);
|
||||
deleteEvent(uid: String);
|
||||
|
||||
getAvailability(dateFrom, dateTo, selectedCalendars: IntegrationCalendar[]): Promise<any>;
|
||||
|
||||
@@ -87,39 +95,39 @@ interface CalendarApiAdapter {
|
||||
|
||||
const MicrosoftOffice365Calendar = (credential): CalendarApiAdapter => {
|
||||
|
||||
const auth = o365Auth(credential);
|
||||
const auth = o365Auth(credential);
|
||||
|
||||
const translateEvent = (event: CalendarEvent) => {
|
||||
const translateEvent = (event: CalendarEvent) => {
|
||||
|
||||
let optional = {};
|
||||
if (event.location) {
|
||||
optional.location = {displayName: event.location};
|
||||
}
|
||||
let optional = {};
|
||||
if (event.location) {
|
||||
optional.location = {displayName: event.location};
|
||||
}
|
||||
|
||||
return {
|
||||
subject: event.title,
|
||||
body: {
|
||||
contentType: 'HTML',
|
||||
content: event.description,
|
||||
},
|
||||
start: {
|
||||
dateTime: event.startTime,
|
||||
timeZone: event.organizer.timeZone,
|
||||
},
|
||||
end: {
|
||||
dateTime: event.endTime,
|
||||
timeZone: event.organizer.timeZone,
|
||||
},
|
||||
attendees: event.attendees.map(attendee => ({
|
||||
emailAddress: {
|
||||
address: attendee.email,
|
||||
name: attendee.name
|
||||
},
|
||||
type: "required"
|
||||
})),
|
||||
...optional
|
||||
}
|
||||
};
|
||||
return {
|
||||
subject: event.title,
|
||||
body: {
|
||||
contentType: 'HTML',
|
||||
content: event.description,
|
||||
},
|
||||
start: {
|
||||
dateTime: event.startTime,
|
||||
timeZone: event.organizer.timeZone,
|
||||
},
|
||||
end: {
|
||||
dateTime: event.endTime,
|
||||
timeZone: event.organizer.timeZone,
|
||||
},
|
||||
attendees: event.attendees.map(attendee => ({
|
||||
emailAddress: {
|
||||
address: attendee.email,
|
||||
name: attendee.name
|
||||
},
|
||||
type: "required"
|
||||
})),
|
||||
...optional
|
||||
}
|
||||
};
|
||||
|
||||
const integrationType = "office365_calendar";
|
||||
|
||||
@@ -243,69 +251,69 @@ const GoogleCalendar = (credential): CalendarApiAdapter => {
|
||||
reject(err);
|
||||
});
|
||||
|
||||
}),
|
||||
createEvent: (event: CalendarEvent) => new Promise((resolve, reject) => {
|
||||
const payload = {
|
||||
summary: event.title,
|
||||
description: event.description,
|
||||
start: {
|
||||
dateTime: event.startTime,
|
||||
timeZone: event.organizer.timeZone,
|
||||
},
|
||||
end: {
|
||||
dateTime: event.endTime,
|
||||
timeZone: event.organizer.timeZone,
|
||||
},
|
||||
attendees: event.attendees,
|
||||
reminders: {
|
||||
useDefault: false,
|
||||
overrides: [
|
||||
{'method': 'email', 'minutes': 60}
|
||||
],
|
||||
},
|
||||
};
|
||||
}),
|
||||
createEvent: (event: CalendarEvent) => new Promise((resolve, reject) => {
|
||||
const payload = {
|
||||
summary: event.title,
|
||||
description: event.description,
|
||||
start: {
|
||||
dateTime: event.startTime,
|
||||
timeZone: event.organizer.timeZone,
|
||||
},
|
||||
end: {
|
||||
dateTime: event.endTime,
|
||||
timeZone: event.organizer.timeZone,
|
||||
},
|
||||
attendees: event.attendees,
|
||||
reminders: {
|
||||
useDefault: false,
|
||||
overrides: [
|
||||
{'method': 'email', 'minutes': 60}
|
||||
],
|
||||
},
|
||||
};
|
||||
|
||||
if (event.location) {
|
||||
payload['location'] = event.location;
|
||||
}
|
||||
if (event.location) {
|
||||
payload['location'] = event.location;
|
||||
}
|
||||
|
||||
const calendar = google.calendar({version: 'v3', auth: myGoogleAuth});
|
||||
calendar.events.insert({
|
||||
auth: myGoogleAuth,
|
||||
calendarId: 'primary',
|
||||
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);
|
||||
});
|
||||
}),
|
||||
updateEvent: (uid: String, event: CalendarEvent) => new Promise((resolve, reject) => {
|
||||
const payload = {
|
||||
summary: event.title,
|
||||
description: event.description,
|
||||
start: {
|
||||
dateTime: event.startTime,
|
||||
timeZone: event.organizer.timeZone,
|
||||
},
|
||||
end: {
|
||||
dateTime: event.endTime,
|
||||
timeZone: event.organizer.timeZone,
|
||||
},
|
||||
attendees: event.attendees,
|
||||
reminders: {
|
||||
useDefault: false,
|
||||
overrides: [
|
||||
{'method': 'email', 'minutes': 60}
|
||||
],
|
||||
},
|
||||
};
|
||||
const calendar = google.calendar({version: 'v3', auth: myGoogleAuth});
|
||||
calendar.events.insert({
|
||||
auth: myGoogleAuth,
|
||||
calendarId: 'primary',
|
||||
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);
|
||||
});
|
||||
}),
|
||||
updateEvent: (uid: String, event: CalendarEvent) => new Promise((resolve, reject) => {
|
||||
const payload = {
|
||||
summary: event.title,
|
||||
description: event.description,
|
||||
start: {
|
||||
dateTime: event.startTime,
|
||||
timeZone: event.organizer.timeZone,
|
||||
},
|
||||
end: {
|
||||
dateTime: event.endTime,
|
||||
timeZone: event.organizer.timeZone,
|
||||
},
|
||||
attendees: event.attendees,
|
||||
reminders: {
|
||||
useDefault: false,
|
||||
overrides: [
|
||||
{'method': 'email', 'minutes': 60}
|
||||
],
|
||||
},
|
||||
};
|
||||
|
||||
if (event.location) {
|
||||
payload['location'] = event.location;
|
||||
}
|
||||
if (event.location) {
|
||||
payload['location'] = event.location;
|
||||
}
|
||||
|
||||
const calendar = google.calendar({version: 'v3', auth: myGoogleAuth});
|
||||
calendar.events.update({
|
||||
@@ -360,17 +368,17 @@ const GoogleCalendar = (credential): CalendarApiAdapter => {
|
||||
|
||||
// factory
|
||||
const calendars = (withCredentials): CalendarApiAdapter[] => withCredentials.map((cred) => {
|
||||
switch (cred.type) {
|
||||
case 'google_calendar':
|
||||
return GoogleCalendar(cred);
|
||||
case 'office365_calendar':
|
||||
return MicrosoftOffice365Calendar(cred);
|
||||
default:
|
||||
return; // unknown credential, could be legacy? In any case, ignore
|
||||
}
|
||||
switch (cred.type) {
|
||||
case 'google_calendar':
|
||||
return GoogleCalendar(cred);
|
||||
case 'office365_calendar':
|
||||
return MicrosoftOffice365Calendar(cred);
|
||||
default:
|
||||
return; // unknown credential, could be legacy? In any case, ignore
|
||||
}
|
||||
}).filter(Boolean);
|
||||
|
||||
const getBusyTimes = (withCredentials, dateFrom, dateTo, selectedCalendars) => Promise.all(
|
||||
const getBusyCalendarTimes = (withCredentials, dateFrom, dateTo, selectedCalendars) => Promise.all(
|
||||
calendars(withCredentials).map(c => c.getAvailability(dateFrom, dateTo, selectedCalendars))
|
||||
).then(
|
||||
(results) => {
|
||||
@@ -384,33 +392,50 @@ const listCalendars = (withCredentials) => Promise.all(
|
||||
(results) => results.reduce((acc, calendars) => acc.concat(calendars), [])
|
||||
);
|
||||
|
||||
const createEvent = (credential, calEvent: CalendarEvent): Promise<any> => {
|
||||
const createEvent = async (credential, calEvent: CalendarEvent): Promise<any> => {
|
||||
const uid: string = translator.fromUUID(uuidv5(JSON.stringify(calEvent), uuidv5.URL));
|
||||
|
||||
createNewEventEmail(
|
||||
calEvent,
|
||||
);
|
||||
const creationResult = credential ? await calendars([credential])[0].createEvent(calEvent) : null;
|
||||
|
||||
if (credential) {
|
||||
return calendars([credential])[0].createEvent(calEvent);
|
||||
}
|
||||
const organizerMail = new EventOrganizerMail(calEvent, uid);
|
||||
const attendeeMail = new EventAttendeeMail(calEvent, uid);
|
||||
await organizerMail.sendEmail();
|
||||
|
||||
return Promise.resolve({});
|
||||
if (!creationResult || !creationResult.disableConfirmationEmail) {
|
||||
await attendeeMail.sendEmail();
|
||||
}
|
||||
|
||||
return {
|
||||
uid,
|
||||
createdEvent: creationResult
|
||||
};
|
||||
};
|
||||
|
||||
const updateEvent = (credential, uid: String, calEvent: CalendarEvent): Promise<any> => {
|
||||
if (credential) {
|
||||
return calendars([credential])[0].updateEvent(uid, calEvent);
|
||||
}
|
||||
const updateEvent = async (credential, uidToUpdate: String, calEvent: CalendarEvent): Promise<any> => {
|
||||
const newUid: string = translator.fromUUID(uuidv5(JSON.stringify(calEvent), uuidv5.URL));
|
||||
|
||||
return Promise.resolve({});
|
||||
const updateResult = credential ? await calendars([credential])[0].updateEvent(uidToUpdate, calEvent) : null;
|
||||
|
||||
const organizerMail = new EventOrganizerRescheduledMail(calEvent, newUid);
|
||||
const attendeeMail = new EventAttendeeRescheduledMail(calEvent, newUid);
|
||||
await organizerMail.sendEmail();
|
||||
|
||||
if (!updateResult || !updateResult.disableConfirmationEmail) {
|
||||
await attendeeMail.sendEmail();
|
||||
}
|
||||
|
||||
return {
|
||||
uid: newUid,
|
||||
updatedEvent: updateResult
|
||||
};
|
||||
};
|
||||
|
||||
const deleteEvent = (credential, uid: String): Promise<any> => {
|
||||
if (credential) {
|
||||
return calendars([credential])[0].deleteEvent(uid);
|
||||
}
|
||||
if (credential) {
|
||||
return calendars([credential])[0].deleteEvent(uid);
|
||||
}
|
||||
|
||||
return Promise.resolve({});
|
||||
return Promise.resolve({});
|
||||
};
|
||||
|
||||
export {getBusyTimes, createEvent, updateEvent, deleteEvent, CalendarEvent, listCalendars, IntegrationCalendar};
|
||||
export {getBusyCalendarTimes, createEvent, updateEvent, deleteEvent, CalendarEvent, listCalendars, IntegrationCalendar};
|
||||
@@ -0,0 +1,55 @@
|
||||
import dayjs, {Dayjs} from "dayjs";
|
||||
import EventMail from "./EventMail";
|
||||
|
||||
export default class EventAttendeeMail extends EventMail {
|
||||
/**
|
||||
* Returns the email text as HTML representation.
|
||||
*
|
||||
* @protected
|
||||
*/
|
||||
protected getHtmlRepresentation(): string {
|
||||
return `
|
||||
<div>
|
||||
Hi ${this.calEvent.attendees[0].name},<br />
|
||||
<br />
|
||||
Your ${this.calEvent.type} with ${this.calEvent.organizer.name} at ${this.getInviteeStart().format('h:mma')}
|
||||
(${this.calEvent.attendees[0].timeZone}) on ${this.getInviteeStart().format('dddd, LL')} is scheduled.<br />
|
||||
<br />` + this.getAdditionalBody() + (
|
||||
this.calEvent.location ? `<strong>Location:</strong> ${this.calEvent.location}<br /><br />` : ''
|
||||
) +
|
||||
`<strong>Additional notes:</strong><br />
|
||||
${this.calEvent.description}<br />
|
||||
` + this.getAdditionalFooter() + `
|
||||
</div>
|
||||
`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the payload object for the nodemailer.
|
||||
*
|
||||
* @protected
|
||||
*/
|
||||
protected getNodeMailerPayload(): Object {
|
||||
return {
|
||||
to: `${this.calEvent.attendees[0].name} <${this.calEvent.attendees[0].email}>`,
|
||||
from: `${this.calEvent.organizer.name} <${this.getMailerOptions().from}>`,
|
||||
replyTo: this.calEvent.organizer.email,
|
||||
subject: `Confirmed: ${this.calEvent.type} with ${this.calEvent.organizer.name} on ${this.getInviteeStart().format('dddd, LL')}`,
|
||||
html: this.getHtmlRepresentation(),
|
||||
text: this.getPlainTextRepresentation(),
|
||||
};
|
||||
}
|
||||
|
||||
protected printNodeMailerError(error: string): void {
|
||||
console.error("SEND_BOOKING_CONFIRMATION_ERROR", this.calEvent.attendees[0].email, error);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the inviteeStart value used at multiple points.
|
||||
*
|
||||
* @private
|
||||
*/
|
||||
protected getInviteeStart(): Dayjs {
|
||||
return <Dayjs>dayjs(this.calEvent.startTime).tz(this.calEvent.attendees[0].timeZone);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
import EventAttendeeMail from "./EventAttendeeMail";
|
||||
|
||||
export default class EventAttendeeRescheduledMail extends EventAttendeeMail {
|
||||
/**
|
||||
* Returns the email text as HTML representation.
|
||||
*
|
||||
* @protected
|
||||
*/
|
||||
protected getHtmlRepresentation(): string {
|
||||
return `
|
||||
<div>
|
||||
Hi ${this.calEvent.attendees[0].name},<br />
|
||||
<br />
|
||||
Your ${this.calEvent.type} with ${this.calEvent.organizer.name} has been rescheduled to ${this.getInviteeStart().format('h:mma')}
|
||||
(${this.calEvent.attendees[0].timeZone}) on ${this.getInviteeStart().format('dddd, LL')}.<br />
|
||||
` + this.getAdditionalFooter() + `
|
||||
</div>
|
||||
`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the payload object for the nodemailer.
|
||||
*
|
||||
* @protected
|
||||
*/
|
||||
protected getNodeMailerPayload(): Object {
|
||||
return {
|
||||
to: `${this.calEvent.attendees[0].name} <${this.calEvent.attendees[0].email}>`,
|
||||
from: `${this.calEvent.organizer.name} <${this.getMailerOptions().from}>`,
|
||||
replyTo: this.calEvent.organizer.email,
|
||||
subject: `Rescheduled: ${this.calEvent.type} with ${this.calEvent.organizer.name} on ${this.getInviteeStart().format('dddd, LL')}`,
|
||||
html: this.getHtmlRepresentation(),
|
||||
text: this.getPlainTextRepresentation(),
|
||||
};
|
||||
}
|
||||
|
||||
protected printNodeMailerError(error: string): void {
|
||||
console.error("SEND_RESCHEDULE_CONFIRMATION_ERROR", this.calEvent.attendees[0].email, error);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,135 @@
|
||||
import {CalendarEvent} from "../calendarClient";
|
||||
import {serverConfig} from "../serverConfig";
|
||||
import nodemailer from 'nodemailer';
|
||||
|
||||
export default abstract class EventMail {
|
||||
calEvent: CalendarEvent;
|
||||
uid: string;
|
||||
|
||||
/**
|
||||
* An EventMail always consists of a CalendarEvent
|
||||
* that stores the very basic data of the event (like date, title etc).
|
||||
* It also needs the UID of the stored booking in our database.
|
||||
*
|
||||
* @param calEvent
|
||||
* @param uid
|
||||
*/
|
||||
constructor(calEvent: CalendarEvent, uid: string) {
|
||||
this.calEvent = calEvent;
|
||||
this.uid = uid;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the email text as HTML representation.
|
||||
*
|
||||
* @protected
|
||||
*/
|
||||
protected abstract getHtmlRepresentation(): string;
|
||||
|
||||
/**
|
||||
* Returns the email text in a plain text representation
|
||||
* by stripping off the HTML tags.
|
||||
*
|
||||
* @protected
|
||||
*/
|
||||
protected getPlainTextRepresentation(): string {
|
||||
return this.stripHtml(this.getHtmlRepresentation());
|
||||
}
|
||||
|
||||
/**
|
||||
* Strips off all HTML tags and leaves plain text.
|
||||
*
|
||||
* @param html
|
||||
* @protected
|
||||
*/
|
||||
protected stripHtml(html: string): string {
|
||||
return html
|
||||
.replace('<br />', "\n")
|
||||
.replace(/<[^>]+>/g, '');
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the payload object for the nodemailer.
|
||||
* @protected
|
||||
*/
|
||||
protected abstract getNodeMailerPayload(): Object;
|
||||
|
||||
/**
|
||||
* Sends the email to the event attendant and returns a Promise.
|
||||
*/
|
||||
public sendEmail(): Promise<any> {
|
||||
return new Promise((resolve, reject) => nodemailer.createTransport(this.getMailerOptions().transport).sendMail(
|
||||
this.getNodeMailerPayload(),
|
||||
(error, info) => {
|
||||
if (error) {
|
||||
this.printNodeMailerError(error);
|
||||
reject(new Error(error));
|
||||
} else {
|
||||
resolve(info);
|
||||
}
|
||||
}));
|
||||
}
|
||||
|
||||
/**
|
||||
* Gathers the required provider information from the config.
|
||||
*
|
||||
* @protected
|
||||
*/
|
||||
protected getMailerOptions(): any {
|
||||
return {
|
||||
transport: serverConfig.transport,
|
||||
from: serverConfig.from,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Can be used to include additional HTML or plain text
|
||||
* content into the mail body. Leave it to an empty
|
||||
* string if not desired.
|
||||
*
|
||||
* @protected
|
||||
*/
|
||||
protected getAdditionalBody(): string {
|
||||
return "";
|
||||
}
|
||||
|
||||
/**
|
||||
* Prints out the desired information when an error
|
||||
* occured while sending the mail.
|
||||
* @param error
|
||||
* @protected
|
||||
*/
|
||||
protected abstract printNodeMailerError(error: string): void;
|
||||
|
||||
/**
|
||||
* Returns a link to reschedule the given booking.
|
||||
*
|
||||
* @protected
|
||||
*/
|
||||
protected getRescheduleLink(): string {
|
||||
return process.env.BASE_URL + '/reschedule/' + this.uid;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a link to cancel the given booking.
|
||||
*
|
||||
* @protected
|
||||
*/
|
||||
protected getCancelLink(): string {
|
||||
return process.env.BASE_URL + '/cancel/' + this.uid;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Defines a footer that will be appended to the email.
|
||||
* @protected
|
||||
*/
|
||||
protected getAdditionalFooter(): string {
|
||||
return `
|
||||
<br/>
|
||||
Need to change this event?<br />
|
||||
Cancel: <a href="${this.getCancelLink()}">${this.getCancelLink()}</a><br />
|
||||
Reschedule: <a href="${this.getRescheduleLink()}">${this.getRescheduleLink()}</a>
|
||||
`;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
import {createEvent} from "ics";
|
||||
import dayjs, {Dayjs} from "dayjs";
|
||||
import EventMail from "./EventMail";
|
||||
|
||||
export default class EventOrganizerMail extends EventMail {
|
||||
/**
|
||||
* Returns the instance's event as an iCal event in string representation.
|
||||
* @protected
|
||||
*/
|
||||
protected getiCalEventAsString(): string {
|
||||
const icsEvent = createEvent({
|
||||
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 }) ),
|
||||
status: "CONFIRMED",
|
||||
});
|
||||
if (icsEvent.error) {
|
||||
throw icsEvent.error;
|
||||
}
|
||||
return icsEvent.value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the email text as HTML representation.
|
||||
*
|
||||
* @protected
|
||||
*/
|
||||
protected getHtmlRepresentation(): string {
|
||||
return `
|
||||
<div>
|
||||
Hi ${this.calEvent.organizer.name},<br />
|
||||
<br />
|
||||
A new event has been scheduled.<br />
|
||||
<br />
|
||||
<strong>Event Type:</strong><br />
|
||||
${this.calEvent.type}<br />
|
||||
<br />
|
||||
<strong>Invitee Email:</strong><br />
|
||||
<a href="mailto:${this.calEvent.attendees[0].email}">${this.calEvent.attendees[0].email}</a><br />
|
||||
<br />` + this.getAdditionalBody() +
|
||||
(
|
||||
this.calEvent.location ? `
|
||||
<strong>Location:</strong><br />
|
||||
${this.calEvent.location}<br />
|
||||
<br />
|
||||
` : ''
|
||||
) +
|
||||
`<strong>Invitee Time Zone:</strong><br />
|
||||
${this.calEvent.attendees[0].timeZone}<br />
|
||||
<br />
|
||||
<strong>Additional notes:</strong><br />
|
||||
${this.calEvent.description}
|
||||
` + this.getAdditionalFooter() + `
|
||||
</div>
|
||||
`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the payload object for the nodemailer.
|
||||
*
|
||||
* @protected
|
||||
*/
|
||||
protected getNodeMailerPayload(): Object {
|
||||
const organizerStart: Dayjs = <Dayjs>dayjs(this.calEvent.startTime).tz(this.calEvent.organizer.timeZone);
|
||||
|
||||
return {
|
||||
icalEvent: {
|
||||
filename: 'event.ics',
|
||||
content: this.getiCalEventAsString(),
|
||||
},
|
||||
from: `Calendso <${this.getMailerOptions().from}>`,
|
||||
to: this.calEvent.organizer.email,
|
||||
subject: `New event: ${this.calEvent.attendees[0].name} - ${organizerStart.format('LT dddd, LL')} - ${this.calEvent.type}`,
|
||||
html: this.getHtmlRepresentation(),
|
||||
text: this.getPlainTextRepresentation(),
|
||||
};
|
||||
}
|
||||
|
||||
protected printNodeMailerError(error: string): void {
|
||||
console.error("SEND_NEW_EVENT_NOTIFICATION_ERROR", this.calEvent.organizer.email, error);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
import dayjs, {Dayjs} from "dayjs";
|
||||
import EventOrganizerMail from "./EventOrganizerMail";
|
||||
|
||||
export default class EventOrganizerRescheduledMail extends EventOrganizerMail {
|
||||
/**
|
||||
* Returns the email text as HTML representation.
|
||||
*
|
||||
* @protected
|
||||
*/
|
||||
protected getHtmlRepresentation(): string {
|
||||
return `
|
||||
<div>
|
||||
Hi ${this.calEvent.organizer.name},<br />
|
||||
<br />
|
||||
Your event has been rescheduled.<br />
|
||||
<br />
|
||||
<strong>Event Type:</strong><br />
|
||||
${this.calEvent.type}<br />
|
||||
<br />
|
||||
<strong>Invitee Email:</strong><br />
|
||||
<a href="mailto:${this.calEvent.attendees[0].email}">${this.calEvent.attendees[0].email}</a><br />
|
||||
<br />` + this.getAdditionalBody() +
|
||||
(
|
||||
this.calEvent.location ? `
|
||||
<strong>Location:</strong><br />
|
||||
${this.calEvent.location}<br />
|
||||
<br />
|
||||
` : ''
|
||||
) +
|
||||
`<strong>Invitee Time Zone:</strong><br />
|
||||
${this.calEvent.attendees[0].timeZone}<br />
|
||||
<br />
|
||||
<strong>Additional notes:</strong><br />
|
||||
${this.calEvent.description}
|
||||
` + this.getAdditionalFooter() + `
|
||||
</div>
|
||||
`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the payload object for the nodemailer.
|
||||
*
|
||||
* @protected
|
||||
*/
|
||||
protected getNodeMailerPayload(): Object {
|
||||
const organizerStart: Dayjs = <Dayjs>dayjs(this.calEvent.startTime).tz(this.calEvent.organizer.timeZone);
|
||||
|
||||
return {
|
||||
icalEvent: {
|
||||
filename: 'event.ics',
|
||||
content: this.getiCalEventAsString(),
|
||||
},
|
||||
from: `Calendso <${this.getMailerOptions().from}>`,
|
||||
to: this.calEvent.organizer.email,
|
||||
subject: `Rescheduled event: ${this.calEvent.attendees[0].name} - ${organizerStart.format('LT dddd, LL')} - ${this.calEvent.type}`,
|
||||
html: this.getHtmlRepresentation(),
|
||||
text: this.getPlainTextRepresentation(),
|
||||
};
|
||||
}
|
||||
|
||||
protected printNodeMailerError(error: string): void {
|
||||
console.error("SEND_RESCHEDULE_EVENT_NOTIFICATION_ERROR", this.calEvent.organizer.email, error);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
import {CalendarEvent} from "../calendarClient";
|
||||
import EventAttendeeMail from "./EventAttendeeMail";
|
||||
import {getFormattedMeetingId, getIntegrationName} from "./helpers";
|
||||
import {VideoCallData} from "../videoClient";
|
||||
|
||||
export default class VideoEventAttendeeMail extends EventAttendeeMail {
|
||||
videoCallData: VideoCallData;
|
||||
|
||||
constructor(calEvent: CalendarEvent, uid: string, videoCallData: VideoCallData) {
|
||||
super(calEvent, uid);
|
||||
this.videoCallData = videoCallData;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds the video call information to the mail body.
|
||||
*
|
||||
* @protected
|
||||
*/
|
||||
protected getAdditionalBody(): string {
|
||||
return `
|
||||
<strong>Video call provider:</strong> ${getIntegrationName(this.videoCallData)}<br />
|
||||
<strong>Meeting ID:</strong> ${getFormattedMeetingId(this.videoCallData)}<br />
|
||||
<strong>Meeting Password:</strong> ${this.videoCallData.password}<br />
|
||||
<strong>Meeting URL:</strong> <a href="${this.videoCallData.url}">${this.videoCallData.url}</a><br />
|
||||
`;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
import {CalendarEvent} from "../calendarClient";
|
||||
import EventOrganizerMail from "./EventOrganizerMail";
|
||||
import {VideoCallData} from "../videoClient";
|
||||
import {getFormattedMeetingId, getIntegrationName} from "./helpers";
|
||||
|
||||
export default class VideoEventOrganizerMail extends EventOrganizerMail {
|
||||
videoCallData: VideoCallData;
|
||||
|
||||
constructor(calEvent: CalendarEvent, uid: string, videoCallData: VideoCallData) {
|
||||
super(calEvent, uid);
|
||||
this.videoCallData = videoCallData;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds the video call information to the mail body
|
||||
* and calendar event description.
|
||||
*
|
||||
* @protected
|
||||
*/
|
||||
protected getAdditionalBody(): string {
|
||||
return `
|
||||
<strong>Video call provider:</strong> ${getIntegrationName(this.videoCallData)}<br />
|
||||
<strong>Meeting ID:</strong> ${getFormattedMeetingId(this.videoCallData)}<br />
|
||||
<strong>Meeting Password:</strong> ${this.videoCallData.password}<br />
|
||||
<strong>Meeting URL:</strong> <a href="${this.videoCallData.url}">${this.videoCallData.url}</a><br />
|
||||
`;
|
||||
}
|
||||
}
|
||||
@@ -1,70 +0,0 @@
|
||||
import nodemailer from 'nodemailer';
|
||||
import {serverConfig} from "../serverConfig";
|
||||
import {CalendarEvent} from "../calendarClient";
|
||||
import dayjs, {Dayjs} from "dayjs";
|
||||
import localizedFormat from "dayjs/plugin/localizedFormat";
|
||||
import utc from "dayjs/plugin/utc";
|
||||
import timezone from "dayjs/plugin/timezone";
|
||||
|
||||
dayjs.extend(localizedFormat);
|
||||
dayjs.extend(utc);
|
||||
dayjs.extend(timezone);
|
||||
|
||||
export default function createConfirmBookedEmail(calEvent: CalendarEvent, cancelLink: string, rescheduleLink: string, options: any = {}) {
|
||||
return sendEmail(calEvent, cancelLink, rescheduleLink, {
|
||||
provider: {
|
||||
transport: serverConfig.transport,
|
||||
from: serverConfig.from,
|
||||
},
|
||||
...options
|
||||
});
|
||||
}
|
||||
|
||||
const sendEmail = (calEvent: CalendarEvent, cancelLink: string, rescheduleLink: string, {
|
||||
provider,
|
||||
}) => new Promise( (resolve, reject) => {
|
||||
|
||||
const { from, transport } = provider;
|
||||
const inviteeStart: Dayjs = <Dayjs>dayjs(calEvent.startTime).tz(calEvent.attendees[0].timeZone);
|
||||
|
||||
nodemailer.createTransport(transport).sendMail(
|
||||
{
|
||||
to: `${calEvent.attendees[0].name} <${calEvent.attendees[0].email}>`,
|
||||
from: `${calEvent.organizer.name} <${from}>`,
|
||||
replyTo: calEvent.organizer.email,
|
||||
subject: `Confirmed: ${calEvent.type} with ${calEvent.organizer.name} on ${inviteeStart.format('dddd, LL')}`,
|
||||
html: html(calEvent, cancelLink, rescheduleLink),
|
||||
text: text(calEvent, cancelLink, rescheduleLink),
|
||||
},
|
||||
(error, info) => {
|
||||
if (error) {
|
||||
console.error("SEND_BOOKING_CONFIRMATION_ERROR", calEvent.attendees[0].email, error);
|
||||
return reject(new Error(error));
|
||||
}
|
||||
return resolve();
|
||||
}
|
||||
)
|
||||
});
|
||||
|
||||
const html = (calEvent: CalendarEvent, cancelLink: string, rescheduleLink: string) => {
|
||||
const inviteeStart: Dayjs = <Dayjs>dayjs(calEvent.startTime).tz(calEvent.attendees[0].timeZone);
|
||||
return `
|
||||
<div>
|
||||
Hi ${calEvent.attendees[0].name},<br />
|
||||
<br />
|
||||
Your ${calEvent.type} with ${calEvent.organizer.name} at ${inviteeStart.format('h:mma')}
|
||||
(${calEvent.attendees[0].timeZone}) on ${inviteeStart.format('dddd, LL')} is scheduled.<br />
|
||||
<br />` + (
|
||||
calEvent.location ? `<strong>Location:</strong> ${calEvent.location}<br /><br />` : ''
|
||||
) +
|
||||
`Additional notes:<br />
|
||||
${calEvent.description}<br />
|
||||
<br />
|
||||
Need to change this event?<br />
|
||||
Cancel: <a href="${cancelLink}">${cancelLink}</a><br />
|
||||
Reschedule: <a href="${rescheduleLink}">${rescheduleLink}</a>
|
||||
</div>
|
||||
`;
|
||||
};
|
||||
|
||||
const text = (evt: CalendarEvent, cancelLink: string, rescheduleLink: string) => html(evt, cancelLink, rescheduleLink).replace('<br />', "\n").replace(/<[^>]+>/g, '');
|
||||
@@ -0,0 +1,20 @@
|
||||
import {VideoCallData} from "../videoClient";
|
||||
|
||||
export function getIntegrationName(videoCallData: VideoCallData): string {
|
||||
//TODO: When there are more complex integration type strings, we should consider using an extra field in the DB for that.
|
||||
const nameProto = videoCallData.type.split("_")[0];
|
||||
return nameProto.charAt(0).toUpperCase() + nameProto.slice(1);
|
||||
}
|
||||
|
||||
export function getFormattedMeetingId(videoCallData: VideoCallData): string {
|
||||
switch(videoCallData.type) {
|
||||
case 'zoom_video':
|
||||
const strId = videoCallData.id.toString();
|
||||
const part1 = strId.slice(0, 3);
|
||||
const part2 = strId.slice(3, 7);
|
||||
const part3 = strId.slice(7, 11);
|
||||
return part1 + " " + part2 + " " + part3;
|
||||
default:
|
||||
return videoCallData.id.toString();
|
||||
}
|
||||
}
|
||||
@@ -1,99 +0,0 @@
|
||||
|
||||
import nodemailer from 'nodemailer';
|
||||
import dayjs, { Dayjs } from "dayjs";
|
||||
import localizedFormat from 'dayjs/plugin/localizedFormat';
|
||||
import utc from 'dayjs/plugin/utc';
|
||||
import timezone from 'dayjs/plugin/timezone';
|
||||
import toArray from 'dayjs/plugin/toArray';
|
||||
import { createEvent } from 'ics';
|
||||
import { CalendarEvent } from '../calendarClient';
|
||||
import { serverConfig } from '../serverConfig';
|
||||
|
||||
dayjs.extend(localizedFormat);
|
||||
dayjs.extend(utc);
|
||||
dayjs.extend(timezone);
|
||||
dayjs.extend(toArray);
|
||||
|
||||
export default function createNewEventEmail(calEvent: CalendarEvent, options: any = {}) {
|
||||
return sendEmail(calEvent, {
|
||||
provider: {
|
||||
transport: serverConfig.transport,
|
||||
from: serverConfig.from,
|
||||
},
|
||||
...options
|
||||
});
|
||||
}
|
||||
|
||||
const icalEventAsString = (calEvent: CalendarEvent): string => {
|
||||
const icsEvent = createEvent({
|
||||
start: dayjs(calEvent.startTime).utc().toArray().slice(0, 6).map((v, i) => i === 1 ? v + 1 : v),
|
||||
startInputType: 'utc',
|
||||
productId: 'calendso/ics',
|
||||
title: `${calEvent.type} with ${calEvent.attendees[0].name}`,
|
||||
description: calEvent.description,
|
||||
duration: { minutes: dayjs(calEvent.endTime).diff(dayjs(calEvent.startTime), 'minute') },
|
||||
organizer: { name: calEvent.organizer.name, email: calEvent.organizer.email },
|
||||
attendees: calEvent.attendees.map( (attendee: any) => ({ name: attendee.name, email: attendee.email }) ),
|
||||
status: "CONFIRMED",
|
||||
});
|
||||
if (icsEvent.error) {
|
||||
throw icsEvent.error;
|
||||
}
|
||||
return icsEvent.value;
|
||||
}
|
||||
|
||||
const sendEmail = (calEvent: CalendarEvent, {
|
||||
provider,
|
||||
}) => new Promise( (resolve, reject) => {
|
||||
const { transport, from } = provider;
|
||||
const organizerStart: Dayjs = <Dayjs>dayjs(calEvent.startTime).tz(calEvent.organizer.timeZone);
|
||||
nodemailer.createTransport(transport).sendMail(
|
||||
{
|
||||
icalEvent: {
|
||||
filename: 'event.ics',
|
||||
content: icalEventAsString(calEvent),
|
||||
},
|
||||
from: `Calendso <${from}>`,
|
||||
to: calEvent.organizer.email,
|
||||
subject: `New event: ${calEvent.attendees[0].name} - ${organizerStart.format('LT dddd, LL')} - ${calEvent.type}`,
|
||||
html: html(calEvent),
|
||||
text: text(calEvent),
|
||||
},
|
||||
(error) => {
|
||||
if (error) {
|
||||
console.error("SEND_NEW_EVENT_NOTIFICATION_ERROR", calEvent.organizer.email, error);
|
||||
return reject(new Error(error));
|
||||
}
|
||||
return resolve();
|
||||
});
|
||||
});
|
||||
|
||||
const html = (evt: CalendarEvent) => `
|
||||
<div>
|
||||
Hi ${evt.organizer.name},<br />
|
||||
<br />
|
||||
A new event has been scheduled.<br />
|
||||
<br />
|
||||
<strong>Event Type:</strong><br />
|
||||
${evt.type}<br />
|
||||
<br />
|
||||
<strong>Invitee Email:</strong><br />
|
||||
<a href="mailto:${evt.attendees[0].email}">${evt.attendees[0].email}</a><br />
|
||||
<br />` +
|
||||
(
|
||||
evt.location ? `
|
||||
<strong>Location:</strong><br />
|
||||
${evt.location}<br />
|
||||
<br />
|
||||
` : ''
|
||||
) +
|
||||
`<strong>Invitee Time Zone:</strong><br />
|
||||
${evt.attendees[0].timeZone}<br />
|
||||
<br />
|
||||
<strong>Additional notes:</strong><br />
|
||||
${evt.description}
|
||||
</div>
|
||||
`;
|
||||
|
||||
// just strip all HTML and convert <br /> to \n
|
||||
const text = (evt: CalendarEvent) => html(evt).replace('<br />', "\n").replace(/<[^>]+>/g, '');
|
||||
@@ -0,0 +1,13 @@
|
||||
export enum EventTypeCustomInputType {
|
||||
Text = 'text',
|
||||
TextLong = 'textLong',
|
||||
Number = 'number',
|
||||
Bool = 'bool',
|
||||
}
|
||||
|
||||
export interface EventTypeCustomInput {
|
||||
id?: number;
|
||||
type: EventTypeCustomInputType;
|
||||
label: string;
|
||||
required: boolean;
|
||||
}
|
||||
@@ -4,6 +4,8 @@ export function getIntegrationName(name: String) {
|
||||
return "Google Calendar";
|
||||
case "office365_calendar":
|
||||
return "Office 365 Calendar";
|
||||
case "zoom_video":
|
||||
return "Zoom";
|
||||
default:
|
||||
return "Unknown";
|
||||
}
|
||||
|
||||
@@ -0,0 +1,239 @@
|
||||
import prisma from "./prisma";
|
||||
import {CalendarEvent} from "./calendarClient";
|
||||
import VideoEventOrganizerMail from "./emails/VideoEventOrganizerMail";
|
||||
import VideoEventAttendeeMail from "./emails/VideoEventAttendeeMail";
|
||||
import {v5 as uuidv5} from 'uuid';
|
||||
import short from 'short-uuid';
|
||||
import EventAttendeeRescheduledMail from "./emails/EventAttendeeRescheduledMail";
|
||||
import EventOrganizerRescheduledMail from "./emails/EventOrganizerRescheduledMail";
|
||||
|
||||
const translator = short();
|
||||
|
||||
export interface VideoCallData {
|
||||
type: string;
|
||||
id: string;
|
||||
password: string;
|
||||
url: string;
|
||||
}
|
||||
|
||||
function handleErrorsJson(response) {
|
||||
if (!response.ok) {
|
||||
response.json().then(console.log);
|
||||
throw Error(response.statusText);
|
||||
}
|
||||
return response.json();
|
||||
}
|
||||
|
||||
function handleErrorsRaw(response) {
|
||||
if (!response.ok) {
|
||||
response.text().then(console.log);
|
||||
throw Error(response.statusText);
|
||||
}
|
||||
return response.text();
|
||||
}
|
||||
|
||||
const zoomAuth = (credential) => {
|
||||
|
||||
const isExpired = (expiryDate) => expiryDate < +(new Date());
|
||||
const authHeader = 'Basic ' + Buffer.from(process.env.ZOOM_CLIENT_ID + ':' + process.env.ZOOM_CLIENT_SECRET).toString('base64');
|
||||
|
||||
const refreshAccessToken = (refreshToken) => fetch('https://zoom.us/oauth/token', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Authorization': authHeader,
|
||||
'Content-Type': 'application/x-www-form-urlencoded'
|
||||
},
|
||||
body: new URLSearchParams({
|
||||
'refresh_token': refreshToken,
|
||||
'grant_type': 'refresh_token',
|
||||
})
|
||||
})
|
||||
.then(handleErrorsJson)
|
||||
.then(async (responseBody) => {
|
||||
// Store new tokens in database.
|
||||
await prisma.credential.update({
|
||||
where: {
|
||||
id: credential.id
|
||||
},
|
||||
data: {
|
||||
key: responseBody
|
||||
}
|
||||
});
|
||||
credential.key.access_token = responseBody.access_token;
|
||||
credential.key.expires_in = Math.round((+(new Date()) / 1000) + responseBody.expires_in);
|
||||
return credential.key.access_token;
|
||||
})
|
||||
|
||||
return {
|
||||
getToken: () => !isExpired(credential.key.expires_in) ? Promise.resolve(credential.key.access_token) : refreshAccessToken(credential.key.refresh_token)
|
||||
};
|
||||
};
|
||||
|
||||
interface VideoApiAdapter {
|
||||
createMeeting(event: CalendarEvent): Promise<any>;
|
||||
|
||||
updateMeeting(uid: String, event: CalendarEvent);
|
||||
|
||||
deleteMeeting(uid: String);
|
||||
|
||||
getAvailability(dateFrom, dateTo): Promise<any>;
|
||||
}
|
||||
|
||||
const ZoomVideo = (credential): VideoApiAdapter => {
|
||||
|
||||
const auth = zoomAuth(credential);
|
||||
|
||||
const translateEvent = (event: CalendarEvent) => {
|
||||
// Documentation at: https://marketplace.zoom.us/docs/api-reference/zoom-api/meetings/meetingcreate
|
||||
return {
|
||||
topic: event.title,
|
||||
type: 2, // Means that this is a scheduled meeting
|
||||
start_time: event.startTime,
|
||||
duration: ((new Date(event.endTime)).getTime() - (new Date(event.startTime)).getTime()) / 60000,
|
||||
//schedule_for: "string", TODO: Used when scheduling the meeting for someone else (needed?)
|
||||
timezone: event.attendees[0].timeZone,
|
||||
//password: "string", TODO: Should we use a password? Maybe generate a random one?
|
||||
agenda: event.description,
|
||||
settings: {
|
||||
host_video: true,
|
||||
participant_video: true,
|
||||
cn_meeting: false, // TODO: true if host meeting in China
|
||||
in_meeting: false, // TODO: true if host meeting in India
|
||||
join_before_host: true,
|
||||
mute_upon_entry: false,
|
||||
watermark: false,
|
||||
use_pmi: false,
|
||||
approval_type: 2,
|
||||
audio: "both",
|
||||
auto_recording: "none",
|
||||
enforce_login: false,
|
||||
registrants_email_notification: true
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
return {
|
||||
getAvailability: (dateFrom, dateTo) => {
|
||||
return auth.getToken().then(
|
||||
// TODO Possibly implement pagination for cases when there are more than 300 meetings already scheduled.
|
||||
(accessToken) => fetch('https://api.zoom.us/v2/users/me/meetings?type=scheduled&page_size=300', {
|
||||
method: 'get',
|
||||
headers: {
|
||||
'Authorization': 'Bearer ' + accessToken
|
||||
}
|
||||
})
|
||||
.then(handleErrorsJson)
|
||||
.then(responseBody => {
|
||||
return responseBody.meetings.map((meeting) => ({
|
||||
start: meeting.start_time,
|
||||
end: (new Date((new Date(meeting.start_time)).getTime() + meeting.duration * 60000)).toISOString()
|
||||
}))
|
||||
})
|
||||
).catch((err) => {
|
||||
console.log(err);
|
||||
});
|
||||
},
|
||||
createMeeting: (event: CalendarEvent) => auth.getToken().then(accessToken => fetch('https://api.zoom.us/v2/users/me/meetings', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Authorization': 'Bearer ' + accessToken,
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify(translateEvent(event))
|
||||
}).then(handleErrorsJson)),
|
||||
deleteMeeting: (uid: String) => auth.getToken().then(accessToken => fetch('https://api.zoom.us/v2/meetings/' + uid, {
|
||||
method: 'DELETE',
|
||||
headers: {
|
||||
'Authorization': 'Bearer ' + accessToken
|
||||
}
|
||||
}).then(handleErrorsRaw)),
|
||||
updateMeeting: (uid: String, event: CalendarEvent) => auth.getToken().then(accessToken => fetch('https://api.zoom.us/v2/meetings/' + uid, {
|
||||
method: 'PATCH',
|
||||
headers: {
|
||||
'Authorization': 'Bearer ' + accessToken,
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify(translateEvent(event))
|
||||
}).then(handleErrorsRaw)),
|
||||
}
|
||||
};
|
||||
|
||||
// factory
|
||||
const videoIntegrations = (withCredentials): VideoApiAdapter[] => withCredentials.map((cred) => {
|
||||
switch (cred.type) {
|
||||
case 'zoom_video':
|
||||
return ZoomVideo(cred);
|
||||
default:
|
||||
return; // unknown credential, could be legacy? In any case, ignore
|
||||
}
|
||||
}).filter(Boolean);
|
||||
|
||||
|
||||
const getBusyVideoTimes = (withCredentials, dateFrom, dateTo) => Promise.all(
|
||||
videoIntegrations(withCredentials).map(c => c.getAvailability(dateFrom, dateTo))
|
||||
).then(
|
||||
(results) => results.reduce((acc, availability) => acc.concat(availability), [])
|
||||
);
|
||||
|
||||
const createMeeting = async (credential, calEvent: CalendarEvent): Promise<any> => {
|
||||
const uid: string = translator.fromUUID(uuidv5(JSON.stringify(calEvent), uuidv5.URL));
|
||||
|
||||
if (!credential) {
|
||||
throw new Error("Credentials must be set! Video platforms are optional, so this method shouldn't even be called when no video credentials are set.");
|
||||
}
|
||||
|
||||
const creationResult = await videoIntegrations([credential])[0].createMeeting(calEvent);
|
||||
|
||||
const videoCallData: VideoCallData = {
|
||||
type: credential.type,
|
||||
id: creationResult.id,
|
||||
password: creationResult.password,
|
||||
url: creationResult.join_url,
|
||||
};
|
||||
|
||||
const organizerMail = new VideoEventOrganizerMail(calEvent, uid, videoCallData);
|
||||
const attendeeMail = new VideoEventAttendeeMail(calEvent, uid, videoCallData);
|
||||
await organizerMail.sendEmail();
|
||||
|
||||
if (!creationResult || !creationResult.disableConfirmationEmail) {
|
||||
await attendeeMail.sendEmail();
|
||||
}
|
||||
|
||||
return {
|
||||
uid,
|
||||
createdEvent: creationResult
|
||||
};
|
||||
};
|
||||
|
||||
const updateMeeting = async (credential, uidToUpdate: String, calEvent: CalendarEvent): Promise<any> => {
|
||||
const newUid: string = translator.fromUUID(uuidv5(JSON.stringify(calEvent), uuidv5.URL));
|
||||
|
||||
if (!credential) {
|
||||
throw new Error("Credentials must be set! Video platforms are optional, so this method shouldn't even be called when no video credentials are set.");
|
||||
}
|
||||
|
||||
const updateResult = credential ? await videoIntegrations([credential])[0].updateMeeting(uidToUpdate, calEvent) : null;
|
||||
|
||||
const organizerMail = new EventOrganizerRescheduledMail(calEvent, newUid);
|
||||
const attendeeMail = new EventAttendeeRescheduledMail(calEvent, newUid);
|
||||
await organizerMail.sendEmail();
|
||||
|
||||
if (!updateResult || !updateResult.disableConfirmationEmail) {
|
||||
await attendeeMail.sendEmail();
|
||||
}
|
||||
|
||||
return {
|
||||
uid: newUid,
|
||||
updatedEvent: updateResult
|
||||
};
|
||||
};
|
||||
|
||||
const deleteMeeting = (credential, uid: String): Promise<any> => {
|
||||
if (credential) {
|
||||
return videoIntegrations([credential])[0].deleteMeeting(uid);
|
||||
}
|
||||
|
||||
return Promise.resolve({});
|
||||
};
|
||||
|
||||
export {getBusyVideoTimes, createMeeting, updateMeeting, deleteMeeting};
|
||||
Reference in new issue
Block a user