Filter admin CPU and GPU brands server-side#432
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Repository UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (2)
✅ Files skipped from review due to trivial changes (1)
🚧 Files skipped from review as they are similar to previous changes (1)
WalkthroughServer-side category filtering for device brands: new DeviceBrandCategory in schema; repository buildWhereClause, list/count, and byIdWithCounts; router now uses the repository; Vitest repository tests added; admin CPU/GPU pages request category-scoped brands and consume brandsQuery.data directly. ChangesDevice Brand Category Filtering
🎯 3 (Moderate) | ⏱️ ~25 minutes 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
✨ Simplify code
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
src/server/api/routers/mobile/deviceBrands.ts (1)
11-42:⚠️ Potential issue | 🟠 Major | 🏗️ Heavy liftRouter should delegate to DeviceBrandsRepository instead of building Prisma queries directly.
This router constructs the
whereclause inline and queriesctx.prisma.deviceBrand.findManydirectly. TheDeviceBrandsRepository.list()method (reviewed in this same PR) implements identical filtering logic viabuildWhereClause(). The maindeviceBrandsRoutercorrectly delegates to the repository.This violates the coding guideline: "Do not put raw Prisma queries or business logic in routers" and "Routers in
src/server/api/routers/are thin orchestration layers that handle auth context, schema-validated input, repository/service calls, and response formatting."Additionally, lines 15-16 use string literals
'cpu'and'gpu'for comparisons, which may violate the guideline: "do not use string literals for enum values in application code (routers)."♻️ Refactor to use repository
get: mobilePublicProcedure.input(GetDeviceBrandsSchema).query(async ({ ctx, input }) => { - const { search, category, limit, sortField, sortDirection } = input ?? {} - - const where: Prisma.DeviceBrandWhereInput = { - ...(search && { name: { contains: search, mode: 'insensitive' } }), - ...(category === 'cpu' && { cpus: { some: {} } }), - ...(category === 'gpu' && { gpus: { some: {} } }), - } - - const orderBy: Prisma.DeviceBrandOrderByWithRelationInput[] = [] - - if (sortField && sortDirection) { - switch (sortField) { - case 'name': - orderBy.push({ name: sortDirection }) - break - case 'devicesCount': - orderBy.push({ devices: { _count: sortDirection } }) - break - } - } - - // Default ordering if no sort specified - if (!orderBy.length) { - orderBy.push({ name: 'asc' }) - } - - return ctx.prisma.deviceBrand.findMany({ - where, - include: { _count: { select: { devices: true } } }, - orderBy, - take: limit, - }) + const repository = new DeviceBrandsRepository(ctx.prisma) + return repository.list(input ?? {}) }),You'll need to add the import:
+import { DeviceBrandsRepository } from '`@/server/repositories/device-brands.repository`'As per coding guidelines, routers in
src/server/api/routers/are thin orchestration layers that delegate to repositories, and do not put raw Prisma queries or business logic in routers.🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@src/server/api/routers/mobile/deviceBrands.ts` around lines 11 - 42, The router is building Prisma queries inline; replace that logic by delegating to the repository: import and call DeviceBrandsRepository.list (which uses buildWhereClause) instead of constructing where/orderBy and calling ctx.prisma.deviceBrand.findMany directly; pass the validated input and ctx (or ctx.prisma) as the repository expects, remove the inline where/orderBy/take logic, and ensure you stop using string literals 'cpu'/'gpu' by using the shared enum/type used by DeviceBrandsRepository for the category field so the router relies entirely on the repository for query semantics.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Outside diff comments:
In `@src/server/api/routers/mobile/deviceBrands.ts`:
- Around line 11-42: The router is building Prisma queries inline; replace that
logic by delegating to the repository: import and call
DeviceBrandsRepository.list (which uses buildWhereClause) instead of
constructing where/orderBy and calling ctx.prisma.deviceBrand.findMany directly;
pass the validated input and ctx (or ctx.prisma) as the repository expects,
remove the inline where/orderBy/take logic, and ensure you stop using string
literals 'cpu'/'gpu' by using the shared enum/type used by
DeviceBrandsRepository for the category field so the router relies entirely on
the repository for query semantics.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro
Run ID: 17d1b250-781c-4a55-b16e-c4744f46a83f
📒 Files selected for processing (6)
src/app/admin/cpus/page.tsxsrc/app/admin/gpus/page.tsxsrc/schemas/deviceBrand.tssrc/server/api/routers/mobile/deviceBrands.tssrc/server/repositories/device-brands.repository.test.tssrc/server/repositories/device-brands.repository.ts
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (1)
src/server/api/routers/mobile/deviceBrands.ts (1)
18-25: 💤 Low valueConsider delegating
getByIdto the repository for consistency.The
getprocedure now delegates toDeviceBrandsRepository, butgetByIdstill uses raw Prisma. For consistency and maintainability, consider adding a repository method for this lookup.♻️ Proposed refactor
In the repository,
byIdalready exists (line 54). Use it here:getById: mobilePublicProcedure.input(GetDeviceBrandByIdSchema).query(async ({ ctx, input }) => { - const brand = await ctx.prisma.deviceBrand.findUnique({ - where: { id: input.id }, - include: { _count: { select: { devices: true } } }, - }) - - return brand || ResourceError.deviceBrand.notFound() + const repository = new DeviceBrandsRepository(ctx.prisma) + const brand = await repository.byId(input.id) + if (!brand) throw ResourceError.deviceBrand.notFound() + + // If counts are needed, add a repository method or include here + return brand }),Note: The current
byIdin the repository does not include_count. If counts are required, extend the repository method to accept an optionalincludeparameter.Based on learnings, routers in
src/server/api/routers/are thin orchestration layers that delegate to repositories, and raw Prisma queries should not be placed in routers.🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@src/server/api/routers/mobile/deviceBrands.ts` around lines 18 - 25, The getById procedure in the router directly uses ctx.prisma instead of delegating to DeviceBrandsRepository; change getById to call DeviceBrandsRepository.byId (or extend DeviceBrandsRepository.byId to accept an optional include parameter to request _count of devices) and return the repository result or ResourceError.deviceBrand.notFound(); ensure you update the router to import/use DeviceBrandsRepository and, if needed, add an include argument to byId so the devices _count is preserved.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@src/server/api/routers/mobile/deviceBrands.ts`:
- Around line 11-12: The handler in mobileDeviceBrandsRouter.get turns an
omitted input into {} and calls DeviceBrandsRepository.list with options {
defaultLimit: undefined }, which prevents the repository's default limit from
being applied and yields an unbounded query; change the call site in
mobileDeviceBrandsRouter.get so you do not override the repository's default
(e.g. pass the original undefined input through or omit the
options/defaultLimit) so DeviceBrandsRepository.list can use its defaultLimit
and GetDeviceBrandsSchema's default limit is respected.
---
Nitpick comments:
In `@src/server/api/routers/mobile/deviceBrands.ts`:
- Around line 18-25: The getById procedure in the router directly uses
ctx.prisma instead of delegating to DeviceBrandsRepository; change getById to
call DeviceBrandsRepository.byId (or extend DeviceBrandsRepository.byId to
accept an optional include parameter to request _count of devices) and return
the repository result or ResourceError.deviceBrand.notFound(); ensure you update
the router to import/use DeviceBrandsRepository and, if needed, add an include
argument to byId so the devices _count is preserved.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro
Run ID: f0e28a66-76e4-4fbb-95a5-f0859103ec69
📒 Files selected for processing (3)
src/server/api/routers/mobile/deviceBrands.tssrc/server/repositories/device-brands.repository.test.tssrc/server/repositories/device-brands.repository.ts
🚧 Files skipped from review as they are similar to previous changes (1)
- src/server/repositories/device-brands.repository.test.ts
Description
Adds a
categoryfilter to the device brands query so CPU and GPU admin pages can request relevant brands from the server instead of hard-coding client-side brand name allowlists.Fixes #396
Type of change
How Has This Been Tested?
Checks run:
./node_modules/.bin/vitest run src/server/repositories/device-brands.repository.test.ts./node_modules/.bin/eslint src/schemas/deviceBrand.ts src/server/repositories/device-brands.repository.ts src/server/repositories/device-brands.repository.test.ts src/server/api/routers/mobile/deviceBrands.ts src/app/admin/cpus/page.tsx src/app/admin/gpus/page.tsx./node_modules/.bin/tsc --noEmit./node_modules/.bin/eslint .(completed with existing React Compiler warnings, 0 errors)Screenshots (if applicable)
N/A
Checklist
Notes for reviewers
pnpm test src/server/repositories/device-brands.repository.test.tscould not run directly in the temp worktree because the pnpm wrapper performs a dependency-status install and requires a local.env.local/DATABASE_URLfor Prisma postinstall. The same test passed through the local Vitest binary after generating/copying ignored local artifacts for verification.Summary by CodeRabbit
New Features
Tests
Documentation