-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Fraud detection improvements #3153
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
Merged
Merged
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
6fd2b9b
WIP email template
devkiran d79d4ed
Implement daily fraud events summary email
devkiran ca329e6
Update the empty state
devkiran 1d8a849
Updated logic to reflect pending fraud status in the AmountRowItem co…
devkiran 6b3972b
Add e2e tests
devkiran ba5f9ef
Fraud event detection by deduplicating pending events
devkiran 8d55624
Add script to remove duplicate pending fraud events
devkiran 21205ed
Update vercel.json
devkiran a0ad9bd
Update partner-application-risk-summary.tsx
devkiran f8ff466
Add upsell state
devkiran 7303686
Update partner-application-risk-summary.tsx
devkiran a3ba601
Merge branch 'main' into fraud-more-changes
devkiran a64799b
Update partner-application-risk-summary.tsx
devkiran fb2551d
Merge branch 'fraud-more-changes' of https://github.com/dubinc/dub in…
devkiran b72bb76
Update vercel.json
devkiran d463e1d
Update get-highest-severity.ts
devkiran ca44378
Update partner-application-risk-summary.tsx
devkiran 976af89
Update partner-application-risk-summary.tsx
devkiran 89d4e5b
add composite index
steven-tey fd1cc83
fix tests (added test-hostname-for-referral-source-banned-do-not-dele…
steven-tey 9633e45
fix tests again
steven-tey 49b97e9
increase wait duration to reduce flakiness
steven-tey 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,166 @@ | ||
| import { handleAndReturnErrorResponse } from "@/lib/api/errors"; | ||
| import { FRAUD_RULES_BY_TYPE } from "@/lib/api/fraud/constants"; | ||
| import { getGroupedFraudEvents } from "@/lib/api/fraud/get-grouped-fraud-events"; | ||
| import { getWorkspaceUsers } from "@/lib/api/get-workspace-users"; | ||
| import { qstash } from "@/lib/cron"; | ||
| import { verifyQstashSignature } from "@/lib/cron/verify-qstash"; | ||
| import { verifyVercelSignature } from "@/lib/cron/verify-vercel"; | ||
| import { queueBatchEmail } from "@/lib/email/queue-batch-email"; | ||
| import type UnresolvedFraudEventsSummary from "@dub/email/templates/unresolved-fraud-events-summary"; | ||
| import { prisma } from "@dub/prisma"; | ||
| import { APP_DOMAIN_WITH_NGROK } from "@dub/utils"; | ||
| import { format, startOfDay } from "date-fns"; | ||
| import { z } from "zod"; | ||
| import { logAndRespond } from "../../utils"; | ||
|
|
||
| export const dynamic = "force-dynamic"; | ||
|
|
||
| const PROGRAMS_BATCH_SIZE = 5; | ||
|
|
||
| const schema = z.object({ | ||
| startingAfter: z.string().optional(), | ||
| }); | ||
|
|
||
| async function handler(req: Request) { | ||
| try { | ||
| let { startingAfter } = schema.parse({}); | ||
|
|
||
| if (req.method === "GET") { | ||
| await verifyVercelSignature(req); | ||
| } else if (req.method === "POST") { | ||
| const rawBody = await req.text(); | ||
| await verifyQstashSignature({ | ||
| req, | ||
| rawBody, | ||
| }); | ||
|
|
||
| ({ startingAfter } = schema.parse(JSON.parse(rawBody))); | ||
| } | ||
|
|
||
| // Get batch of programs with unresolved fraud events | ||
| const programs = await prisma.program.findMany({ | ||
| where: { | ||
| fraudEvents: { | ||
| some: { | ||
| status: "pending", | ||
| // Only include programs with fraud events created today so daily summaries | ||
| createdAt: { | ||
| gte: startOfDay(new Date()), | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| select: { | ||
| id: true, | ||
| name: true, | ||
| slug: true, | ||
| workspace: { | ||
| select: { | ||
| slug: true, | ||
| }, | ||
| }, | ||
| }, | ||
| take: PROGRAMS_BATCH_SIZE, | ||
| ...(startingAfter && { | ||
| skip: 1, | ||
| cursor: { | ||
| id: startingAfter, | ||
| }, | ||
| }), | ||
| orderBy: { | ||
| id: "asc", | ||
| }, | ||
| }); | ||
|
|
||
| if (programs.length === 0) { | ||
| return logAndRespond( | ||
| "No more programs found to send fraud events summary.", | ||
| ); | ||
| } | ||
|
|
||
| const batchDate = format(new Date(), "yyyy-MM-dd"); | ||
|
|
||
| for (const program of programs) { | ||
| try { | ||
| const fraudEvents = await getGroupedFraudEvents({ | ||
| programId: program.id, | ||
| status: "pending", | ||
| page: 1, | ||
| pageSize: 6, | ||
| sortBy: "createdAt", | ||
| sortOrder: "asc", | ||
| }); | ||
|
|
||
| if (fraudEvents.length === 0) { | ||
| continue; | ||
| } | ||
|
|
||
| const transformedFraudEvents = fraudEvents.map( | ||
| ({ type, count, groupKey, partner }) => ({ | ||
| name: FRAUD_RULES_BY_TYPE[type].name, | ||
| count, | ||
| groupKey, | ||
| partner, | ||
| }), | ||
| ); | ||
|
|
||
| // Get workspace users to send the email to | ||
| const { users } = await getWorkspaceUsers({ | ||
| role: "owner", | ||
| programId: program.id, | ||
| notificationPreference: "fraudEventsSummary", | ||
| }); | ||
|
|
||
| if (users.length === 0) { | ||
| continue; | ||
| } | ||
|
|
||
| await queueBatchEmail<typeof UnresolvedFraudEventsSummary>( | ||
| users.map((user) => ({ | ||
| to: user.email, | ||
| subject: `Fraud events pending review for ${program.name}`, | ||
| variant: "notifications", | ||
| templateName: "UnresolvedFraudEventsSummary", | ||
| templateProps: { | ||
| email: user.email, | ||
| workspace: program.workspace, | ||
| program, | ||
| fraudEvents: transformedFraudEvents, | ||
| }, | ||
| })), | ||
| { | ||
| idempotencyKey: `fraud-events-summary/${program.id}/${batchDate}`, | ||
| }, | ||
| ); | ||
| } catch (error) { | ||
| console.error( | ||
| `Error collecting email payloads for program ${program.id}: ${error.message}`, | ||
| ); | ||
| continue; | ||
| } | ||
| } | ||
|
|
||
| if (programs.length === PROGRAMS_BATCH_SIZE) { | ||
| startingAfter = programs[programs.length - 1].id; | ||
|
|
||
| await qstash.publishJSON({ | ||
| url: `${APP_DOMAIN_WITH_NGROK}/api/cron/fraud/summary`, | ||
| method: "POST", | ||
| body: { | ||
| startingAfter, | ||
| }, | ||
| }); | ||
|
|
||
| return logAndRespond( | ||
| `Scheduled next batch for fraud events summary (startingAfter: ${startingAfter})`, | ||
| ); | ||
| } | ||
|
|
||
| return logAndRespond("Finished sending fraud events summary."); | ||
| } catch (error) { | ||
| return handleAndReturnErrorResponse(error); | ||
| } | ||
| } | ||
|
|
||
| // GET/POST /api/cron/fraud/summary | ||
| export { handler as GET, handler as POST }; | ||
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
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
27 changes: 0 additions & 27 deletions
27
apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/fraud/fraud-events-empty-state.tsx
This file was deleted.
Oops, something went wrong.
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
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.