Skip to content

Commit fed7b2f

Browse files
authored
Use integration ID for captureWebhookLog userId (dubinc#3741)
1 parent 2c11477 commit fed7b2f

File tree

6 files changed

+46
-38
lines changed

6 files changed

+46
-38
lines changed

apps/web/app/(ee)/api/appsflyer/webhook/route.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ export const GET = withAxiom(async (req) => {
142142
captureWebhookLog({
143143
workspaceId: workspace.id,
144144
method: req.method,
145-
path: "/api/appsflyer/webhook",
145+
path: "/appsflyer/webhook",
146146
statusCode: 200,
147147
duration: Date.now() - startTime,
148148
requestBody: queryParams,
@@ -160,7 +160,7 @@ export const GET = withAxiom(async (req) => {
160160
captureWebhookLog({
161161
workspaceId: workspace.id,
162162
method: req.method,
163-
path: "/api/appsflyer/webhook",
163+
path: "/appsflyer/webhook",
164164
statusCode: errorResponse.status,
165165
duration: Date.now() - startTime,
166166
requestBody: queryParams,

apps/web/app/(ee)/api/stripe/integration/webhook/route.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ export const POST = withAxiom(async (req: Request) => {
156156
await captureWebhookLog({
157157
workspaceId: result.workspaceId,
158158
method: req.method,
159-
path: "/api/stripe/integration/webhook",
159+
path: "/stripe/integration/webhook",
160160
statusCode: 200,
161161
duration: Date.now() - startTime,
162162
requestBody: event,

apps/web/lib/api-logs/capture-webhook-log.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { WEBHOOK_REQUEST_ACTORS_BY_PATH } from "./constants";
12
import { recordApiLog } from "./record-api-log";
23

34
async function parseResponseBody(responseBody: unknown): Promise<unknown> {
@@ -24,13 +25,14 @@ export async function captureWebhookLog({
2425
}: {
2526
workspaceId: string;
2627
method: string;
27-
path: string;
28+
path: keyof typeof WEBHOOK_REQUEST_ACTORS_BY_PATH;
2829
statusCode: number;
2930
duration: number;
3031
requestBody: unknown;
3132
responseBody: unknown;
3233
userAgent: string | null;
3334
}) {
35+
const actor = WEBHOOK_REQUEST_ACTORS_BY_PATH[path];
3436
return await recordApiLog({
3537
workspaceId,
3638
method,
@@ -42,7 +44,7 @@ export async function captureWebhookLog({
4244
requestBody,
4345
responseBody: await parseResponseBody(responseBody),
4446
tokenId: null,
45-
userId: null,
47+
userId: actor.id,
4648
requestType: "webhook",
4749
});
4850
}

apps/web/lib/api-logs/constants.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
import type { PlanProps } from "@/lib/types";
2+
import {
3+
APPSFLYER_INTEGRATION_ID,
4+
SHOPIFY_INTEGRATION_ID,
5+
STRIPE_INTEGRATION_ID,
6+
} from "@dub/utils";
27

38
// Route patterns for parameterized path matching.
49
// Used both for logging eligibility and route pattern extraction.
@@ -90,3 +95,24 @@ export const METHOD_BADGE_VARIANTS: Record<string, string> = {
9095
DELETE: "error",
9196
GET: "success",
9297
} as const;
98+
99+
export const WEBHOOK_REQUEST_ACTORS_BY_PATH = {
100+
"/appsflyer/webhook": {
101+
id: APPSFLYER_INTEGRATION_ID,
102+
name: "AppsFlyer",
103+
image:
104+
"https://dubassets.com/integrations/int_1KN8JP7ET3VQQRF7ZQEVNFPJ5_2Geprc8",
105+
},
106+
"/stripe/integration/webhook": {
107+
id: STRIPE_INTEGRATION_ID,
108+
name: "Stripe",
109+
image:
110+
"https://dubassets.com/integrations/clzra1ya60001wnj4a89zcg9h_jtyaGa7",
111+
},
112+
"/shopify/integration/webhook": {
113+
id: SHOPIFY_INTEGRATION_ID,
114+
name: "Shopify",
115+
image:
116+
"https://dubassets.com/integrations/int_iWOtrZgmcyU6XDwKr4AYYqLN_jUmF77W",
117+
},
118+
} as const;

apps/web/lib/api-logs/enrich-api-logs.ts

Lines changed: 12 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,6 @@
11
import { prisma } from "@dub/prisma";
22
import { ApiLogTB, EnrichedApiLog } from "../types";
3-
4-
const WEBHOOK_REQUEST_ACTORS: Record<
5-
string,
6-
{ id: string; name: string; image: string }
7-
> = {
8-
"/appsflyer/webhook": {
9-
id: "appsflyer",
10-
name: "AppsFlyer",
11-
image:
12-
"https://dubassets.com/integrations/int_1KN8JP7ET3VQQRF7ZQEVNFPJ5_2Geprc8",
13-
},
14-
"/stripe/integration/webhook": {
15-
id: "stripe",
16-
name: "Stripe",
17-
image:
18-
"https://dubassets.com/integrations/clzra1ya60001wnj4a89zcg9h_jtyaGa7",
19-
},
20-
"/shopify/integration/webhook": {
21-
id: "shopify",
22-
name: "Shopify",
23-
image:
24-
"https://dubassets.com/integrations/int_iWOtrZgmcyU6XDwKr4AYYqLN_jUmF77W",
25-
},
26-
};
3+
import { WEBHOOK_REQUEST_ACTORS_BY_PATH } from "./constants";
274

285
export async function enrichApiLogs(
296
logs: ApiLogTB | ApiLogTB[],
@@ -72,18 +49,21 @@ export async function enrichApiLogs(
7249

7350
const tokenMap = new Map(tokens.map((t) => [t.id, t] as const));
7451
const userMap = new Map(users.map((u) => [u.id, u] as const));
52+
const webhookRequestActorsMap = new Map(
53+
Object.values(WEBHOOK_REQUEST_ACTORS_BY_PATH).map((actor) => [
54+
actor.id as string, // coerce back to string from const type
55+
{ ...actor, email: null },
56+
]),
57+
);
7558

7659
const enriched = logsArray.map((log) => ({
7760
...log,
7861
token: log.token_id ? tokenMap.get(log.token_id) ?? null : null,
79-
user: WEBHOOK_REQUEST_ACTORS[log.path]
80-
? {
81-
...WEBHOOK_REQUEST_ACTORS[log.path],
82-
email: null,
83-
}
84-
: log.user_id
85-
? userMap.get(log.user_id) ?? null
86-
: null,
62+
user: log.user_id
63+
? webhookRequestActorsMap.get(log.user_id) ??
64+
userMap.get(log.user_id) ??
65+
null
66+
: null,
8767
}));
8868

8969
return isSingle ? enriched[0] : enriched;

apps/web/ui/logs/logs-table.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ export function LogsTable() {
171171
cell: ({ row }: { row: Row<EnrichedApiLog> }) => (
172172
<TimestampTooltip
173173
timestamp={row.original.timestamp}
174-
rows={["local"]}
174+
rows={["local", "utc", "unix"]}
175175
side="left"
176176
delayDuration={150}
177177
>

0 commit comments

Comments
 (0)