Repository navigation
feat: save feedback in the database #50
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
91eed70
feat: save feedback in the database
KMKoushik 85e5777
fix: harden feedback submit, import and replies after review
KMKoushik e0c9d96
fix: bind reply keys to their message and settle stuck replies
KMKoushik 97339e0
fix: only the attempt holding a reply may complete it
KMKoushik f07f15f
fix: let operators reconcile replies stuck in SENDING after 10 minutes
KMKoushik d5b60f0
refactor: rename the NEW feedback status to OPEN
KMKoushik File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| import assert from 'node:assert/strict'; | ||
| import { describe, it } from 'node:test'; | ||
|
|
||
| import { keyForDraft } from './draftKey'; | ||
|
|
||
| describe('keyForDraft', () => { | ||
| it('should keep the key for retries and change it when the text is edited', () => { | ||
| let count = 0; | ||
| const newKey = () => `key-${++count}`; | ||
|
|
||
| const first = keyForDraft(null, 'Some feedback', newKey); | ||
| assert.equal(keyForDraft(first, 'Some feedback', newKey).key, first.key); | ||
| assert.notEqual(keyForDraft(first, 'Some feedback, edited', newKey).key, first.key); | ||
| }); | ||
| }); |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| /** | ||
| * The idempotency key a feedback draft is sent with. | ||
| */ | ||
| export type DraftKey = { text: string; key: string }; | ||
|
|
||
| /** | ||
| * The key to send `text` with: the previous one while the text is unchanged, so retries of the | ||
| * same draft are saved once, and a new one once it's edited, so the server never mistakes the | ||
| * edited text for the earlier attempt. | ||
| */ | ||
| export const keyForDraft = (previous: DraftKey | null, text: string, newKey: () => string) => | ||
| previous && previous.text === text ? previous : { text, key: newKey() }; |
64 changes: 64 additions & 0 deletions
64
prisma/migrations/20261110120000_add_feedback/migration.sql
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| -- CreateEnum | ||
| CREATE TYPE "FeedbackStatus" AS ENUM ('OPEN', 'REPLIED', 'CLOSED'); | ||
|
|
||
| -- CreateEnum | ||
| CREATE TYPE "FeedbackReplyStatus" AS ENUM ('SENDING', 'SENT', 'FAILED'); | ||
|
|
||
| -- CreateTable | ||
| CREATE TABLE "Feedback" ( | ||
| "id" TEXT NOT NULL, | ||
| "userId" INTEGER, | ||
| "email" TEXT NOT NULL, | ||
| "name" TEXT, | ||
| "message" TEXT NOT NULL, | ||
| "source" TEXT NOT NULL, | ||
| "appVersion" TEXT, | ||
| "clientKey" TEXT, | ||
| "externalId" TEXT, | ||
| "status" "FeedbackStatus" NOT NULL DEFAULT 'OPEN', | ||
| "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
| "updatedAt" TIMESTAMP(3) NOT NULL, | ||
|
|
||
| CONSTRAINT "Feedback_pkey" PRIMARY KEY ("id") | ||
| ); | ||
|
|
||
| -- CreateTable | ||
| CREATE TABLE "FeedbackReply" ( | ||
| "id" TEXT NOT NULL, | ||
| "feedbackId" TEXT NOT NULL, | ||
| "message" TEXT NOT NULL, | ||
| "idempotencyKey" TEXT NOT NULL, | ||
| "deliveryStatus" "FeedbackReplyStatus" NOT NULL DEFAULT 'SENDING', | ||
| "attemptId" TEXT, | ||
| "attemptStartedAt" TIMESTAMP(3), | ||
| "sentAt" TIMESTAMP(3), | ||
| "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
| "updatedAt" TIMESTAMP(3) NOT NULL, | ||
|
|
||
| CONSTRAINT "FeedbackReply_pkey" PRIMARY KEY ("id") | ||
| ); | ||
|
|
||
| -- CreateIndex | ||
| CREATE UNIQUE INDEX "Feedback_clientKey_key" ON "Feedback"("clientKey"); | ||
|
|
||
| -- CreateIndex | ||
| CREATE UNIQUE INDEX "Feedback_externalId_key" ON "Feedback"("externalId"); | ||
|
|
||
| -- CreateIndex | ||
| CREATE INDEX "Feedback_status_createdAt_idx" ON "Feedback"("status", "createdAt"); | ||
|
|
||
| -- CreateIndex | ||
| CREATE INDEX "Feedback_userId_createdAt_idx" ON "Feedback"("userId", "createdAt"); | ||
|
|
||
| -- CreateIndex | ||
| CREATE UNIQUE INDEX "FeedbackReply_idempotencyKey_key" ON "FeedbackReply"("idempotencyKey"); | ||
|
|
||
| -- CreateIndex | ||
| CREATE INDEX "FeedbackReply_feedbackId_idx" ON "FeedbackReply"("feedbackId"); | ||
|
|
||
| -- AddForeignKey | ||
| ALTER TABLE "Feedback" ADD CONSTRAINT "Feedback_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE; | ||
|
|
||
| -- AddForeignKey | ||
| ALTER TABLE "FeedbackReply" ADD CONSTRAINT "FeedbackReply_feedbackId_fkey" FOREIGN KEY ("feedbackId") REFERENCES "Feedback"("id") ON DELETE CASCADE ON UPDATE CASCADE; | ||
|
|
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| import { AdminApiError, adminApiRoute, queryParam } from '~/server/adminApi'; | ||
| import { | ||
| FeedbackNotFoundError, | ||
| setFeedbackStatus, | ||
| statusBody, | ||
| } from '~/server/api/services/feedbackAdmin'; | ||
|
|
||
| // Read by `adminApiRoute`, once the request is authorised | ||
| export const config = { api: { bodyParser: false } }; | ||
|
|
||
| /** | ||
| * PATCH /api/admin/feedback/{id} with `{ "status": "OPEN" | "REPLIED" | "CLOSED" }` | ||
| */ | ||
| export default adminApiRoute(['PATCH'], async (req, res, body) => { | ||
| const id = queryParam(req.query.id) ?? ''; | ||
| const { status } = statusBody.parse(body); | ||
|
|
||
| try { | ||
| res.status(200).json(await setFeedbackStatus(id, status)); | ||
| } catch (error) { | ||
| if (error instanceof FeedbackNotFoundError) { | ||
| throw new AdminApiError(404, error.message); | ||
| } | ||
| throw error; | ||
| } | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Show the server's reason when a retry cannot succeed.
submitFeedbackreturns errors that a retry cannot fix:TOO_MANY_REQUESTSwith "Try again in an hour."BAD_REQUESTwith "Add an email address to send feedback".This catch block ignores the error and always shows "Failed to submit feedback, please try again". When the user is rate-limited, every retry fails for up to one hour, and the user does not learn why. Show the server message for these codes. Keep the generic text for other errors.
Proposed fix
The mobile handler in
mobile/src/app/(app)/feedback.tsx(Lines 45-48) has the same gap.Committable suggestion