Skip to content

refactor(analytics): replace any casts with typed prehandler and prisma where clause (#546)#563

Merged
Harxhit merged 5 commits into
Dev-Card:mainfrom
ramnnn2006:refactor/analytics-type-safety
Jun 15, 2026
Merged

refactor(analytics): replace any casts with typed prehandler and prisma where clause (#546)#563
Harxhit merged 5 commits into
Dev-Card:mainfrom
ramnnn2006:refactor/analytics-type-safety

Conversation

@ramnnn2006

@ramnnn2006 ramnnn2006 commented Jun 13, 2026

Copy link
Copy Markdown
Contributor

Summary

the pr removes the remaining any usages in the analytics routes and replaces the hand-rolled inline auth preHandler with the shared app.authenticate decorator, so request.user and the prisma where clause are properly typed instead of cast to any. this is the analytics.ts slice of the type-safety umbrella.

Fixes #546


Type of Change

  • Bug fix
  • New feature
  • Refactor (no functional change)
  • UI / Design change
  • Tests only
  • Documentation
  • Infrastructure / DevOps
  • Security

What Changed

  • replaced the inline preHandler in both analytics routes (which cast request.server as any and (app as any).authenticate and fell back to a manual jwtVerify) with preHandler: [app.authenticate], the same decorator the rest of the backend uses.
  • dropped the (request.user as any).id / .username casts in apps/backend/src/routes/analytics.ts and used request.user.id / request.user.username directly.
  • typed the views query filter as Prisma.CardViewWhereInput instead of const whereClause: any, with import type { Prisma } from '@prisma/client'.
  • in apps/backend/src/types/fastify.d.ts, declared the authenticate decorator on FastifyInstance and augmented @fastify/jwt's FastifyJWT so request.user carries { id, username }. went through FastifyJWT rather than augmenting FastifyRequest.user directly because @fastify/jwt already owns that field.

How to Test

  1. npm run typecheck -w apps/backend passes with no errors.
  2. the changed files lint clean (npx eslint src/routes/analytics.ts is clean).
  3. hit GET /api/analytics/overview and GET /api/analytics/views with a valid token and confirm they still return the same data, and that an unauthenticated request still gets a 401. behaviour is unchanged, this is types and a swap to the shared auth decorator.

Checklist

  • My code follows the project's coding style (changed files lint clean; pre-existing repo lint errors are out of scope).
  • TypeScript compiles without errors (npm run typecheck).
  • I have added or updated tests for the changes I made.
  • All tests pass locally (no new failures vs main).
  • I have updated documentation where necessary.
  • No new console.log or debug statements left in the code.
  • Breaking changes are documented in this PR description.

Screenshots / Recordings

n/a, no ui changes.


Additional Context

scope here is just analytics.ts plus the shared fastify type file, matching #546. the other files in the umbrella (cards, connect, event, etc) have their own sub-issues so they're out of scope. note the template's checklist commands say pnpm but the repo moved to npm workspaces, so i ran the npm equivalents

Copilot AI review requested due to automatic review settings June 13, 2026 14:07
@vercel

vercel Bot commented Jun 13, 2026

Copy link
Copy Markdown

@ramnnn2006 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

Copy link
Copy Markdown

Hi @ramnnn2006,

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 added backend gssoc:approved Required label for every approved PR. Gives the base +50 points and enables contribution tracking. labels Jun 13, 2026
@github-actions

github-actions Bot commented Jun 13, 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
Check -
Build -

Last updated: Sun, 14 Jun 2026 10:56:23 GMT

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

This PR tightens authentication handling and TypeScript typings for Fastify JWT-authenticated analytics endpoints.

Changes:

  • Added Fastify module augmentations for app.authenticate and JWT request.user typing.
  • Replaced inline JWT verification preHandlers with a shared app.authenticate preHandler.
  • Improved Prisma typing for the analytics /views query where clause.

Reviewed changes

Copilot reviewed 1 out of 2 changed files in this pull request and generated 1 comment.

File Description
apps/backend/src/types/fastify.d.ts Adds Fastify/JWT type augmentations for authentication and typed request.user.
apps/backend/src/routes/analytics.ts Uses app.authenticate for route protection and strengthens Prisma where typing.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread apps/backend/src/routes/analytics.ts

@Harxhit Harxhit left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Approving

@Harxhit Harxhit merged commit 076ff4b into Dev-Card:main Jun 15, 2026
5 of 6 checks passed
@github-actions

Copy link
Copy Markdown

Congratulations @ramnnn2006 on getting PR #563 merged!

Thank you for your contribution to the project.

To receive the appropriate GSSoC labels and recognition, please mention @Harxhit in the #get-labels channel on our Discord server and share your merged PR link.

@Harxhit Harxhit added level:intermediate Moderate difficulty contribution requiring some project understanding. (+35 pts) quality:clean PR is well-structured, readable, and follows good practices. (×1.2 multiplier) type:performance Performance optimization (+15 pts) type:refactor Code refactoring/cleanup (+10 pts) type:bug Bug fixes (+10 pts) labels Jun 15, 2026
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. level:intermediate Moderate difficulty contribution requiring some project understanding. (+35 pts) quality:clean PR is well-structured, readable, and follows good practices. (×1.2 multiplier) type:bug Bug fixes (+10 pts) type:performance Performance optimization (+15 pts) type:refactor Code refactoring/cleanup (+10 pts)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Improve Type Safety in analytics.ts and Remove Remaining any Usage

3 participants