My-Weekly-ToDo-List/src/lib/google-calendar.ts

189 lines
4.5 KiB
TypeScript

// Google Calendar OAuth wrapper
import { google } from 'googleapis';
export interface GoogleCalendarEvent {
id: string;
summary: string;
description?: string;
start: {
dateTime?: string;
date?: string;
};
end: {
dateTime?: string;
date?: string;
};
attendees?: Array<{
email: string;
displayName?: string;
}>;
location?: string;
colorId?: string;
}
export interface GoogleCalendar {
id: string;
summary: string;
primary?: boolean;
backgroundColor?: string;
}
/**
* Initialize Google Calendar OAuth client
*/
export const initializeOAuth = (clientId: string, clientSecret: string, redirectUri: string) => {
const oauth2Client = new google.auth.OAuth2(
clientId,
clientSecret,
redirectUri
);
return oauth2Client;
};
/**
* Get user calendar list using OAuth token
*/
export const getUserCalendars = async (
oauth2Client: any,
accessToken: string
): Promise<GoogleCalendar[]> => {
// Set access token
oauth2Client.setCredentials({ access_token: accessToken });
try {
const calendar = google.calendar({ version: 'v3', auth: oauth2Client });
const response = await calendar.calendarList.list();
return response.data.items?.map((item: any) => ({
id: item.id,
summary: item.summary,
primary: item.primary,
backgroundColor: item.backgroundColor,
})) || [];
} catch (error) {
console.error('Error fetching user calendars:', error);
throw new Error('Failed to fetch user calendars');
}
};
/**
* Get upcoming events for a specified time period
*/
export const getUpcomingEvents = async (
oauth2Client: any,
accessToken: string,
calendarId: string,
timeMin: string,
timeMax: string
): Promise<GoogleCalendarEvent[]> => {
// Set access token
oauth2Client.setCredentials({ access_token: accessToken });
try {
const calendar = google.calendar({ version: 'v3', auth: oauth2Client });
const response = await calendar.events.list({
calendarId,
timeMin,
timeMax,
singleEvents: true,
orderBy: 'startTime',
});
return response.data.items?.map((item: any) => ({
id: item.id,
summary: item.summary,
description: item.description,
start: item.start,
end: item.end,
attendees: item.attendees,
location: item.location,
colorId: item.colorId,
})) || [];
} catch (error) {
console.error('Error fetching upcoming events:', error);
throw new Error('Failed to fetch upcoming events');
}
};
/**
* Create a new event
*/
export const createEvent = async (
oauth2Client: any,
accessToken: string,
calendarId: string,
event: Partial<GoogleCalendarEvent>
): Promise<GoogleCalendarEvent> => {
oauth2Client.setCredentials({ access_token: accessToken });
try {
const calendar = google.calendar({ version: 'v3', auth: oauth2Client });
const response = await calendar.events.insert({
calendarId,
requestBody: {
summary: event.summary,
description: event.description,
start: event.start,
end: event.end,
location: event.location,
},
});
return response.data as any;
} catch (error) {
console.error('Error creating event:', error);
throw new Error('Failed to create event');
}
};
/**
* Update an existing event
*/
export const updateEvent = async (
oauth2Client: any,
accessToken: string,
calendarId: string,
eventId: string,
event: Partial<GoogleCalendarEvent>
): Promise<GoogleCalendarEvent> => {
oauth2Client.setCredentials({ access_token: accessToken });
try {
const calendar = google.calendar({ version: 'v3', auth: oauth2Client });
const response = await calendar.events.patch({
calendarId,
eventId,
requestBody: {
summary: event.summary,
description: event.description,
start: event.start,
end: event.end,
location: event.location,
},
});
return response.data as any;
} catch (error) {
console.error('Error updating event:', error);
throw new Error('Failed to update event');
}
};
/**
* Delete an event
*/
export const deleteEvent = async (
oauth2Client: any,
accessToken: string,
calendarId: string,
eventId: string
): Promise<void> => {
oauth2Client.setCredentials({ access_token: accessToken });
try {
const calendar = google.calendar({ version: 'v3', auth: oauth2Client });
await calendar.events.delete({
calendarId,
eventId,
});
} catch (error) {
console.error('Error deleting event:', error);
throw new Error('Failed to delete event');
}
};