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');
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 {
type: string;
title: string;
@ -57,6 +61,11 @@ interface CalendarEvent {
interface CalendarApiAdapter {
createEvent(event: CalendarEvent): Promise<any>;
updateEvent(uid: String, event: CalendarEvent);
deleteEvent(uid: String);
getAvailability(dateFrom, dateTo): Promise<any>;
}
@ -122,7 +131,10 @@ const MicrosoftOffice365Calendar = (credential): CalendarApiAdapter => {
})
.then(handleErrors)
.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) => {
console.log(err);
@ -138,7 +150,13 @@ const MicrosoftOffice365Calendar = (credential): CalendarApiAdapter => {
}).then(handleErrors).then((responseBody) => ({
...responseBody,
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);
});
}),
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
const calendars = (withCredentials): CalendarApiAdapter[] => withCredentials.map((cred) => {
switch (cred.type) {
case 'google_calendar': return GoogleCalendar(cred);
case 'office365_calendar': return MicrosoftOffice365Calendar(cred);
case 'google_calendar':
return GoogleCalendar(cred);
case 'office365_calendar':
return MicrosoftOffice365Calendar(cred);
default:
return; // unknown credential, could be legacy? In any case, ignore
}
@ -244,4 +283,20 @@ const createEvent = (credential, calEvent: CalendarEvent): Promise<any> => {
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};

View file

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

View file

@ -28,6 +28,7 @@ export default async function handler(req, res) {
});
//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({
where: {