Prepared google calendar deletion

This commit is contained in:
nicolas 2021-06-07 01:10:56 +02:00
parent d05ae49e8d
commit b376e9e5a4
3 changed files with 115 additions and 56 deletions

View file

@ -1,4 +1,3 @@
const {google} = require('googleapis'); const {google} = require('googleapis');
import createNewEventEmail from "./emails/new-event"; import createNewEventEmail from "./emails/new-event";
@ -43,7 +42,12 @@ const o365Auth = (credential) => {
}; };
}; };
interface Person { name?: string, email: string, timeZone: string } interface Person {
name?: string,
email: string,
timeZone: string
}
interface CalendarEvent { interface CalendarEvent {
type: string; type: string;
title: string; title: string;
@ -57,6 +61,11 @@ interface CalendarEvent {
interface CalendarApiAdapter { interface CalendarApiAdapter {
createEvent(event: CalendarEvent): Promise<any>; createEvent(event: CalendarEvent): Promise<any>;
updateEvent(uid: String, event: CalendarEvent);
deleteEvent(uid: String);
getAvailability(dateFrom, dateTo): Promise<any>; getAvailability(dateFrom, dateTo): Promise<any>;
} }
@ -122,7 +131,10 @@ const MicrosoftOffice365Calendar = (credential): CalendarApiAdapter => {
}) })
.then(handleErrors) .then(handleErrors)
.then(responseBody => { .then(responseBody => {
return responseBody.value[0].scheduleItems.map( (evt) => ({ start: evt.start.dateTime + 'Z', end: evt.end.dateTime + 'Z' })) return responseBody.value[0].scheduleItems.map((evt) => ({
start: evt.start.dateTime + 'Z',
end: evt.end.dateTime + 'Z'
}))
}) })
).catch((err) => { ).catch((err) => {
console.log(err); console.log(err);
@ -138,7 +150,13 @@ const MicrosoftOffice365Calendar = (credential): CalendarApiAdapter => {
}).then(handleErrors).then((responseBody) => ({ }).then(handleErrors).then((responseBody) => ({
...responseBody, ...responseBody,
disableConfirmationEmail: true, disableConfirmationEmail: true,
}))) }))),
deleteEvent: (uid: String) => {
//TODO Implement
},
updateEvent: (uid: String, event: CalendarEvent) => {
//TODO Implement
},
} }
}; };
@ -210,6 +228,25 @@ const GoogleCalendar = (credential): CalendarApiAdapter => {
} }
return resolve(event.data); return resolve(event.data);
}); });
}),
updateEvent: (uid: String, event: CalendarEvent) => new Promise((resolve, reject) => {
//TODO implement
}),
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: true,
}, function (err, event) {
if (err) {
console.log('There was an error contacting the Calendar service: ' + err);
return reject(err);
}
return resolve(event.data);
});
}) })
}; };
}; };
@ -217,8 +254,10 @@ const GoogleCalendar = (credential): CalendarApiAdapter => {
// factory // factory
const calendars = (withCredentials): CalendarApiAdapter[] => withCredentials.map((cred) => { const calendars = (withCredentials): CalendarApiAdapter[] => withCredentials.map((cred) => {
switch (cred.type) { switch (cred.type) {
case 'google_calendar': return GoogleCalendar(cred); case 'google_calendar':
case 'office365_calendar': return MicrosoftOffice365Calendar(cred); return GoogleCalendar(cred);
case 'office365_calendar':
return MicrosoftOffice365Calendar(cred);
default: default:
return; // unknown credential, could be legacy? In any case, ignore return; // unknown credential, could be legacy? In any case, ignore
} }
@ -244,4 +283,20 @@ const createEvent = (credential, calEvent: CalendarEvent): Promise<any> => {
return Promise.resolve({}); return Promise.resolve({});
}; };
const updateEvent = (credential, uid: String, calEvent: CalendarEvent): Promise<any> => {
if (credential) {
return calendars([credential])[0].updateEvent(uid, calEvent);
}
return Promise.resolve({});
};
const deleteEvent = (credential, uid: String): Promise<any> => {
if (credential) {
return calendars([credential])[0].deleteEvent(uid);
}
return Promise.resolve({});
};
export {getBusyTimes, createEvent, CalendarEvent}; export {getBusyTimes, createEvent, CalendarEvent};

View file

@ -53,7 +53,10 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
userId: currentUser.id, userId: currentUser.id,
references: { references: {
create: [ create: [
//TODO Create references {
type: currentUser.credentials[0].type,
uid: result.id
}
] ]
}, },
eventTypeId: eventType.id, eventTypeId: eventType.id,

View file

@ -28,6 +28,7 @@ export default async function handler(req, res) {
}); });
//TODO Delete booking from calendar integrations //TODO Delete booking from calendar integrations
//TODO Perhaps send emails to user and client to tell about the cancellation
const deleteBooking = await prisma.booking.delete({ const deleteBooking = await prisma.booking.delete({
where: { where: {