-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Recall integration #815
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
edulelis
wants to merge
25
commits into
elie222:main
Choose a base branch
from
edulelis:recall-integration
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Recall integration #815
Changes from 4 commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
1b1247a
Recall integration
edulelis bc742f1
Cleanup comments
edulelis 9ba3970
PR feedback
edulelis 7bdd809
PR feedback
edulelis 966a32a
Fix svix signature verification
edulelis cda1b75
PR feedback
edulelis 8b723c8
PR feedback
edulelis 73b8197
Merge branch 'main' of github.com:elie222/inbox-zero into recall-inte…
edulelis a0c4fad
Add migration
edulelis 61377aa
Clean unused types
edulelis d8c5172
PR feedback
edulelis c902a82
Fix schema prisma
edulelis 11586ad
Fix schema merge with main
edulelis c115c60
Fix bot not saving transcripts
edulelis 29d2939
Merge branch 'main' of github.com:elie222/inbox-zero into recall-inte…
edulelis a900840
Merge branch 'main' into recall-integration
elie222 2ea3641
schema updatedat
elie222 5a7a450
rename enum
elie222 dd5be3c
Add transcript setting page
edulelis 1ad6c69
PR feedback
edulelis ff24361
Merge branch 'recall-integration' of github.com:edulelis/inbox-zero i…
edulelis 86b196f
Merge branch 'main' of github.com:elie222/inbox-zero into recall-inte…
edulelis 8c58bd8
PR feedback
edulelis 3e25216
Revert changes
edulelis 26195ae
Merge branch 'main' of github.com:elie222/inbox-zero into recall-inte…
edulelis File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,180 @@ | ||
| import { NextResponse } from "next/server"; | ||
| import { env } from "@/env"; | ||
| import prisma from "@/utils/prisma"; | ||
| import { | ||
| createRecallCalendar, | ||
| getRecallCalendar, | ||
| deleteRecallCalendar, | ||
| } from "@/utils/recall/calendar"; | ||
| import { createScopedLogger } from "@/utils/logger"; | ||
| import { withEmailAccount } from "@/utils/middleware"; | ||
|
|
||
| const logger = createScopedLogger("recall/calendar"); | ||
|
|
||
| export type CreateRecallCalendarResponse = Awaited< | ||
| ReturnType<typeof createRecallCalendarForConnection> | ||
| >; | ||
|
|
||
| export const POST = withEmailAccount(async (request) => { | ||
| const { emailAccountId } = request.auth; | ||
|
|
||
| const result = await createRecallCalendarForConnection({ emailAccountId }); | ||
| return NextResponse.json(result); | ||
| }); | ||
|
|
||
| export const DELETE = withEmailAccount(async (request) => { | ||
| const { emailAccountId } = request.auth; | ||
|
|
||
| const result = await deleteRecallCalendarForConnection({ emailAccountId }); | ||
| return NextResponse.json(result); | ||
| }); | ||
edulelis marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| async function createRecallCalendarForConnection({ | ||
| emailAccountId, | ||
| }: { | ||
| emailAccountId: string; | ||
| }) { | ||
| const connection = await prisma.calendarConnection.findFirst({ | ||
| where: { | ||
| emailAccountId, | ||
| isConnected: true, | ||
| }, | ||
| include: { | ||
| calendars: { | ||
| where: { | ||
| isEnabled: true, | ||
| primary: true, | ||
| }, | ||
| }, | ||
| }, | ||
| orderBy: { | ||
| createdAt: "desc", | ||
| }, | ||
| }); | ||
|
|
||
| if (!connection) { | ||
| throw new Error("No connected calendar found for this email account"); | ||
| } | ||
|
|
||
| if (connection.calendars.length === 0) { | ||
| throw new Error("No enabled primary calendar found"); | ||
| } | ||
|
|
||
| if (connection.recallCalendarId) { | ||
| const recallCalendar = await getRecallCalendar(connection.recallCalendarId); | ||
| if (recallCalendar) { | ||
| logger.info("Recall calendar already exists", { | ||
| recallCalendarId: connection.recallCalendarId, | ||
| status: recallCalendar.status, | ||
| }); | ||
| return { | ||
| recallCalendarId: connection.recallCalendarId, | ||
| status: recallCalendar.status, | ||
| }; | ||
| } else { | ||
| await prisma.calendarConnection.update({ | ||
| where: { id: connection.id }, | ||
| data: { recallCalendarId: null }, | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| const platform = | ||
| connection.provider === "google" ? "google_calendar" : "microsoft_outlook"; | ||
|
|
||
| const oauthClientId = | ||
| connection.provider === "google" | ||
| ? env.GOOGLE_CLIENT_ID | ||
| : env.MICROSOFT_CLIENT_ID; | ||
| const oauthClientSecret = | ||
| connection.provider === "google" | ||
| ? env.GOOGLE_CLIENT_SECRET | ||
| : env.MICROSOFT_CLIENT_SECRET; | ||
|
|
||
| if (!oauthClientId || !oauthClientSecret) { | ||
| throw new Error(`Missing OAuth credentials for ${connection.provider}`); | ||
| } | ||
|
|
||
| if (!connection.refreshToken) { | ||
| throw new Error("No refresh token available for calendar connection"); | ||
| } | ||
|
|
||
| const recallCalendar = await createRecallCalendar({ | ||
| oauth_client_id: oauthClientId, | ||
| oauth_client_secret: oauthClientSecret, | ||
| oauth_refresh_token: connection.refreshToken, | ||
| platform, | ||
| }); | ||
|
|
||
| await prisma.calendarConnection.update({ | ||
| where: { id: connection.id }, | ||
| data: { recallCalendarId: recallCalendar.id }, | ||
| }); | ||
|
|
||
| logger.info("Successfully created and linked Recall calendar", { | ||
| emailAccountId, | ||
| connectionId: connection.id, | ||
| recallCalendarId: recallCalendar.id, | ||
| platform, | ||
| }); | ||
|
|
||
| return { | ||
| recallCalendarId: recallCalendar.id, | ||
| status: recallCalendar.status, | ||
| platform: recallCalendar.platform, | ||
| }; | ||
| } | ||
|
|
||
| async function deleteRecallCalendarForConnection({ | ||
| emailAccountId, | ||
| }: { | ||
| emailAccountId: string; | ||
| }) { | ||
| const connections = await prisma.calendarConnection.findMany({ | ||
| where: { | ||
| emailAccountId, | ||
| recallCalendarId: { not: null }, | ||
| }, | ||
| }); | ||
|
|
||
| const results = []; | ||
|
|
||
| for (const connection of connections) { | ||
| if (!connection.recallCalendarId) continue; | ||
|
|
||
| try { | ||
| await deleteRecallCalendar(connection.recallCalendarId); | ||
|
|
||
| await prisma.calendarConnection.update({ | ||
| where: { id: connection.id }, | ||
| data: { recallCalendarId: null }, | ||
| }); | ||
|
|
||
| results.push({ | ||
| connectionId: connection.id, | ||
| recallCalendarId: connection.recallCalendarId, | ||
| status: "deleted", | ||
| }); | ||
|
|
||
| logger.info("Successfully deleted Recall calendar", { | ||
| connectionId: connection.id, | ||
| recallCalendarId: connection.recallCalendarId, | ||
| }); | ||
| } catch (error) { | ||
| logger.error("Failed to delete Recall calendar", { | ||
| error: error instanceof Error ? error.message : error, | ||
| connectionId: connection.id, | ||
| recallCalendarId: connection.recallCalendarId, | ||
| }); | ||
|
|
||
| results.push({ | ||
| connectionId: connection.id, | ||
| recallCalendarId: connection.recallCalendarId, | ||
| status: "error", | ||
| error: error instanceof Error ? error.message : "Unknown error", | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| return { results }; | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.