Skip to content

fix(analytics): correct totalFollows query — use followerId not targetUsername#597

Open
hariom888 wants to merge 2 commits into
Dev-Card:mainfrom
hariom888:fix/analytics-totalfollows-query
Open

fix(analytics): correct totalFollows query — use followerId not targetUsername#597
hariom888 wants to merge 2 commits into
Dev-Card:mainfrom
hariom888:fix/analytics-totalfollows-query

Conversation

@hariom888

Copy link
Copy Markdown
Contributor

Summary

Fixes #567

The totalFollows metric in GET /api/analytics/overview was permanently zero for all real users due to a field/semantic mismatch between how follow.ts writes followLog rows and how analytics.ts reads them.

Root cause

Layer Field Value written / queried
follow.ts writes followerId authenticated user's internal userId
follow.ts writes targetUsername external platform handle (e.g. a GitHub username)
analytics.ts read targetUsername authenticated user's own DevCard username

Because targetUsername in followLog always holds an external handle (never a DevCard username), the analytics query matched zero rows in all realistic cases — producing a totalFollows of 0 regardless of how many follows the user had actually performed.

The code comment // Follows performed BY this user correctly described the intent (outbound follow count), but the filter targetUsername: username implemented the opposite semantic (inbound follow count), and even that was broken because inbound DevCard-to-DevCard follows don't exist in the schema.

Fix

Changed the followLog.count filter from:

// BEFORE — always 0; queries a field that never holds a DevCard username
where: { targetUsername: username, status: 'success' }

to:

// AFTER — correct; counts outbound follow actions by this user
where: { followerId: userId, status: 'success' }

Also removed the now-unused username variable from the request.user destructure.

Tests added

Two new tests in analytics.test.ts:

  1. Asserts followLog.count is called with { followerId: MOCK_USER_ID, status: 'success' } and explicitly asserts targetUsername is absent from the query — this would have caught the original bug.
  2. Asserts totalFollows is 0 when no matching follow rows exist — baseline correctness check.

Files changed

  • apps/backend/src/routes/analytics.ts — fix query; remove unused username
  • apps/backend/src/__tests__/analytics.test.ts — two regression tests

Testing

cd apps/backend
npm test -- --reporter=verbose

All existing tests continue to pass; new tests demonstrate the fix.

The totalFollows query in /api/analytics/overview was filtering by 	argetUsername: username (the authenticated user's own DevCard username), but follow.ts writes followLog rows with 	argetUsername = the external platform handle being followed and ollowerId = the actor's user ID.

This mismatch caused totalFollows to always be 0 for real users because no followLog row ever has targetUsername equal to a DevCard username — they hold external handles like GitHub usernames.

Fix: query by ollowerId: userId to correctly count outbound follow actions performed by the authenticated user, which is also what the existing code comment ('Follows performed BY this user') intended.

Also removes the now-unused username destructure from request.user.
@vercel

vercel Bot commented Jun 17, 2026

Copy link
Copy Markdown

@hariom888 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.

@github-actions github-actions Bot added backend gssoc:approved Required label for every approved PR. Gives the base +50 points and enables contribution tracking. labels Jun 17, 2026
@github-actions

Copy link
Copy Markdown

Hi @hariom888,

Thanks for opening this pull request.

This PR has been automatically classified based on the files modified.

Applied Labels

  • gssoc:approved
  • backend

Primary Review Area

  • backend

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!

@github-actions

github-actions Bot commented Jun 17, 2026

Copy link
Copy Markdown

CI — All Checks Passed

Backend — PASS

Check Result
Lint PASS
Test PASS
Typecheck PASS

Mobile — SKIP

Check Result
Lint -
Test -

Web — SKIP

Check Result
Build -

Last updated: Wed, 17 Jun 2026 20:24:52 GMT

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backend gssoc:approved Required label for every approved PR. Gives the base +50 points and enables contribution tracking.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: /api/follow/:platform/:targetUsername and /log write inconsistent followLog records, breaking analytics totalFollows count

1 participant