Use calendarView instead of events
When calling Microsoft Graph use calendars/calendarView instead of calendars/events to allow occurences to be returned.pull/397/head
parent
062b92be29
commit
04d7a280ef
|
@ -10,8 +10,14 @@ import CalEventParser from "./CalEventParser";
|
||||||
const { google } = require("googleapis");
|
const { google } = require("googleapis");
|
||||||
|
|
||||||
const googleAuth = (credential) => {
|
const googleAuth = (credential) => {
|
||||||
const { client_secret, client_id, redirect_uris } = JSON.parse(process.env.GOOGLE_API_CREDENTIALS).web;
|
const { client_secret, client_id, redirect_uris } = JSON.parse(
|
||||||
const myGoogleAuth = new google.auth.OAuth2(client_id, client_secret, redirect_uris[0]);
|
process.env.GOOGLE_API_CREDENTIALS
|
||||||
|
).web;
|
||||||
|
const myGoogleAuth = new google.auth.OAuth2(
|
||||||
|
client_id,
|
||||||
|
client_secret,
|
||||||
|
redirect_uris[0]
|
||||||
|
);
|
||||||
myGoogleAuth.setCredentials(credential.key);
|
myGoogleAuth.setCredentials(credential.key);
|
||||||
|
|
||||||
const isExpired = () => myGoogleAuth.isTokenExpiring();
|
const isExpired = () => myGoogleAuth.isTokenExpiring();
|
||||||
|
@ -43,7 +49,8 @@ const googleAuth = (credential) => {
|
||||||
});
|
});
|
||||||
|
|
||||||
return {
|
return {
|
||||||
getToken: () => (!isExpired() ? Promise.resolve(myGoogleAuth) : refreshAccessToken()),
|
getToken: () =>
|
||||||
|
!isExpired() ? Promise.resolve(myGoogleAuth) : refreshAccessToken(),
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -81,7 +88,9 @@ const o365Auth = (credential) => {
|
||||||
.then(handleErrorsJson)
|
.then(handleErrorsJson)
|
||||||
.then((responseBody) => {
|
.then((responseBody) => {
|
||||||
credential.key.access_token = responseBody.access_token;
|
credential.key.access_token = responseBody.access_token;
|
||||||
credential.key.expiry_date = Math.round(+new Date() / 1000 + responseBody.expires_in);
|
credential.key.expiry_date = Math.round(
|
||||||
|
+new Date() / 1000 + responseBody.expires_in
|
||||||
|
);
|
||||||
return prisma.credential
|
return prisma.credential
|
||||||
.update({
|
.update({
|
||||||
where: {
|
where: {
|
||||||
|
@ -139,7 +148,11 @@ export interface CalendarApiAdapter {
|
||||||
|
|
||||||
deleteEvent(uid: string);
|
deleteEvent(uid: string);
|
||||||
|
|
||||||
getAvailability(dateFrom, dateTo, selectedCalendars: IntegrationCalendar[]): Promise<unknown>;
|
getAvailability(
|
||||||
|
dateFrom,
|
||||||
|
dateTo,
|
||||||
|
selectedCalendars: IntegrationCalendar[]
|
||||||
|
): Promise<unknown>;
|
||||||
|
|
||||||
listCalendars(): Promise<IntegrationCalendar[]>;
|
listCalendars(): Promise<IntegrationCalendar[]>;
|
||||||
}
|
}
|
||||||
|
@ -206,7 +219,7 @@ const MicrosoftOffice365Calendar = (credential): CalendarApiAdapter => {
|
||||||
|
|
||||||
return {
|
return {
|
||||||
getAvailability: (dateFrom, dateTo, selectedCalendars) => {
|
getAvailability: (dateFrom, dateTo, selectedCalendars) => {
|
||||||
const filter = "?$filter=start/dateTime ge '" + dateFrom + "' and end/dateTime le '" + dateTo + "'";
|
const filter = "?startdatetime=" + dateFrom + "&enddatetime=" + dateTo;
|
||||||
return auth
|
return auth
|
||||||
.getToken()
|
.getToken()
|
||||||
.then((accessToken) => {
|
.then((accessToken) => {
|
||||||
|
@ -229,7 +242,7 @@ const MicrosoftOffice365Calendar = (credential): CalendarApiAdapter => {
|
||||||
headers: {
|
headers: {
|
||||||
Prefer: 'outlook.timezone="Etc/GMT"',
|
Prefer: 'outlook.timezone="Etc/GMT"',
|
||||||
},
|
},
|
||||||
url: `/me/calendars/${calendarId}/events${filter}`,
|
url: `/me/calendars/${calendarId}/calendarView${filter}`,
|
||||||
}));
|
}));
|
||||||
|
|
||||||
return fetch("https://graph.microsoft.com/v1.0/$batch", {
|
return fetch("https://graph.microsoft.com/v1.0/$batch", {
|
||||||
|
@ -309,7 +322,10 @@ const GoogleCalendar = (credential): CalendarApiAdapter => {
|
||||||
getAvailability: (dateFrom, dateTo, selectedCalendars) =>
|
getAvailability: (dateFrom, dateTo, selectedCalendars) =>
|
||||||
new Promise((resolve, reject) =>
|
new Promise((resolve, reject) =>
|
||||||
auth.getToken().then((myGoogleAuth) => {
|
auth.getToken().then((myGoogleAuth) => {
|
||||||
const calendar = google.calendar({ version: "v3", auth: myGoogleAuth });
|
const calendar = google.calendar({
|
||||||
|
version: "v3",
|
||||||
|
auth: myGoogleAuth,
|
||||||
|
});
|
||||||
const selectedCalendarIds = selectedCalendars
|
const selectedCalendarIds = selectedCalendars
|
||||||
.filter((e) => e.integration === integrationType)
|
.filter((e) => e.integration === integrationType)
|
||||||
.map((e) => e.externalId);
|
.map((e) => e.externalId);
|
||||||
|
@ -320,7 +336,9 @@ const GoogleCalendar = (credential): CalendarApiAdapter => {
|
||||||
}
|
}
|
||||||
|
|
||||||
(selectedCalendarIds.length == 0
|
(selectedCalendarIds.length == 0
|
||||||
? calendar.calendarList.list().then((cals) => cals.data.items.map((cal) => cal.id))
|
? calendar.calendarList
|
||||||
|
.list()
|
||||||
|
.then((cals) => cals.data.items.map((cal) => cal.id))
|
||||||
: Promise.resolve(selectedCalendarIds)
|
: Promise.resolve(selectedCalendarIds)
|
||||||
)
|
)
|
||||||
.then((calsIds) => {
|
.then((calsIds) => {
|
||||||
|
@ -336,12 +354,19 @@ const GoogleCalendar = (credential): CalendarApiAdapter => {
|
||||||
if (err) {
|
if (err) {
|
||||||
reject(err);
|
reject(err);
|
||||||
}
|
}
|
||||||
resolve(Object.values(apires.data.calendars).flatMap((item) => item["busy"]));
|
resolve(
|
||||||
|
Object.values(apires.data.calendars).flatMap(
|
||||||
|
(item) => item["busy"]
|
||||||
|
)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
})
|
})
|
||||||
.catch((err) => {
|
.catch((err) => {
|
||||||
console.error("There was an error contacting google calendar service: ", err);
|
console.error(
|
||||||
|
"There was an error contacting google calendar service: ",
|
||||||
|
err
|
||||||
|
);
|
||||||
reject(err);
|
reject(err);
|
||||||
});
|
});
|
||||||
})
|
})
|
||||||
|
@ -375,7 +400,10 @@ const GoogleCalendar = (credential): CalendarApiAdapter => {
|
||||||
payload["conferenceData"] = event.conferenceData;
|
payload["conferenceData"] = event.conferenceData;
|
||||||
}
|
}
|
||||||
|
|
||||||
const calendar = google.calendar({ version: "v3", auth: myGoogleAuth });
|
const calendar = google.calendar({
|
||||||
|
version: "v3",
|
||||||
|
auth: myGoogleAuth,
|
||||||
|
});
|
||||||
calendar.events.insert(
|
calendar.events.insert(
|
||||||
{
|
{
|
||||||
auth: myGoogleAuth,
|
auth: myGoogleAuth,
|
||||||
|
@ -385,7 +413,10 @@ const GoogleCalendar = (credential): CalendarApiAdapter => {
|
||||||
},
|
},
|
||||||
function (err, event) {
|
function (err, event) {
|
||||||
if (err) {
|
if (err) {
|
||||||
console.error("There was an error contacting google calendar service: ", err);
|
console.error(
|
||||||
|
"There was an error contacting google calendar service: ",
|
||||||
|
err
|
||||||
|
);
|
||||||
return reject(err);
|
return reject(err);
|
||||||
}
|
}
|
||||||
return resolve(event.data);
|
return resolve(event.data);
|
||||||
|
@ -418,7 +449,10 @@ const GoogleCalendar = (credential): CalendarApiAdapter => {
|
||||||
payload["location"] = event.location;
|
payload["location"] = event.location;
|
||||||
}
|
}
|
||||||
|
|
||||||
const calendar = google.calendar({ version: "v3", auth: myGoogleAuth });
|
const calendar = google.calendar({
|
||||||
|
version: "v3",
|
||||||
|
auth: myGoogleAuth,
|
||||||
|
});
|
||||||
calendar.events.update(
|
calendar.events.update(
|
||||||
{
|
{
|
||||||
auth: myGoogleAuth,
|
auth: myGoogleAuth,
|
||||||
|
@ -430,7 +464,10 @@ const GoogleCalendar = (credential): CalendarApiAdapter => {
|
||||||
},
|
},
|
||||||
function (err, event) {
|
function (err, event) {
|
||||||
if (err) {
|
if (err) {
|
||||||
console.error("There was an error contacting google calendar service: ", err);
|
console.error(
|
||||||
|
"There was an error contacting google calendar service: ",
|
||||||
|
err
|
||||||
|
);
|
||||||
return reject(err);
|
return reject(err);
|
||||||
}
|
}
|
||||||
return resolve(event.data);
|
return resolve(event.data);
|
||||||
|
@ -441,7 +478,10 @@ const GoogleCalendar = (credential): CalendarApiAdapter => {
|
||||||
deleteEvent: (uid: string) =>
|
deleteEvent: (uid: string) =>
|
||||||
new Promise((resolve, reject) =>
|
new Promise((resolve, reject) =>
|
||||||
auth.getToken().then((myGoogleAuth) => {
|
auth.getToken().then((myGoogleAuth) => {
|
||||||
const calendar = google.calendar({ version: "v3", auth: myGoogleAuth });
|
const calendar = google.calendar({
|
||||||
|
version: "v3",
|
||||||
|
auth: myGoogleAuth,
|
||||||
|
});
|
||||||
calendar.events.delete(
|
calendar.events.delete(
|
||||||
{
|
{
|
||||||
auth: myGoogleAuth,
|
auth: myGoogleAuth,
|
||||||
|
@ -452,7 +492,10 @@ const GoogleCalendar = (credential): CalendarApiAdapter => {
|
||||||
},
|
},
|
||||||
function (err, event) {
|
function (err, event) {
|
||||||
if (err) {
|
if (err) {
|
||||||
console.error("There was an error contacting google calendar service: ", err);
|
console.error(
|
||||||
|
"There was an error contacting google calendar service: ",
|
||||||
|
err
|
||||||
|
);
|
||||||
return reject(err);
|
return reject(err);
|
||||||
}
|
}
|
||||||
return resolve(event.data);
|
return resolve(event.data);
|
||||||
|
@ -463,7 +506,10 @@ const GoogleCalendar = (credential): CalendarApiAdapter => {
|
||||||
listCalendars: () =>
|
listCalendars: () =>
|
||||||
new Promise((resolve, reject) =>
|
new Promise((resolve, reject) =>
|
||||||
auth.getToken().then((myGoogleAuth) => {
|
auth.getToken().then((myGoogleAuth) => {
|
||||||
const calendar = google.calendar({ version: "v3", auth: myGoogleAuth });
|
const calendar = google.calendar({
|
||||||
|
version: "v3",
|
||||||
|
auth: myGoogleAuth,
|
||||||
|
});
|
||||||
calendar.calendarList
|
calendar.calendarList
|
||||||
.list()
|
.list()
|
||||||
.then((cals) => {
|
.then((cals) => {
|
||||||
|
@ -480,7 +526,10 @@ const GoogleCalendar = (credential): CalendarApiAdapter => {
|
||||||
);
|
);
|
||||||
})
|
})
|
||||||
.catch((err) => {
|
.catch((err) => {
|
||||||
console.error("There was an error contacting google calendar service: ", err);
|
console.error(
|
||||||
|
"There was an error contacting google calendar service: ",
|
||||||
|
err
|
||||||
|
);
|
||||||
reject(err);
|
reject(err);
|
||||||
});
|
});
|
||||||
})
|
})
|
||||||
|
@ -503,19 +552,29 @@ const calendars = (withCredentials): CalendarApiAdapter[] =>
|
||||||
})
|
})
|
||||||
.filter(Boolean);
|
.filter(Boolean);
|
||||||
|
|
||||||
const getBusyCalendarTimes = (withCredentials, dateFrom, dateTo, selectedCalendars) =>
|
const getBusyCalendarTimes = (
|
||||||
|
withCredentials,
|
||||||
|
dateFrom,
|
||||||
|
dateTo,
|
||||||
|
selectedCalendars
|
||||||
|
) =>
|
||||||
Promise.all(
|
Promise.all(
|
||||||
calendars(withCredentials).map((c) => c.getAvailability(dateFrom, dateTo, selectedCalendars))
|
calendars(withCredentials).map((c) =>
|
||||||
|
c.getAvailability(dateFrom, dateTo, selectedCalendars)
|
||||||
|
)
|
||||||
).then((results) => {
|
).then((results) => {
|
||||||
return results.reduce((acc, availability) => acc.concat(availability), []);
|
return results.reduce((acc, availability) => acc.concat(availability), []);
|
||||||
});
|
});
|
||||||
|
|
||||||
const listCalendars = (withCredentials) =>
|
const listCalendars = (withCredentials) =>
|
||||||
Promise.all(calendars(withCredentials).map((c) => c.listCalendars())).then((results) =>
|
Promise.all(calendars(withCredentials).map((c) => c.listCalendars())).then(
|
||||||
results.reduce((acc, calendars) => acc.concat(calendars), [])
|
(results) => results.reduce((acc, calendars) => acc.concat(calendars), [])
|
||||||
);
|
);
|
||||||
|
|
||||||
const createEvent = async (credential: Credential, calEvent: CalendarEvent): Promise<unknown> => {
|
const createEvent = async (
|
||||||
|
credential: Credential,
|
||||||
|
calEvent: CalendarEvent
|
||||||
|
): Promise<unknown> => {
|
||||||
const parser: CalEventParser = new CalEventParser(calEvent);
|
const parser: CalEventParser = new CalEventParser(calEvent);
|
||||||
const uid: string = parser.getUid();
|
const uid: string = parser.getUid();
|
||||||
/*
|
/*
|
||||||
|
@ -525,7 +584,9 @@ const createEvent = async (credential: Credential, calEvent: CalendarEvent): Pro
|
||||||
*/
|
*/
|
||||||
const richEvent: CalendarEvent = parser.asRichEventPlain();
|
const richEvent: CalendarEvent = parser.asRichEventPlain();
|
||||||
|
|
||||||
const creationResult = credential ? await calendars([credential])[0].createEvent(richEvent) : null;
|
const creationResult = credential
|
||||||
|
? await calendars([credential])[0].createEvent(richEvent)
|
||||||
|
: null;
|
||||||
|
|
||||||
const maybeHangoutLink = creationResult?.hangoutLink;
|
const maybeHangoutLink = creationResult?.hangoutLink;
|
||||||
const maybeEntryPoints = creationResult?.entryPoints;
|
const maybeEntryPoints = creationResult?.entryPoints;
|
||||||
|
|
Loading…
Reference in New Issue