fix(analytics): make follow logging idempotent and prevent duplicate follow events#532
fix(analytics): make follow logging idempotent and prevent duplicate follow events#532Ridanshi 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: |
|
@ShantKhatri She didn't address the changes I asked for. |
Let's discuss all the issues created by @Ridanshi in this week's community call, as I'm not able to understand these issues. Requesting @Ridanshi to please have a short demo on issue reproduction steps. |
|
@Harxhit @ShantKhatri If it would help, I'm happy to put together a reproduction script showing the duplicate follow events before and after the fix, or walk through any part of the implementation. Just let me know what would be most useful. |
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.