Skip to content

feat(be): server-side deduplication for project & chat creation#37

Open
soumya813 wants to merge 2 commits into
Muneerali199:mainfrom
soumya813:fix/be-dedupe-projects
Open

feat(be): server-side deduplication for project & chat creation#37
soumya813 wants to merge 2 commits into
Muneerali199:mainfrom
soumya813:fix/be-dedupe-projects

Conversation

@soumya813

@soumya813 soumya813 commented Aug 19, 2025

Copy link
Copy Markdown

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

@vercel

vercel Bot commented Aug 19, 2025

Copy link
Copy Markdown

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

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant