From 91eed706ae939ac44b9beb1233a89067979797aa Mon Sep 17 00:00:00 2001
From: KM Koushik
Date: Tue, 10 Nov 2026 09:54:39 +1100
Subject: [PATCH 1/6] feat: save feedback in the database
Feedback is stored before the notification email is sent, deduped by a
per-draft client key or the same message within a day, and limited to 10
an hour per person. Adds an admin API (bearer ADMIN_API_TOKEN) to list,
reply to, close and import feedback. Feedback is deleted with the account.
---
.env.example | 3 +
mobile/src/app/(app)/feedback.tsx | 22 +-
.../20261110120000_add_feedback/migration.sql | 56 ++++
prisma/schema.prisma | 43 +++
src/components/Account/SubmitFeedback.tsx | 34 +-
src/env.js | 3 +
src/pages/api/admin/feedback/[id]/index.ts | 23 ++
src/pages/api/admin/feedback/[id]/reply.ts | 35 +++
src/pages/api/admin/feedback/import.ts | 14 +
src/pages/api/admin/feedback/index.ts | 11 +
src/pages/delete-account.tsx | 2 +-
src/pages/privacy.tsx | 5 +
src/server/adminApi.ts | 61 ++++
src/server/adminAuth.ts | 30 ++
src/server/api/routers/user.ts | 16 +-
src/server/api/services/accountDeletion.ts | 6 +-
src/server/api/services/feedbackAdmin.ts | 291 ++++++++++++++++++
src/server/api/services/feedbackService.ts | 154 +++++++++
src/server/mailer.ts | 85 ++++-
src/tests/accountDeletion.integration.test.ts | 3 +
src/tests/feedback.integration.test.ts | 230 ++++++++++++++
src/tests/feedback.test.ts | 283 +++++++++++++++++
22 files changed, 1384 insertions(+), 26 deletions(-)
create mode 100644 prisma/migrations/20261110120000_add_feedback/migration.sql
create mode 100644 src/pages/api/admin/feedback/[id]/index.ts
create mode 100644 src/pages/api/admin/feedback/[id]/reply.ts
create mode 100644 src/pages/api/admin/feedback/import.ts
create mode 100644 src/pages/api/admin/feedback/index.ts
create mode 100644 src/server/adminApi.ts
create mode 100644 src/server/adminAuth.ts
create mode 100644 src/server/api/services/feedbackAdmin.ts
create mode 100644 src/server/api/services/feedbackService.ts
create mode 100644 src/tests/feedback.integration.test.ts
create mode 100644 src/tests/feedback.test.ts
diff --git a/.env.example b/.env.example
index 827826b..490ab12 100644
--- a/.env.example
+++ b/.env.example
@@ -75,6 +75,9 @@ WEB_PUSH_EMAIL=
# Email options
FEEDBACK_EMAIL=
+# Bearer token for the internal admin API (/api/admin), at least 32 characters. Off when empty.
+ADMIN_API_TOKEN=
+
# Discord webhook for error notifications
DISCORD_WEBHOOK_URL=
diff --git a/mobile/src/app/(app)/feedback.tsx b/mobile/src/app/(app)/feedback.tsx
index 29b6014..d31418c 100644
--- a/mobile/src/app/(app)/feedback.tsx
+++ b/mobile/src/app/(app)/feedback.tsx
@@ -1,6 +1,8 @@
-import React, { useState } from 'react';
+import Constants from 'expo-constants';
+import * as Crypto from 'expo-crypto';
+import React, { useRef, useState } from 'react';
import { useTranslation } from 'react-i18next';
-import { ScrollView, StyleSheet } from 'react-native';
+import { Platform, ScrollView, StyleSheet } from 'react-native';
import { Input, ModalHeader, Section, icons, toast } from '@/components/ui';
import { api } from '@/lib/api';
@@ -15,20 +17,32 @@ const Feedback = () => {
const { t } = useTranslation();
const submitFeedback = api.user.submitFeedback.useMutation();
const [feedback, setFeedback] = useState('');
+ // One key per draft: resending it (a double tap, a retry after an error) never saves it twice
+ const [clientKey] = useState(() => Crypto.randomUUID());
+ // Covers taps before the header re-renders with the spinner
+ const submitting = useRef(false);
const trimmed = feedback.trim();
const allowClose = useConfirmDiscard(0 < trimmed.length && !submitFeedback.isLoading);
const submit = async () => {
- if (MIN_LENGTH > trimmed.length) {
+ if (MIN_LENGTH > trimmed.length || submitting.current) {
return;
}
+ submitting.current = true;
try {
- await submitFeedback.mutateAsync({ feedback: trimmed });
+ await submitFeedback.mutateAsync({
+ feedback: trimmed,
+ clientKey,
+ source: 'ios' === Platform.OS ? 'ios' : 'android',
+ appVersion: Constants.expoConfig?.version,
+ });
} catch (error) {
console.warn(error);
toast.error(t('account.feedback_failed'));
return;
+ } finally {
+ submitting.current = false;
}
toast.success(t('account.feedback_sent'));
diff --git a/prisma/migrations/20261110120000_add_feedback/migration.sql b/prisma/migrations/20261110120000_add_feedback/migration.sql
new file mode 100644
index 0000000..7b95272
--- /dev/null
+++ b/prisma/migrations/20261110120000_add_feedback/migration.sql
@@ -0,0 +1,56 @@
+-- CreateEnum
+CREATE TYPE "FeedbackStatus" AS ENUM ('NEW', 'REPLIED', 'CLOSED');
+
+-- 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 'NEW',
+ "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,
+ "sentAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
+
+ 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;
+
diff --git a/prisma/schema.prisma b/prisma/schema.prisma
index 696907b..f6cda0f 100644
--- a/prisma/schema.prisma
+++ b/prisma/schema.prisma
@@ -72,6 +72,7 @@ model User {
friendOf Friendship[] @relation("FriendFriendships")
recurringExpenses RecurringExpense[]
devicePushTokens DevicePushToken[]
+ feedback Feedback[]
}
model VerificationToken {
@@ -381,3 +382,45 @@ model QuickSplitExpense {
@@index([splitId, createdAt])
}
+
+enum FeedbackStatus {
+ NEW
+ REPLIED
+ CLOSED
+}
+
+/// Feedback sent from the app. Deleted with the account.
+model Feedback {
+ id String @id @default(cuid())
+ userId Int?
+ /// The sender's email and name when they sent it
+ email String
+ name String?
+ message String
+ /// `web`, `ios`, `android`, `mobile` (older app builds that don't say which) or `import`
+ source String
+ appVersion String?
+ /// Sent by the app once per draft, so a retried submit doesn't save it twice
+ clientKey String? @unique
+ /// Only set on feedback imported from the old feedback emails
+ externalId String? @unique
+ status FeedbackStatus @default(NEW)
+ createdAt DateTime @default(now())
+ updatedAt DateTime @updatedAt
+ user User? @relation(fields: [userId], references: [id], onDelete: Cascade)
+ replies FeedbackReply[]
+
+ @@index([status, createdAt])
+ @@index([userId, createdAt])
+}
+
+model FeedbackReply {
+ id String @id @default(cuid())
+ feedbackId String
+ message String
+ idempotencyKey String @unique
+ sentAt DateTime @default(now())
+ feedback Feedback @relation(fields: [feedbackId], references: [id], onDelete: Cascade)
+
+ @@index([feedbackId])
+}
diff --git a/src/components/Account/SubmitFeedback.tsx b/src/components/Account/SubmitFeedback.tsx
index 9891a8b..35cb473 100644
--- a/src/components/Account/SubmitFeedback.tsx
+++ b/src/components/Account/SubmitFeedback.tsx
@@ -1,4 +1,4 @@
-import React, { useState } from 'react';
+import React, { useRef, useState } from 'react';
import { AppDrawer } from '../ui/drawer';
import { ChevronRight, MessageSquare } from 'lucide-react';
import { api } from '~/utils/api';
@@ -12,25 +12,46 @@ import { toast } from 'sonner';
const feedbackSchema = z.object({
feedback: z
.string({ required_error: 'Feedback is required' })
- .min(10, { message: 'Feedback should be at least 10 characters' }),
+ .trim()
+ .min(10, { message: 'Feedback should be at least 10 characters' })
+ .max(5000, { message: 'Feedback should be at most 5000 characters' }),
});
export const SubmitFeedback: React.FC = () => {
const submitFeedbackMutation = api.user.submitFeedback.useMutation();
const [feedbackOpen, setFeedbackOpen] = useState(false);
+ // One key per draft: resending the same draft (a double tap, a retry) never saves it twice
+ const [clientKey, setClientKey] = useState(() => crypto.randomUUID());
+ // Covers taps before the disabled button re-renders
+ const submitting = useRef(false);
const feedbackForm = useForm>({
resolver: zodResolver(feedbackSchema),
});
async function onGroupSubmit(values: z.infer) {
+ if (submitting.current) {
+ return;
+ }
+
+ submitting.current = true;
try {
- await submitFeedbackMutation.mutateAsync({ feedback: values.feedback });
- feedbackForm.reset();
- toast.success('Feedback submitted', { duration: 1500 });
+ await submitFeedbackMutation.mutateAsync({
+ feedback: values.feedback,
+ clientKey,
+ source: 'web',
+ });
} catch (e) {
- toast.error('Failed to submit feedback');
+ // Keep the drawer and the text, so it can be sent again as it is
+ toast.error('Failed to submit feedback, please try again');
+ return;
+ } finally {
+ submitting.current = false;
}
+
+ feedbackForm.reset({ feedback: '' });
+ setClientKey(crypto.randomUUID());
+ toast.success('Feedback submitted', { duration: 1500 });
setFeedbackOpen(false);
}
@@ -53,6 +74,7 @@ export const SubmitFeedback: React.FC = () => {
className="h-[70vh]"
shouldCloseOnAction={false}
actionTitle="Submit"
+ actionDisabled={submitFeedbackMutation.isLoading}
actionOnClick={async () => {
await feedbackForm.handleSubmit(onGroupSubmit)();
}}
diff --git a/src/env.js b/src/env.js
index 1dab66d..8cc8e98 100644
--- a/src/env.js
+++ b/src/env.js
@@ -70,6 +70,8 @@ export const env = createEnv({
/** Access token for Expo's enhanced push security, needed once it's turned on for the project. */
EXPO_ACCESS_TOKEN: z.string().optional(),
FEEDBACK_EMAIL: z.string().optional(),
+ /** Bearer token for the internal admin API under /api/admin. The API is off when unset. */
+ ADMIN_API_TOKEN: z.string().min(32).optional(),
DISCORD_WEBHOOK_URL: z.string().optional(),
ADSENSE_CLIENT: z.string().startsWith('ca-pub-').optional(),
ADSENSE_FEED_SLOT: z.string().optional(),
@@ -135,6 +137,7 @@ export const env = createEnv({
WEB_PUSH_PUBLIC_KEY: process.env.WEB_PUSH_PUBLIC_KEY,
EXPO_ACCESS_TOKEN: process.env.EXPO_ACCESS_TOKEN,
FEEDBACK_EMAIL: process.env.FEEDBACK_EMAIL,
+ ADMIN_API_TOKEN: process.env.ADMIN_API_TOKEN,
DISCORD_WEBHOOK_URL: process.env.DISCORD_WEBHOOK_URL,
ADSENSE_CLIENT: process.env.ADSENSE_CLIENT,
ADSENSE_FEED_SLOT: process.env.ADSENSE_FEED_SLOT,
diff --git a/src/pages/api/admin/feedback/[id]/index.ts b/src/pages/api/admin/feedback/[id]/index.ts
new file mode 100644
index 0000000..996c2ae
--- /dev/null
+++ b/src/pages/api/admin/feedback/[id]/index.ts
@@ -0,0 +1,23 @@
+import { AdminApiError, adminApiRoute, queryParam } from '~/server/adminApi';
+import {
+ FeedbackNotFoundError,
+ setFeedbackStatus,
+ statusBody,
+} from '~/server/api/services/feedbackAdmin';
+
+/**
+ * PATCH /api/admin/feedback/{id} with `{ "status": "NEW" | "REPLIED" | "CLOSED" }`
+ */
+export default adminApiRoute(['PATCH'], async (req, res) => {
+ const id = queryParam(req.query.id) ?? '';
+ const { status } = statusBody.parse(req.body);
+
+ try {
+ res.status(200).json(await setFeedbackStatus(id, status));
+ } catch (error) {
+ if (error instanceof FeedbackNotFoundError) {
+ throw new AdminApiError(404, error.message);
+ }
+ throw error;
+ }
+});
diff --git a/src/pages/api/admin/feedback/[id]/reply.ts b/src/pages/api/admin/feedback/[id]/reply.ts
new file mode 100644
index 0000000..8628245
--- /dev/null
+++ b/src/pages/api/admin/feedback/[id]/reply.ts
@@ -0,0 +1,35 @@
+import { AdminApiError, adminApiRoute, queryParam } from '~/server/adminApi';
+import {
+ FeedbackNotFoundError,
+ IdempotencyConflictError,
+ replyBody,
+ replyToFeedback,
+} from '~/server/api/services/feedbackAdmin';
+import { MailNotSentError } from '~/server/mailer';
+
+/**
+ * POST /api/admin/feedback/{id}/reply with `{ "message": string, "idempotencyKey": string }`
+ * Emails the reply and records it: 201 when sent now, 200 when that key was already sent, 502 when
+ * the email couldn't be sent (nothing is recorded then, so it's safe to retry).
+ */
+export default adminApiRoute(['POST'], async (req, res) => {
+ const id = queryParam(req.query.id) ?? '';
+ const body = replyBody.parse(req.body);
+
+ try {
+ const { reply, created } = await replyToFeedback(id, body);
+ res.status(created ? 201 : 200).json({ reply, created });
+ } catch (error) {
+ if (error instanceof FeedbackNotFoundError) {
+ throw new AdminApiError(404, error.message);
+ }
+ if (error instanceof IdempotencyConflictError) {
+ throw new AdminApiError(409, error.message);
+ }
+ if (error instanceof MailNotSentError) {
+ console.error('Feedback reply email not sent', id, error.message);
+ throw new AdminApiError(502, `Email not sent: ${error.message}`);
+ }
+ throw error;
+ }
+});
diff --git a/src/pages/api/admin/feedback/import.ts b/src/pages/api/admin/feedback/import.ts
new file mode 100644
index 0000000..ec3f4a1
--- /dev/null
+++ b/src/pages/api/admin/feedback/import.ts
@@ -0,0 +1,14 @@
+import { adminApiRoute } from '~/server/adminApi';
+import { importBody, importFeedback } from '~/server/api/services/feedbackAdmin';
+
+// A full batch of long messages can be past Next's default 1mb.
+export const config = { api: { bodyParser: { sizeLimit: '10mb' } } };
+
+/**
+ * POST /api/admin/feedback/import with `{ "items": [...] }`, at most 500 items.
+ * Safe to run again: items already imported (by `externalId`) are skipped.
+ */
+export default adminApiRoute(['POST'], async (req, res) => {
+ const body = importBody.parse(req.body);
+ res.status(200).json(await importFeedback(body));
+});
diff --git a/src/pages/api/admin/feedback/index.ts b/src/pages/api/admin/feedback/index.ts
new file mode 100644
index 0000000..4644ed6
--- /dev/null
+++ b/src/pages/api/admin/feedback/index.ts
@@ -0,0 +1,11 @@
+import { adminApiRoute } from '~/server/adminApi';
+import { listFeedback, listFeedbackQuery } from '~/server/api/services/feedbackAdmin';
+
+/**
+ * GET /api/admin/feedback?since=&status=&limit=<1-200>&cursor=
+ * Feedback newest first, with `nextCursor` for the next page.
+ */
+export default adminApiRoute(['GET'], async (req, res) => {
+ const query = listFeedbackQuery.parse(req.query);
+ res.status(200).json(await listFeedback(query));
+});
diff --git a/src/pages/delete-account.tsx b/src/pages/delete-account.tsx
index bb27316..48727ae 100644
--- a/src/pages/delete-account.tsx
+++ b/src/pages/delete-account.tsx
@@ -44,7 +44,7 @@ const DeleteAccount: NextPage<{ feedbackEmail: string }> = ({ feedbackEmail }) =
What gets deleted
Your name, email address, profile photo, sign-in methods (Google, Apple, email), sessions,
- notification settings and your friends list are deleted straight away.
+ notification settings, your friends list and feedback you sent us are deleted straight away.
What is kept
diff --git a/src/pages/privacy.tsx b/src/pages/privacy.tsx
index d0ea07f..5bfc580 100644
--- a/src/pages/privacy.tsx
+++ b/src/pages/privacy.tsx
@@ -36,6 +36,11 @@ const Privacy: NextPage<{ feedbackEmail: string; adsEnabled: boolean }> = ({
We automatically collect information about how you interact with the Service, such as
pages visited and features used.
+
Feedback
+
+ When you send us feedback, we store it with your account, name and email address so we can
+ reply. It is deleted when you delete your account.
+
Quick splits
When you use Quick split without an account, we store the split's name, the names you
diff --git a/src/server/adminApi.ts b/src/server/adminApi.ts
new file mode 100644
index 0000000..a2053e2
--- /dev/null
+++ b/src/server/adminApi.ts
@@ -0,0 +1,61 @@
+import { type NextApiRequest, type NextApiResponse } from 'next';
+import { ZodError } from 'zod';
+
+import { env } from '~/env';
+import { isAdminAuthorized } from '~/server/adminAuth';
+import { isJsonContentType } from '~/server/mobileAuthTokens';
+
+export class AdminApiError extends Error {
+ constructor(
+ public status: number,
+ message: string,
+ ) {
+ super(message);
+ }
+}
+
+type Handler = (req: NextApiRequest, res: NextApiResponse) => Promise;
+
+/**
+ * An internal admin API route: answers 404 unless ADMIN_API_TOKEN is set, then needs
+ * `Authorization: Bearer `. Only `methods` are allowed, bodies must be JSON and
+ * every response is JSON. `AdminApiError` and zod errors become their status codes.
+ */
+export const adminApiRoute =
+ (methods: string[], handler: Handler) => async (req: NextApiRequest, res: NextApiResponse) => {
+ res.setHeader('Cache-Control', 'no-store');
+
+ if (!env.ADMIN_API_TOKEN) {
+ return res.status(404).json({ error: 'Not found' });
+ }
+
+ if (!isAdminAuthorized(req.headers.authorization, env.ADMIN_API_TOKEN)) {
+ return res.status(401).json({ error: 'Unauthorized' });
+ }
+
+ if (!req.method || !methods.includes(req.method)) {
+ res.setHeader('Allow', methods.join(', '));
+ return res.status(405).json({ error: 'Method not allowed' });
+ }
+
+ if ('GET' !== req.method && !isJsonContentType(req.headers['content-type'])) {
+ return res.status(415).json({ error: 'Send a JSON body' });
+ }
+
+ try {
+ await handler(req, res);
+ } catch (error) {
+ if (error instanceof AdminApiError) {
+ return res.status(error.status).json({ error: error.message });
+ }
+ if (error instanceof ZodError) {
+ return res.status(400).json({ error: 'Invalid request', issues: error.issues });
+ }
+ console.error('Admin API error', error);
+ return res.status(500).json({ error: 'Internal server error' });
+ }
+ };
+
+/** A single string route or query param. */
+export const queryParam = (value: string | string[] | undefined) =>
+ Array.isArray(value) ? value[0] : value;
diff --git a/src/server/adminAuth.ts b/src/server/adminAuth.ts
new file mode 100644
index 0000000..12e6e76
--- /dev/null
+++ b/src/server/adminAuth.ts
@@ -0,0 +1,30 @@
+import { createHash, timingSafeEqual } from 'crypto';
+
+import { getBearerToken } from '~/server/mobileAuthTokens';
+
+/**
+ * Pure helpers for the internal admin API. Kept free of db and env imports so they can be unit
+ * tested.
+ */
+
+const digest = (value: string) => createHash('sha256').update(value).digest();
+
+/**
+ * Whether `authorization` is `Bearer `. Compares digests in constant time, so neither the
+ * token nor its length leaks through timing. Never true without an expected token.
+ */
+export const isAdminAuthorized = (
+ authorization: string | undefined,
+ expected: string | undefined,
+) => {
+ if (!expected) {
+ return false;
+ }
+
+ const token = getBearerToken(authorization);
+ if (!token) {
+ return false;
+ }
+
+ return timingSafeEqual(digest(token), digest(expected));
+};
diff --git a/src/server/api/routers/user.ts b/src/server/api/routers/user.ts
index 513a93d..1c0977e 100644
--- a/src/server/api/routers/user.ts
+++ b/src/server/api/routers/user.ts
@@ -21,13 +21,14 @@ import { TRPCError } from '@trpc/server';
import { randomUUID } from 'crypto';
import { getDocumentUploadUrl } from '~/server/storage';
import { FILE_SIZE_LIMIT } from '~/lib/constants';
-import { sendFeedbackEmail, sendInviteEmail } from '~/server/mailer';
+import { sendInviteEmail } from '~/server/mailer';
import { pushNotification } from '~/server/notification';
import { DEVICE_PLATFORMS, isValidExpoPushToken } from '~/utils/expoPush';
import { toFixedNumber, toUIString } from '~/utils/numbers';
import { SplitwiseGroupSchema, SplitwiseUserSchema } from '~/types';
import { env } from '~/env';
import { runBalanceTransaction } from '../services/balanceTransaction';
+import { submitFeedback, submitFeedbackInput } from '../services/feedbackService';
import { getBalanceBreakdownWithFriend } from '../services/balanceBreakdown';
import { sendRecurringStatusPushNotification } from '../services/notificationService';
import { pauseRulesWith, recurrenceInputSchema } from '../services/recurringService';
@@ -451,11 +452,14 @@ export const userRouter = createTRPCRouter({
await deleteExpense(input.expenseId, ctx.session.user.id);
}),
- submitFeedback: protectedProcedure
- .input(z.object({ feedback: z.string().min(10) }))
- .mutation(async ({ input, ctx }) => {
- await sendFeedbackEmail(input.feedback, ctx.session.user);
- }),
+ submitFeedback: protectedProcedure.input(submitFeedbackInput).mutation(async ({ input, ctx }) => {
+ const { id } = await submitFeedback(
+ ctx.session.user,
+ input,
+ ctx.mobileSessionId ? 'mobile' : 'web',
+ );
+ return { id };
+ }),
getFriend: protectedProcedure
.input(z.object({ friendId: z.number() }))
diff --git a/src/server/api/services/accountDeletion.ts b/src/server/api/services/accountDeletion.ts
index e71eba6..a8eb410 100644
--- a/src/server/api/services/accountDeletion.ts
+++ b/src/server/api/services/accountDeletion.ts
@@ -344,8 +344,8 @@ const leaveSettledGroups = async (tx: Prisma.TransactionClient, userId: number)
};
/**
- * Removes the person, not what they share: their name, email, sign-in methods, sessions, devices
- * and friendships go. Expenses, balances and notes stay for the people they share them with,
+ * Removes the person, not what they share: their name, email, sign-in methods, sessions, devices,
+ * friendships and feedback go. Expenses, balances and notes stay for the people they share them with,
* against an anonymous user row.
*/
const anonymiseAccount = async (
@@ -366,6 +366,8 @@ const anonymiseAccount = async (
await tx.devicePushToken.deleteMany({ where: { userId } });
await tx.session.deleteMany({ where: { userId } });
await tx.pushNotification.deleteMany({ where: { userId } });
+ // The row stays, so its cascade doesn't remove these
+ await tx.feedback.deleteMany({ where: { userId } });
await tx.verificationToken.deleteMany({
where: {
OR: [
diff --git a/src/server/api/services/feedbackAdmin.ts b/src/server/api/services/feedbackAdmin.ts
new file mode 100644
index 0000000..89a70b6
--- /dev/null
+++ b/src/server/api/services/feedbackAdmin.ts
@@ -0,0 +1,291 @@
+import { FeedbackStatus, Prisma, type PrismaClient } from '@prisma/client';
+import { z } from 'zod';
+
+import { db as defaultDb } from '~/server/db';
+import { sendFeedbackReplyEmail } from '~/server/mailer';
+
+import { isUniqueViolation } from './feedbackService';
+
+/**
+ * Reviewing and replying to feedback, for the internal admin API.
+ */
+
+export const FEEDBACK_LIST_MAX = 200;
+export const FEEDBACK_IMPORT_MAX = 500;
+const IMPORTED_MESSAGE_MAX = 50_000;
+const REPLY_MAX = 20_000;
+/** Replies send their email inside the transaction, so allow for a slow SMTP server. */
+const REPLY_TRANSACTION_TIMEOUT_MS = 30_000;
+
+export class FeedbackNotFoundError extends Error {}
+export class IdempotencyConflictError extends Error {}
+
+const statusSchema = z.nativeEnum(FeedbackStatus);
+
+export const listFeedbackQuery = z.object({
+ since: z.string().datetime({ offset: true }).optional(),
+ status: statusSchema.optional(),
+ limit: z.coerce.number().int().min(1).max(FEEDBACK_LIST_MAX).default(50),
+ cursor: z.string().min(1).optional(),
+});
+
+export const replyBody = z.object({
+ message: z.string().trim().min(1).max(REPLY_MAX),
+ idempotencyKey: z.string().trim().min(1).max(200),
+});
+
+export const statusBody = z.object({ status: statusSchema });
+
+export const importBody = z.object({
+ items: z
+ .array(
+ z.object({
+ externalId: z.string().trim().min(1).max(500),
+ email: z.string().trim().email(),
+ message: z.string().trim().min(1).max(IMPORTED_MESSAGE_MAX),
+ createdAt: z.string().datetime({ offset: true }),
+ source: z.literal('import').optional(),
+ replies: z
+ .array(
+ z.object({
+ message: z.string().trim().min(1).max(REPLY_MAX),
+ sentAt: z.string().datetime({ offset: true }),
+ idempotencyKey: z.string().trim().min(1).max(200),
+ }),
+ )
+ .optional(),
+ }),
+ )
+ .max(FEEDBACK_IMPORT_MAX),
+});
+
+const feedbackInclude = {
+ user: { select: { id: true, name: true, email: true, deletedAt: true } },
+ replies: {
+ select: { id: true, message: true, idempotencyKey: true, sentAt: true },
+ orderBy: { sentAt: 'asc' },
+ },
+} satisfies Prisma.FeedbackInclude;
+
+type FeedbackWithDetails = Prisma.FeedbackGetPayload<{ include: typeof feedbackInclude }>;
+
+const toFeedbackJson = (feedback: FeedbackWithDetails) => ({
+ id: feedback.id,
+ createdAt: feedback.createdAt.toISOString(),
+ updatedAt: feedback.updatedAt.toISOString(),
+ status: feedback.status,
+ source: feedback.source,
+ appVersion: feedback.appVersion,
+ message: feedback.message,
+ email: feedback.email,
+ name: feedback.name,
+ user: feedback.user
+ ? {
+ id: feedback.user.id,
+ name: feedback.user.name,
+ email: feedback.user.email,
+ deletedAt: feedback.user.deletedAt?.toISOString() ?? null,
+ }
+ : null,
+ replies: feedback.replies.map(toReplyJson),
+});
+
+const toReplyJson = (reply: {
+ id: string;
+ message: string;
+ idempotencyKey: string;
+ sentAt: Date;
+}) => ({
+ id: reply.id,
+ message: reply.message,
+ idempotencyKey: reply.idempotencyKey,
+ sentAt: reply.sentAt.toISOString(),
+});
+
+/**
+ * Newest first. Pass the previous page's `nextCursor` as `cursor` for the next page.
+ */
+export const listFeedback = async (
+ query: z.infer,
+ db: PrismaClient = defaultDb,
+) => {
+ const rows = await db.feedback.findMany({
+ where: {
+ ...(query.status ? { status: query.status } : {}),
+ ...(query.since ? { createdAt: { gte: new Date(query.since) } } : {}),
+ },
+ include: feedbackInclude,
+ orderBy: [{ createdAt: 'desc' }, { id: 'desc' }],
+ take: query.limit + 1,
+ ...(query.cursor ? { cursor: { id: query.cursor }, skip: 1 } : {}),
+ });
+
+ const items = rows.slice(0, query.limit);
+ return {
+ items: items.map(toFeedbackJson),
+ nextCursor: rows.length > query.limit ? items.at(-1)?.id ?? null : null,
+ };
+};
+
+export const setFeedbackStatus = async (
+ id: string,
+ status: FeedbackStatus,
+ db: PrismaClient = defaultDb,
+) => {
+ try {
+ const feedback = await db.feedback.update({
+ where: { id },
+ data: { status },
+ include: feedbackInclude,
+ });
+ return toFeedbackJson(feedback);
+ } catch (error) {
+ if (error instanceof Prisma.PrismaClientKnownRequestError && 'P2025' === error.code) {
+ throw new FeedbackNotFoundError('Feedback not found');
+ }
+ throw error;
+ }
+};
+
+type ReplyDeps = { db: PrismaClient; send: typeof sendFeedbackReplyEmail };
+
+/**
+ * Emails a reply to whoever sent the feedback and records it. A reply with the same
+ * `idempotencyKey` returns the recorded one without emailing again. Throws `MailNotSentError` and
+ * records nothing when the email doesn't go out.
+ */
+export const replyToFeedback = async (
+ id: string,
+ { message, idempotencyKey }: z.infer,
+ { db, send }: ReplyDeps = { db: defaultDb, send: sendFeedbackReplyEmail },
+) => {
+ const run = () =>
+ db.$transaction(
+ async (tx) => {
+ // One reply at a time per feedback, so a retried request waits and then finds the first.
+ const locked = await tx.$queryRaw<{ id: string }[]>`
+ SELECT id FROM "Feedback" WHERE id = ${id} FOR UPDATE`;
+ if (0 === locked.length) {
+ throw new FeedbackNotFoundError('Feedback not found');
+ }
+
+ const existing = await tx.feedbackReply.findUnique({ where: { idempotencyKey } });
+ if (existing) {
+ if (existing.feedbackId !== id) {
+ throw new IdempotencyConflictError('idempotencyKey was used for other feedback');
+ }
+ return { reply: existing, created: false };
+ }
+
+ const feedback = await tx.feedback.findUniqueOrThrow({
+ where: { id },
+ include: { user: { select: { email: true } } },
+ });
+
+ await send({
+ to: feedback.user?.email ?? feedback.email,
+ reply: message,
+ original: feedback.message,
+ });
+
+ const reply = await tx.feedbackReply.create({
+ data: { feedbackId: id, message, idempotencyKey },
+ });
+ await tx.feedback.update({ where: { id }, data: { status: FeedbackStatus.REPLIED } });
+ return { reply, created: true };
+ },
+ { timeout: REPLY_TRANSACTION_TIMEOUT_MS },
+ );
+
+ try {
+ const { reply, created } = await run();
+ return { reply: toReplyJson(reply), created };
+ } catch (error) {
+ // Another feedback's reply took the key between our check and insert
+ if (isUniqueViolation(error, 'idempotencyKey')) {
+ throw new IdempotencyConflictError('idempotencyKey was used for other feedback');
+ }
+ throw error;
+ }
+};
+
+/**
+ * Adds feedback from before it was saved here, e.g. from the feedback emails. Items already
+ * imported (by `externalId`) are skipped, so it's safe to run again. Links each to the account with
+ * the same email, if there is one.
+ */
+export const importFeedback = async (
+ { items }: z.infer,
+ db: PrismaClient = defaultDb,
+) => {
+ const counts = { created: 0, skipped: 0, linked: 0 };
+ if (0 === items.length) {
+ return counts;
+ }
+
+ const existing = await db.feedback.findMany({
+ where: { externalId: { in: items.map((item) => item.externalId) } },
+ select: { externalId: true },
+ });
+ const imported = new Set(existing.map((feedback) => feedback.externalId));
+
+ const emails = [...new Set(items.map((item) => item.email.toLowerCase()))];
+ const users = await db.$queryRaw<{ id: number; email: string }[]>`
+ SELECT id, lower(email) AS email FROM "User"
+ WHERE lower(email) IN (${Prisma.join(emails)}) AND "deletedAt" IS NULL
+ ORDER BY id`;
+ const userIdByEmail = new Map();
+ users.forEach((user) => {
+ if (!userIdByEmail.has(user.email)) {
+ userIdByEmail.set(user.email, user.id);
+ }
+ });
+
+ // One at a time keeps the counts exact when the same externalId appears twice in a batch.
+ await items.reduce(async (previous, item) => {
+ await previous;
+
+ if (imported.has(item.externalId)) {
+ counts.skipped += 1;
+ return;
+ }
+
+ const userId = userIdByEmail.get(item.email.toLowerCase()) ?? null;
+ const replies = item.replies ?? [];
+ try {
+ await db.feedback.create({
+ data: {
+ externalId: item.externalId,
+ userId,
+ email: item.email,
+ message: item.message,
+ source: 'import',
+ status: 0 < replies.length ? FeedbackStatus.REPLIED : FeedbackStatus.NEW,
+ createdAt: new Date(item.createdAt),
+ replies: {
+ create: replies.map((reply) => ({
+ message: reply.message,
+ idempotencyKey: reply.idempotencyKey,
+ sentAt: new Date(reply.sentAt),
+ })),
+ },
+ },
+ });
+ } catch (error) {
+ if (isUniqueViolation(error, 'externalId')) {
+ imported.add(item.externalId);
+ counts.skipped += 1;
+ return;
+ }
+ throw error;
+ }
+
+ imported.add(item.externalId);
+ counts.created += 1;
+ if (null !== userId) {
+ counts.linked += 1;
+ }
+ }, Promise.resolve());
+
+ return counts;
+};
diff --git a/src/server/api/services/feedbackService.ts b/src/server/api/services/feedbackService.ts
new file mode 100644
index 0000000..6c5e6bf
--- /dev/null
+++ b/src/server/api/services/feedbackService.ts
@@ -0,0 +1,154 @@
+import { type Feedback, Prisma, type PrismaClient } from '@prisma/client';
+import { TRPCError } from '@trpc/server';
+import { z } from 'zod';
+
+import { db as defaultDb } from '~/server/db';
+import { type FeedbackEmailDetails, sendFeedbackEmail } from '~/server/mailer';
+
+const HOUR_MS = 60 * 60 * 1000;
+
+export const FEEDBACK_MIN_LENGTH = 10;
+export const FEEDBACK_MAX_LENGTH = 5000;
+export const FEEDBACK_SOURCES = ['web', 'ios', 'android'] as const;
+
+/** The same message from the same person within this window is a resend, not new feedback. */
+export const DUPLICATE_WINDOW_MS = 24 * HOUR_MS;
+export const FEEDBACK_RATE_LIMIT = { limit: 10, windowMs: HOUR_MS };
+
+export const submitFeedbackInput = z.object({
+ feedback: z.string().trim().min(FEEDBACK_MIN_LENGTH).max(FEEDBACK_MAX_LENGTH),
+ /** One per draft, so retrying the same draft never saves it twice. */
+ clientKey: z.string().uuid().optional(),
+ source: z.enum(FEEDBACK_SOURCES).optional(),
+ appVersion: z.string().trim().max(50).optional(),
+});
+
+export type SubmitFeedbackInput = z.infer;
+
+export type FeedbackSender = { id: number; email?: string | null; name?: string | null };
+
+type Deps = {
+ db: PrismaClient;
+ notify: (feedback: FeedbackEmailDetails) => Promise;
+};
+
+const defaultDeps: Deps = { db: defaultDb, notify: sendFeedbackEmail };
+
+/**
+ * Whether more feedback from someone who sent `recentCount` in the last rate limit window would
+ * be too many.
+ */
+export const isOverFeedbackLimit = (recentCount: number) =>
+ recentCount >= FEEDBACK_RATE_LIMIT.limit;
+
+export const isUniqueViolation = (error: unknown, field: string) =>
+ error instanceof Prisma.PrismaClientKnownRequestError &&
+ 'P2002' === error.code &&
+ JSON.stringify(error.meta?.target ?? '').includes(field);
+
+/**
+ * The feedback this submission repeats, if any: the same draft (`clientKey`), or the same message
+ * from the same person within a day, which covers older apps that don't send a key.
+ */
+const findExisting = async (
+ tx: Prisma.TransactionClient,
+ userId: number,
+ message: string,
+ clientKey: string | undefined,
+ now: Date,
+) => {
+ if (clientKey) {
+ const byKey = await tx.feedback.findUnique({ where: { clientKey } });
+ if (byKey) {
+ if (byKey.userId !== userId) {
+ throw new TRPCError({ code: 'CONFLICT', message: 'Feedback key already used' });
+ }
+ return byKey;
+ }
+ }
+
+ return tx.feedback.findFirst({
+ where: { userId, message, createdAt: { gte: new Date(now.getTime() - DUPLICATE_WINDOW_MS) } },
+ orderBy: { createdAt: 'desc' },
+ });
+};
+
+/**
+ * Saves feedback, then emails us about it without waiting. Resubmitting returns the saved
+ * feedback instead of saving or emailing it again.
+ */
+export const submitFeedback = async (
+ user: FeedbackSender,
+ input: SubmitFeedbackInput,
+ /** Where it came from when the client doesn't say: older app builds only send the message. */
+ fallbackSource: 'web' | 'mobile',
+ { db, notify }: Deps = defaultDeps,
+): Promise<{ id: string; created: boolean }> => {
+ if (!user.email) {
+ throw new TRPCError({ code: 'BAD_REQUEST', message: 'Add an email address to send feedback' });
+ }
+
+ const email = user.email;
+ const message = input.feedback.trim();
+ const now = new Date();
+
+ let result: { feedback: Feedback; created: boolean };
+ try {
+ // Locking the sender's row makes their submissions one at a time, so two quick resends can't
+ // both miss each other in the checks below.
+ result = await db.$transaction(async (tx) => {
+ await tx.$queryRaw`SELECT id FROM "User" WHERE id = ${user.id} FOR UPDATE`;
+
+ const existing = await findExisting(tx, user.id, message, input.clientKey, now);
+ if (existing) {
+ return { feedback: existing, created: false };
+ }
+
+ const recentCount = await tx.feedback.count({
+ where: {
+ userId: user.id,
+ createdAt: { gte: new Date(now.getTime() - FEEDBACK_RATE_LIMIT.windowMs) },
+ },
+ });
+ if (isOverFeedbackLimit(recentCount)) {
+ throw new TRPCError({
+ code: 'TOO_MANY_REQUESTS',
+ message: 'Too much feedback at once. Try again in an hour.',
+ });
+ }
+
+ const feedback = await tx.feedback.create({
+ data: {
+ userId: user.id,
+ email,
+ name: user.name ?? null,
+ message,
+ source: input.source ?? fallbackSource,
+ // An empty version is no version
+ appVersion: input.appVersion ? input.appVersion : null,
+ clientKey: input.clientKey ?? null,
+ },
+ });
+ return { feedback, created: true };
+ });
+ } catch (error) {
+ if (!input.clientKey || !isUniqueViolation(error, 'clientKey')) {
+ throw error;
+ }
+
+ const existing = await db.feedback.findUnique({ where: { clientKey: input.clientKey } });
+ if (existing?.userId !== user.id) {
+ throw new TRPCError({ code: 'CONFLICT', message: 'Feedback key already used' });
+ }
+ result = { feedback: existing, created: false };
+ }
+
+ const { feedback, created } = result;
+ if (created) {
+ notify(feedback).catch((error) =>
+ console.error('Failed to email feedback', feedback.id, error),
+ );
+ }
+
+ return { id: feedback.id, created };
+};
diff --git a/src/server/mailer.ts b/src/server/mailer.ts
index 9d0d6c1..5479b28 100644
--- a/src/server/mailer.ts
+++ b/src/server/mailer.ts
@@ -1,4 +1,3 @@
-import { type User } from 'next-auth';
import nodemailer, { type Transporter } from 'nodemailer';
import { env } from '~/env';
import { sendToDiscord } from './service-notification';
@@ -100,15 +99,87 @@ export async function sendInviteEmail(email: string, name: string) {
await sendMail(email, subject, text, html);
}
-export async function sendFeedbackEmail(feedback: string, user: User) {
- console.log('Received feedback from: ', user.email, 'Feedback: ', feedback);
+export type FeedbackEmailDetails = {
+ id: string;
+ message: string;
+ email: string;
+ name: string | null;
+ source: string;
+ appVersion: string | null;
+};
+
+/**
+ * Tells us about new feedback. The subject keeps the `Feedback received on SplitPro` prefix that
+ * existing inbox filters match.
+ */
+export async function sendFeedbackEmail(feedback: FeedbackEmailDetails) {
+ if (!env.FEEDBACK_EMAIL) return false;
+
+ const sender = feedback.name ?? feedback.email;
+ const subject = `Feedback received on SplitPro from ${sender}`;
+ const version = feedback.appVersion ? ` ${feedback.appVersion}` : '';
+ const text = `Feedback created by ${sender} :\n\nFeedback: ${feedback.message}\n\nemail: ${feedback.email}\nsource: ${feedback.source}${version}\nfeedback id: ${feedback.id}`;
+ const html = `
`;
+
+ return sendMail(env.FEEDBACK_EMAIL, subject, text, html, feedback.email);
+}
- if (!env.FEEDBACK_EMAIL) return;
+/**
+ * Replies to someone's feedback, quoting what they sent. Unlike the other emails this throws when
+ * the email can't be sent, so the caller doesn't record a reply that never went out.
+ */
+export async function sendFeedbackReplyEmail({
+ to,
+ reply,
+ original,
+}: {
+ to: string;
+ reply: string;
+ original: string;
+}) {
+ const subject = 'Re: your SplitPro feedback';
+ const quoted = original
+ .split('\n')
+ .map((line) => `> ${line}`)
+ .join('\n');
+ const text = `${reply}\n\n${quoted}`;
+ const html = `
${escapeHtml(reply)}
${escapeHtml(original)}
`;
+
+ await deliverMail(to, subject, text, html, env.FEEDBACK_EMAIL);
+}
+
+export const escapeHtml = (value: string) =>
+ value
+ .replace(/&/g, '&')
+ .replace(//g, '>')
+ .replace(/"/g, '"')
+ .replace(/'/g, ''');
- const subject = `Feedback received on SplitPro from ${user.name}`;
- const text = `Feedback created by ${user.name} :\n\nFeedback: ${feedback}\n\nemail: ${user.email}`;
+export class MailNotSentError extends Error {}
- await sendMail(env.FEEDBACK_EMAIL, subject, text, text, user.email ?? undefined);
+/**
+ * Sends an email and throws `MailNotSentError` when SMTP isn't configured or sending fails.
+ */
+async function deliverMail(
+ email: string,
+ subject: string,
+ text: string,
+ html: string,
+ replyTo?: string,
+) {
+ const transporter = getTransporter();
+ if (!transporter) {
+ throw new MailNotSentError('SMTP server not configured');
+ }
+
+ try {
+ await transporter.sendMail({ to: email, from: env.FROM_EMAIL, subject, text, html, replyTo });
+ } catch (error) {
+ throw new MailNotSentError(error instanceof Error ? error.message : 'Unknown error', {
+ cause: error,
+ });
+ }
}
async function sendMail(
diff --git a/src/tests/accountDeletion.integration.test.ts b/src/tests/accountDeletion.integration.test.ts
index ede19e6..5683fbe 100644
--- a/src/tests/accountDeletion.integration.test.ts
+++ b/src/tests/accountDeletion.integration.test.ts
@@ -209,6 +209,8 @@ void test(
});
assert.equal(await t.db.verificationToken.count({ where: { identifier } }), 1);
+ await caller.user.submitFeedback({ feedback: 'Feedback that goes with the account' });
+
// An Apple link with Apple failing doesn't stop the deletion
await t.db.account.create({
data: {
@@ -235,6 +237,7 @@ void test(
const deleted = await t.db.user.findUniqueOrThrow({ where: { id: eve.id } });
assert.ok(deleted.deletedAt);
assert.equal(deleted.email, null);
+ assert.equal(await t.db.feedback.count({ where: { userId: eve.id } }), 0);
// Single use: the same code doesn't work again
await assert.rejects(deleteWith(code), withCode('BAD_REQUEST'));
diff --git a/src/tests/feedback.integration.test.ts b/src/tests/feedback.integration.test.ts
new file mode 100644
index 0000000..2a60455
--- /dev/null
+++ b/src/tests/feedback.integration.test.ts
@@ -0,0 +1,230 @@
+import assert from 'node:assert/strict';
+import { randomUUID } from 'node:crypto';
+import { test } from 'node:test';
+
+const databaseUrl = process.env.TEST_DATABASE_URL;
+
+const setup = async () => {
+ assert.match(new URL(databaseUrl!).pathname, /test/i, 'Refusing to use a non-test database');
+ process.env.DATABASE_URL = databaseUrl;
+ process.env.SKIP_ENV_VALIDATION = '1';
+
+ const [{ db }, feedbackService, feedbackAdmin, { MailNotSentError }] = await Promise.all([
+ import('~/server/db'),
+ import('~/server/api/services/feedbackService'),
+ import('~/server/api/services/feedbackAdmin'),
+ import('~/server/mailer'),
+ ]);
+
+ const suffix = `${Date.now()}-${Math.round(Math.random() * 1e9)}`;
+ const emailOf = (name: string) => `${name.toLowerCase()}-${suffix}@example.com`;
+ const createUser = (name: string) =>
+ db.user.create({ data: { email: emailOf(name), name, currency: 'USD' } });
+
+ return {
+ db,
+ suffix,
+ emailOf,
+ createUser,
+ MailNotSentError,
+ ...feedbackService,
+ ...feedbackAdmin,
+ };
+};
+
+void test(
+ 'feedback: concurrent resends save one row and email once',
+ { skip: !databaseUrl },
+ async () => {
+ const t = await setup();
+ const alice = await t.createUser('Alice');
+ const emailed: string[] = [];
+ const deps = {
+ db: t.db,
+ notify: async ({ id }: { id: string }) => {
+ emailed.push(id);
+ },
+ };
+
+ try {
+ const clientKey = randomUUID();
+ const withKey = await Promise.all(
+ Array.from({ length: 5 }, () =>
+ t.submitFeedback(alice, { feedback: 'Double tapped feedback', clientKey }, 'web', deps),
+ ),
+ );
+ assert.equal(new Set(withKey.map(({ id }) => id)).size, 1);
+
+ // Older apps send no key: the same text at once is still one row
+ const withoutKey = await Promise.all(
+ Array.from({ length: 5 }, () =>
+ t.submitFeedback(alice, { feedback: 'Old app feedback' }, 'mobile', deps),
+ ),
+ );
+ assert.equal(new Set(withoutKey.map(({ id }) => id)).size, 1);
+
+ const rows = await t.db.feedback.findMany({ where: { userId: alice.id } });
+ assert.equal(rows.length, 2);
+ assert.equal(emailed.length, 2);
+ assert.deepEqual(rows.map((row) => row.source).sort(), ['mobile', 'web']);
+ } finally {
+ await t.db.user.delete({ where: { id: alice.id } });
+ }
+
+ assert.equal(await t.db.feedback.count({ where: { userId: alice.id } }), 0);
+ },
+);
+
+void test(
+ 'feedback: replies are sent once per key and only recorded when sent',
+ { skip: !databaseUrl },
+ async () => {
+ const t = await setup();
+ const bob = await t.createUser('Bob');
+ const sent: { to: string; reply: string; original: string }[] = [];
+ const send = async (email: { to: string; reply: string; original: string }) => {
+ sent.push(email);
+ };
+
+ try {
+ const { id } = await t.submitFeedback(bob, { feedback: 'Can you add CSV export?' }, 'web', {
+ db: t.db,
+ notify: async () => undefined,
+ });
+
+ // A failed email records nothing
+ await assert.rejects(
+ t.replyToFeedback(
+ id,
+ { message: 'Yes!', idempotencyKey: `k1-${t.suffix}` },
+ {
+ db: t.db,
+ send: async () => {
+ throw new t.MailNotSentError('SMTP server not configured');
+ },
+ },
+ ),
+ t.MailNotSentError,
+ );
+ assert.equal(await t.db.feedbackReply.count({ where: { feedbackId: id } }), 0);
+ assert.equal((await t.db.feedback.findUniqueOrThrow({ where: { id } })).status, 'NEW');
+
+ const replies = await Promise.all(
+ Array.from({ length: 3 }, () =>
+ t.replyToFeedback(
+ id,
+ { message: 'Yes, coming soon', idempotencyKey: `k1-${t.suffix}` },
+ { db: t.db, send },
+ ),
+ ),
+ );
+ assert.equal(sent.length, 1);
+ assert.deepEqual(sent[0], {
+ to: bob.email,
+ reply: 'Yes, coming soon',
+ original: 'Can you add CSV export?',
+ });
+ assert.equal(replies.filter(({ created }) => created).length, 1);
+ assert.equal(new Set(replies.map(({ reply }) => reply.id)).size, 1);
+
+ const page = await t.listFeedback({ limit: 200, status: 'REPLIED' });
+ const listed = page.items.find((item) => item.id === id);
+ assert.equal(listed?.replies.length, 1);
+ assert.equal(listed?.user?.id, bob.id);
+
+ // The key belongs to that feedback
+ const other = await t.submitFeedback(bob, { feedback: 'Another idea for you' }, 'web', {
+ db: t.db,
+ notify: async () => undefined,
+ });
+ await assert.rejects(
+ t.replyToFeedback(
+ other.id,
+ { message: 'Thanks', idempotencyKey: `k1-${t.suffix}` },
+ { db: t.db, send },
+ ),
+ t.IdempotencyConflictError,
+ );
+ await assert.rejects(
+ t.replyToFeedback(
+ 'missing',
+ { message: 'Thanks', idempotencyKey: `k2-${t.suffix}` },
+ { db: t.db, send },
+ ),
+ t.FeedbackNotFoundError,
+ );
+
+ const closed = await t.setFeedbackStatus(other.id, 'CLOSED', t.db);
+ assert.equal(closed.status, 'CLOSED');
+ } finally {
+ await t.db.user.delete({ where: { id: bob.id } });
+ }
+ },
+);
+
+void test('feedback: importing twice skips what was imported', { skip: !databaseUrl }, async () => {
+ const t = await setup();
+ const carol = await t.createUser('Carol');
+ const items = [
+ {
+ externalId: `ext-1-${t.suffix}`,
+ email: carol.email!.toUpperCase(),
+ message: 'Imported feedback with a reply',
+ createdAt: '2026-01-02T03:04:05.000Z',
+ replies: [
+ {
+ message: 'Thanks!',
+ sentAt: '2026-01-03T03:04:05.000Z',
+ idempotencyKey: `ext-1-reply-${t.suffix}`,
+ },
+ ],
+ },
+ {
+ externalId: `ext-2-${t.suffix}`,
+ email: t.emailOf('nobody'),
+ message: 'Imported feedback from someone without an account',
+ createdAt: '2026-01-04T03:04:05.000Z',
+ },
+ ];
+
+ try {
+ assert.deepEqual(await t.importFeedback({ items }, t.db), {
+ created: 2,
+ skipped: 0,
+ linked: 1,
+ });
+ assert.deepEqual(await t.importFeedback({ items }, t.db), {
+ created: 0,
+ skipped: 2,
+ linked: 0,
+ });
+
+ const imported = await t.db.feedback.findMany({
+ where: { externalId: { in: items.map((item) => item.externalId) } },
+ include: { replies: true },
+ orderBy: { createdAt: 'asc' },
+ });
+ assert.equal(imported[0]?.userId, carol.id);
+ assert.equal(imported[0]?.status, 'REPLIED');
+ assert.equal(imported[0]?.source, 'import');
+ assert.equal(imported[0]?.replies.length, 1);
+ assert.equal(imported[0]?.createdAt.toISOString(), '2026-01-02T03:04:05.000Z');
+ assert.equal(imported[1]?.userId, null);
+ assert.equal(imported[1]?.status, 'NEW');
+
+ // Paging with `since` and a cursor
+ const first = await t.listFeedback({ limit: 1, since: '2026-01-01T00:00:00Z' }, t.db);
+ assert.equal(first.items.length, 1);
+ assert.ok(first.nextCursor);
+ const second = await t.listFeedback(
+ { limit: 1, since: '2026-01-01T00:00:00Z', cursor: first.nextCursor },
+ t.db,
+ );
+ assert.notEqual(second.items[0]?.id, first.items[0]?.id);
+ } finally {
+ await t.db.feedback.deleteMany({
+ where: { externalId: { in: items.map((item) => item.externalId) } },
+ });
+ await t.db.user.delete({ where: { id: carol.id } });
+ }
+});
diff --git a/src/tests/feedback.test.ts b/src/tests/feedback.test.ts
new file mode 100644
index 0000000..0a7b20d
--- /dev/null
+++ b/src/tests/feedback.test.ts
@@ -0,0 +1,283 @@
+import assert from 'node:assert/strict';
+import { randomUUID } from 'node:crypto';
+import { describe, it } from 'node:test';
+import { Prisma, type Feedback, type PrismaClient } from '@prisma/client';
+
+import { isAdminAuthorized } from '~/server/adminAuth';
+
+process.env.SKIP_ENV_VALIDATION = '1';
+process.env.DATABASE_URL ??= 'postgresql://unused:unused@localhost:1/unused';
+
+const { FEEDBACK_RATE_LIMIT, isOverFeedbackLimit, submitFeedback, submitFeedbackInput } =
+ await import('~/server/api/services/feedbackService');
+
+const TOKEN = 'a'.repeat(40);
+
+void describe('isAdminAuthorized', () => {
+ void it('accepts the exact bearer token', () => {
+ assert.equal(isAdminAuthorized(`Bearer ${TOKEN}`, TOKEN), true);
+ assert.equal(isAdminAuthorized(`bearer ${TOKEN}`, TOKEN), true);
+ });
+
+ void it('rejects anything else', () => {
+ [
+ undefined,
+ '',
+ 'Bearer',
+ `Basic ${TOKEN}`,
+ `Bearer ${TOKEN}b`,
+ `Bearer ${TOKEN.slice(1)}`,
+ `Bearer ${'b'.repeat(40)}`,
+ TOKEN,
+ ].forEach((header) => assert.equal(isAdminAuthorized(header, TOKEN), false, String(header)));
+ });
+
+ void it('is off without a configured token', () => {
+ assert.equal(isAdminAuthorized('Bearer ', undefined), false);
+ assert.equal(isAdminAuthorized('Bearer undefined', undefined), false);
+ assert.equal(isAdminAuthorized(`Bearer ${TOKEN}`, ''), false);
+ });
+});
+
+void describe('submitFeedbackInput', () => {
+ void it('accepts what older app builds send', () => {
+ const parsed = submitFeedbackInput.parse({ feedback: ' This is my feedback ' });
+ assert.deepEqual(parsed, { feedback: 'This is my feedback' });
+ });
+
+ void it('checks length after trimming, and the key format', () => {
+ assert.equal(submitFeedbackInput.safeParse({ feedback: ' short ' }).success, false);
+ assert.equal(submitFeedbackInput.safeParse({ feedback: 'x'.repeat(5001) }).success, false);
+ assert.equal(
+ submitFeedbackInput.safeParse({ feedback: 'long enough', clientKey: 'nope' }).success,
+ false,
+ );
+ assert.equal(
+ submitFeedbackInput.safeParse({ feedback: 'long enough', source: 'import' }).success,
+ false,
+ );
+ });
+});
+
+void describe('isOverFeedbackLimit', () => {
+ void it('allows up to the limit per window', () => {
+ assert.equal(isOverFeedbackLimit(FEEDBACK_RATE_LIMIT.limit - 1), false);
+ assert.equal(isOverFeedbackLimit(FEEDBACK_RATE_LIMIT.limit), true);
+ });
+});
+
+/** Just enough of Prisma's feedback API for `submitFeedback`, kept in memory. */
+const createFakeDb = () => {
+ const rows: Feedback[] = [];
+ let failNextCreateWithDuplicateKey = false;
+
+ const feedback = {
+ findUnique: async ({ where }: { where: { clientKey: string } }) =>
+ rows.find((row) => row.clientKey === where.clientKey) ?? null,
+ findFirst: async ({
+ where,
+ }: {
+ where: { userId: number; message: string; createdAt: { gte: Date } };
+ }) =>
+ rows
+ .filter(
+ (row) =>
+ row.userId === where.userId &&
+ row.message === where.message &&
+ row.createdAt >= where.createdAt.gte,
+ )
+ .at(-1) ?? null,
+ count: async ({ where }: { where: { userId: number; createdAt: { gte: Date } } }) =>
+ rows.filter((row) => row.userId === where.userId && row.createdAt >= where.createdAt.gte)
+ .length,
+ create: async ({
+ data,
+ }: {
+ data: Omit;
+ }) => {
+ if (failNextCreateWithDuplicateKey) {
+ failNextCreateWithDuplicateKey = false;
+ // Another request saved the same draft first
+ rows.push({ ...newRow(data), id: `fb-${rows.length + 1}` });
+ throw new Prisma.PrismaClientKnownRequestError('Unique constraint failed', {
+ code: 'P2002',
+ clientVersion: 'test',
+ meta: { target: ['clientKey'] },
+ });
+ }
+ const row = { ...newRow(data), id: `fb-${rows.length + 1}` };
+ rows.push(row);
+ return row;
+ },
+ };
+
+ const newRow = (data: Omit): Feedback => ({
+ ...data,
+ id: '',
+ externalId: null,
+ status: 'NEW',
+ createdAt: new Date(),
+ updatedAt: new Date(),
+ });
+
+ const tx = { feedback, $queryRaw: async () => [] };
+ const db = {
+ ...tx,
+ $transaction: async (run: (client: typeof tx) => Promise) => run(tx),
+ } as unknown as PrismaClient;
+
+ return {
+ db,
+ rows,
+ failNextCreate: () => {
+ failNextCreateWithDuplicateKey = true;
+ },
+ };
+};
+
+const alice = { id: 1, email: 'alice@example.com', name: 'Alice' };
+const bob = { id: 2, email: 'bob@example.com', name: 'Bob' };
+
+const setup = () => {
+ const fake = createFakeDb();
+ const emailed: string[] = [];
+ const deps = {
+ db: fake.db,
+ notify: async ({ id }: { id: string }) => {
+ emailed.push(id);
+ },
+ };
+ return { ...fake, emailed, deps };
+};
+
+void describe('submitFeedback', () => {
+ void it('saves feedback, emails once and defaults the source', async () => {
+ const { rows, emailed, deps } = setup();
+ const result = await submitFeedback(alice, { feedback: ' Love the app! ' }, 'mobile', deps);
+
+ assert.deepEqual(result, { id: 'fb-1', created: true });
+ assert.equal(rows[0]?.message, 'Love the app!');
+ assert.equal(rows[0]?.source, 'mobile');
+ assert.equal(rows[0]?.email, alice.email);
+ assert.deepEqual(emailed, ['fb-1']);
+ });
+
+ void it('returns the saved feedback for the same draft key, without emailing again', async () => {
+ const { rows, emailed, deps } = setup();
+ const clientKey = randomUUID();
+ const input = { feedback: 'Please add dark mode', clientKey, source: 'web' as const };
+
+ const first = await submitFeedback(alice, input, 'web', deps);
+ const second = await submitFeedback(alice, input, 'web', deps);
+
+ assert.equal(second.id, first.id);
+ assert.equal(second.created, false);
+ assert.equal(rows.length, 1);
+ assert.equal(emailed.length, 1);
+ });
+
+ void it('refuses a draft key someone else used', async () => {
+ const { deps } = setup();
+ const clientKey = randomUUID();
+ await submitFeedback(alice, { feedback: 'Please add dark mode', clientKey }, 'web', deps);
+
+ await assert.rejects(
+ submitFeedback(bob, { feedback: 'Something else entirely', clientKey }, 'web', deps),
+ (error: Error & { code?: string }) => 'CONFLICT' === error.code,
+ );
+ });
+
+ void it('treats the same message within a day as a resend, for apps without a key', async () => {
+ const { rows, emailed, deps } = setup();
+ const first = await submitFeedback(alice, { feedback: 'Exports are broken' }, 'mobile', deps);
+ const again = await submitFeedback(alice, { feedback: ' Exports are broken ' }, 'mobile', deps);
+ // A new draft with the same text is still the same feedback
+ const withKey = await submitFeedback(
+ alice,
+ { feedback: 'Exports are broken', clientKey: randomUUID() },
+ 'web',
+ deps,
+ );
+
+ assert.equal(again.id, first.id);
+ assert.equal(withKey.id, first.id);
+ assert.equal(rows.length, 1);
+ assert.equal(emailed.length, 1);
+
+ // Someone else's identical message is theirs
+ await submitFeedback(bob, { feedback: 'Exports are broken' }, 'web', deps);
+ assert.equal(rows.length, 2);
+
+ // After a day it's new feedback
+ rows[0]!.createdAt = new Date(Date.now() - 25 * 60 * 60 * 1000);
+ const later = await submitFeedback(alice, { feedback: 'Exports are broken' }, 'web', deps);
+ assert.equal(later.created, true);
+ assert.equal(rows.length, 3);
+ });
+
+ void it('limits how much one person sends in an hour', async () => {
+ const { rows, deps } = setup();
+ await Array.from({ length: FEEDBACK_RATE_LIMIT.limit }).reduce>(
+ async (previous, _, index) => {
+ await previous;
+ return submitFeedback(alice, { feedback: `Feedback number ${index}` }, 'web', deps);
+ },
+ Promise.resolve(),
+ );
+
+ await assert.rejects(
+ submitFeedback(alice, { feedback: 'One more piece of feedback' }, 'web', deps),
+ (error: Error & { code?: string }) => 'TOO_MANY_REQUESTS' === error.code,
+ );
+ // Resends of what was saved still answer, and others aren't limited
+ const resend = await submitFeedback(alice, { feedback: 'Feedback number 3' }, 'web', deps);
+ assert.equal(resend.created, false);
+ await submitFeedback(bob, { feedback: 'Bob has thoughts too' }, 'web', deps);
+ assert.equal(rows.length, FEEDBACK_RATE_LIMIT.limit + 1);
+ });
+
+ void it('returns the other request’s feedback when both save the same draft at once', async () => {
+ const { rows, emailed, deps, failNextCreate } = setup();
+ failNextCreate();
+
+ const result = await submitFeedback(
+ alice,
+ { feedback: 'Racing submit', clientKey: randomUUID() },
+ 'web',
+ deps,
+ );
+
+ assert.equal(result.created, false);
+ assert.equal(result.id, rows[0]?.id);
+ assert.equal(emailed.length, 0);
+ });
+
+ void it('still saves when the email fails', async () => {
+ const { rows, deps } = setup();
+ const errors: unknown[] = [];
+ const originalError = console.error;
+ console.error = (...args: unknown[]) => errors.push(args);
+ try {
+ const result = await submitFeedback(alice, { feedback: 'SMTP is down today' }, 'web', {
+ ...deps,
+ notify: async () => {
+ throw new Error('SMTP down');
+ },
+ });
+ await new Promise((resolve) => setImmediate(resolve));
+ assert.equal(result.created, true);
+ assert.equal(rows.length, 1);
+ assert.equal(errors.length, 1);
+ } finally {
+ console.error = originalError;
+ }
+ });
+
+ void it('needs an email address to reply to', async () => {
+ const { deps } = setup();
+ await assert.rejects(
+ submitFeedback({ id: 3, email: null }, { feedback: 'No email here' }, 'web', deps),
+ (error: Error & { code?: string }) => 'BAD_REQUEST' === error.code,
+ );
+ });
+});
From 85e5777a3e0569b516cf6744e6350dd6d49599be Mon Sep 17 00:00:00 2001
From: KM Koushik
Date: Tue, 10 Nov 2026 10:10:32 +1100
Subject: [PATCH 2/6] fix: harden feedback submit, import and replies after
review
- Submit locks the live account and snapshots its email and name, so a
deleted account can't save feedback; a draft key sent with different
text is a conflict
- Import stores only feedback of live accounts, locked while it's saved,
and reports unmatched items
- Replies are reserved as SENDING, sent outside any transaction with
bounded SMTP timeouts, then marked SENT or FAILED; uncertain sends are
never resent
- Send errors only expose fixed messages and error codes
- Admin routes read the body only after the token check
- Clients use a new key when the text changes; the app enforces the
5000 character limit
---
mobile/src/app/(app)/feedback.tsx | 21 +-
mobile/src/i18n/locales/en.json | 1 +
mobile/src/lib/draftKey.test.ts | 15 +
mobile/src/lib/draftKey.ts | 12 +
.../20261110120000_add_feedback/migration.sql | 8 +-
prisma/schema.prisma | 20 +-
src/components/Account/SubmitFeedback.tsx | 10 +-
src/pages/api/admin/feedback/[id]/index.ts | 7 +-
src/pages/api/admin/feedback/[id]/reply.ts | 30 +-
src/pages/api/admin/feedback/import.ts | 20 +-
src/pages/api/admin/feedback/index.ts | 3 +
src/server/adminApi.ts | 59 ++-
src/server/api/services/feedbackAdmin.ts | 307 ++++++++------
src/server/api/services/feedbackService.ts | 41 +-
src/server/mailErrors.ts | 82 ++++
src/server/mailer.ts | 66 ++-
src/tests/feedback.integration.test.ts | 380 ++++++++++++++----
src/tests/feedback.test.ts | 216 +++++++++-
src/utils/draftKey.ts | 12 +
19 files changed, 1032 insertions(+), 278 deletions(-)
create mode 100644 mobile/src/lib/draftKey.test.ts
create mode 100644 mobile/src/lib/draftKey.ts
create mode 100644 src/server/mailErrors.ts
create mode 100644 src/utils/draftKey.ts
diff --git a/mobile/src/app/(app)/feedback.tsx b/mobile/src/app/(app)/feedback.tsx
index d31418c..125eba2 100644
--- a/mobile/src/app/(app)/feedback.tsx
+++ b/mobile/src/app/(app)/feedback.tsx
@@ -6,34 +6,39 @@ import { Platform, ScrollView, StyleSheet } from 'react-native';
import { Input, ModalHeader, Section, icons, toast } from '@/components/ui';
import { api } from '@/lib/api';
+import { type DraftKey, keyForDraft } from '@/lib/draftKey';
import { goBackOr, useConfirmDiscard } from '@/lib/navigation';
import { colors, spacing } from '@/theme/tokens';
-/** The server's `submitFeedback` minimum. */
+/** The server's `submitFeedback` limits. */
const MIN_LENGTH = 10;
+const MAX_LENGTH = 5000;
/** Port of the web's src/components/Account/SubmitFeedback.tsx, as a modal form. */
const Feedback = () => {
const { t } = useTranslation();
const submitFeedback = api.user.submitFeedback.useMutation();
const [feedback, setFeedback] = useState('');
- // One key per draft: resending it (a double tap, a retry after an error) never saves it twice
- const [clientKey] = useState(() => Crypto.randomUUID());
+ // Resending the same text (a double tap, a retry after an error) reuses its key, so it's saved
+ // once; edited text gets a new one
+ const draftKey = useRef(null);
// Covers taps before the header re-renders with the spinner
const submitting = useRef(false);
const trimmed = feedback.trim();
+ const tooLong = MAX_LENGTH < trimmed.length;
const allowClose = useConfirmDiscard(0 < trimmed.length && !submitFeedback.isLoading);
const submit = async () => {
- if (MIN_LENGTH > trimmed.length || submitting.current) {
+ if (MIN_LENGTH > trimmed.length || tooLong || submitting.current) {
return;
}
submitting.current = true;
+ draftKey.current = keyForDraft(draftKey.current, trimmed, () => Crypto.randomUUID());
try {
await submitFeedback.mutateAsync({
feedback: trimmed,
- clientKey,
+ clientKey: draftKey.current.key,
source: 'ios' === Platform.OS ? 'ios' : 'android',
appVersion: Constants.expoConfig?.version,
});
@@ -61,7 +66,9 @@ const Feedback = () => {
keyboardShouldPersistTaps="handled"
keyboardDismissMode="interactive"
>
-
+
{
confirm={{
label: t('account.feedback_submit'),
icon: icons.sendFill,
- disabled: MIN_LENGTH > trimmed.length,
+ disabled: MIN_LENGTH > trimmed.length || tooLong,
loading: submitFeedback.isLoading,
onPress: () => void submit(),
}}
diff --git a/mobile/src/i18n/locales/en.json b/mobile/src/i18n/locales/en.json
index a741be4..6d7008e 100644
--- a/mobile/src/i18n/locales/en.json
+++ b/mobile/src/i18n/locales/en.json
@@ -81,6 +81,7 @@
"feedback": "Send feedback",
"feedback_placeholder": "Enter your feedback",
"feedback_too_short": "Feedback should be at least 10 characters",
+ "feedback_too_long": "Feedback should be at most 5000 characters",
"feedback_submit": "Submit",
"feedback_sent": "Feedback submitted",
"feedback_failed": "Failed to submit feedback",
diff --git a/mobile/src/lib/draftKey.test.ts b/mobile/src/lib/draftKey.test.ts
new file mode 100644
index 0000000..46aea6e
--- /dev/null
+++ b/mobile/src/lib/draftKey.test.ts
@@ -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);
+ });
+});
diff --git a/mobile/src/lib/draftKey.ts b/mobile/src/lib/draftKey.ts
new file mode 100644
index 0000000..18378c4
--- /dev/null
+++ b/mobile/src/lib/draftKey.ts
@@ -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() };
diff --git a/prisma/migrations/20261110120000_add_feedback/migration.sql b/prisma/migrations/20261110120000_add_feedback/migration.sql
index 7b95272..592fbf1 100644
--- a/prisma/migrations/20261110120000_add_feedback/migration.sql
+++ b/prisma/migrations/20261110120000_add_feedback/migration.sql
@@ -1,6 +1,9 @@
-- CreateEnum
CREATE TYPE "FeedbackStatus" AS ENUM ('NEW', 'REPLIED', 'CLOSED');
+-- CreateEnum
+CREATE TYPE "FeedbackReplyStatus" AS ENUM ('SENDING', 'SENT', 'FAILED');
+
-- CreateTable
CREATE TABLE "Feedback" (
"id" TEXT NOT NULL,
@@ -25,7 +28,10 @@ CREATE TABLE "FeedbackReply" (
"feedbackId" TEXT NOT NULL,
"message" TEXT NOT NULL,
"idempotencyKey" TEXT NOT NULL,
- "sentAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
+ "deliveryStatus" "FeedbackReplyStatus" NOT NULL DEFAULT 'SENDING',
+ "sentAt" TIMESTAMP(3),
+ "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
+ "updatedAt" TIMESTAMP(3) NOT NULL,
CONSTRAINT "FeedbackReply_pkey" PRIMARY KEY ("id")
);
diff --git a/prisma/schema.prisma b/prisma/schema.prisma
index f6cda0f..32164fe 100644
--- a/prisma/schema.prisma
+++ b/prisma/schema.prisma
@@ -414,13 +414,25 @@ model Feedback {
@@index([userId, createdAt])
}
+enum FeedbackReplyStatus {
+ /// Reserved, the email is being sent, or it's unknown whether it went
+ SENDING
+ SENT
+ /// Certainly not sent, so it's safe to send again
+ FAILED
+}
+
model FeedbackReply {
- id String @id @default(cuid())
+ id String @id @default(cuid())
feedbackId String
message String
- idempotencyKey String @unique
- sentAt DateTime @default(now())
- feedback Feedback @relation(fields: [feedbackId], references: [id], onDelete: Cascade)
+ idempotencyKey String @unique
+ deliveryStatus FeedbackReplyStatus @default(SENDING)
+ /// When the email was accepted for delivery
+ sentAt DateTime?
+ createdAt DateTime @default(now())
+ updatedAt DateTime @updatedAt
+ feedback Feedback @relation(fields: [feedbackId], references: [id], onDelete: Cascade)
@@index([feedbackId])
}
diff --git a/src/components/Account/SubmitFeedback.tsx b/src/components/Account/SubmitFeedback.tsx
index 35cb473..2d1c388 100644
--- a/src/components/Account/SubmitFeedback.tsx
+++ b/src/components/Account/SubmitFeedback.tsx
@@ -8,6 +8,7 @@ import { useForm } from 'react-hook-form';
import { zodResolver } from '@hookform/resolvers/zod';
import { Form, FormControl, FormField, FormItem, FormMessage } from '../ui/form';
import { toast } from 'sonner';
+import { type DraftKey, keyForDraft } from '~/utils/draftKey';
const feedbackSchema = z.object({
feedback: z
@@ -20,8 +21,8 @@ const feedbackSchema = z.object({
export const SubmitFeedback: React.FC = () => {
const submitFeedbackMutation = api.user.submitFeedback.useMutation();
const [feedbackOpen, setFeedbackOpen] = useState(false);
- // One key per draft: resending the same draft (a double tap, a retry) never saves it twice
- const [clientKey, setClientKey] = useState(() => crypto.randomUUID());
+ // Resending the same text (a double tap, a retry) reuses its key, so it's saved once
+ const draftKey = useRef(null);
// Covers taps before the disabled button re-renders
const submitting = useRef(false);
@@ -35,10 +36,11 @@ export const SubmitFeedback: React.FC = () => {
}
submitting.current = true;
+ draftKey.current = keyForDraft(draftKey.current, values.feedback, () => crypto.randomUUID());
try {
await submitFeedbackMutation.mutateAsync({
feedback: values.feedback,
- clientKey,
+ clientKey: draftKey.current.key,
source: 'web',
});
} catch (e) {
@@ -50,7 +52,7 @@ export const SubmitFeedback: React.FC = () => {
}
feedbackForm.reset({ feedback: '' });
- setClientKey(crypto.randomUUID());
+ draftKey.current = null;
toast.success('Feedback submitted', { duration: 1500 });
setFeedbackOpen(false);
}
diff --git a/src/pages/api/admin/feedback/[id]/index.ts b/src/pages/api/admin/feedback/[id]/index.ts
index 996c2ae..58cbc60 100644
--- a/src/pages/api/admin/feedback/[id]/index.ts
+++ b/src/pages/api/admin/feedback/[id]/index.ts
@@ -5,12 +5,15 @@ import {
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": "NEW" | "REPLIED" | "CLOSED" }`
*/
-export default adminApiRoute(['PATCH'], async (req, res) => {
+export default adminApiRoute(['PATCH'], async (req, res, body) => {
const id = queryParam(req.query.id) ?? '';
- const { status } = statusBody.parse(req.body);
+ const { status } = statusBody.parse(body);
try {
res.status(200).json(await setFeedbackStatus(id, status));
diff --git a/src/pages/api/admin/feedback/[id]/reply.ts b/src/pages/api/admin/feedback/[id]/reply.ts
index 8628245..7740578 100644
--- a/src/pages/api/admin/feedback/[id]/reply.ts
+++ b/src/pages/api/admin/feedback/[id]/reply.ts
@@ -2,33 +2,47 @@ import { AdminApiError, adminApiRoute, queryParam } from '~/server/adminApi';
import {
FeedbackNotFoundError,
IdempotencyConflictError,
+ ReplyInProgressError,
replyBody,
replyToFeedback,
} from '~/server/api/services/feedbackAdmin';
import { MailNotSentError } from '~/server/mailer';
+// Read by `adminApiRoute`, once the request is authorised
+export const config = { api: { bodyParser: false } };
+
/**
* POST /api/admin/feedback/{id}/reply with `{ "message": string, "idempotencyKey": string }`
- * Emails the reply and records it: 201 when sent now, 200 when that key was already sent, 502 when
- * the email couldn't be sent (nothing is recorded then, so it's safe to retry).
+ * Emails the reply and records it. 201 when sent now, 200 when that key was already sent, 409 when
+ * it's being sent or it's unknown whether it went, 502 when the email wasn't sent (retry with the
+ * same key) or it's unknown whether it went (not retried).
*/
-export default adminApiRoute(['POST'], async (req, res) => {
+export default adminApiRoute(['POST'], async (req, res, body) => {
const id = queryParam(req.query.id) ?? '';
- const body = replyBody.parse(req.body);
+ const input = replyBody.parse(body);
try {
- const { reply, created } = await replyToFeedback(id, body);
+ const { reply, created } = await replyToFeedback(id, input);
res.status(created ? 201 : 200).json({ reply, created });
} catch (error) {
if (error instanceof FeedbackNotFoundError) {
throw new AdminApiError(404, error.message);
}
- if (error instanceof IdempotencyConflictError) {
+ if (error instanceof IdempotencyConflictError || error instanceof ReplyInProgressError) {
throw new AdminApiError(409, error.message);
}
if (error instanceof MailNotSentError) {
- console.error('Feedback reply email not sent', id, error.message);
- throw new AdminApiError(502, `Email not sent: ${error.message}`);
+ // Only codes: the error message can contain the recipient or the server's reply
+ console.error('Feedback reply email not sent', {
+ feedbackId: id,
+ code: error.code,
+ responseCode: error.responseCode,
+ definite: error.definite,
+ });
+ throw new AdminApiError(
+ 502,
+ error.definite ? 'Email not sent' : 'Email delivery uncertain; not resending',
+ );
}
throw error;
}
diff --git a/src/pages/api/admin/feedback/import.ts b/src/pages/api/admin/feedback/import.ts
index ec3f4a1..df08915 100644
--- a/src/pages/api/admin/feedback/import.ts
+++ b/src/pages/api/admin/feedback/import.ts
@@ -1,14 +1,20 @@
import { adminApiRoute } from '~/server/adminApi';
import { importBody, importFeedback } from '~/server/api/services/feedbackAdmin';
-// A full batch of long messages can be past Next's default 1mb.
-export const config = { api: { bodyParser: { sizeLimit: '10mb' } } };
+// Read by `adminApiRoute`, once the request is authorised
+export const config = { api: { bodyParser: false } };
/**
* POST /api/admin/feedback/import with `{ "items": [...] }`, at most 500 items.
- * Safe to run again: items already imported (by `externalId`) are skipped.
+ * Only feedback from existing accounts is stored; the rest is counted as `unmatched`. Safe to run
+ * again: items already imported (by `externalId`) are skipped.
*/
-export default adminApiRoute(['POST'], async (req, res) => {
- const body = importBody.parse(req.body);
- res.status(200).json(await importFeedback(body));
-});
+export default adminApiRoute(
+ ['POST'],
+ async (_req, res, body) => {
+ const input = importBody.parse(body);
+ res.status(200).json(await importFeedback(input));
+ },
+ // A full batch of long messages is past the default 1mb
+ { maxBodyBytes: 10 * 1024 * 1024 },
+);
diff --git a/src/pages/api/admin/feedback/index.ts b/src/pages/api/admin/feedback/index.ts
index 4644ed6..0c549c7 100644
--- a/src/pages/api/admin/feedback/index.ts
+++ b/src/pages/api/admin/feedback/index.ts
@@ -1,6 +1,9 @@
import { adminApiRoute } from '~/server/adminApi';
import { listFeedback, listFeedbackQuery } from '~/server/api/services/feedbackAdmin';
+// Read by `adminApiRoute`, once the request is authorised
+export const config = { api: { bodyParser: false } };
+
/**
* GET /api/admin/feedback?since=&status=&limit=<1-200>&cursor=
* Feedback newest first, with `nextCursor` for the next page.
diff --git a/src/server/adminApi.ts b/src/server/adminApi.ts
index a2053e2..c7cca7c 100644
--- a/src/server/adminApi.ts
+++ b/src/server/adminApi.ts
@@ -14,15 +14,58 @@ export class AdminApiError extends Error {
}
}
-type Handler = (req: NextApiRequest, res: NextApiResponse) => Promise;
+const DEFAULT_MAX_BODY_BYTES = 1024 * 1024;
+
+type Handler = (req: NextApiRequest, res: NextApiResponse, body: unknown) => Promise;
+
+/**
+ * Reads and parses a JSON body of at most `maxBytes`.
+ */
+const readJsonBody = async (req: NextApiRequest, maxBytes: number): Promise => {
+ const chunks: Buffer[] = [];
+ let size = 0;
+
+ for await (const chunk of req) {
+ const buffer = Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk as string);
+ size += buffer.length;
+ if (size > maxBytes) {
+ throw new AdminApiError(413, 'Body too large');
+ }
+ chunks.push(buffer);
+ }
+
+ const text = Buffer.concat(chunks).toString('utf8');
+ if ('' === text.trim()) {
+ return undefined;
+ }
+
+ try {
+ return JSON.parse(text) as unknown;
+ } catch {
+ throw new AdminApiError(400, 'Invalid JSON');
+ }
+};
+
+/**
+ * Only an error's name and Prisma or Node code: messages can contain request data.
+ */
+const safeErrorDetails = (error: unknown) => {
+ if (!(error instanceof Error)) {
+ return { name: typeof error };
+ }
+ const { code } = error as { code?: unknown };
+ return { name: error.name, code: 'string' === typeof code ? code : undefined };
+};
/**
* An internal admin API route: answers 404 unless ADMIN_API_TOKEN is set, then needs
- * `Authorization: Bearer `. Only `methods` are allowed, bodies must be JSON and
- * every response is JSON. `AdminApiError` and zod errors become their status codes.
+ * `Authorization: Bearer `. Only `methods` are allowed, bodies must be JSON (read
+ * only once the request is authorised, so the route needs `bodyParser: false` in its config) and every response is
+ * JSON. `AdminApiError` and zod errors become their status codes.
*/
export const adminApiRoute =
- (methods: string[], handler: Handler) => async (req: NextApiRequest, res: NextApiResponse) => {
+ (methods: string[], handler: Handler, { maxBodyBytes = DEFAULT_MAX_BODY_BYTES } = {}) =>
+ async (req: NextApiRequest, res: NextApiResponse) => {
res.setHeader('Cache-Control', 'no-store');
if (!env.ADMIN_API_TOKEN) {
@@ -38,12 +81,14 @@ export const adminApiRoute =
return res.status(405).json({ error: 'Method not allowed' });
}
- if ('GET' !== req.method && !isJsonContentType(req.headers['content-type'])) {
+ const hasBody = 'GET' !== req.method;
+ if (hasBody && !isJsonContentType(req.headers['content-type'])) {
return res.status(415).json({ error: 'Send a JSON body' });
}
try {
- await handler(req, res);
+ const body = hasBody ? await readJsonBody(req, maxBodyBytes) : undefined;
+ await handler(req, res, body);
} catch (error) {
if (error instanceof AdminApiError) {
return res.status(error.status).json({ error: error.message });
@@ -51,7 +96,7 @@ export const adminApiRoute =
if (error instanceof ZodError) {
return res.status(400).json({ error: 'Invalid request', issues: error.issues });
}
- console.error('Admin API error', error);
+ console.error('Admin API error', safeErrorDetails(error));
return res.status(500).json({ error: 'Internal server error' });
}
};
diff --git a/src/server/api/services/feedbackAdmin.ts b/src/server/api/services/feedbackAdmin.ts
index 89a70b6..ede3cc5 100644
--- a/src/server/api/services/feedbackAdmin.ts
+++ b/src/server/api/services/feedbackAdmin.ts
@@ -1,8 +1,14 @@
-import { FeedbackStatus, Prisma, type PrismaClient } from '@prisma/client';
+import {
+ type FeedbackReply,
+ FeedbackReplyStatus,
+ FeedbackStatus,
+ Prisma,
+ type PrismaClient,
+} from '@prisma/client';
import { z } from 'zod';
import { db as defaultDb } from '~/server/db';
-import { sendFeedbackReplyEmail } from '~/server/mailer';
+import { MailNotSentError, sendFeedbackReplyEmail } from '~/server/mailer';
import { isUniqueViolation } from './feedbackService';
@@ -14,11 +20,13 @@ export const FEEDBACK_LIST_MAX = 200;
export const FEEDBACK_IMPORT_MAX = 500;
const IMPORTED_MESSAGE_MAX = 50_000;
const REPLY_MAX = 20_000;
-/** Replies send their email inside the transaction, so allow for a slow SMTP server. */
-const REPLY_TRANSACTION_TIMEOUT_MS = 30_000;
+/** Reserving a reply retries this often when another request changes it meanwhile. */
+const RESERVE_ATTEMPTS = 3;
export class FeedbackNotFoundError extends Error {}
export class IdempotencyConflictError extends Error {}
+/** A reply with this key is being sent, or it's unknown whether it went: not sending it again. */
+export class ReplyInProgressError extends Error {}
const statusSchema = z.nativeEnum(FeedbackStatus);
@@ -59,15 +67,31 @@ export const importBody = z.object({
.max(FEEDBACK_IMPORT_MAX),
});
+const replySelect = {
+ id: true,
+ message: true,
+ idempotencyKey: true,
+ deliveryStatus: true,
+ sentAt: true,
+ createdAt: true,
+} satisfies Prisma.FeedbackReplySelect;
+
const feedbackInclude = {
user: { select: { id: true, name: true, email: true, deletedAt: true } },
- replies: {
- select: { id: true, message: true, idempotencyKey: true, sentAt: true },
- orderBy: { sentAt: 'asc' },
- },
+ replies: { select: replySelect, orderBy: { createdAt: 'asc' } },
} satisfies Prisma.FeedbackInclude;
type FeedbackWithDetails = Prisma.FeedbackGetPayload<{ include: typeof feedbackInclude }>;
+type ReplyDetails = Pick;
+
+const toReplyJson = (reply: ReplyDetails) => ({
+ id: reply.id,
+ message: reply.message,
+ idempotencyKey: reply.idempotencyKey,
+ deliveryStatus: reply.deliveryStatus,
+ sentAt: reply.sentAt?.toISOString() ?? null,
+ createdAt: reply.createdAt.toISOString(),
+});
const toFeedbackJson = (feedback: FeedbackWithDetails) => ({
id: feedback.id,
@@ -90,18 +114,6 @@ const toFeedbackJson = (feedback: FeedbackWithDetails) => ({
replies: feedback.replies.map(toReplyJson),
});
-const toReplyJson = (reply: {
- id: string;
- message: string;
- idempotencyKey: string;
- sentAt: Date;
-}) => ({
- id: reply.id,
- message: reply.message,
- idempotencyKey: reply.idempotencyKey,
- sentAt: reply.sentAt.toISOString(),
-});
-
/**
* Newest first. Pass the previous page's `nextCursor` as `cursor` for the next page.
*/
@@ -147,116 +159,158 @@ export const setFeedbackStatus = async (
}
};
+type Reservation =
+ | { send: false; reply: ReplyDetails }
+ | { send: true; reply: ReplyDetails; to: string; original: string };
+
+/**
+ * Claims the reply with this key for sending, or finds that it was sent. Each step is a single
+ * atomic write (the unique key on insert, a conditional update from FAILED), so only one request
+ * ever holds a reply in SENDING, and nothing stays locked while the email goes out.
+ */
+const reserveReply = async (
+ db: PrismaClient,
+ id: string,
+ message: string,
+ idempotencyKey: string,
+ attempt = 1,
+): Promise => {
+ const retry = () => {
+ if (RESERVE_ATTEMPTS <= attempt) {
+ throw new ReplyInProgressError('Delivery in progress or uncertain; not resending');
+ }
+ return reserveReply(db, id, message, idempotencyKey, attempt + 1);
+ };
+
+ const feedback = await db.feedback.findUnique({
+ where: { id },
+ select: { message: true, email: true, user: { select: { email: true } } },
+ });
+ if (!feedback) {
+ throw new FeedbackNotFoundError('Feedback not found');
+ }
+ const target = { to: feedback.user?.email ?? feedback.email, original: feedback.message };
+
+ const existing = await db.feedbackReply.findUnique({
+ where: { idempotencyKey },
+ select: { ...replySelect, feedbackId: true },
+ });
+
+ if (!existing) {
+ try {
+ const reply = await db.feedbackReply.create({
+ data: { feedbackId: id, message, idempotencyKey },
+ select: replySelect,
+ });
+ return { send: true, reply, ...target };
+ } catch (error) {
+ // Another request took the key first: look again at what it did
+ if (isUniqueViolation(error, 'idempotencyKey')) {
+ return retry();
+ }
+ throw error;
+ }
+ }
+
+ if (existing.feedbackId !== id) {
+ throw new IdempotencyConflictError('idempotencyKey was used for other feedback');
+ }
+ if (FeedbackReplyStatus.SENT === existing.deliveryStatus) {
+ return { send: false, reply: existing };
+ }
+ if (FeedbackReplyStatus.SENDING === existing.deliveryStatus) {
+ throw new ReplyInProgressError('Delivery in progress or uncertain; not resending');
+ }
+
+ // FAILED certainly wasn't sent: claim it again, unless another retry just did
+ const { count } = await db.feedbackReply.updateMany({
+ where: { id: existing.id, deliveryStatus: FeedbackReplyStatus.FAILED },
+ data: { deliveryStatus: FeedbackReplyStatus.SENDING, message },
+ });
+ if (0 === count) {
+ return retry();
+ }
+ return {
+ send: true,
+ reply: { ...existing, message, deliveryStatus: FeedbackReplyStatus.SENDING },
+ ...target,
+ };
+};
+
type ReplyDeps = { db: PrismaClient; send: typeof sendFeedbackReplyEmail };
/**
- * Emails a reply to whoever sent the feedback and records it. A reply with the same
- * `idempotencyKey` returns the recorded one without emailing again. Throws `MailNotSentError` and
- * records nothing when the email doesn't go out.
+ * Emails a reply to whoever sent the feedback and records it, at most once per `idempotencyKey`.
+ *
+ * The reply is reserved as SENDING first, then the email is sent outside any transaction. When it
+ * certainly failed, the reply is marked FAILED and the same key can be retried; when it's uncertain
+ * (e.g. the connection dropped mid-send) it stays SENDING and isn't sent again. Throws
+ * `MailNotSentError` in both cases, and `ReplyInProgressError` for a key that's SENDING.
*/
export const replyToFeedback = async (
id: string,
{ message, idempotencyKey }: z.infer,
{ db, send }: ReplyDeps = { db: defaultDb, send: sendFeedbackReplyEmail },
) => {
- const run = () =>
- db.$transaction(
- async (tx) => {
- // One reply at a time per feedback, so a retried request waits and then finds the first.
- const locked = await tx.$queryRaw<{ id: string }[]>`
- SELECT id FROM "Feedback" WHERE id = ${id} FOR UPDATE`;
- if (0 === locked.length) {
- throw new FeedbackNotFoundError('Feedback not found');
- }
-
- const existing = await tx.feedbackReply.findUnique({ where: { idempotencyKey } });
- if (existing) {
- if (existing.feedbackId !== id) {
- throw new IdempotencyConflictError('idempotencyKey was used for other feedback');
- }
- return { reply: existing, created: false };
- }
-
- const feedback = await tx.feedback.findUniqueOrThrow({
- where: { id },
- include: { user: { select: { email: true } } },
- });
-
- await send({
- to: feedback.user?.email ?? feedback.email,
- reply: message,
- original: feedback.message,
- });
-
- const reply = await tx.feedbackReply.create({
- data: { feedbackId: id, message, idempotencyKey },
- });
- await tx.feedback.update({ where: { id }, data: { status: FeedbackStatus.REPLIED } });
- return { reply, created: true };
- },
- { timeout: REPLY_TRANSACTION_TIMEOUT_MS },
- );
+ const reservation = await reserveReply(db, id, message, idempotencyKey);
+ if (!reservation.send) {
+ return { reply: toReplyJson(reservation.reply), created: false };
+ }
try {
- const { reply, created } = await run();
- return { reply: toReplyJson(reply), created };
+ await send({ to: reservation.to, reply: message, original: reservation.original });
} catch (error) {
- // Another feedback's reply took the key between our check and insert
- if (isUniqueViolation(error, 'idempotencyKey')) {
- throw new IdempotencyConflictError('idempotencyKey was used for other feedback');
+ if (error instanceof MailNotSentError && error.definite) {
+ await db.feedbackReply.update({
+ where: { id: reservation.reply.id },
+ data: { deliveryStatus: FeedbackReplyStatus.FAILED },
+ });
}
throw error;
}
+
+ const [reply] = await db.$transaction([
+ db.feedbackReply.update({
+ where: { id: reservation.reply.id },
+ data: { deliveryStatus: FeedbackReplyStatus.SENT, sentAt: new Date() },
+ select: replySelect,
+ }),
+ db.feedback.updateMany({ where: { id }, data: { status: FeedbackStatus.REPLIED } }),
+ ]);
+ return { reply: toReplyJson(reply), created: true };
};
+type ImportItem = z.infer['items'][number];
+type ImportOutcome = 'created' | 'skipped' | 'unmatched';
+
/**
- * Adds feedback from before it was saved here, e.g. from the feedback emails. Items already
- * imported (by `externalId`) are skipped, so it's safe to run again. Links each to the account with
- * the same email, if there is one.
+ * Imports one item for a live account with its email, in a transaction that holds that account
+ * against deletion until the feedback is saved.
*/
-export const importFeedback = async (
- { items }: z.infer,
- db: PrismaClient = defaultDb,
-) => {
- const counts = { created: 0, skipped: 0, linked: 0 };
- if (0 === items.length) {
- return counts;
- }
-
- const existing = await db.feedback.findMany({
- where: { externalId: { in: items.map((item) => item.externalId) } },
- select: { externalId: true },
- });
- const imported = new Set(existing.map((feedback) => feedback.externalId));
-
- const emails = [...new Set(items.map((item) => item.email.toLowerCase()))];
- const users = await db.$queryRaw<{ id: number; email: string }[]>`
- SELECT id, lower(email) AS email FROM "User"
- WHERE lower(email) IN (${Prisma.join(emails)}) AND "deletedAt" IS NULL
- ORDER BY id`;
- const userIdByEmail = new Map();
- users.forEach((user) => {
- if (!userIdByEmail.has(user.email)) {
- userIdByEmail.set(user.email, user.id);
- }
- });
-
- // One at a time keeps the counts exact when the same externalId appears twice in a batch.
- await items.reduce(async (previous, item) => {
- await previous;
+const importItem = async (db: PrismaClient, item: ImportItem): Promise => {
+ try {
+ return await db.$transaction(async (tx) => {
+ const [user] = await tx.$queryRaw<{ id: number }[]>`
+ SELECT id FROM "User"
+ WHERE lower(email) = lower(${item.email}) AND "deletedAt" IS NULL
+ ORDER BY id LIMIT 1 FOR SHARE`;
+ if (!user) {
+ return 'unmatched';
+ }
- if (imported.has(item.externalId)) {
- counts.skipped += 1;
- return;
- }
+ const existing = await tx.feedback.findUnique({
+ where: { externalId: item.externalId },
+ select: { id: true },
+ });
+ if (existing) {
+ return 'skipped';
+ }
- const userId = userIdByEmail.get(item.email.toLowerCase()) ?? null;
- const replies = item.replies ?? [];
- try {
- await db.feedback.create({
+ const replies = item.replies ?? [];
+ await tx.feedback.create({
data: {
externalId: item.externalId,
- userId,
+ userId: user.id,
email: item.email,
message: item.message,
source: 'import',
@@ -266,25 +320,40 @@ export const importFeedback = async (
create: replies.map((reply) => ({
message: reply.message,
idempotencyKey: reply.idempotencyKey,
+ deliveryStatus: FeedbackReplyStatus.SENT,
sentAt: new Date(reply.sentAt),
+ createdAt: new Date(reply.sentAt),
})),
},
},
});
- } catch (error) {
- if (isUniqueViolation(error, 'externalId')) {
- imported.add(item.externalId);
- counts.skipped += 1;
- return;
- }
- throw error;
+ return 'created';
+ });
+ } catch (error) {
+ // Imported meanwhile by another run
+ if (isUniqueViolation(error, 'externalId')) {
+ return 'skipped';
}
+ throw error;
+ }
+};
- imported.add(item.externalId);
- counts.created += 1;
- if (null !== userId) {
- counts.linked += 1;
- }
+/**
+ * Adds feedback from before it was saved here, e.g. from the feedback emails. Only feedback from
+ * an existing, not deleted, account is imported: anything else is counted as `unmatched` and not
+ * stored, so feedback of deleted accounts doesn't come back. Items already imported (by
+ * `externalId`) are skipped, so it's safe to run again.
+ */
+export const importFeedback = async (
+ { items }: z.infer,
+ db: PrismaClient = defaultDb,
+) => {
+ const counts: Record = { created: 0, skipped: 0, unmatched: 0 };
+
+ // One at a time keeps the counts exact when the same externalId appears twice in a batch.
+ await items.reduce(async (previous, item) => {
+ await previous;
+ counts[await importItem(db, item)] += 1;
}, Promise.resolve());
return counts;
diff --git a/src/server/api/services/feedbackService.ts b/src/server/api/services/feedbackService.ts
index 6c5e6bf..34845d0 100644
--- a/src/server/api/services/feedbackService.ts
+++ b/src/server/api/services/feedbackService.ts
@@ -4,6 +4,7 @@ import { z } from 'zod';
import { db as defaultDb } from '~/server/db';
import { type FeedbackEmailDetails, sendFeedbackEmail } from '~/server/mailer';
+import { safeMailErrorDetails } from '~/server/mailErrors';
const HOUR_MS = 60 * 60 * 1000;
@@ -25,7 +26,8 @@ export const submitFeedbackInput = z.object({
export type SubmitFeedbackInput = z.infer;
-export type FeedbackSender = { id: number; email?: string | null; name?: string | null };
+/** Only the id is used: the email and name are read from the account when it's saved. */
+export type FeedbackSender = { id: number };
type Deps = {
db: PrismaClient;
@@ -47,8 +49,9 @@ export const isUniqueViolation = (error: unknown, field: string) =>
JSON.stringify(error.meta?.target ?? '').includes(field);
/**
- * The feedback this submission repeats, if any: the same draft (`clientKey`), or the same message
- * from the same person within a day, which covers older apps that don't send a key.
+ * The feedback this submission repeats, if any: the same draft (`clientKey`) with the same text, or
+ * the same message from the same person within a day, which covers older apps that don't send a
+ * key.
*/
const findExisting = async (
tx: Prisma.TransactionClient,
@@ -60,7 +63,9 @@ const findExisting = async (
if (clientKey) {
const byKey = await tx.feedback.findUnique({ where: { clientKey } });
if (byKey) {
- if (byKey.userId !== userId) {
+ // Another person's key, or the same draft edited after a failed send: never report text
+ // as saved when it wasn't
+ if (byKey.userId !== userId || byKey.message !== message) {
throw new TRPCError({ code: 'CONFLICT', message: 'Feedback key already used' });
}
return byKey;
@@ -84,20 +89,26 @@ export const submitFeedback = async (
fallbackSource: 'web' | 'mobile',
{ db, notify }: Deps = defaultDeps,
): Promise<{ id: string; created: boolean }> => {
- if (!user.email) {
- throw new TRPCError({ code: 'BAD_REQUEST', message: 'Add an email address to send feedback' });
- }
-
- const email = user.email;
const message = input.feedback.trim();
const now = new Date();
let result: { feedback: Feedback; created: boolean };
try {
// Locking the sender's row makes their submissions one at a time, so two quick resends can't
- // both miss each other in the checks below.
+ // both miss each other in the checks below. It also waits for an account deletion that's under
+ // way, and the email and name come from the locked row, not the possibly stale session.
result = await db.$transaction(async (tx) => {
- await tx.$queryRaw`SELECT id FROM "User" WHERE id = ${user.id} FOR UPDATE`;
+ const [sender] = await tx.$queryRaw<{ email: string | null; name: string | null }[]>`
+ SELECT email, name FROM "User" WHERE id = ${user.id} AND "deletedAt" IS NULL FOR UPDATE`;
+ if (!sender) {
+ throw new TRPCError({ code: 'UNAUTHORIZED', message: 'Account not found' });
+ }
+ if (!sender.email) {
+ throw new TRPCError({
+ code: 'BAD_REQUEST',
+ message: 'Add an email address to send feedback',
+ });
+ }
const existing = await findExisting(tx, user.id, message, input.clientKey, now);
if (existing) {
@@ -120,8 +131,8 @@ export const submitFeedback = async (
const feedback = await tx.feedback.create({
data: {
userId: user.id,
- email,
- name: user.name ?? null,
+ email: sender.email,
+ name: sender.name,
message,
source: input.source ?? fallbackSource,
// An empty version is no version
@@ -137,7 +148,7 @@ export const submitFeedback = async (
}
const existing = await db.feedback.findUnique({ where: { clientKey: input.clientKey } });
- if (existing?.userId !== user.id) {
+ if (existing?.userId !== user.id || existing.message !== message) {
throw new TRPCError({ code: 'CONFLICT', message: 'Feedback key already used' });
}
result = { feedback: existing, created: false };
@@ -146,7 +157,7 @@ export const submitFeedback = async (
const { feedback, created } = result;
if (created) {
notify(feedback).catch((error) =>
- console.error('Failed to email feedback', feedback.id, error),
+ console.error('Failed to email feedback', feedback.id, safeMailErrorDetails(error)),
);
}
diff --git a/src/server/mailErrors.ts b/src/server/mailErrors.ts
new file mode 100644
index 0000000..4ba7cee
--- /dev/null
+++ b/src/server/mailErrors.ts
@@ -0,0 +1,82 @@
+/**
+ * Pure helpers for SMTP send errors. Kept free of db and env imports so they can be unit tested.
+ */
+
+/** Our own code for "SMTP isn't configured", which never reaches a server. */
+export const MAIL_NOT_CONFIGURED = 'ENOTCONFIGURED';
+
+/** Failures that happen before the server could have accepted the message. */
+const DEFINITE_CODES = new Set([
+ MAIL_NOT_CONFIGURED,
+ 'EAUTH',
+ 'ENOAUTH',
+ 'EOAUTH2',
+ 'EENVELOPE',
+ 'EMESSAGE',
+ 'EDNS',
+ 'ETLS',
+ 'EREQUIRETLS',
+ 'ECONFIG',
+]);
+/** Timeouts while still connecting, before anything was sent. */
+const SETUP_TIMEOUTS = new Set(['CONNECT_TIMEOUT', 'GREETING_TIMEOUT', 'UPGRADE_TIMEOUT']);
+/** Socket errors that only happen while opening the connection. */
+const CONNECT_ERRORS = new Set([
+ 'ECONNREFUSED',
+ 'ENOTFOUND',
+ 'EHOSTUNREACH',
+ 'ENETUNREACH',
+ 'EAI_AGAIN',
+]);
+
+type SmtpError = {
+ code?: unknown;
+ originalCode?: unknown;
+ responseCode?: unknown;
+ timeoutType?: unknown;
+};
+
+const asSmtpError = (error: unknown): SmtpError =>
+ error && 'object' === typeof error ? (error as SmtpError) : {};
+
+const ERROR_CODE = /^E[A-Z0-9_]{1,30}$/;
+
+/**
+ * The parts of a send error that are safe to log or keep: nodemailer's error code and the SMTP
+ * reply code. Never the message, which can contain addresses or the server's reply text.
+ */
+export const safeMailErrorDetails = (error: unknown) => {
+ const { code, responseCode } = asSmtpError(error);
+ return {
+ code: 'string' === typeof code && ERROR_CODE.test(code) ? code : undefined,
+ responseCode:
+ 'number' === typeof responseCode && Number.isInteger(responseCode) ? responseCode : undefined,
+ };
+};
+
+/**
+ * Whether a send error means the message certainly wasn't accepted, so sending it again can't
+ * deliver it twice. Dropped connections and timeouts once connected are uncertain: the server may
+ * have accepted the message before the connection went.
+ */
+export const isDefiniteSendFailure = (error: unknown) => {
+ const { code, originalCode, responseCode, timeoutType } = asSmtpError(error);
+
+ // The server answered, and refused
+ if ('number' === typeof responseCode && 400 <= responseCode) {
+ return true;
+ }
+ if ('string' !== typeof code) {
+ return false;
+ }
+ if (DEFINITE_CODES.has(code)) {
+ return true;
+ }
+ if ('ETIMEDOUT' === code) {
+ return 'string' === typeof timeoutType && SETUP_TIMEOUTS.has(timeoutType);
+ }
+ if ('ESOCKET' === code) {
+ return 'string' === typeof originalCode && CONNECT_ERRORS.has(originalCode);
+ }
+ return false;
+};
diff --git a/src/server/mailer.ts b/src/server/mailer.ts
index 5479b28..e8de50c 100644
--- a/src/server/mailer.ts
+++ b/src/server/mailer.ts
@@ -1,12 +1,21 @@
import nodemailer, { type Transporter } from 'nodemailer';
import { env } from '~/env';
+import { MAIL_NOT_CONFIGURED, isDefiniteSendFailure, safeMailErrorDetails } from './mailErrors';
import { sendToDiscord } from './service-notification';
let transporter: Transporter;
+let boundedTransporter: Transporter;
-const getTransporter = () => {
- if (transporter) return transporter;
+/**
+ * For sends a request waits on: gives up instead of hanging on a slow or silent server.
+ */
+const BOUNDED_TIMEOUTS = {
+ connectionTimeout: 10_000,
+ greetingTimeout: 10_000,
+ socketTimeout: 15_000,
+};
+const createTransporter = (timeouts?: typeof BOUNDED_TIMEOUTS) => {
const host = env.EMAIL_SERVER_HOST;
const port = parseInt(env.EMAIL_SERVER_PORT ?? '');
const user = env.EMAIL_SERVER_USER;
@@ -27,10 +36,30 @@ const getTransporter = () => {
tls: {
rejectUnauthorized: env.NODE_ENV !== 'development',
},
+ ...timeouts,
};
- transporter = nodemailer.createTransport(transport);
- return transporter;
+ return nodemailer.createTransport(transport);
+};
+
+const getTransporter = () => {
+ if (transporter) return transporter;
+
+ const created = createTransporter();
+ if (created) {
+ transporter = created;
+ }
+ return created;
+};
+
+const getBoundedTransporter = () => {
+ if (boundedTransporter) return boundedTransporter;
+
+ const created = createTransporter(BOUNDED_TIMEOUTS);
+ if (created) {
+ boundedTransporter = created;
+ }
+ return created;
};
export async function sendSignUpEmail(email: string, token: string, url: string) {
@@ -156,10 +185,27 @@ export const escapeHtml = (value: string) =>
.replace(/"/g, '"')
.replace(/'/g, ''');
-export class MailNotSentError extends Error {}
+/**
+ * An email that wasn't sent, or might not have been. Carries only nodemailer's error code and the
+ * SMTP reply code, never the error message, so it's safe to log.
+ */
+export class MailNotSentError extends Error {
+ /** True when the server certainly didn't accept the message, so retrying can't send it twice. */
+ definite: boolean;
+ code?: string;
+ responseCode?: number;
+
+ constructor(error: unknown) {
+ super('Email not sent');
+ this.name = 'MailNotSentError';
+ this.definite = isDefiniteSendFailure(error);
+ Object.assign(this, safeMailErrorDetails(error));
+ }
+}
/**
- * Sends an email and throws `MailNotSentError` when SMTP isn't configured or sending fails.
+ * Sends an email with bounded timeouts and throws `MailNotSentError` when SMTP isn't configured or
+ * sending fails.
*/
async function deliverMail(
email: string,
@@ -168,17 +214,15 @@ async function deliverMail(
html: string,
replyTo?: string,
) {
- const transporter = getTransporter();
+ const transporter = getBoundedTransporter();
if (!transporter) {
- throw new MailNotSentError('SMTP server not configured');
+ throw new MailNotSentError({ code: MAIL_NOT_CONFIGURED });
}
try {
await transporter.sendMail({ to: email, from: env.FROM_EMAIL, subject, text, html, replyTo });
} catch (error) {
- throw new MailNotSentError(error instanceof Error ? error.message : 'Unknown error', {
- cause: error,
- });
+ throw new MailNotSentError(error);
}
}
diff --git a/src/tests/feedback.integration.test.ts b/src/tests/feedback.integration.test.ts
index 2a60455..aeeb1b8 100644
--- a/src/tests/feedback.integration.test.ts
+++ b/src/tests/feedback.integration.test.ts
@@ -75,45 +75,107 @@ void test(
},
);
+/** How account deletion leaves the user row; see `anonymiseAccount`. */
+const anonymised = () => ({ deletedAt: new Date(), email: null, name: 'Deleted user' });
+
void test(
- 'feedback: replies are sent once per key and only recorded when sent',
+ 'feedback: a deleted account can’t send feedback with its old session',
+ { skip: !databaseUrl },
+ async () => {
+ const t = await setup();
+ const dora = await t.createUser('Dora');
+ const erin = await t.createUser('Erin');
+ const deps = { db: t.db, notify: async () => undefined };
+ const isUnauthorized = (error: Error & { code?: string }) => 'UNAUTHORIZED' === error.code;
+
+ try {
+ // Already deleted: the session still has the old email and name
+ const staleDora = { id: dora.id, email: dora.email, name: dora.name };
+ await t.db.user.update({
+ where: { id: dora.id },
+ data: anonymised(),
+ });
+ await assert.rejects(
+ t.submitFeedback(staleDora, { feedback: 'Sent after deleting' }, 'web', deps),
+ isUnauthorized,
+ );
+
+ // Deleted while the submit waits for the account
+ let release!: () => void;
+ let locked!: () => void;
+ const isLocked = new Promise((resolve) => (locked = resolve));
+ const deletion = t.db.$transaction(async (tx) => {
+ await tx.$queryRaw`SELECT id FROM "User" WHERE id = ${erin.id} FOR UPDATE`;
+ locked();
+ await new Promise((resolve) => (release = resolve));
+ await tx.user.update({
+ where: { id: erin.id },
+ data: anonymised(),
+ });
+ });
+ await isLocked;
+ const submit = t.submitFeedback(erin, { feedback: 'Sent while deleting' }, 'web', deps);
+ await new Promise((resolve) => setTimeout(resolve, 200));
+ release();
+ await deletion;
+ await assert.rejects(submit, isUnauthorized);
+
+ assert.equal(await t.db.feedback.count({ where: { userId: { in: [dora.id, erin.id] } } }), 0);
+ } finally {
+ await t.db.user.deleteMany({ where: { id: { in: [dora.id, erin.id] } } });
+ }
+ },
+);
+
+type ReplyEmail = { to: string; reply: string; original: string };
+
+void test(
+ 'feedback: replies are sent once per key and recorded with their delivery',
{ skip: !databaseUrl },
async () => {
const t = await setup();
const bob = await t.createUser('Bob');
- const sent: { to: string; reply: string; original: string }[] = [];
- const send = async (email: { to: string; reply: string; original: string }) => {
+ const sent: ReplyEmail[] = [];
+ const send = async (email: ReplyEmail) => {
sent.push(email);
};
+ const quiet = { db: t.db, notify: async () => undefined };
+ const key = (name: string) => `${name}-${t.suffix}`;
try {
- const { id } = await t.submitFeedback(bob, { feedback: 'Can you add CSV export?' }, 'web', {
- db: t.db,
- notify: async () => undefined,
- });
+ const { id } = await t.submitFeedback(
+ bob,
+ { feedback: 'Can you add CSV export?' },
+ 'web',
+ quiet,
+ );
- // A failed email records nothing
+ // A definite failure is recorded as FAILED, and the same key sends again
await assert.rejects(
t.replyToFeedback(
id,
- { message: 'Yes!', idempotencyKey: `k1-${t.suffix}` },
+ { message: 'Yes!', idempotencyKey: key('k1') },
{
db: t.db,
send: async () => {
- throw new t.MailNotSentError('SMTP server not configured');
+ throw new t.MailNotSentError({ code: 'ENOTCONFIGURED' });
},
},
),
- t.MailNotSentError,
+ (error: Error & { definite?: boolean }) => true === error.definite,
);
- assert.equal(await t.db.feedbackReply.count({ where: { feedbackId: id } }), 0);
+ const failed = await t.db.feedbackReply.findUniqueOrThrow({
+ where: { idempotencyKey: key('k1') },
+ });
+ assert.equal(failed.deliveryStatus, 'FAILED');
+ assert.equal(failed.sentAt, null);
assert.equal((await t.db.feedback.findUniqueOrThrow({ where: { id } })).status, 'NEW');
- const replies = await Promise.all(
+ const settled = await Promise.allSettled(
Array.from({ length: 3 }, () =>
t.replyToFeedback(
id,
- { message: 'Yes, coming soon', idempotencyKey: `k1-${t.suffix}` },
+ { message: 'Yes, coming soon', idempotencyKey: key('k1') },
{ db: t.db, send },
),
),
@@ -124,23 +186,39 @@ void test(
reply: 'Yes, coming soon',
original: 'Can you add CSV export?',
});
- assert.equal(replies.filter(({ created }) => created).length, 1);
- assert.equal(new Set(replies.map(({ reply }) => reply.id)).size, 1);
+ const created = settled.filter(
+ (result) => 'fulfilled' === result.status && result.value.created,
+ );
+ assert.equal(created.length, 1);
+ // The others found it sent, or still sending
+ settled.forEach((result) => {
+ if ('rejected' === result.status) {
+ assert.ok(result.reason instanceof t.ReplyInProgressError);
+ }
+ });
+
+ const again = await t.replyToFeedback(
+ id,
+ { message: 'Yes, coming soon', idempotencyKey: key('k1') },
+ { db: t.db, send },
+ );
+ assert.equal(again.created, false);
+ assert.equal(again.reply.deliveryStatus, 'SENT');
+ assert.ok(again.reply.sentAt);
+ assert.equal(sent.length, 1);
const page = await t.listFeedback({ limit: 200, status: 'REPLIED' });
const listed = page.items.find((item) => item.id === id);
assert.equal(listed?.replies.length, 1);
+ assert.equal(listed?.replies[0]?.deliveryStatus, 'SENT');
assert.equal(listed?.user?.id, bob.id);
// The key belongs to that feedback
- const other = await t.submitFeedback(bob, { feedback: 'Another idea for you' }, 'web', {
- db: t.db,
- notify: async () => undefined,
- });
+ const other = await t.submitFeedback(bob, { feedback: 'Another idea for you' }, 'web', quiet);
await assert.rejects(
t.replyToFeedback(
other.id,
- { message: 'Thanks', idempotencyKey: `k1-${t.suffix}` },
+ { message: 'Thanks', idempotencyKey: key('k1') },
{ db: t.db, send },
),
t.IdempotencyConflictError,
@@ -148,7 +226,7 @@ void test(
await assert.rejects(
t.replyToFeedback(
'missing',
- { message: 'Thanks', idempotencyKey: `k2-${t.suffix}` },
+ { message: 'Thanks', idempotencyKey: key('k2') },
{ db: t.db, send },
),
t.FeedbackNotFoundError,
@@ -162,69 +240,199 @@ void test(
},
);
-void test('feedback: importing twice skips what was imported', { skip: !databaseUrl }, async () => {
- const t = await setup();
- const carol = await t.createUser('Carol');
- const items = [
- {
- externalId: `ext-1-${t.suffix}`,
- email: carol.email!.toUpperCase(),
- message: 'Imported feedback with a reply',
- createdAt: '2026-01-02T03:04:05.000Z',
- replies: [
- {
- message: 'Thanks!',
- sentAt: '2026-01-03T03:04:05.000Z',
- idempotencyKey: `ext-1-reply-${t.suffix}`,
- },
- ],
- },
- {
- externalId: `ext-2-${t.suffix}`,
- email: t.emailOf('nobody'),
- message: 'Imported feedback from someone without an account',
- createdAt: '2026-01-04T03:04:05.000Z',
- },
- ];
-
- try {
- assert.deepEqual(await t.importFeedback({ items }, t.db), {
- created: 2,
- skipped: 0,
- linked: 1,
- });
- assert.deepEqual(await t.importFeedback({ items }, t.db), {
- created: 0,
- skipped: 2,
- linked: 0,
- });
+void test(
+ 'feedback: one key used on two feedback at once sends one email',
+ { skip: !databaseUrl },
+ async () => {
+ const t = await setup();
+ const fay = await t.createUser('Fay');
+ const sent: ReplyEmail[] = [];
+ const quiet = { db: t.db, notify: async () => undefined };
- const imported = await t.db.feedback.findMany({
- where: { externalId: { in: items.map((item) => item.externalId) } },
- include: { replies: true },
- orderBy: { createdAt: 'asc' },
- });
- assert.equal(imported[0]?.userId, carol.id);
- assert.equal(imported[0]?.status, 'REPLIED');
- assert.equal(imported[0]?.source, 'import');
- assert.equal(imported[0]?.replies.length, 1);
- assert.equal(imported[0]?.createdAt.toISOString(), '2026-01-02T03:04:05.000Z');
- assert.equal(imported[1]?.userId, null);
- assert.equal(imported[1]?.status, 'NEW');
-
- // Paging with `since` and a cursor
- const first = await t.listFeedback({ limit: 1, since: '2026-01-01T00:00:00Z' }, t.db);
- assert.equal(first.items.length, 1);
- assert.ok(first.nextCursor);
- const second = await t.listFeedback(
- { limit: 1, since: '2026-01-01T00:00:00Z', cursor: first.nextCursor },
- t.db,
- );
- assert.notEqual(second.items[0]?.id, first.items[0]?.id);
- } finally {
- await t.db.feedback.deleteMany({
- where: { externalId: { in: items.map((item) => item.externalId) } },
+ try {
+ const first = await t.submitFeedback(fay, { feedback: 'First feedback here' }, 'web', quiet);
+ const second = await t.submitFeedback(
+ fay,
+ { feedback: 'Second feedback here' },
+ 'web',
+ quiet,
+ );
+ const idempotencyKey = `shared-${t.suffix}`;
+
+ const settled = await Promise.allSettled(
+ [first.id, second.id, first.id, second.id].map((id) =>
+ t.replyToFeedback(
+ id,
+ { message: 'Thanks!', idempotencyKey },
+ {
+ db: t.db,
+ send: async (email) => {
+ sent.push(email);
+ },
+ },
+ ),
+ ),
+ );
+
+ assert.equal(sent.length, 1);
+ assert.equal(settled.filter((r) => 'fulfilled' === r.status && r.value.created).length, 1);
+ settled.forEach((result) => {
+ if ('rejected' === result.status) {
+ assert.ok(
+ result.reason instanceof t.IdempotencyConflictError ||
+ result.reason instanceof t.ReplyInProgressError,
+ );
+ }
+ });
+ assert.equal(await t.db.feedbackReply.count({ where: { idempotencyKey } }), 1);
+ } finally {
+ await t.db.user.delete({ where: { id: fay.id } });
+ }
+ },
+);
+
+void test(
+ 'feedback: a reply that may have gone out is never sent again',
+ { skip: !databaseUrl },
+ async () => {
+ const t = await setup();
+ const gus = await t.createUser('Gus');
+ let sends = 0;
+ const quiet = { db: t.db, notify: async () => undefined };
+ const idempotencyKey = `uncertain-${t.suffix}`;
+
+ try {
+ const { id } = await t.submitFeedback(
+ gus,
+ { feedback: 'Feedback for an uncertain reply' },
+ 'web',
+ quiet,
+ );
+
+ // The connection dropped once the message was on its way
+ await assert.rejects(
+ t.replyToFeedback(
+ id,
+ { message: 'Thanks!', idempotencyKey },
+ {
+ db: t.db,
+ send: async () => {
+ sends += 1;
+ throw new t.MailNotSentError({ code: 'ECONNECTION' });
+ },
+ },
+ ),
+ (error: Error & { definite?: boolean }) => false === error.definite,
+ );
+ const reply = await t.db.feedbackReply.findUniqueOrThrow({ where: { idempotencyKey } });
+ assert.equal(reply.deliveryStatus, 'SENDING');
+
+ // Retrying while SENDING doesn't send
+ await assert.rejects(
+ t.replyToFeedback(
+ id,
+ { message: 'Thanks!', idempotencyKey },
+ {
+ db: t.db,
+ send: async () => {
+ sends += 1;
+ },
+ },
+ ),
+ t.ReplyInProgressError,
+ );
+ assert.equal(sends, 1);
+ } finally {
+ await t.db.user.delete({ where: { id: gus.id } });
+ }
+ },
+);
+
+void test(
+ 'feedback: import stores only live accounts’ feedback, once',
+ { skip: !databaseUrl },
+ async () => {
+ const t = await setup();
+ const carol = await t.createUser('Carol');
+ const hank = await t.createUser('Hank');
+ await t.db.user.update({
+ where: { id: hank.id },
+ data: anonymised(),
});
- await t.db.user.delete({ where: { id: carol.id } });
- }
-});
+ const items = [
+ {
+ externalId: `ext-1-${t.suffix}`,
+ email: carol.email!.toUpperCase(),
+ message: 'Imported feedback with a reply',
+ createdAt: '2026-01-02T03:04:05.000Z',
+ replies: [
+ {
+ message: 'Thanks!',
+ sentAt: '2026-01-03T03:04:05.000Z',
+ idempotencyKey: `ext-1-reply-${t.suffix}`,
+ },
+ ],
+ },
+ {
+ externalId: `ext-2-${t.suffix}`,
+ email: carol.email!,
+ message: 'Imported feedback without a reply',
+ createdAt: '2026-01-04T03:04:05.000Z',
+ },
+ {
+ externalId: `ext-3-${t.suffix}`,
+ email: t.emailOf('nobody'),
+ message: 'Imported feedback from someone without an account',
+ createdAt: '2026-01-04T03:04:05.000Z',
+ },
+ {
+ externalId: `ext-4-${t.suffix}`,
+ // The deleted account's old address
+ email: hank.email!,
+ message: 'Imported feedback from a deleted account',
+ createdAt: '2026-01-05T03:04:05.000Z',
+ },
+ ];
+ const externalIds = items.map((item) => item.externalId);
+
+ try {
+ assert.deepEqual(await t.importFeedback({ items }, t.db), {
+ created: 2,
+ skipped: 0,
+ unmatched: 2,
+ });
+ assert.deepEqual(await t.importFeedback({ items }, t.db), {
+ created: 0,
+ skipped: 2,
+ unmatched: 2,
+ });
+
+ const imported = await t.db.feedback.findMany({
+ where: { externalId: { in: externalIds } },
+ include: { replies: true },
+ orderBy: { createdAt: 'asc' },
+ });
+ assert.equal(imported.length, 2);
+ assert.ok(imported.every((feedback) => feedback.userId === carol.id));
+ assert.equal(imported[0]?.status, 'REPLIED');
+ assert.equal(imported[0]?.source, 'import');
+ assert.equal(imported[0]?.replies[0]?.deliveryStatus, 'SENT');
+ assert.equal(imported[0]?.replies[0]?.sentAt?.toISOString(), '2026-01-03T03:04:05.000Z');
+ assert.equal(imported[0]?.createdAt.toISOString(), '2026-01-02T03:04:05.000Z');
+ assert.equal(imported[1]?.status, 'NEW');
+
+ // Paging with `since` and a cursor
+ const first = await t.listFeedback({ limit: 1, since: '2026-01-01T00:00:00Z' }, t.db);
+ assert.equal(first.items.length, 1);
+ assert.ok(first.nextCursor);
+ const second = await t.listFeedback(
+ { limit: 1, since: '2026-01-01T00:00:00Z', cursor: first.nextCursor },
+ t.db,
+ );
+ assert.notEqual(second.items[0]?.id, first.items[0]?.id);
+ } finally {
+ await t.db.feedback.deleteMany({ where: { externalId: { in: externalIds } } });
+ await t.db.user.deleteMany({ where: { id: { in: [carol.id, hank.id] } } });
+ }
+ },
+);
diff --git a/src/tests/feedback.test.ts b/src/tests/feedback.test.ts
index 0a7b20d..a6322bf 100644
--- a/src/tests/feedback.test.ts
+++ b/src/tests/feedback.test.ts
@@ -4,14 +4,18 @@ import { describe, it } from 'node:test';
import { Prisma, type Feedback, type PrismaClient } from '@prisma/client';
import { isAdminAuthorized } from '~/server/adminAuth';
+import { isDefiniteSendFailure, safeMailErrorDetails } from '~/server/mailErrors';
+import { keyForDraft } from '~/utils/draftKey';
process.env.SKIP_ENV_VALIDATION = '1';
process.env.DATABASE_URL ??= 'postgresql://unused:unused@localhost:1/unused';
+const TOKEN = 'a'.repeat(40);
+process.env.ADMIN_API_TOKEN = TOKEN;
const { FEEDBACK_RATE_LIMIT, isOverFeedbackLimit, submitFeedback, submitFeedbackInput } =
await import('~/server/api/services/feedbackService');
-const TOKEN = 'a'.repeat(40);
+const { adminApiRoute } = await import('~/server/adminApi');
void describe('isAdminAuthorized', () => {
void it('accepts the exact bearer token', () => {
@@ -120,7 +124,19 @@ const createFakeDb = () => {
updatedAt: new Date(),
});
- const tx = { feedback, $queryRaw: async () => [] };
+ // Live accounts, as the sender lock reads them
+ const users = new Map([
+ [1, { email: 'alice@example.com', name: 'Alice' }],
+ [2, { email: 'bob@example.com', name: 'Bob' }],
+ [3, { email: null, name: 'No Email' }],
+ ]);
+ const tx = {
+ feedback,
+ $queryRaw: async (_query: TemplateStringsArray, userId: number) => {
+ const user = users.get(userId);
+ return user ? [user] : [];
+ },
+ };
const db = {
...tx,
$transaction: async (run: (client: typeof tx) => Promise) => run(tx),
@@ -129,14 +145,16 @@ const createFakeDb = () => {
return {
db,
rows,
+ users,
failNextCreate: () => {
failNextCreateWithDuplicateKey = true;
},
};
};
-const alice = { id: 1, email: 'alice@example.com', name: 'Alice' };
-const bob = { id: 2, email: 'bob@example.com', name: 'Bob' };
+// Stale sessions on purpose: the email and name come from the account when it's saved
+const alice = { id: 1, email: 'old-alice@example.com', name: 'Old Alice' };
+const bob = { id: 2, email: 'old-bob@example.com', name: 'Old Bob' };
const setup = () => {
const fake = createFakeDb();
@@ -158,7 +176,8 @@ void describe('submitFeedback', () => {
assert.deepEqual(result, { id: 'fb-1', created: true });
assert.equal(rows[0]?.message, 'Love the app!');
assert.equal(rows[0]?.source, 'mobile');
- assert.equal(rows[0]?.email, alice.email);
+ assert.equal(rows[0]?.email, 'alice@example.com');
+ assert.equal(rows[0]?.name, 'Alice');
assert.deepEqual(emailed, ['fb-1']);
});
@@ -176,6 +195,31 @@ void describe('submitFeedback', () => {
assert.equal(emailed.length, 1);
});
+ void it('refuses a draft key sent again with different text', async () => {
+ const { rows, emailed, deps } = setup();
+ const clientKey = randomUUID();
+ await submitFeedback(alice, { feedback: 'First version of my idea', clientKey }, 'web', deps);
+
+ await assert.rejects(
+ submitFeedback(alice, { feedback: 'Edited version of my idea', clientKey }, 'web', deps),
+ (error: Error & { code?: string }) => 'CONFLICT' === error.code,
+ );
+ assert.equal(rows.length, 1);
+ assert.equal(emailed.length, 1);
+ });
+
+ void it('refuses a deleted account, whatever its session says', async () => {
+ const { rows, users, emailed, deps } = setup();
+ users.delete(alice.id);
+
+ await assert.rejects(
+ submitFeedback(alice, { feedback: 'Sent after deleting' }, 'web', deps),
+ (error: Error & { code?: string }) => 'UNAUTHORIZED' === error.code,
+ );
+ assert.equal(rows.length, 0);
+ assert.equal(emailed.length, 0);
+ });
+
void it('refuses a draft key someone else used', async () => {
const { deps } = setup();
const clientKey = randomUUID();
@@ -261,13 +305,22 @@ void describe('submitFeedback', () => {
const result = await submitFeedback(alice, { feedback: 'SMTP is down today' }, 'web', {
...deps,
notify: async () => {
- throw new Error('SMTP down');
+ throw Object.assign(new Error('SMTP down for alice@example.com'), {
+ code: 'EENVELOPE',
+ responseCode: 550,
+ });
},
});
await new Promise((resolve) => setImmediate(resolve));
assert.equal(result.created, true);
assert.equal(rows.length, 1);
assert.equal(errors.length, 1);
+ // Only the id and codes are logged, never the message, recipient or feedback
+ const logged = JSON.stringify(errors);
+ assert.ok(!logged.includes('SMTP down'));
+ assert.ok(logged.includes('EENVELOPE'));
+ assert.ok(!logged.includes('alice@example.com'));
+ assert.ok(!logged.includes('SMTP is down today'));
} finally {
console.error = originalError;
}
@@ -276,8 +329,157 @@ void describe('submitFeedback', () => {
void it('needs an email address to reply to', async () => {
const { deps } = setup();
await assert.rejects(
- submitFeedback({ id: 3, email: null }, { feedback: 'No email here' }, 'web', deps),
+ submitFeedback({ id: 3 }, { feedback: 'No email here' }, 'web', deps),
(error: Error & { code?: string }) => 'BAD_REQUEST' === error.code,
);
});
});
+
+void describe('keyForDraft', () => {
+ let count = 0;
+ const newKey = () => `key-${++count}`;
+
+ void it('keeps the key while the text is unchanged and changes it with the text', () => {
+ const first = keyForDraft(null, 'Some feedback', newKey);
+ const retry = keyForDraft(first, 'Some feedback', newKey);
+ const edited = keyForDraft(retry, 'Some feedback, edited', newKey);
+ const editedRetry = keyForDraft(edited, 'Some feedback, edited', newKey);
+
+ assert.equal(retry.key, first.key);
+ assert.notEqual(edited.key, first.key);
+ assert.equal(editedRetry.key, edited.key);
+ // Back to the first text is a new attempt too
+ assert.notEqual(keyForDraft(editedRetry, 'Some feedback', newKey).key, first.key);
+ });
+});
+
+void describe('SMTP send errors', () => {
+ const smtpError = (fields: Record) =>
+ Object.assign(new Error('550 rejected for someone@example.com'), fields);
+
+ void it('only treats failures before the server could accept the message as definite', () => {
+ // Refused by the server, or never reached it
+ [
+ { code: 'ENOTCONFIGURED' },
+ { code: 'EAUTH', responseCode: 535 },
+ { code: 'EENVELOPE', responseCode: 550 },
+ { code: 'EMESSAGE', responseCode: 554 },
+ { code: 'EDNS' },
+ { code: 'ESOCKET', originalCode: 'ECONNREFUSED' },
+ { code: 'ETIMEDOUT', timeoutType: 'CONNECT_TIMEOUT' },
+ { code: 'ETIMEDOUT', timeoutType: 'GREETING_TIMEOUT' },
+ ].forEach((fields) =>
+ assert.equal(isDefiniteSendFailure(smtpError(fields)), true, JSON.stringify(fields)),
+ );
+
+ // Could have been accepted before the connection went
+ [
+ { code: 'ECONNECTION' },
+ { code: 'ESOCKET', originalCode: 'ECONNRESET' },
+ { code: 'ETIMEDOUT', timeoutType: 'SOCKET_TIMEOUT' },
+ { code: 'EPROTOCOL' },
+ {},
+ ].forEach((fields) =>
+ assert.equal(isDefiniteSendFailure(smtpError(fields)), false, JSON.stringify(fields)),
+ );
+ assert.equal(isDefiniteSendFailure('boom'), false);
+ });
+
+ void it('keeps only the error code and reply code', () => {
+ assert.deepEqual(safeMailErrorDetails(smtpError({ code: 'EENVELOPE', responseCode: 550 })), {
+ code: 'EENVELOPE',
+ responseCode: 550,
+ });
+ assert.deepEqual(
+ safeMailErrorDetails(smtpError({ code: 'someone@example.com', responseCode: '550' })),
+ { code: undefined, responseCode: undefined },
+ );
+ });
+});
+
+void describe('adminApiRoute', () => {
+ type Sent = { status: number; body: unknown; headers: Record };
+
+ const call = async (
+ route: ReturnType,
+ {
+ method = 'POST',
+ authorization = `Bearer ${TOKEN}`,
+ body = '',
+ onRead,
+ }: { method?: string; authorization?: string; body?: string; onRead?: () => void },
+ ) => {
+ const sent: Sent = { status: 0, body: undefined, headers: {} };
+ const req = {
+ method,
+ query: {},
+ headers: { authorization, 'content-type': 'application/json' },
+ async *[Symbol.asyncIterator]() {
+ onRead?.();
+ yield Buffer.from(body);
+ },
+ };
+ const res = {
+ setHeader: (name: string, value: string) => {
+ sent.headers[name] = value;
+ },
+ status: (status: number) => {
+ sent.status = status;
+ return res;
+ },
+ json: (value: unknown) => {
+ sent.body = value;
+ return res;
+ },
+ };
+ await route(req as never, res as never);
+ return sent;
+ };
+
+ const echo = adminApiRoute(
+ ['POST'],
+ async (_req, res, body) => {
+ res.status(200).json({ body });
+ },
+ { maxBodyBytes: 32 },
+ );
+
+ void it('rejects a bad token without reading the body', async () => {
+ let read = false;
+ const sent = await call(echo, {
+ authorization: 'Bearer wrong',
+ body: '{}',
+ onRead: () => {
+ read = true;
+ },
+ });
+ assert.equal(sent.status, 401);
+ assert.equal(read, false);
+ });
+
+ void it('parses JSON bodies up to the limit', async () => {
+ assert.deepEqual(await call(echo, { body: '{"a":1}' }), {
+ status: 200,
+ body: { body: { a: 1 } },
+ headers: { 'Cache-Control': 'no-store' },
+ });
+ assert.equal((await call(echo, { body: `{"a":"${'x'.repeat(40)}"}` })).status, 413);
+ assert.equal((await call(echo, { body: '{nope' })).status, 400);
+ assert.equal((await call(echo, { method: 'GET' })).status, 405);
+ });
+
+ void it('logs no message for unexpected errors', async () => {
+ const errors: unknown[] = [];
+ const originalError = console.error;
+ console.error = (...args: unknown[]) => errors.push(args);
+ try {
+ const failing = adminApiRoute(['POST'], async () => {
+ throw new Error('secret@example.com');
+ });
+ assert.equal((await call(failing, { body: '{}' })).status, 500);
+ } finally {
+ console.error = originalError;
+ }
+ assert.ok(!JSON.stringify(errors).includes('secret@example.com'));
+ });
+});
diff --git a/src/utils/draftKey.ts b/src/utils/draftKey.ts
new file mode 100644
index 0000000..18378c4
--- /dev/null
+++ b/src/utils/draftKey.ts
@@ -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() };
From e0c9d9691e5bbc35275233bd462f6069c7529b8f Mon Sep 17 00:00:00 2001
From: KM Koushik
Date: Tue, 10 Nov 2026 10:20:28 +1100
Subject: [PATCH 3/6] fix: bind reply keys to their message and settle stuck
replies
- A reply key with a different message is a conflict, whether the reply
was sent or failed; failed replies retry only with the same message
- The feedback notification email logs and reports only the feedback id
and error codes, never the recipient or the server's reply
- PATCH /api/admin/feedback/{id}/reply/{replyId} lets an operator mark a
reply stuck in SENDING as SENT or FAILED
---
.../admin/feedback/[id]/reply/[replyId].ts | 33 ++++
.../[id]/{reply.ts => reply/index.ts} | 8 +-
src/server/api/services/feedbackAdmin.ts | 60 ++++++-
src/server/mailer.ts | 20 ++-
src/tests/feedback.integration.test.ts | 152 +++++++++++++++++-
src/tests/feedback.test.ts | 86 ++++++++++
6 files changed, 347 insertions(+), 12 deletions(-)
create mode 100644 src/pages/api/admin/feedback/[id]/reply/[replyId].ts
rename src/pages/api/admin/feedback/[id]/{reply.ts => reply/index.ts} (77%)
diff --git a/src/pages/api/admin/feedback/[id]/reply/[replyId].ts b/src/pages/api/admin/feedback/[id]/reply/[replyId].ts
new file mode 100644
index 0000000..8562b1f
--- /dev/null
+++ b/src/pages/api/admin/feedback/[id]/reply/[replyId].ts
@@ -0,0 +1,33 @@
+import { AdminApiError, adminApiRoute, queryParam } from '~/server/adminApi';
+import {
+ ReplyNotFoundError,
+ ReplyNotSendingError,
+ resolveReply,
+ resolveReplyBody,
+} from '~/server/api/services/feedbackAdmin';
+
+// Read by `adminApiRoute`, once the request is authorised
+export const config = { api: { bodyParser: false } };
+
+/**
+ * PATCH /api/admin/feedback/{id}/reply/{replyId} with `{ "deliveryStatus": "SENT" | "FAILED" }`
+ * Settles a reply stuck in SENDING once you've checked whether it was delivered. SENT records it
+ * as sent; FAILED lets the same key and message be sent again. 409 unless it's SENDING.
+ */
+export default adminApiRoute(['PATCH'], async (req, res, body) => {
+ const feedbackId = queryParam(req.query.id) ?? '';
+ const replyId = queryParam(req.query.replyId) ?? '';
+ const input = resolveReplyBody.parse(body);
+
+ try {
+ res.status(200).json({ reply: await resolveReply(feedbackId, replyId, input) });
+ } catch (error) {
+ if (error instanceof ReplyNotFoundError) {
+ throw new AdminApiError(404, error.message);
+ }
+ if (error instanceof ReplyNotSendingError) {
+ throw new AdminApiError(409, error.message);
+ }
+ throw error;
+ }
+});
diff --git a/src/pages/api/admin/feedback/[id]/reply.ts b/src/pages/api/admin/feedback/[id]/reply/index.ts
similarity index 77%
rename from src/pages/api/admin/feedback/[id]/reply.ts
rename to src/pages/api/admin/feedback/[id]/reply/index.ts
index 7740578..9d2569d 100644
--- a/src/pages/api/admin/feedback/[id]/reply.ts
+++ b/src/pages/api/admin/feedback/[id]/reply/index.ts
@@ -13,9 +13,11 @@ export const config = { api: { bodyParser: false } };
/**
* POST /api/admin/feedback/{id}/reply with `{ "message": string, "idempotencyKey": string }`
- * Emails the reply and records it. 201 when sent now, 200 when that key was already sent, 409 when
- * it's being sent or it's unknown whether it went, 502 when the email wasn't sent (retry with the
- * same key) or it's unknown whether it went (not retried).
+ * Emails the reply and records it. A key stands for one message: send an edited reply with a new
+ * key. 201 when sent now, 200 when that key and message were already sent, 409 when the key was
+ * used for other feedback or another message, or the reply is being sent or it's unknown whether
+ * it went (resolve it with PATCH .../reply/{replyId}), 502 when the email wasn't sent (retry with
+ * the same key and message) or it's unknown whether it went (not retried).
*/
export default adminApiRoute(['POST'], async (req, res, body) => {
const id = queryParam(req.query.id) ?? '';
diff --git a/src/server/api/services/feedbackAdmin.ts b/src/server/api/services/feedbackAdmin.ts
index ede3cc5..a04ab02 100644
--- a/src/server/api/services/feedbackAdmin.ts
+++ b/src/server/api/services/feedbackAdmin.ts
@@ -27,6 +27,9 @@ export class FeedbackNotFoundError extends Error {}
export class IdempotencyConflictError extends Error {}
/** A reply with this key is being sent, or it's unknown whether it went: not sending it again. */
export class ReplyInProgressError extends Error {}
+export class ReplyNotFoundError extends Error {}
+/** Only a SENDING reply can be resolved by hand. */
+export class ReplyNotSendingError extends Error {}
const statusSchema = z.nativeEnum(FeedbackStatus);
@@ -44,6 +47,10 @@ export const replyBody = z.object({
export const statusBody = z.object({ status: statusSchema });
+export const resolveReplyBody = z.object({
+ deliveryStatus: z.enum([FeedbackReplyStatus.SENT, FeedbackReplyStatus.FAILED]),
+});
+
export const importBody = z.object({
items: z
.array(
@@ -215,6 +222,10 @@ const reserveReply = async (
if (existing.feedbackId !== id) {
throw new IdempotencyConflictError('idempotencyKey was used for other feedback');
}
+ // A key stands for one message: an edited reply needs a new key, also after a failure
+ if (existing.message !== message) {
+ throw new IdempotencyConflictError('idempotencyKey was used for a different message');
+ }
if (FeedbackReplyStatus.SENT === existing.deliveryStatus) {
return { send: false, reply: existing };
}
@@ -225,14 +236,14 @@ const reserveReply = async (
// FAILED certainly wasn't sent: claim it again, unless another retry just did
const { count } = await db.feedbackReply.updateMany({
where: { id: existing.id, deliveryStatus: FeedbackReplyStatus.FAILED },
- data: { deliveryStatus: FeedbackReplyStatus.SENDING, message },
+ data: { deliveryStatus: FeedbackReplyStatus.SENDING },
});
if (0 === count) {
return retry();
}
return {
send: true,
- reply: { ...existing, message, deliveryStatus: FeedbackReplyStatus.SENDING },
+ reply: { ...existing, deliveryStatus: FeedbackReplyStatus.SENDING },
...target,
};
};
@@ -261,8 +272,9 @@ export const replyToFeedback = async (
await send({ to: reservation.to, reply: message, original: reservation.original });
} catch (error) {
if (error instanceof MailNotSentError && error.definite) {
- await db.feedbackReply.update({
- where: { id: reservation.reply.id },
+ // Unless an operator resolved it meanwhile
+ await db.feedbackReply.updateMany({
+ where: { id: reservation.reply.id, deliveryStatus: FeedbackReplyStatus.SENDING },
data: { deliveryStatus: FeedbackReplyStatus.FAILED },
});
}
@@ -280,6 +292,46 @@ export const replyToFeedback = async (
return { reply: toReplyJson(reply), created: true };
};
+/**
+ * Settles a reply stuck in SENDING, e.g. after the connection dropped mid-send, once an operator
+ * has checked whether it was delivered: SENT records it as sent, FAILED lets the same key and
+ * message be sent again. Never done automatically.
+ */
+export const resolveReply = async (
+ feedbackId: string,
+ replyId: string,
+ { deliveryStatus }: z.infer,
+ db: PrismaClient = defaultDb,
+) =>
+ db.$transaction(async (tx) => {
+ const sent = FeedbackReplyStatus.SENT === deliveryStatus;
+ const { count } = await tx.feedbackReply.updateMany({
+ where: { id: replyId, feedbackId, deliveryStatus: FeedbackReplyStatus.SENDING },
+ data: sent ? { deliveryStatus, sentAt: new Date() } : { deliveryStatus },
+ });
+
+ const reply = await tx.feedbackReply.findFirst({
+ where: { id: replyId, feedbackId },
+ select: replySelect,
+ });
+ if (!reply) {
+ throw new ReplyNotFoundError('Reply not found');
+ }
+ if (0 === count) {
+ throw new ReplyNotSendingError(
+ `Reply is ${reply.deliveryStatus}; only SENDING replies can be resolved`,
+ );
+ }
+
+ if (sent) {
+ await tx.feedback.update({
+ where: { id: feedbackId },
+ data: { status: FeedbackStatus.REPLIED },
+ });
+ }
+ return toReplyJson(reply);
+ });
+
type ImportItem = z.infer['items'][number];
type ImportOutcome = 'created' | 'skipped' | 'unmatched';
diff --git a/src/server/mailer.ts b/src/server/mailer.ts
index e8de50c..aa5db6c 100644
--- a/src/server/mailer.ts
+++ b/src/server/mailer.ts
@@ -139,9 +139,13 @@ export type FeedbackEmailDetails = {
/**
* Tells us about new feedback. The subject keeps the `Feedback received on SplitPro` prefix that
- * existing inbox filters match.
+ * existing inbox filters match. Best effort: returns whether it was sent, and on failure logs and
+ * reports only the feedback id and error codes, never the recipient or the server's reply.
*/
-export async function sendFeedbackEmail(feedback: FeedbackEmailDetails) {
+export async function sendFeedbackEmail(
+ feedback: FeedbackEmailDetails,
+ deliver: typeof deliverMail = deliverMail,
+) {
if (!env.FEEDBACK_EMAIL) return false;
const sender = feedback.name ?? feedback.email;
@@ -150,7 +154,17 @@ export async function sendFeedbackEmail(feedback: FeedbackEmailDetails) {
const text = `Feedback created by ${sender} :\n\nFeedback: ${feedback.message}\n\nemail: ${feedback.email}\nsource: ${feedback.source}${version}\nfeedback id: ${feedback.id}`;
const html = `