fix(analytics): make follow logging idempotent and prevent duplicate follow events#532
Open
Ridanshi wants to merge 3 commits into
Open
fix(analytics): make follow logging idempotent and prevent duplicate follow events#532Ridanshi wants to merge 3 commits into
Ridanshi wants to merge 3 commits into
Conversation
… logs
POST /:platform/:targetUsername/log accepted free-form `status` and
`layer` values and wrote them directly to followLog without validation.
Both fields feed analytics counters (totalFollows) and the
follower-state dashboard via `status: 'success'` queries, so an
authenticated user could fabricate successful follow events, inflate
engagement metrics, and manipulate the dashboard.
Fix:
- Add `followLogSchema` (Zod) in validations/follow.validation.ts with
strict enum allowlists:
status → 'success' | 'failed' | 'pending'
layer → 'foreground' | 'background'
- Validate request body with safeParse before any database write;
invalid payloads return 400 without touching followLog.create()
- Remove unsafe free-form defaults ('success' / 'webview') that
silently accepted omitted fields
- Response body on validation failure contains only { error } —
no Zod internals, paths, or stack traces are exposed
Layer 1 (API follow) writes status/layer internally and is unaffected.
Tests: 22 cases covering all valid enum combinations, all rejection
paths, DB-not-called guarantee on failure, correct payload written to
DB, and opaque error responses.
Closes Dev-Card#301
FollowLog had no unique constraint, so repeated follow requests (retries, double-taps, concurrent clicks) each appended a new row — inflating the totalFollows counter displayed in the analytics dashboard. Changes: - schema: add @@unique([followerId, targetUsername, platform]) to FollowLog - schema: add updatedAt field (required by Prisma upsert) - migration: deduplicates existing rows (keeps most-recent per group) before creating the unique index — safe on a populated database - routes/follow.ts: replace followLog.create() with followLog.upsert() in both the Layer-1 API path and the Layer-2/3/4 manual-log path - tests: update mocks to upsert; add three idempotency tests verifying the correct composite where-key, update/create shape, and multi-target isolation
|
@Ridanshi is attempting to deploy a commit to the Prashantkumar Khatri's projects Team on Vercel. A member of the Team first needs to authorize it. |
|
Hi @Ridanshi, Thanks for opening this pull request. This PR has been automatically classified based on the files modified. Applied Labels
Primary Review Area
Reviewer@Harxhit has been identified as the primary reviewer for this pull request. If you have any questions regarding the affected area or implementation details, feel free to reach out to the assigned reviewer. Thank you for your contribution! |
CI — All Checks PassedBackend — PASS
Mobile — SKIP
Web — SKIP
Last updated: |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Closes #484
Summary
This PR fixes an analytics integrity issue where repeated follow requests could generate duplicate follow log records and inflate engagement metrics.
The follow operation itself was effectively idempotent, but follow-event logging was not.
Changes
Test Coverage
Added/updated tests covering:
Result
Follow analytics now accurately represent real follow relationships and cannot be inflated through duplicate requests or retries.