mirror of
https://github.com/tomru/pfadi-bussle.git
synced 2026-03-03 06:27:11 +01:00
add/delete entry in google calendar
This commit is contained in:
79
lib/googlecalendar.ts
Normal file
79
lib/googlecalendar.ts
Normal file
@@ -0,0 +1,79 @@
|
||||
|
||||
import {google} from 'googleapis';
|
||||
import { Booking } from '../db/booking';
|
||||
|
||||
const keyFile = process.env.GOOGLE_SERVICE_ACCOUNT_KEY_FILE;
|
||||
const calendarId = process.env.GOOGLE_CALENDAR_ID;
|
||||
|
||||
const auth = new google.auth.GoogleAuth({
|
||||
keyFile,
|
||||
scopes: [
|
||||
'https://www.googleapis.com/auth/calendar',
|
||||
'https://www.googleapis.com/auth/calendar.readonly'
|
||||
],
|
||||
});
|
||||
|
||||
const calendar = google.calendar({
|
||||
version: 'v3',
|
||||
auth,
|
||||
});
|
||||
|
||||
export async function getNewEvents() {
|
||||
const { data } = await calendar.events.list({
|
||||
calendarId,
|
||||
timeMin: new Date().toISOString()}
|
||||
);
|
||||
|
||||
return data.items;
|
||||
}
|
||||
|
||||
function getSummary(booking: Partial<Booking>) : string {
|
||||
let summary = "";
|
||||
|
||||
if (booking.org) {
|
||||
summary += `${booking.org} - `;
|
||||
}
|
||||
|
||||
summary += booking.name;
|
||||
|
||||
return summary;
|
||||
}
|
||||
|
||||
export async function createCalendarEvent(booking : Booking) : Promise<Booking> {
|
||||
|
||||
const response = await calendar.events.insert({
|
||||
auth,
|
||||
calendarId,
|
||||
requestBody: {
|
||||
summary: getSummary(booking),
|
||||
// description,
|
||||
start: { date: booking.startDate },
|
||||
end: { date: booking.endDate },
|
||||
}
|
||||
});
|
||||
|
||||
booking.calendarEventId = response.data.id;
|
||||
|
||||
return booking;
|
||||
}
|
||||
|
||||
export async function deleteCalendarEvent(booking: Booking) {
|
||||
await calendar.events.delete({
|
||||
auth,
|
||||
calendarId,
|
||||
eventId: booking.calendarEventId,
|
||||
});
|
||||
|
||||
booking.calendarEventId = null;
|
||||
}
|
||||
|
||||
//export async function patchCalendarEvent(booking: { calendarEventId: string } & Partial<Booking> ) : Promise<Booking> {
|
||||
// const response = await calendar.events.patch({
|
||||
// auth,
|
||||
// calendarId,
|
||||
// eventId: booking.calendarEventId,
|
||||
// });
|
||||
//
|
||||
// return booking;
|
||||
//}
|
||||
|
||||
Reference in New Issue
Block a user