-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Fix empty digest email #744
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,4 +1,5 @@ | ||||||||||||||||||||||||||||
| import { NextResponse } from "next/server"; | ||||||||||||||||||||||||||||
| import { verifySignatureAppRouter } from "@upstash/qstash/nextjs"; | ||||||||||||||||||||||||||||
| import { digestBody } from "./validation"; | ||||||||||||||||||||||||||||
| import { DigestStatus } from "@prisma/client"; | ||||||||||||||||||||||||||||
| import { createScopedLogger } from "@/utils/logger"; | ||||||||||||||||||||||||||||
|
|
@@ -7,9 +8,57 @@ import { RuleName } from "@/utils/rule/consts"; | |||||||||||||||||||||||||||
| import { getRuleNameByExecutedAction } from "@/utils/actions/rule"; | ||||||||||||||||||||||||||||
| import { aiSummarizeEmailForDigest } from "@/utils/ai/digest/summarize-email-for-digest"; | ||||||||||||||||||||||||||||
| import { getEmailAccountWithAi } from "@/utils/user/get"; | ||||||||||||||||||||||||||||
| import type { DigestEmailSummarySchema } from "@/app/api/resend/digest/validation"; | ||||||||||||||||||||||||||||
| import type { StoredDigestContent } from "@/app/api/resend/digest/validation"; | ||||||||||||||||||||||||||||
| import { withError } from "@/utils/middleware"; | ||||||||||||||||||||||||||||
| import { verifySignatureAppRouter } from "@upstash/qstash/dist/nextjs"; | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| export const POST = withError( | ||||||||||||||||||||||||||||
| verifySignatureAppRouter(async (request: Request) => { | ||||||||||||||||||||||||||||
| const logger = createScopedLogger("digest"); | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||
| const body = digestBody.parse(await request.json()); | ||||||||||||||||||||||||||||
| const { emailAccountId, coldEmailId, actionId, message } = body; | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| logger.with({ emailAccountId, messageId: message.id }); | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
|
Comment on lines
+19
to
+23
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Return 400 on invalid body; avoid throwing on parse. Use safeParse and a consistent error payload per API guidelines. - const body = digestBody.parse(await request.json());
- const { emailAccountId, coldEmailId, actionId, message } = body;
+ const json = await request.json();
+ const parsed = digestBody.safeParse(json);
+ if (!parsed.success) {
+ logger.error("Invalid request body", { error: parsed.error });
+ return NextResponse.json({ error: "Invalid request body" }, { status: 400 });
+ }
+ const { emailAccountId, coldEmailId, actionId, message } = parsed.data;📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||
| const emailAccount = await getEmailAccountWithAi({ | ||||||||||||||||||||||||||||
| emailAccountId, | ||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||
| if (!emailAccount) { | ||||||||||||||||||||||||||||
| throw new Error("Email account not found"); | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| const ruleName = await resolveRuleName(actionId); | ||||||||||||||||||||||||||||
| const summary = await aiSummarizeEmailForDigest({ | ||||||||||||||||||||||||||||
| ruleName, | ||||||||||||||||||||||||||||
| emailAccount, | ||||||||||||||||||||||||||||
| messageToSummarize: { | ||||||||||||||||||||||||||||
| ...message, | ||||||||||||||||||||||||||||
| to: message.to || "", | ||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| if (!summary?.content) { | ||||||||||||||||||||||||||||
| logger.info("Skipping digest item because it is not worth summarizing"); | ||||||||||||||||||||||||||||
| return new NextResponse("OK", { status: 200 }); | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| await upsertDigest({ | ||||||||||||||||||||||||||||
| messageId: message.id || "", | ||||||||||||||||||||||||||||
| threadId: message.threadId || "", | ||||||||||||||||||||||||||||
| emailAccountId, | ||||||||||||||||||||||||||||
| actionId, | ||||||||||||||||||||||||||||
| coldEmailId, | ||||||||||||||||||||||||||||
| content: summary, | ||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| return new NextResponse("OK", { status: 200 }); | ||||||||||||||||||||||||||||
| } catch (error) { | ||||||||||||||||||||||||||||
| logger.error("Failed to process digest", { error }); | ||||||||||||||||||||||||||||
| return new NextResponse("Internal Server Error", { status: 500 }); | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
| }), | ||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| async function resolveRuleName(actionId?: string): Promise<string> { | ||||||||||||||||||||||||||||
| if (!actionId) return RuleName.ColdEmail; | ||||||||||||||||||||||||||||
|
|
@@ -113,7 +162,7 @@ async function upsertDigest({ | |||||||||||||||||||||||||||
| emailAccountId: string; | ||||||||||||||||||||||||||||
| actionId?: string; | ||||||||||||||||||||||||||||
| coldEmailId?: string; | ||||||||||||||||||||||||||||
| content: DigestEmailSummarySchema; | ||||||||||||||||||||||||||||
| content: StoredDigestContent; | ||||||||||||||||||||||||||||
| }) { | ||||||||||||||||||||||||||||
| const logger = createScopedLogger("digest").with({ | ||||||||||||||||||||||||||||
| messageId, | ||||||||||||||||||||||||||||
|
|
@@ -156,52 +205,3 @@ async function upsertDigest({ | |||||||||||||||||||||||||||
| throw error; | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| export const POST = withError( | ||||||||||||||||||||||||||||
| verifySignatureAppRouter(async (request: Request) => { | ||||||||||||||||||||||||||||
| const logger = createScopedLogger("digest"); | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||
| const body = digestBody.parse(await request.json()); | ||||||||||||||||||||||||||||
| const { emailAccountId, coldEmailId, actionId, message } = body; | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| logger.with({ emailAccountId, messageId: message.id }); | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| const emailAccount = await getEmailAccountWithAi({ | ||||||||||||||||||||||||||||
| emailAccountId, | ||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||
| if (!emailAccount) { | ||||||||||||||||||||||||||||
| throw new Error("Email account not found"); | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| const ruleName = await resolveRuleName(actionId); | ||||||||||||||||||||||||||||
| const summary = await aiSummarizeEmailForDigest({ | ||||||||||||||||||||||||||||
| ruleName, | ||||||||||||||||||||||||||||
| emailAccount, | ||||||||||||||||||||||||||||
| messageToSummarize: { | ||||||||||||||||||||||||||||
| ...message, | ||||||||||||||||||||||||||||
| to: message.to || "", | ||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| if (!summary?.content) { | ||||||||||||||||||||||||||||
| logger.info("Skipping digest item because it is not worth summarizing"); | ||||||||||||||||||||||||||||
| return new NextResponse("OK", { status: 200 }); | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| await upsertDigest({ | ||||||||||||||||||||||||||||
| messageId: message.id || "", | ||||||||||||||||||||||||||||
| threadId: message.threadId || "", | ||||||||||||||||||||||||||||
| emailAccountId, | ||||||||||||||||||||||||||||
| actionId, | ||||||||||||||||||||||||||||
| coldEmailId, | ||||||||||||||||||||||||||||
| content: summary, | ||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| return new NextResponse("OK", { status: 200 }); | ||||||||||||||||||||||||||||
| } catch (error) { | ||||||||||||||||||||||||||||
| logger.error("Failed to process digest", { error }); | ||||||||||||||||||||||||||||
| return new NextResponse("Internal Server Error", { status: 500 }); | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
| }), | ||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.