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
3 changes: 0 additions & 3 deletions apps/web/app/api/outlook/webhook/logger.ts

This file was deleted.

44 changes: 0 additions & 44 deletions apps/web/app/api/outlook/webhook/process-history-item.ts

This file was deleted.

37 changes: 25 additions & 12 deletions apps/web/app/api/outlook/webhook/process-history.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,21 @@ import { NextResponse } from "next/server";
import { captureException } from "@/utils/error";
import { createEmailProvider } from "@/utils/email/provider";
import type { OutlookResourceData } from "@/app/api/outlook/webhook/types";
import { processHistoryItem } from "@/app/api/outlook/webhook/process-history-item";
import { logger } from "@/app/api/outlook/webhook/logger";
import { processHistoryItem } from "@/utils/webhook/process-history-item";
import {
validateWebhookAccount,
getWebhookEmailAccount,
} from "@/utils/webhook/validate-webhook-account";
import type { Logger } from "@/utils/logger";

export async function processHistoryForUser({
subscriptionId,
resourceData,
logger,
}: {
subscriptionId: string;
resourceData: OutlookResourceData;
logger: Logger;
}) {
const emailAccount = await getWebhookEmailAccount(
{
Expand All @@ -23,9 +25,17 @@ export async function processHistoryForUser({
logger,
);

logger = logger.with({
email: emailAccount?.email,
emailAccountId: emailAccount?.id,
});

const validation = await validateWebhookAccount(emailAccount, logger);

if (!validation.success) {
logger.error("Error validating webhook account", {
error: validation.response.status,
});
return validation.response;
}

Expand All @@ -44,16 +54,20 @@ export async function processHistoryForUser({
});

try {
await processHistoryItem(resourceData, {
provider,
hasAutomationRules,
hasAiAccess: userHasAiAccess,
rules: validatedEmailAccount.rules,
emailAccount: {
...validatedEmailAccount,
account: { provider: accountProvider },
await processHistoryItem(
{ messageId: resourceData.id },
{
provider,
emailAccount: {
...validatedEmailAccount,
account: { provider: accountProvider },
},
hasAutomationRules,
hasAiAccess: userHasAiAccess,
rules: validatedEmailAccount.rules,
logger,
},
});
);

return NextResponse.json({ ok: true });
} catch (error) {
Expand All @@ -68,7 +82,6 @@ export async function processHistoryForUser({
validatedEmailAccount.email,
);
logger.error("Error processing webhook", {
subscriptionId,
resourceData,
email: validatedEmailAccount.email,
error:
Expand Down
12 changes: 7 additions & 5 deletions apps/web/app/api/outlook/webhook/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { z } from "zod";
import { after, NextResponse } from "next/server";
import { withError } from "@/utils/middleware";
import { processHistoryForUser } from "@/app/api/outlook/webhook/process-history";
import { logger } from "@/app/api/outlook/webhook/logger";
import { createScopedLogger, type Logger } from "@/utils/logger";
import { env } from "@/env";
import { webhookBodySchema } from "@/app/api/outlook/webhook/types";
import { handleWebhookError } from "@/utils/webhook/error-handler";
Expand All @@ -14,6 +14,8 @@ export const POST = withError(async (request) => {
const searchParams = new URL(request.url).searchParams;
const validationToken = searchParams.get("validationToken");

const logger = createScopedLogger("outlook/webhook");

if (validationToken) {
logger.info("Received validation request", { validationToken });
return new NextResponse(validationToken, {
Expand Down Expand Up @@ -65,26 +67,28 @@ export const POST = withError(async (request) => {

// Process notifications asynchronously using after() to avoid Microsoft webhook timeout
// Microsoft expects a response within 3 seconds
after(() => processNotificationsAsync(notifications));
after(() => processNotificationsAsync(notifications, logger));

return NextResponse.json({ ok: true });
});

async function processNotificationsAsync(
notifications: z.infer<typeof webhookBodySchema>["value"],
log: Logger,
) {
for (const notification of notifications) {
const { subscriptionId, resourceData } = notification;
const logger = log.with({ subscriptionId, messageId: resourceData.id });

logger.info("Processing notification", {
subscriptionId,
changeType: notification.changeType,
});

try {
await processHistoryForUser({
subscriptionId,
resourceData,
logger,
});
} catch (error) {
const emailAccount = await getWebhookEmailAccount(
Expand All @@ -93,7 +97,6 @@ async function processNotificationsAsync(
).catch((error) => {
logger.error("Error getting email account", {
error: error instanceof Error ? error.message : error,
subscriptionId,
});
return null;
});
Expand All @@ -107,7 +110,6 @@ async function processNotificationsAsync(
} else {
logger.error("Error processing notification (no email account found)", {
error: error instanceof Error ? error.message : error,
subscriptionId,
});
}
}
Expand Down
5 changes: 3 additions & 2 deletions apps/web/utils/email/google.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ import type {
EmailFilter,
EmailSignature,
} from "@/utils/email/types";
import { createScopedLogger } from "@/utils/logger";
import { createScopedLogger, type Logger } from "@/utils/logger";
import { extractEmailAddress } from "@/utils/email";
import { getGmailSignatures } from "@/utils/gmail/signature-settings";

Expand Down Expand Up @@ -923,6 +923,7 @@ export class GmailProvider implements EmailProvider {
id: string;
conversationId?: string;
};
logger?: Logger;
}): Promise<void> {
await processHistoryForUser(
{
Expand All @@ -932,7 +933,7 @@ export class GmailProvider implements EmailProvider {
{
startHistoryId: options.startHistoryId?.toString(),
},
logger,
options.logger || logger,
);
}

Expand Down
4 changes: 3 additions & 1 deletion apps/web/utils/email/microsoft.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import {
import { trashThread } from "@/utils/outlook/trash";
import { markSpam } from "@/utils/outlook/spam";
import { handlePreviousDraftDeletion } from "@/utils/ai/choose-rule/draft-management";
import { createScopedLogger } from "@/utils/logger";
import { type Logger, createScopedLogger } from "@/utils/logger";
import {
getThreadMessages,
getThreadsFromSenderWithSubject,
Expand Down Expand Up @@ -1241,6 +1241,7 @@ export class OutlookProvider implements EmailProvider {
id: string;
conversationId?: string;
};
logger?: Logger;
}): Promise<void> {
if (!options.subscriptionId) {
throw new Error(
Expand All @@ -1254,6 +1255,7 @@ export class OutlookProvider implements EmailProvider {
id: options.historyId?.toString() || "0",
conversationId: options.startHistoryId?.toString() || null,
},
logger: options.logger || logger,
});
}

Expand Down
2 changes: 2 additions & 0 deletions apps/web/utils/email/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { ParsedMessage } from "@/utils/types";
import type { InboxZeroLabel } from "@/utils/label";
import type { ThreadsQuery } from "@/app/api/threads/validation";
import type { OutlookFolder } from "@/utils/outlook/folders";
import type { Logger } from "@/utils/logger";

export interface EmailThread {
id: string;
Expand Down Expand Up @@ -220,6 +221,7 @@ export interface EmailProvider {
id: string;
conversationId?: string;
};
logger?: Logger;
}): Promise<void>;
watchEmails(): Promise<{
expirationDate: Date;
Expand Down
2 changes: 1 addition & 1 deletion version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v2.17.40
v2.17.41
Loading