Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion apps/web/app/api/ai/digest/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ export async function POST(request: Request) {
const summary = await aiSummarizeEmailForDigest({
ruleName,
emailAccount,
messageToSummarize: message,
messageToSummarize: {
...message,
to: message.to || "",
},
});

await upsertDigest({
Expand Down
2 changes: 1 addition & 1 deletion apps/web/app/api/ai/digest/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export const digestBody = z.object({
id: z.string(),
threadId: z.string(),
from: z.string(),
to: z.string(),
to: z.string().optional(),
Comment thread
elie222 marked this conversation as resolved.
subject: z.string(),
content: z.string(),
}),
Expand Down
3 changes: 1 addition & 2 deletions apps/web/app/api/resend/digest/all/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,7 @@ async function sendDigestAllUpdate() {
queueName: "email-digest-all",
parallelism: 3, // Allow up to 3 concurrent jobs from this queue
url,
body: { emailAccountId: emailAccount.id },
headers: getCronSecretHeader(),
body: { emailAccountId: emailAccount.id, CRON_SECRET: env.CRON_SECRET },
Comment thread
elie222 marked this conversation as resolved.
Comment thread
edulelis marked this conversation as resolved.
});
} catch (error) {
logger.error("Failed to publish to Qstash", {
Expand Down
65 changes: 30 additions & 35 deletions apps/web/app/api/resend/digest/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,39 +168,34 @@ async function sendEmail({
item.action?.executedRule?.rule?.name || RuleName.ColdEmail,
);

// Only include if it's one of our known categories
const categoryResult = digestCategorySchema.safeParse(ruleName);
if (categoryResult.success) {
const category = categoryResult.data;
if (!acc[category]) {
acc[category] = [];
}

let parsedContent: unknown;
try {
parsedContent = JSON.parse(item.content);
} catch (error) {
logger.warn("Failed to parse digest item content, skipping item", {
messageId: item.messageId,
digestId: digest.id,
error: error instanceof Error ? error.message : "Unknown error",
});
return; // Skip this item and continue with the next one
}

const contentResult =
DigestEmailSummarySchema.safeParse(parsedContent);

if (contentResult.success) {
acc[category].push({
content: {
entries: contentResult.data?.entries || [],
summary: contentResult.data?.summary,
},
from: extractNameFromEmail(message?.headers?.from || ""),
subject: message?.headers?.subject || "",
});
}
const category = ruleName;
Comment thread
elie222 marked this conversation as resolved.
if (!acc[category]) {
acc[category] = [];
}

let parsedContent: unknown;
try {
parsedContent = JSON.parse(item.content);
} catch (error) {
logger.warn("Failed to parse digest item content, skipping item", {
messageId: item.messageId,
digestId: digest.id,
error: error instanceof Error ? error.message : "Unknown error",
});
return; // Skip this item and continue with the next one
}

const contentResult = DigestEmailSummarySchema.safeParse(parsedContent);

if (contentResult.success) {
acc[category].push({
content: {
entries: contentResult.data?.entries || [],
summary: contentResult.data?.summary,
},
from: extractNameFromEmail(message?.headers?.from || ""),
subject: message?.headers?.subject || "",
});
}
});
return acc;
Expand All @@ -213,11 +208,11 @@ async function sendEmail({
from: env.RESEND_FROM_EMAIL,
to: emailAccount.email,
emailProps: {
...executedRulesByRule,
baseUrl: env.NEXT_PUBLIC_BASE_URL,
unsubscribeToken: token,
date: new Date(),
},
...executedRulesByRule,
} as any, // Type assertion needed due to generic DigestEmailProps
Comment thread
edulelis marked this conversation as resolved.
});

// Only update database if email sending succeeded
Expand Down
25 changes: 6 additions & 19 deletions apps/web/app/api/resend/digest/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,25 +30,12 @@ export const digestSummarySchema = z.string().transform((str) => {
}
});

export const digestCategorySchema = z.enum([
"newsletter",
"receipt",
"marketing",
"calendar",
"coldEmail",
"notification",
"toReply",
]);

export const digestSchema = z.object({
newsletter: z.array(digestItemSchema).optional(),
receipt: z.array(digestItemSchema).optional(),
marketing: z.array(digestItemSchema).optional(),
calendar: z.array(digestItemSchema).optional(),
coldEmail: z.array(digestItemSchema).optional(),
notification: z.array(digestItemSchema).optional(),
toReply: z.array(digestItemSchema).optional(),
});
export const digestCategorySchema = z.string();

export const digestSchema = z.record(
z.string(),
z.array(digestItemSchema).optional(),
);

export const sendDigestEmailBody = z.object({ emailAccountId: z.string() });

Expand Down
2 changes: 1 addition & 1 deletion apps/web/utils/digest/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export async function enqueueDigestItem({
id: email.id,
threadId: email.threadId,
from: email.headers.from,
to: email.headers.to,
to: email.headers.to || "",
subject: email.headers.subject,
content: email.textPlain || "",
},
Expand Down
Loading
Loading