Skip to content

Commit

Permalink
Merge pull request #85 from enpit-super-koyomi/feature/multi-calendar
Browse files Browse the repository at this point in the history
複数のカレンダーの取得に対応
  • Loading branch information
HosokawaR authored Jan 23, 2025
2 parents 9b99294 + 5f283e8 commit f14c07e
Showing 1 changed file with 32 additions and 32 deletions.
64 changes: 32 additions & 32 deletions src/lib/googleCalendar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,47 +17,47 @@ export class GoogleCalendar implements Calendar {

async getEvents(): Promise<CalEvent[]> {
const { data: calendars } = await this.client.calendarList.list()
const primaryCalendarId = calendars.items?.find(calendar => calendar.primary)?.id
if (!primaryCalendarId) {
throw new Error("Primary calendar not found")
}

const { data: events } = await this.client.events.list({
calendarId: primaryCalendarId!,
timeMin: new Date().toISOString(),
timeMax: new Date(Date.now() + FETCH_EVENTS_DURATION).toISOString(),
singleEvents: true,
orderBy: "startTime",
})
const calendarIds = calendars.items?.map(calendar => calendar.id).filter(it => it != null) ?? []

const events = await Promise.all(
calendarIds.map(async calendarId => {
const { data: events } = await this.client.events.list({
calendarId,
timeMin: new Date().toISOString(),
timeMax: new Date(Date.now() + FETCH_EVENTS_DURATION).toISOString(),
singleEvents: true,
orderBy: "startTime",
})
return events
}),
)

return (
events.items?.map(event => ({
id: event.id ?? null,
summary: event.summary ?? DEFAULT_EVENT.summary,
description: event.description ?? null,
location: event.location ?? null,
start: event.start ? new Date(event.start.dateTime!) : DEFAULT_EVENT.start,
end: event.end ? new Date(event.end.dateTime!) : DEFAULT_EVENT.end,
status: event.status
? this.validateEventStatus(event.status)
? event.status
: null
: null,
attendees: event.attendees?.map(it => ({ email: it.email ?? null })) ?? [],
})) ?? []
return events.flatMap(
event =>
event.items?.map(event => ({
id: event.id ?? null,
summary: event.summary ?? DEFAULT_EVENT.summary,
description: event.description ?? null,
location: event.location ?? null,
start: event.start ? new Date(event.start.dateTime!) : DEFAULT_EVENT.start,
end: event.end ? new Date(event.end.dateTime!) : DEFAULT_EVENT.end,
status: event.status
? this.validateEventStatus(event.status)
? event.status
: null
: null,
attendees: event.attendees?.map(it => ({ email: it.email ?? null })) ?? [],
})) ?? [],
)
}

async getBusyPeriods(): Promise<SlimedCalEvent[]> {
const { data: calendars } = await this.client.calendarList.list()
const primaryCalendarId = calendars.items?.find(calendar => calendar.primary)?.id
if (!primaryCalendarId) {
throw new Error("Primary calendar not found")
}
const calendarIds = calendars.items?.map(calendar => calendar.id) ?? []

const freebusyResult = await this.client.freebusy.query({
requestBody: {
items: [{ id: primaryCalendarId }],
items: calendarIds.map(id => ({ id })),
groupExpansionMax: 100,
calendarExpansionMax: 50,
timeMin: new Date().toISOString(),
Expand Down

0 comments on commit f14c07e

Please sign in to comment.