make meeting creation user level
This commit is contained in:
@@ -1,7 +1,34 @@
|
||||
'use server'
|
||||
import { clerkClient, auth } from '@clerk/nextjs/server'
|
||||
import { google } from 'googleapis'
|
||||
import { env } from '~/env'
|
||||
import { getGoogleCalendarClient, getGoogleCalendarId } from '~/server/googleCalendar'
|
||||
|
||||
function googleCalendarDate(date: Date) {
|
||||
return date.toISOString().replace(/[-:]|\.\d{3}/g, '')
|
||||
}
|
||||
|
||||
function createGoogleCalendarTemplateLink({
|
||||
title,
|
||||
description,
|
||||
startTime,
|
||||
endTime,
|
||||
gregorEmail,
|
||||
}: {
|
||||
title: string
|
||||
description: string
|
||||
startTime: Date
|
||||
endTime: Date
|
||||
gregorEmail: string
|
||||
}) {
|
||||
const params = new URLSearchParams({
|
||||
action: 'TEMPLATE',
|
||||
text: title,
|
||||
dates: `${googleCalendarDate(startTime)}/${googleCalendarDate(endTime)}`,
|
||||
details: description,
|
||||
add: gregorEmail,
|
||||
})
|
||||
|
||||
return `https://calendar.google.com/calendar/render?${params.toString()}`
|
||||
}
|
||||
|
||||
export async function scheduleMeeting({
|
||||
title,
|
||||
@@ -19,59 +46,39 @@ export async function scheduleMeeting({
|
||||
attendeeName?: string
|
||||
}) {
|
||||
try {
|
||||
const clerk = await clerkClient()
|
||||
const userAuth = await auth()
|
||||
const user = await clerk.users.getUser(userAuth.userId?userAuth.userId:"")
|
||||
// Get admin's Google OAuth token to create the event on Gregor's calendar
|
||||
const adminTokenResponse = await clerk.users.getUserOauthAccessToken(
|
||||
env.ADMIN_USER_CLERK_ID,
|
||||
'oauth_google',
|
||||
)
|
||||
const adminToken = adminTokenResponse.data[0]
|
||||
|
||||
if (!adminToken?.token) {
|
||||
return { success: false, error: 'Admin Google Calendar not connected. Ensure the admin account is linked with Google and has calendar scope enabled.' }
|
||||
}
|
||||
|
||||
// Try to resolve visitor's Google email for the invite
|
||||
let visitorEmail: string | undefined = attendeeEmail
|
||||
if (!visitorEmail) {
|
||||
visitorEmail = user?.emailAddresses.at(0)?.emailAddress ?? undefined
|
||||
}
|
||||
|
||||
const oAuth2Client = new google.auth.OAuth2()
|
||||
oAuth2Client.setCredentials({ access_token: adminToken.token })
|
||||
const calendar = google.calendar({ version: 'v3', auth: oAuth2Client })
|
||||
const calendar = getGoogleCalendarClient()
|
||||
|
||||
const startTime = new Date(dateTime)
|
||||
const endTime = new Date(startTime.getTime() + durationMinutes * 60 * 1000)
|
||||
const attendeeNote = attendeeEmail
|
||||
? `\n\nVisitor: ${attendeeName ?? 'Unknown'} <${attendeeEmail}>`
|
||||
: ''
|
||||
const eventDescription = `${description}${attendeeNote}`
|
||||
|
||||
const attendees: { email: string; displayName?: string }[] = []
|
||||
if (visitorEmail) {
|
||||
attendees.push({ email: visitorEmail, displayName: attendeeName })
|
||||
const eventRequest = {
|
||||
summary: title,
|
||||
description: eventDescription,
|
||||
start: { dateTime: startTime.toISOString(), timeZone: 'UTC' },
|
||||
end: { dateTime: endTime.toISOString(), timeZone: 'UTC' },
|
||||
}
|
||||
|
||||
const event = await calendar.events.insert({
|
||||
calendarId: 'primary',
|
||||
sendUpdates: 'all',
|
||||
requestBody: {
|
||||
summary: title,
|
||||
description,
|
||||
start: { dateTime: startTime.toISOString(), timeZone: 'UTC' },
|
||||
end: { dateTime: endTime.toISOString(), timeZone: 'UTC' },
|
||||
attendees,
|
||||
},
|
||||
sendNotifications: true
|
||||
calendarId: getGoogleCalendarId(),
|
||||
requestBody: eventRequest,
|
||||
})
|
||||
|
||||
const inviteLink = event.data.htmlLink ?? undefined
|
||||
const addToCalendarLink = createGoogleCalendarTemplateLink({
|
||||
title,
|
||||
description,
|
||||
startTime,
|
||||
endTime,
|
||||
gregorEmail: env.GREGOR_MEETING_EMAIL,
|
||||
})
|
||||
|
||||
return {
|
||||
success: true,
|
||||
eventId: event.data.id,
|
||||
htmlLink: inviteLink,
|
||||
inviteLink,
|
||||
message: `Meeting "${title}" scheduled for ${startTime.toLocaleString()}${visitorEmail ? `. Invite sent to ${visitorEmail}.` : '.'}${inviteLink ? ` Calendar invite: ${inviteLink}` : ''}`,
|
||||
addToCalendarLink,
|
||||
message: `Meeting "${title}" scheduled for ${startTime.toLocaleString()}.${attendeeEmail ? ` Visitor email noted: ${attendeeEmail}.` : ''} The add-to-calendar link invites ${env.GREGOR_MEETING_EMAIL}.`,
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Failed to schedule meeting:', error)
|
||||
|
||||
Reference in New Issue
Block a user