Repository navigation
feat(api): add community template API - #61
David-2610 wants to merge 2 commits into
Conversation
|
Warning Review limit reached
More reviews will be available in 46 minutes and 26 seconds. Learn how PR review limits work. To continue reviewing without waiting, enable usage-based billing in the billing tab. ⌛ How to resolve this issue?After more reviews become available, a review can be triggered using the To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based credits. 🚦 How do rate limits work?CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability. For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window. Please see our Fair Usage Limits Policy for further information. ℹ️ Review info⚙️ Run configurationConfiguration used: Organization UI Review profile: ASSERTIVE Plan: Pro Plus Run ID: 📒 Files selected for processing (3)
📝 WalkthroughWalkthroughAdds a template management feature: the Prisma ChangesTemplate API
Sequence Diagram(s)sequenceDiagram
participant Client
participant templateRouter
participant createTemplate
participant listTemplates
participant Prisma DB
rect rgba(100, 149, 237, 0.5)
note over Client,Prisma DB: POST /api/v1/templates
Client->>templateRouter: POST / with auth token + body
templateRouter->>createTemplate: validated body (title, formId?, fields?)
alt formId present, fields absent
createTemplate->>Prisma DB: form.findFirst(id=formId, createdById=userId)
Prisma DB-->>createTemplate: form | null
alt form not found/not owned
createTemplate-->>Client: 404 Source form not found
end
end
createTemplate->>Prisma DB: template.create({...body, createdById, status})
Prisma DB-->>createTemplate: created template record
createTemplate-->>Client: 201 + template
end
rect rgba(60, 179, 113, 0.5)
note over Client,Prisma DB: GET /api/v1/templates
Client->>templateRouter: GET /?category=...
templateRouter->>listTemplates: req.query.category
listTemplates->>Prisma DB: template.findMany(status=PUBLISHED, category?, orderBy, select)
Prisma DB-->>listTemplates: Template[] with createdBy
listTemplates-->>Client: 200 + {success: true, templates}
end
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
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.
Actionable comments posted: 4
🤖 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 `@apps/api/src/controllers/template.controller.ts`:
- Around line 39-51: The template creation flow is allowing an unverified formId
to be saved when fields are also present, so tighten the request validation in
template-schemas.ts to enforce the intended formId XOR fields rule. Update the
create path in template.controller.ts around prisma.template.create so formId
cannot be persisted unless it has already passed the ownership/existence check,
and ensure the branch that handles resolvedFields does not carry through a raw
formId.
- Around line 68-93: The template listing in the controller uses
prisma.template.findMany without any pagination, so it can grow unbounded as the
catalog expands. Update the community templates endpoint to accept pagination
inputs (such as page/limit or cursor-based params), apply skip/take or
cursor/limit in the findMany call, and enforce a reasonable maximum page size.
Keep the existing ordering and selected fields in the template query while
adding the pagination logic around that same findMany call.
In `@apps/api/src/lib/template-schemas.ts`:
- Around line 14-16: The template schema currently allows invalid combinations
because both formId and fields are optional, so the “either formId or fields”
contract is not enforced. Add a refine/superRefine on the schema in
template-schemas.ts to reject requests when neither or both are provided, and
make sure the validation failure surfaces as a 400 before the controller reaches
the resolvedFields logic.
In `@packages/db/prisma/schema/templates.prisma`:
- Around line 30-32: The `createdBy` relation in the templates Prisma model is
cascading deletes, which will remove community templates when a user is deleted.
Update the `createdById`/`createdBy` schema so shared templates survive author
deletion, using either a nullable `createdById` with `onDelete: SetNull` or a
restrictive relation instead of `Cascade`. Make this change in the
`templates.prisma` model definition where `createdById` and `createdBy` are
declared.
🪄 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: Organization UI
Review profile: ASSERTIVE
Plan: Pro Plus
Run ID: 7fea9167-84ea-475d-8d29-28aaef64b9e3
📒 Files selected for processing (6)
apps/api/src/controllers/template.controller.tsapps/api/src/lib/template-schemas.tsapps/api/src/routes/index.tsapps/api/src/routes/template/template.routes.tspackages/db/prisma/schema/templates.prismapackages/db/prisma/schema/user.prisma
📜 Review details
🔇 Additional comments (3)
packages/db/prisma/schema/user.prisma (1)
33-33: LGTM!apps/api/src/routes/template/template.routes.ts (1)
12-13: LGTM!apps/api/src/routes/index.ts (1)
9-9: LGTM!Also applies to: 26-26
Yes, the template will still exist |
I closed the previous PR and opened a new one with the latest changes. The new PR separates the onDelete: SetNull behavior. Now, when a form is deleted, the template remains and only its formId is set to null. |
What's Changed
This PR introduces a Community Templates system that allows users to publish forms as reusable templates and browse publicly available templates created by other users.
Closes #47
Key Implementation Details
1. Database Schema Updates
Added a new
Templatemodel with support for:title,description,category)iconSymbol,images)DRAFT,PUBLISHED,ARCHIVED)createdByIdformIdfeatured,useCount)Updated the
Usermodel to include atemplatesrelation.Added indexes for:
categorycreatedByIdstatusfeatured2. Validation Layer
Created
CreateTemplateSchemafor template creation requests.Validation includes:
3. Template Creation API
Added:
POST /api/v1/templatesImplementation details:
formIdError handling:
404 Not Foundif the referenced form does not exist or is not owned by the authenticated user4. Template Listing API
Added:
GET /api/v1/templatesImplementation details:
Publicly accessible endpoint
Returns only published templates
Supports optional category-based filtering
Orders results by:
Response includes:
5. Route Registration
Registered template routes under:
/api/v1/templatesAvailable endpoints:
GET /api/v1/templatesPOST /api/v1/templatesTemplate creation is protected using the existing authentication middleware.
Community Template API
Added backend support for community templates, enabling:
Checklist
Release Notes
Community Templates support has been added to the API. Authenticated users can now publish forms as reusable templates, while all users can browse published templates through the new templates endpoint.
/api/v1/templates.