-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Provide context to resolve fraud comments #3453
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
Draft
marcusljf
wants to merge
3
commits into
main
Choose a base branch
from
resolved-fraud-comment
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+553
−50
Draft
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| import type { FraudRuleType } from "@dub/prisma/client"; | ||
|
|
||
| const FRAUD_RESOLUTION_COMMENT_PREFIX = "[[dub-fraud-resolution:"; | ||
| const FRAUD_RESOLUTION_COMMENT_SUFFIX = "]]"; | ||
|
|
||
| export interface FraudResolutionCommentMetadata { | ||
| groupId: string; | ||
| type: FraudRuleType; | ||
| } | ||
|
|
||
| export function serializeFraudResolutionComment({ | ||
| metadata, | ||
| note, | ||
| }: { | ||
| metadata: FraudResolutionCommentMetadata; | ||
| note: string; | ||
| }) { | ||
| return `${FRAUD_RESOLUTION_COMMENT_PREFIX}${JSON.stringify(metadata)}${FRAUD_RESOLUTION_COMMENT_SUFFIX}\n${note}`; | ||
| } | ||
|
|
||
| export function parseFraudResolutionComment(text: string): { | ||
| metadata: FraudResolutionCommentMetadata | null; | ||
| note: string; | ||
| } { | ||
| if (!text.startsWith(FRAUD_RESOLUTION_COMMENT_PREFIX)) { | ||
| return { | ||
| metadata: null, | ||
| note: text, | ||
| }; | ||
| } | ||
|
|
||
| const suffixIndex = text.indexOf(FRAUD_RESOLUTION_COMMENT_SUFFIX); | ||
|
|
||
| if (suffixIndex === -1) { | ||
| return { | ||
| metadata: null, | ||
| note: text, | ||
| }; | ||
| } | ||
|
|
||
| const metadataString = text.slice( | ||
| FRAUD_RESOLUTION_COMMENT_PREFIX.length, | ||
| suffixIndex, | ||
| ); | ||
|
|
||
| try { | ||
| const metadata = JSON.parse(metadataString) as FraudResolutionCommentMetadata; | ||
|
|
||
| if ( | ||
| !metadata || | ||
| typeof metadata.groupId !== "string" || | ||
| typeof metadata.type !== "string" | ||
| ) { | ||
| return { | ||
| metadata: null, | ||
| note: text, | ||
| }; | ||
| } | ||
|
|
||
| const contentStart = suffixIndex + FRAUD_RESOLUTION_COMMENT_SUFFIX.length; | ||
| const hasNewLine = text.charAt(contentStart) === "\n"; | ||
|
|
||
| return { | ||
| metadata, | ||
| note: text.slice(hasNewLine ? contentStart + 1 : contentStart), | ||
| }; | ||
| } catch { | ||
| return { | ||
| metadata: null, | ||
| note: text, | ||
| }; | ||
| } | ||
| } |
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
107 changes: 107 additions & 0 deletions
107
apps/web/scripts/migrations/backfill-fraud-resolution-comment-metadata.ts
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,107 @@ | ||
| import { parseFraudResolutionComment } from "@/lib/fraud-resolution-comment"; | ||
| import { prisma } from "@dub/prisma"; | ||
| import "dotenv-flow/config"; | ||
|
|
||
| async function main() { | ||
| const dryRun = process.argv.includes("--dry-run"); | ||
|
|
||
| const comments = await prisma.partnerComment.findMany({ | ||
| where: { | ||
| metadata: null, | ||
| }, | ||
| select: { | ||
| id: true, | ||
| programId: true, | ||
| partnerId: true, | ||
| userId: true, | ||
| text: true, | ||
| createdAt: true, | ||
| }, | ||
| orderBy: { | ||
| createdAt: "asc", | ||
| }, | ||
| }); | ||
|
|
||
| let upgradedFromLegacyEncoded = 0; | ||
| let upgradedFromResolvedGroupMatch = 0; | ||
| let ambiguous = 0; | ||
| let unmatched = 0; | ||
|
|
||
| for (const comment of comments) { | ||
| const legacy = parseFraudResolutionComment(comment.text); | ||
|
|
||
| if (legacy.metadata) { | ||
| if (!dryRun) { | ||
| await prisma.partnerComment.update({ | ||
| where: { id: comment.id }, | ||
| data: { | ||
| text: legacy.note, | ||
| metadata: { | ||
| source: "fraudResolution", | ||
| groupId: legacy.metadata.groupId, | ||
| type: legacy.metadata.type, | ||
| }, | ||
| }, | ||
| }); | ||
| } | ||
|
|
||
| upgradedFromLegacyEncoded++; | ||
| continue; | ||
| } | ||
|
|
||
| const matchingGroups = await prisma.fraudEventGroup.findMany({ | ||
| where: { | ||
| programId: comment.programId, | ||
| partnerId: comment.partnerId, | ||
| userId: comment.userId, | ||
| status: "resolved", | ||
| resolutionReason: comment.text, | ||
| }, | ||
| select: { | ||
| id: true, | ||
| type: true, | ||
| resolvedAt: true, | ||
| }, | ||
| }); | ||
|
|
||
| if (matchingGroups.length === 0) { | ||
| unmatched++; | ||
| continue; | ||
| } | ||
|
|
||
| if (matchingGroups.length > 1) { | ||
| ambiguous++; | ||
| continue; | ||
| } | ||
|
|
||
| const [group] = matchingGroups; | ||
|
|
||
| if (!dryRun) { | ||
| await prisma.partnerComment.update({ | ||
| where: { id: comment.id }, | ||
| data: { | ||
| metadata: { | ||
| source: "fraudResolution", | ||
| groupId: group.id, | ||
| type: group.type, | ||
| }, | ||
| }, | ||
| }); | ||
| } | ||
|
|
||
| upgradedFromResolvedGroupMatch++; | ||
| } | ||
|
|
||
| console.log( | ||
| [ | ||
| `Scanned ${comments.length} comments with null metadata`, | ||
| `Upgraded from legacy encoded comments: ${upgradedFromLegacyEncoded}`, | ||
| `Upgraded from fraud group match: ${upgradedFromResolvedGroupMatch}`, | ||
| `Ambiguous matches skipped: ${ambiguous}`, | ||
| `No matches found: ${unmatched}`, | ||
| `Mode: ${dryRun ? "dry-run" : "write"}`, | ||
| ].join("\n"), | ||
| ); | ||
| } | ||
|
|
||
| main(); |
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.