feat(be): server-side deduplication for project & chat creation#37
Open
soumya813 wants to merge 2 commits into
Open
feat(be): server-side deduplication for project & chat creation#37soumya813 wants to merge 2 commits into
soumya813 wants to merge 2 commits into
Conversation
|
@soumya813 is attempting to deploy a commit to the muneerali199's projects Team on Vercel. A member of the Team first needs to authorize it. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Prevent duplicate project/chat rows and incorrect token accounting when users click Generate multiple times or the client retries. Adds server-side deduplication (DB + app logic) and frontend calls the new backend API for authoritative project creation.
Problem
Rapid/retried client requests can create duplicate project and chat rows in the DB and cause incorrect token decrements. Client-only guards (localStorage or UI disable) are fragile against retries or network issues.
What I changed
Backend
Add SQLite-backed persistence + prompt normalization and md5 hash.
Add unique index on (user_id, prompt_hash) to prevent duplicate persisted rows.
Implement createOrGetProject(userId, prompt) which: checks for recent identical projects (2 minute window) and returns existing row or inserts a new one.
Add in-memory request coalescing (pendingRequests) to collapse near-simultaneous identical requests.
New/updated endpoints:
POST /project — createOrGetProject (request: { userId, prompt })
GET /projects/:userId
GET /chats/:userId
Chat rows are created server-side only for newly created projects to avoid duplicate chat events.
Improve error handling in existing endpoints.
Frontend
Add lightweight API client for backend endpoints and client-side request coalescing.
Update Home to call POST /project and to load projects/chats from server when signed in (falls back to localStorage).
Adds a migration helper to move localStorage data server-side (no destructive migration implemented).
Files changed
database.ts (new) — DB wrapper, prompt normalization, dedupe logic
index.ts (modified) — request coalescing, new endpoints, DB wiring
projects.db (new DB file)
projects.ts (new) — client API + coalescing
Home.tsx (modified) — uses backend API for project creation