-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
205 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { GroupRepository, config } from '@synk-cal/core' | ||
import { GoogleGroupRepository } from '@synk-cal/google' | ||
|
||
export function getGroupRepository(): GroupRepository | undefined { | ||
if (config.GROUP_PROVIDER === 'google') { | ||
return new GoogleGroupRepository() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
export interface Group { | ||
id: string | ||
email: string | ||
name: string | ||
description?: string | ||
} | ||
|
||
export interface GroupMember { | ||
id: string | ||
email: string | ||
type: string | ||
} | ||
|
||
export interface GroupRepository { | ||
getGroups(): Promise<Group[]> | ||
getGroupMembers(groupId: string): Promise<GroupMember[]> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
export * from './calendar' | ||
export { config } from './config' | ||
export * from './group' | ||
export * from './notification' | ||
export * from './reminder' | ||
export * from './user' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { type Group, type GroupMember, type GroupRepository, config } from '@synk-cal/core' | ||
import { type cloudidentity_v1, google } from 'googleapis' | ||
|
||
let cloudIdentityClient: cloudidentity_v1.Cloudidentity | null = null | ||
|
||
export class GoogleGroupRepository implements GroupRepository { | ||
async getCloudIdentityClient() { | ||
if (cloudIdentityClient) { | ||
return cloudIdentityClient | ||
} | ||
|
||
// Google Cloud Identity API setup | ||
const auth = new google.auth.GoogleAuth({ | ||
keyFile: process.env.GOOGLE_APPLICATION_CREDENTIALS, | ||
scopes: [ | ||
'https://www.googleapis.com/auth/cloud-identity.groups.readonly', | ||
'https://www.googleapis.com/auth/cloud-identity.users.readonly', | ||
], | ||
}) | ||
|
||
const authClient = await auth.getClient() | ||
// @ts-expect-error | ||
cloudIdentityClient = google.cloudidentity({ version: 'v1', auth: authClient }) | ||
|
||
return cloudIdentityClient | ||
} | ||
|
||
async getGroups(): Promise<Group[]> { | ||
const client = await this.getCloudIdentityClient() | ||
|
||
const response = await client.groups.list({ | ||
parent: `customers/${config.GROUP_CUSTOMER_ID}`, | ||
view: 'BASIC', | ||
// FIXME: handle pagination | ||
pageSize: 1000, | ||
}) | ||
|
||
const groups = response.data.groups || [] | ||
|
||
return groups.map((group) => ({ | ||
id: group.name ?? '', | ||
email: group.groupKey?.id ?? '', | ||
name: group.displayName ?? '', | ||
description: group.description ?? undefined, | ||
})) | ||
} | ||
|
||
async getGroupMembers(groupId: string): Promise<GroupMember[]> { | ||
const client = await this.getCloudIdentityClient() | ||
|
||
const response = await client.groups.memberships.list({ | ||
parent: groupId, | ||
// FIXME: handle pagination | ||
pageSize: 1000, | ||
}) | ||
|
||
const memberships = response.data.memberships || [] | ||
|
||
return memberships.map((membership) => ({ | ||
id: membership.name ?? '', | ||
email: membership.preferredMemberKey?.id ?? '', | ||
type: membership.type ?? '', | ||
})) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from './firestore' | ||
export * from './google_calendar' | ||
export * from './google_group' | ||
export * from './iap' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { type CalendarEvent, type CalendarRepository, Group, GroupMember, type GroupRepository } from '@synk-cal/core' | ||
|
||
interface GetEventsUseCaseParams { | ||
calendarRepository: CalendarRepository | ||
groupRepository?: GroupRepository | ||
minDate: Date | ||
maxDate: Date | ||
} | ||
|
||
export const getEvents = async ({ | ||
calendarRepository, | ||
groupRepository, | ||
minDate, | ||
maxDate, | ||
}: GetEventsUseCaseParams): Promise<CalendarEvent[]> => { | ||
const events = await calendarRepository.getEvents(minDate, maxDate) | ||
|
||
const expandedEvents: CalendarEvent[] = [] | ||
|
||
const groupCache: Record<string, { group: Group; members?: GroupMember[] }> = {} | ||
if (groupRepository) { | ||
const groups = await groupRepository.getGroups() | ||
for (const group of groups) { | ||
// members are fetched lazily | ||
groupCache[group.email] = { group } | ||
console.log(group.email) | ||
} | ||
} | ||
|
||
for (const event of events) { | ||
const expandedAttendees = [] | ||
for (const attendee of event.people) { | ||
const cachedGroup = attendee.email ? groupCache[attendee.email] : undefined | ||
if (cachedGroup && groupRepository) { | ||
try { | ||
let groupMembers: GroupMember[] | ||
if (cachedGroup.members) { | ||
groupMembers = cachedGroup.members | ||
} else { | ||
groupMembers = await groupRepository.getGroupMembers(cachedGroup.group.id) | ||
groupCache[cachedGroup.group.email] = { ...cachedGroup, members: groupMembers } | ||
} | ||
for (const member of groupMembers) { | ||
expandedAttendees.push({ | ||
email: member.email, | ||
responseStatus: attendee.responseStatus, | ||
organizer: false, | ||
}) | ||
} | ||
} catch (error) { | ||
// fallback | ||
console.error(`Error expanding group ${attendee.email}:`, error) | ||
expandedAttendees.push(attendee) | ||
} | ||
} else { | ||
expandedAttendees.push(attendee) | ||
} | ||
} | ||
|
||
expandedEvents.push({ | ||
...event, | ||
people: expandedAttendees, | ||
}) | ||
} | ||
|
||
return expandedEvents | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from './get_events' | ||
export * from './process_reminders' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters