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..125eba2 100644 --- a/mobile/src/app/(app)/feedback.tsx +++ b/mobile/src/app/(app)/feedback.tsx @@ -1,34 +1,53 @@ -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'; +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(''); + // 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) { + 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 }); + await submitFeedback.mutateAsync({ + feedback: trimmed, + clientKey: draftKey.current.key, + 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')); @@ -47,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 new file mode 100644 index 0000000..05a4244 --- /dev/null +++ b/prisma/migrations/20261110120000_add_feedback/migration.sql @@ -0,0 +1,64 @@ +-- CreateEnum +CREATE TYPE "FeedbackStatus" AS ENUM ('OPEN', 'REPLIED', 'CLOSED'); + +-- CreateEnum +CREATE TYPE "FeedbackReplyStatus" AS ENUM ('SENDING', 'SENT', 'FAILED'); + +-- CreateTable +CREATE TABLE "Feedback" ( + "id" TEXT NOT NULL, + "userId" INTEGER, + "email" TEXT NOT NULL, + "name" TEXT, + "message" TEXT NOT NULL, + "source" TEXT NOT NULL, + "appVersion" TEXT, + "clientKey" TEXT, + "externalId" TEXT, + "status" "FeedbackStatus" NOT NULL DEFAULT 'OPEN', + "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + "updatedAt" TIMESTAMP(3) NOT NULL, + + CONSTRAINT "Feedback_pkey" PRIMARY KEY ("id") +); + +-- CreateTable +CREATE TABLE "FeedbackReply" ( + "id" TEXT NOT NULL, + "feedbackId" TEXT NOT NULL, + "message" TEXT NOT NULL, + "idempotencyKey" TEXT NOT NULL, + "deliveryStatus" "FeedbackReplyStatus" NOT NULL DEFAULT 'SENDING', + "attemptId" TEXT, + "attemptStartedAt" TIMESTAMP(3), + "sentAt" TIMESTAMP(3), + "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + "updatedAt" TIMESTAMP(3) NOT NULL, + + CONSTRAINT "FeedbackReply_pkey" PRIMARY KEY ("id") +); + +-- CreateIndex +CREATE UNIQUE INDEX "Feedback_clientKey_key" ON "Feedback"("clientKey"); + +-- CreateIndex +CREATE UNIQUE INDEX "Feedback_externalId_key" ON "Feedback"("externalId"); + +-- CreateIndex +CREATE INDEX "Feedback_status_createdAt_idx" ON "Feedback"("status", "createdAt"); + +-- CreateIndex +CREATE INDEX "Feedback_userId_createdAt_idx" ON "Feedback"("userId", "createdAt"); + +-- CreateIndex +CREATE UNIQUE INDEX "FeedbackReply_idempotencyKey_key" ON "FeedbackReply"("idempotencyKey"); + +-- CreateIndex +CREATE INDEX "FeedbackReply_feedbackId_idx" ON "FeedbackReply"("feedbackId"); + +-- AddForeignKey +ALTER TABLE "Feedback" ADD CONSTRAINT "Feedback_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "FeedbackReply" ADD CONSTRAINT "FeedbackReply_feedbackId_fkey" FOREIGN KEY ("feedbackId") REFERENCES "Feedback"("id") ON DELETE CASCADE ON UPDATE CASCADE; + diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 696907b..a1bc809 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,60 @@ model QuickSplitExpense { @@index([splitId, createdAt]) } + +enum FeedbackStatus { + OPEN + 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(OPEN) + createdAt DateTime @default(now()) + updatedAt DateTime @updatedAt + user User? @relation(fields: [userId], references: [id], onDelete: Cascade) + replies FeedbackReply[] + + @@index([status, createdAt]) + @@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()) + feedbackId String + message String + idempotencyKey String @unique + deliveryStatus FeedbackReplyStatus @default(SENDING) + /// The send attempt that holds it while SENDING: only that attempt may complete it + attemptId String? + attemptStartedAt DateTime? + /// 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 9891a8b..2d1c388 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'; @@ -8,29 +8,52 @@ 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 .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); + // 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); const feedbackForm = useForm>({ resolver: zodResolver(feedbackSchema), }); async function onGroupSubmit(values: z.infer) { + if (submitting.current) { + return; + } + + submitting.current = true; + draftKey.current = keyForDraft(draftKey.current, values.feedback, () => crypto.randomUUID()); try { - await submitFeedbackMutation.mutateAsync({ feedback: values.feedback }); - feedbackForm.reset(); - toast.success('Feedback submitted', { duration: 1500 }); + await submitFeedbackMutation.mutateAsync({ + feedback: values.feedback, + clientKey: draftKey.current.key, + 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: '' }); + draftKey.current = null; + toast.success('Feedback submitted', { duration: 1500 }); setFeedbackOpen(false); } @@ -53,6 +76,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..f5a7b79 --- /dev/null +++ b/src/pages/api/admin/feedback/[id]/index.ts @@ -0,0 +1,26 @@ +import { AdminApiError, adminApiRoute, queryParam } from '~/server/adminApi'; +import { + FeedbackNotFoundError, + setFeedbackStatus, + statusBody, +} from '~/server/api/services/feedbackAdmin'; + +// Read by `adminApiRoute`, once the request is authorised +export const config = { api: { bodyParser: false } }; + +/** + * PATCH /api/admin/feedback/{id} with `{ "status": "OPEN" | "REPLIED" | "CLOSED" }` + */ +export default adminApiRoute(['PATCH'], async (req, res, body) => { + const id = queryParam(req.query.id) ?? ''; + const { status } = statusBody.parse(body); + + try { + res.status(200).json(await setFeedbackStatus(id, status)); + } catch (error) { + if (error instanceof FeedbackNotFoundError) { + throw new AdminApiError(404, error.message); + } + throw error; + } +}); 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..d49ad8c --- /dev/null +++ b/src/pages/api/admin/feedback/[id]/reply/[replyId].ts @@ -0,0 +1,36 @@ +import { AdminApiError, adminApiRoute, queryParam } from '~/server/adminApi'; +import { + ReplyNotFoundError, + ReplyNotSendingError, + ReplyStillSendingError, + 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. Only do this after checking whether the email arrived: SENT + * records it as sent; FAILED lets the same key and message be sent again. Allowed only from + * SENDING, once the send attempt started at least 10 minutes ago and is treated as abandoned; + * before that, 409 "Reply is still sending; try again later". + */ +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 || error instanceof ReplyStillSendingError) { + throw new AdminApiError(409, error.message); + } + throw error; + } +}); diff --git a/src/pages/api/admin/feedback/[id]/reply/index.ts b/src/pages/api/admin/feedback/[id]/reply/index.ts new file mode 100644 index 0000000..9d2569d --- /dev/null +++ b/src/pages/api/admin/feedback/[id]/reply/index.ts @@ -0,0 +1,51 @@ +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. 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) ?? ''; + const input = replyBody.parse(body); + + try { + 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 || error instanceof ReplyInProgressError) { + throw new AdminApiError(409, error.message); + } + if (error instanceof MailNotSentError) { + // 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 new file mode 100644 index 0000000..df08915 --- /dev/null +++ b/src/pages/api/admin/feedback/import.ts @@ -0,0 +1,20 @@ +import { adminApiRoute } from '~/server/adminApi'; +import { importBody, importFeedback } from '~/server/api/services/feedbackAdmin'; + +// 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. + * 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, 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 new file mode 100644 index 0000000..2535e27 --- /dev/null +++ b/src/pages/api/admin/feedback/index.ts @@ -0,0 +1,14 @@ +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. + */ +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..c7cca7c --- /dev/null +++ b/src/server/adminApi.ts @@ -0,0 +1,106 @@ +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); + } +} + +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 (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, { maxBodyBytes = DEFAULT_MAX_BODY_BYTES } = {}) => + 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' }); + } + + const hasBody = 'GET' !== req.method; + if (hasBody && !isJsonContentType(req.headers['content-type'])) { + return res.status(415).json({ error: 'Send a JSON body' }); + } + + try { + 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 }); + } + if (error instanceof ZodError) { + return res.status(400).json({ error: 'Invalid request', issues: error.issues }); + } + console.error('Admin API error', safeErrorDetails(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..9357b1f --- /dev/null +++ b/src/server/api/services/feedbackAdmin.ts @@ -0,0 +1,469 @@ +import { + type FeedbackReply, + FeedbackReplyStatus, + FeedbackStatus, + Prisma, + type PrismaClient, +} from '@prisma/client'; +import { randomUUID } from 'crypto'; +import { z } from 'zod'; + +import { db as defaultDb } from '~/server/db'; +import { MailNotSentError, 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; +/** 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 {} +export class ReplyNotFoundError extends Error {} +/** Only a SENDING reply can be resolved by hand. */ +export class ReplyNotSendingError extends Error {} +/** Its send attempt started less than `RECONCILE_AFTER_MS` ago, so it can't be resolved yet. */ +export class ReplyStillSendingError extends Error {} + +/** + * A reply stuck in SENDING this long after its attempt started is treated as abandoned, and an + * operator may resolve it. This doesn't prove the send has stopped (the SMTP socket timeout is for + * inactivity), so resolving stays a manual step, taken only after checking whether the email + * arrived. + */ +export const RECONCILE_AFTER_MS = 10 * 60 * 1000; + +/** A new attempt to send a reply: only the attempt holding a SENDING reply may complete it. */ +const newAttempt = () => ({ attemptId: randomUUID(), attemptStartedAt: new Date() }); + +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 resolveReplyBody = z.object({ + deliveryStatus: z.enum([FeedbackReplyStatus.SENT, FeedbackReplyStatus.FAILED]), +}); + +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 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: 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, + 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), +}); + +/** + * 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 Reservation = + | { send: false; reply: ReplyDetails } + | { send: true; reply: ReplyDetails; attemptId: string; 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 attempt + * ever holds a reply in SENDING, and nothing stays locked while the email goes out. The claim + * gets a new `attemptId`. + */ +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 claim = newAttempt(); + const reply = await db.feedbackReply.create({ + data: { feedbackId: id, message, idempotencyKey, ...claim }, + select: replySelect, + }); + return { send: true, reply, attemptId: claim.attemptId, ...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'); + } + // 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 }; + } + 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 claim = newAttempt(); + const { count } = await db.feedbackReply.updateMany({ + where: { id: existing.id, deliveryStatus: FeedbackReplyStatus.FAILED }, + data: { deliveryStatus: FeedbackReplyStatus.SENDING, ...claim }, + }); + if (0 === count) { + return retry(); + } + return { + send: true, + reply: { ...existing, deliveryStatus: FeedbackReplyStatus.SENDING }, + attemptId: claim.attemptId, + ...target, + }; +}; + +type ReplyDeps = { db: PrismaClient; send: typeof sendFeedbackReplyEmail }; + +/** + * 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 reservation = await reserveReply(db, id, message, idempotencyKey); + if (!reservation.send) { + return { reply: toReplyJson(reservation.reply), created: false }; + } + + // Completing only ever touches the reply while this attempt still holds it: once an operator + // resolved it, or another attempt claimed it since, this one's outcome is no longer its to record. + const owned = { + id: reservation.reply.id, + deliveryStatus: FeedbackReplyStatus.SENDING, + attemptId: reservation.attemptId, + }; + const lostOwnership = () => + console.warn('Feedback reply attempt no longer holds the reply', reservation.reply.id); + + try { + await send({ to: reservation.to, reply: message, original: reservation.original }); + } catch (error) { + if (error instanceof MailNotSentError && error.definite) { + const { count } = await db.feedbackReply.updateMany({ + where: owned, + data: { deliveryStatus: FeedbackReplyStatus.FAILED }, + }); + if (0 === count) { + lostOwnership(); + } + } + throw error; + } + + const recorded = await db.$transaction(async (tx) => { + const { count } = await tx.feedbackReply.updateMany({ + where: owned, + data: { deliveryStatus: FeedbackReplyStatus.SENT, sentAt: new Date() }, + }); + if (1 === count) { + await tx.feedback.updateMany({ where: { id }, data: { status: FeedbackStatus.REPLIED } }); + } + const reply = await tx.feedbackReply.findUniqueOrThrow({ + where: { id: reservation.reply.id }, + select: replySelect, + }); + return { reply, created: 1 === count }; + }); + if (!recorded.created) { + lostOwnership(); + } + return { reply: toReplyJson(recorded.reply), created: recorded.created }; +}; + +/** + * Settles a reply stuck in SENDING, e.g. after the connection dropped mid-send. An operator action, + * to take only after checking whether the email arrived: SENT records it as sent, FAILED lets the + * same key and message be sent again. Never done automatically, and only once its attempt started + * at least `RECONCILE_AFTER_MS` ago and is treated as abandoned; that attempt can't complete it + * afterwards. + */ +export const resolveReply = async ( + feedbackId: string, + replyId: string, + { deliveryStatus }: z.infer, + db: PrismaClient = defaultDb, + now = new Date(), +) => + db.$transaction(async (tx) => { + const sent = FeedbackReplyStatus.SENT === deliveryStatus; + const { count } = await tx.feedbackReply.updateMany({ + where: { + id: replyId, + feedbackId, + deliveryStatus: FeedbackReplyStatus.SENDING, + OR: [ + { attemptStartedAt: null }, + { attemptStartedAt: { lt: new Date(now.getTime() - RECONCILE_AFTER_MS) } }, + ], + }, + data: { + deliveryStatus, + attemptId: null, + ...(sent ? { sentAt: new Date() } : {}), + }, + }); + + const reply = await tx.feedbackReply.findFirst({ + where: { id: replyId, feedbackId }, + select: replySelect, + }); + if (!reply) { + throw new ReplyNotFoundError('Reply not found'); + } + if (0 === count) { + if (FeedbackReplyStatus.SENDING === reply.deliveryStatus) { + throw new ReplyStillSendingError('Reply is still sending; try again later'); + } + 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'; + +/** + * Imports one item for a live account with its email, in a transaction that holds that account + * against deletion until the feedback is saved. + */ +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'; + } + + const existing = await tx.feedback.findUnique({ + where: { externalId: item.externalId }, + select: { id: true }, + }); + if (existing) { + return 'skipped'; + } + + const replies = item.replies ?? []; + await tx.feedback.create({ + data: { + externalId: item.externalId, + userId: user.id, + email: item.email, + message: item.message, + source: 'import', + status: 0 < replies.length ? FeedbackStatus.REPLIED : FeedbackStatus.OPEN, + createdAt: new Date(item.createdAt), + replies: { + create: replies.map((reply) => ({ + message: reply.message, + idempotencyKey: reply.idempotencyKey, + deliveryStatus: FeedbackReplyStatus.SENT, + sentAt: new Date(reply.sentAt), + createdAt: new Date(reply.sentAt), + })), + }, + }, + }); + return 'created'; + }); + } catch (error) { + // Imported meanwhile by another run + if (isUniqueViolation(error, 'externalId')) { + return 'skipped'; + } + throw error; + } +}; + +/** + * 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 new file mode 100644 index 0000000..34845d0 --- /dev/null +++ b/src/server/api/services/feedbackService.ts @@ -0,0 +1,165 @@ +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'; +import { safeMailErrorDetails } from '~/server/mailErrors'; + +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; + +/** 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; + 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`) 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, + userId: number, + message: string, + clientKey: string | undefined, + now: Date, +) => { + if (clientKey) { + const byKey = await tx.feedback.findUnique({ where: { clientKey } }); + if (byKey) { + // 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; + } + } + + 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 }> => { + 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. 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) => { + 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) { + 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: sender.email, + name: sender.name, + 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 || existing.message !== message) { + 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, safeMailErrorDetails(error)), + ); + } + + return { id: feedback.id, created }; +}; 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 9d0d6c1..aa5db6c 100644 --- a/src/server/mailer.ts +++ b/src/server/mailer.ts @@ -1,13 +1,21 @@ -import { type User } from 'next-auth'; 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; @@ -28,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) { @@ -100,15 +128,116 @@ 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; +}; - if (!env.FEEDBACK_EMAIL) return; +/** + * Tells us about new feedback. The subject keeps the `Feedback received on SplitPro` prefix that + * 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, + deliver: typeof deliverMail = deliverMail, +) { + if (!env.FEEDBACK_EMAIL) return false; - const subject = `Feedback received on SplitPro from ${user.name}`; - const text = `Feedback created by ${user.name} :\n\nFeedback: ${feedback}\n\nemail: ${user.email}`; + 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 = `

Feedback created by ${escapeHtml(sender)}:

${escapeHtml(feedback.message)}

email: ${escapeHtml(feedback.email)}
source: ${escapeHtml(feedback.source + version)}
feedback id: ${escapeHtml(feedback.id)}

`; - await sendMail(env.FEEDBACK_EMAIL, subject, text, text, user.email ?? undefined); + try { + await deliver(env.FEEDBACK_EMAIL, subject, text, html, feedback.email); + return true; + } catch (error) { + const { code, responseCode } = safeMailErrorDetails(error); + console.error('Feedback email not sent', { feedbackId: feedback.id, code, responseCode }); + await sendToDiscord( + `Feedback email not sent for feedback ${feedback.id} (code: ${code ?? 'unknown'}, response code: ${responseCode ?? 'none'})`, + ).catch(() => undefined); + return false; + } +} + +/** + * 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, '''); + +/** + * 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 with bounded timeouts 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 = getBoundedTransporter(); + if (!transporter) { + 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); + } } 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..8f716e1 --- /dev/null +++ b/src/tests/feedback.integration.test.ts @@ -0,0 +1,822 @@ +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); + }, +); + +/** How account deletion leaves the user row; see `anonymiseAccount`. */ +const anonymised = () => ({ deletedAt: new Date(), email: null, name: 'Deleted user' }); + +void test( + '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 }; + +/** Makes a reply's send attempt old enough to count as abandoned, as if time had passed. */ +const settleAttempt = async (t: Awaited>, replyId: string) => { + await t.db.feedbackReply.update({ + where: { id: replyId }, + data: { attemptStartedAt: new Date(Date.now() - t.RECONCILE_AFTER_MS - 1000) }, + }); +}; + +const deferred = () => { + let resolve!: () => void; + let reject!: (error: Error) => void; + const promise = new Promise((done, fail) => { + resolve = done; + reject = fail; + }); + return { promise, resolve, reject }; +}; + +/** + * A send that waits until the test settles it, and says when it started: the email is then "in + * flight". + */ +const pausedSend = () => { + const started = deferred(); + const finish = deferred(); + const send = async () => { + started.resolve(); + await finish.promise; + }; + return { send, started: started.promise, finish }; +}; + +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: 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', + quiet, + ); + + // A definite failure is recorded as FAILED, and the same key and message send again + await assert.rejects( + t.replyToFeedback( + id, + { message: 'Yes, coming soon', idempotencyKey: key('k1') }, + { + db: t.db, + send: async () => { + throw new t.MailNotSentError({ code: 'ENOTCONFIGURED' }); + }, + }, + ), + (error: Error & { definite?: boolean }) => true === error.definite, + ); + 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, 'OPEN'); + + const settled = await Promise.allSettled( + Array.from({ length: 3 }, () => + t.replyToFeedback( + id, + { message: 'Yes, coming soon', idempotencyKey: key('k1') }, + { 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?', + }); + 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', quiet); + await assert.rejects( + t.replyToFeedback( + other.id, + { message: 'Thanks', idempotencyKey: key('k1') }, + { db: t.db, send }, + ), + t.IdempotencyConflictError, + ); + await assert.rejects( + t.replyToFeedback( + 'missing', + { message: 'Thanks', idempotencyKey: key('k2') }, + { 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: 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 }; + + 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(), + }); + 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, 'OPEN'); + + // 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] } } }); + } + }, +); + +void test( + 'feedback: a reply key stands for one message, also after a failure', + { skip: !databaseUrl }, + async () => { + const t = await setup(); + const ivy = await t.createUser('Ivy'); + const sent: ReplyEmail[] = []; + const send = async (email: ReplyEmail) => { + sent.push(email); + }; + const quiet = { db: t.db, notify: async () => undefined }; + const reply = ( + id: string, + message: string, + idempotencyKey: string, + deps = { db: t.db, send }, + ) => t.replyToFeedback(id, { message, idempotencyKey }, deps); + + try { + const { id } = await t.submitFeedback( + ivy, + { feedback: 'Feedback for keyed replies' }, + 'web', + quiet, + ); + + // SENT: the same key with another message is a conflict, not the earlier success + const sentKey = `sent-${t.suffix}`; + await reply(id, 'First answer', sentKey); + await assert.rejects(reply(id, 'Edited answer', sentKey), t.IdempotencyConflictError); + assert.equal(sent.length, 1); + + // FAILED: only the same message is retried; an edit needs a new key + const failedKey = `failed-${t.suffix}`; + await assert.rejects( + reply(id, 'Answer that fails', failedKey, { + db: t.db, + send: async () => { + throw new t.MailNotSentError({ code: 'ENOTCONFIGURED' }); + }, + }), + t.MailNotSentError, + ); + await assert.rejects(reply(id, 'Answer, edited', failedKey), t.IdempotencyConflictError); + const stillFailed = await t.db.feedbackReply.findUniqueOrThrow({ + where: { idempotencyKey: failedKey }, + }); + assert.equal(stillFailed.deliveryStatus, 'FAILED'); + assert.equal(stillFailed.message, 'Answer that fails'); + assert.equal(sent.length, 1); + + const retried = await reply(id, 'Answer that fails', failedKey); + assert.equal(retried.created, true); + assert.equal(sent.length, 2); + assert.equal(sent[1]?.reply, 'Answer that fails'); + } finally { + await t.db.user.delete({ where: { id: ivy.id } }); + } + }, +); + +void test( + 'feedback: an operator settles a reply stuck in SENDING', + { skip: !databaseUrl }, + async () => { + const t = await setup(); + const jo = await t.createUser('Jo'); + let sends = 0; + const send = async () => { + sends += 1; + }; + const dropped = async () => { + sends += 1; + throw new t.MailNotSentError({ code: 'ECONNECTION' }); + }; + const quiet = { db: t.db, notify: async () => undefined }; + + try { + const { id } = await t.submitFeedback( + jo, + { feedback: 'Feedback with stuck replies' }, + 'web', + quiet, + ); + const stuck = async (idempotencyKey: string) => { + await assert.rejects( + t.replyToFeedback( + id, + { message: 'Thanks!', idempotencyKey }, + { db: t.db, send: dropped }, + ), + t.MailNotSentError, + ); + const reply = await t.db.feedbackReply.findUniqueOrThrow({ where: { idempotencyKey } }); + // Its attempt isn't treated as abandoned yet, so it can't be settled + await assert.rejects( + t.resolveReply(id, reply.id, { deliveryStatus: 'FAILED' }, t.db), + t.ReplyStillSendingError, + ); + await settleAttempt(t, reply.id); + return reply; + }; + + // Delivered after all: recorded as sent, and the key answers without sending + const deliveredKey = `delivered-${t.suffix}`; + const delivered = await stuck(deliveredKey); + assert.equal(delivered.deliveryStatus, 'SENDING'); + const resolved = await t.resolveReply(id, delivered.id, { deliveryStatus: 'SENT' }, t.db); + assert.equal(resolved.deliveryStatus, 'SENT'); + assert.ok(resolved.sentAt); + assert.equal((await t.db.feedback.findUniqueOrThrow({ where: { id } })).status, 'REPLIED'); + const again = await t.replyToFeedback( + id, + { message: 'Thanks!', idempotencyKey: deliveredKey }, + { db: t.db, send }, + ); + assert.equal(again.created, false); + assert.equal(sends, 1); + + // Only from SENDING + await assert.rejects( + t.resolveReply(id, delivered.id, { deliveryStatus: 'FAILED' }, t.db), + t.ReplyNotSendingError, + ); + + // Never delivered: FAILED makes the same key and message send again + const lostKey = `lost-${t.suffix}`; + const lost = await stuck(lostKey); + const failed = await t.resolveReply(id, lost.id, { deliveryStatus: 'FAILED' }, t.db); + assert.equal(failed.deliveryStatus, 'FAILED'); + assert.equal(failed.sentAt, null); + const resent = await t.replyToFeedback( + id, + { message: 'Thanks!', idempotencyKey: lostKey }, + { db: t.db, send }, + ); + assert.equal(resent.created, true); + assert.equal(sends, 3); + + // The reply has to belong to that feedback + const other = await t.submitFeedback(jo, { feedback: 'Some other feedback' }, 'web', quiet); + await assert.rejects( + t.resolveReply(other.id, lost.id, { deliveryStatus: 'SENT' }, t.db), + t.ReplyNotFoundError, + ); + await assert.rejects( + t.resolveReply(id, 'missing', { deliveryStatus: 'SENT' }, t.db), + t.ReplyNotFoundError, + ); + } finally { + await t.db.user.delete({ where: { id: jo.id } }); + } + }, +); + +void test( + 'feedback: a late outcome of an attempt an operator resolved never touches a newer attempt', + { skip: !databaseUrl }, + async () => { + const t = await setup(); + const kim = await t.createUser('Kim'); + const quiet = { db: t.db, notify: async () => undefined }; + const idempotencyKey = `race-${t.suffix}`; + const input = { message: 'Thanks for writing!', idempotencyKey }; + let sends = 0; + const counted = (send: () => Promise) => async () => { + sends += 1; + await send(); + }; + + try { + const { id } = await t.submitFeedback(kim, { feedback: 'Feedback for a race' }, 'web', quiet); + + // A is in flight + const a = pausedSend(); + const attemptA = t.replyToFeedback(id, input, { db: t.db, send: counted(a.send) }); + await a.started; + const reply = await t.db.feedbackReply.findUniqueOrThrow({ where: { idempotencyKey } }); + + // Too young to settle by hand + await assert.rejects( + t.resolveReply(id, reply.id, { deliveryStatus: 'FAILED' }, t.db), + t.ReplyStillSendingError, + ); + // Still too young just under the limit + await t.db.feedbackReply.update({ + where: { id: reply.id }, + data: { attemptStartedAt: new Date(Date.now() - t.RECONCILE_AFTER_MS + 60_000) }, + }); + await assert.rejects( + t.resolveReply(id, reply.id, { deliveryStatus: 'FAILED' }, t.db), + t.ReplyStillSendingError, + ); + + // Once it looks over, the operator marks it FAILED, and B claims it + await settleAttempt(t, reply.id); + await t.resolveReply(id, reply.id, { deliveryStatus: 'FAILED' }, t.db); + const b = pausedSend(); + const attemptB = t.replyToFeedback(id, input, { db: t.db, send: counted(b.send) }); + await b.started; + const claimedByB = await t.db.feedbackReply.findUniqueOrThrow({ where: { idempotencyKey } }); + assert.equal(claimedByB.deliveryStatus, 'SENDING'); + assert.ok(claimedByB.attemptId); + + // A fails late: B's attempt keeps the reply + const originalWarn = console.warn; + const warnings: unknown[] = []; + console.warn = (...args: unknown[]) => warnings.push(args); + try { + a.finish.reject(new t.MailNotSentError({ code: 'ENOTCONFIGURED' })); + await assert.rejects(attemptA, t.MailNotSentError); + } finally { + console.warn = originalWarn; + } + assert.deepEqual(warnings, [['Feedback reply attempt no longer holds the reply', reply.id]]); + const afterA = await t.db.feedbackReply.findUniqueOrThrow({ where: { idempotencyKey } }); + assert.equal(afterA.deliveryStatus, 'SENDING'); + assert.equal(afterA.attemptId, claimedByB.attemptId); + + // So no third attempt can start + await assert.rejects( + t.replyToFeedback(id, input, { db: t.db, send: counted(async () => undefined) }), + t.ReplyInProgressError, + ); + + // B completes it + b.finish.resolve(); + const done = await attemptB; + assert.equal(done.created, true); + assert.equal(done.reply.deliveryStatus, 'SENT'); + assert.equal(sends, 2); + } finally { + await t.db.user.delete({ where: { id: kim.id } }); + } + }, +); + +void test( + 'feedback: a stale success can’t mark a reclaimed reply sent', + { skip: !databaseUrl }, + async () => { + const t = await setup(); + const lee = await t.createUser('Lee'); + const quiet = { db: t.db, notify: async () => undefined }; + const idempotencyKey = `stale-${t.suffix}`; + const input = { message: 'Thanks for the idea!', idempotencyKey }; + const originalWarn = console.warn; + console.warn = () => undefined; + + try { + const { id } = await t.submitFeedback( + lee, + { feedback: 'Feedback for a stale send' }, + 'web', + quiet, + ); + + const a = pausedSend(); + const attemptA = t.replyToFeedback(id, input, { db: t.db, send: a.send }); + await a.started; + const reply = await t.db.feedbackReply.findUniqueOrThrow({ where: { idempotencyKey } }); + await settleAttempt(t, reply.id); + await t.resolveReply(id, reply.id, { deliveryStatus: 'FAILED' }, t.db); + + const b = pausedSend(); + const attemptB = t.replyToFeedback(id, input, { db: t.db, send: b.send }); + await b.started; + + // A's email went out after all, but B holds the reply now + a.finish.resolve(); + const lateA = await attemptA; + assert.equal(lateA.created, false); + assert.equal(lateA.reply.deliveryStatus, 'SENDING'); + const afterA = await t.db.feedbackReply.findUniqueOrThrow({ where: { idempotencyKey } }); + assert.equal(afterA.deliveryStatus, 'SENDING'); + assert.equal(afterA.sentAt, null); + assert.equal((await t.db.feedback.findUniqueOrThrow({ where: { id } })).status, 'OPEN'); + + // B's own outcome is what's recorded + b.finish.reject(new t.MailNotSentError({ code: 'ENOTCONFIGURED' })); + await assert.rejects(attemptB, t.MailNotSentError); + const afterB = await t.db.feedbackReply.findUniqueOrThrow({ where: { idempotencyKey } }); + assert.equal(afterB.deliveryStatus, 'FAILED'); + + // And a resolved attempt can't complete the reply either + await assert.rejects( + t.resolveReply(id, reply.id, { deliveryStatus: 'SENT' }, t.db), + t.ReplyNotSendingError, + ); + } finally { + console.warn = originalWarn; + await t.db.user.delete({ where: { id: lee.id } }); + } + }, +); + +void test( + 'feedback: one feedback can get several replies, each sent and listed in order', + { skip: !databaseUrl }, + async () => { + const t = await setup(); + const max = await t.createUser('Max'); + const sent: ReplyEmail[] = []; + const deps = { + db: t.db, + send: async (email: ReplyEmail) => { + sent.push(email); + }, + }; + const quiet = { db: t.db, notify: async () => undefined }; + + try { + const { id } = await t.submitFeedback( + max, + { feedback: 'A question in two parts' }, + 'web', + quiet, + ); + assert.equal((await t.db.feedback.findUniqueOrThrow({ where: { id } })).status, 'OPEN'); + + const first = await t.replyToFeedback( + id, + { message: 'Answer to the first part', idempotencyKey: `first-${t.suffix}` }, + deps, + ); + const second = await t.replyToFeedback( + id, + { message: 'And the second part', idempotencyKey: `second-${t.suffix}` }, + deps, + ); + assert.equal(first.created, true); + assert.equal(second.created, true); + assert.deepEqual( + sent.map(({ reply }) => reply), + ['Answer to the first part', 'And the second part'], + ); + + const page = await t.listFeedback({ limit: 200, status: 'REPLIED' }); + const listed = page.items.find((item) => item.id === id); + assert.deepEqual( + listed?.replies.map(({ message, deliveryStatus }) => [message, deliveryStatus]), + [ + ['Answer to the first part', 'SENT'], + ['And the second part', 'SENT'], + ], + ); + } finally { + await t.db.user.delete({ where: { id: max.id } }); + } + }, +); diff --git a/src/tests/feedback.test.ts b/src/tests/feedback.test.ts new file mode 100644 index 0000000..0ca42fc --- /dev/null +++ b/src/tests/feedback.test.ts @@ -0,0 +1,571 @@ +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'; +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 { adminApiRoute } = await import('~/server/adminApi'); +const { MailNotSentError, sendFeedbackEmail } = await import('~/server/mailer'); +const { env } = await import('~/env'); + +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: 'OPEN', + createdAt: new Date(), + updatedAt: new Date(), + }); + + // 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), + } as unknown as PrismaClient; + + return { + db, + rows, + users, + failNextCreate: () => { + failNextCreateWithDuplicateKey = true; + }, + }; +}; + +// 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(); + 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@example.com'); + assert.equal(rows[0]?.name, 'Alice'); + 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 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(); + 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 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; + } + }); + + void it('needs an email address to reply to', async () => { + const { deps } = setup(); + await assert.rejects( + 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')); + }); +}); + +void describe('sendFeedbackEmail', () => { + const feedback = { + id: 'fb-123', + message: 'The feedback itself', + email: 'sender@example.com', + name: 'Sender', + source: 'web', + appVersion: null, + }; + const smtpRejection = () => + Object.assign(new Error('550 5.1.1 : Recipient address rejected'), { + code: 'EENVELOPE', + responseCode: 550, + response: '550 5.1.1 : Recipient address rejected', + }); + + /** Runs `run` with console output and Discord posts captured. */ + const capture = async (run: () => Promise) => { + const mutableEnv = env as { -readonly [Key in keyof typeof env]: (typeof env)[Key] }; + const originalEnv = { ...env }; + const original = { error: console.error, log: console.log, warn: console.warn }; + const originalFetch = globalThis.fetch; + const output: unknown[] = []; + const record = (...args: unknown[]) => { + output.push(args); + }; + mutableEnv.FEEDBACK_EMAIL = 'feedback-inbox@example.com'; + mutableEnv.DISCORD_WEBHOOK_URL = 'https://discord.invalid/webhook'; + console.error = record; + console.log = record; + console.warn = record; + globalThis.fetch = (async (_url: string, init?: { body?: string }) => { + output.push(['discord', init?.body]); + return new Response(null, { status: 204 }); + }) as typeof fetch; + + try { + return { result: await run(), output: JSON.stringify(output) }; + } finally { + Object.assign(mutableEnv, originalEnv); + Object.assign(console, original); + globalThis.fetch = originalFetch; + } + }; + + void it('logs and reports only the feedback id and codes when the server rejects it', async () => { + const thrown = [smtpRejection(), new MailNotSentError(smtpRejection())]; + + await thrown.reduce(async (previous, error) => { + await previous; + const { result, output } = await capture(() => + sendFeedbackEmail(feedback, async () => { + throw error; + }), + ); + + assert.equal(result, false); + assert.ok(output.includes('fb-123')); + assert.ok(output.includes('EENVELOPE')); + assert.ok(output.includes('discord')); + [ + 'feedback-inbox@example.com', + 'sender@example.com', + 'Recipient address', + '550 5.1.1', + ].forEach((secret) => assert.ok(!output.includes(secret), `${secret} leaked: ${output}`)); + }, Promise.resolve()); + }); + + void it('sends to the feedback inbox with the sender as reply-to', async () => { + const calls: unknown[][] = []; + const { result } = await capture(() => + sendFeedbackEmail(feedback, async (...args) => { + calls.push(args); + }), + ); + + assert.equal(result, true); + assert.equal(calls[0]?.[0], 'feedback-inbox@example.com'); + assert.match(String(calls[0]?.[1]), /^Feedback received on SplitPro/); + assert.equal(calls[0]?.[4], 'sender@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() };