Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ TEST_DATABASE_URL=postgresql://postgres:postgres@localhost:5433/hedwig_test npm

Without `TEST_DATABASE_URL`, the database suite is skipped. In CI it is required, and the `test` job provides a Postgres service.

Test files run in parallel and would share the same database, so keep database tests in `test/email-checker.test.ts` or run them serially. Import `test/helpers/test-env.ts` first in any database test file.
Every database test file shares the same database and clears it between tests, so `npm test` runs test files one at a time (`--test-concurrency=1`). Import `test/helpers/test-env.ts` first in any database test file.

## Pull Request Checklist

Expand Down
4 changes: 2 additions & 2 deletions app/api/internal/lists/[id]/contacts/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { contacts, lists } from '@/lib/db/schema'
import { eq, ilike, and, count, SQL } from 'drizzle-orm'
import { createContactSchema } from '@/lib/validations/contacts'
import { auditFromSession, logAudit } from '@/lib/audit'
import { isUniqueViolation } from '@/lib/db/errors'
import { getQueue, JOBS } from '@/lib/queue'
import { logger } from '@/lib/logger'
import { enqueueContactEmailVerification } from '@/lib/email-verify'
Expand Down Expand Up @@ -112,8 +113,7 @@ export async function POST(

return NextResponse.json(created, { status: 201 })
} catch (e: unknown) {
const msg = e instanceof Error ? e.message : ''
if (msg.includes('unique') || msg.includes('duplicate')) {
if (isUniqueViolation(e)) {
return NextResponse.json({ error: 'A contact with this email already exists in this list' }, { status: 409 })
}
throw e
Expand Down
24 changes: 19 additions & 5 deletions app/api/v1/lists/[listId]/contacts/[id]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { eq, and } from 'drizzle-orm'
import { withApiAuth } from '@/lib/api-auth'
import { updateContactSchema } from '@/lib/validations/contacts'
import { auditFromApiKey, logAudit } from '@/lib/audit'
import { isUniqueViolation } from '@/lib/db/errors'

export async function PUT(
req: NextRequest,
Expand Down Expand Up @@ -33,11 +34,24 @@ export async function PUT(
if (parsed.data.metadata !== undefined) updateData.metadata = parsed.data.metadata
if (parsed.data.status !== undefined) updateData.status = parsed.data.status

const [updated] = await db
.update(contacts)
.set(updateData)
.where(and(eq(contacts.id, params.id), eq(contacts.listId, params.listId)))
.returning()
let updated: typeof contacts.$inferSelect | undefined
try {
const rows = await db
.update(contacts)
.set(updateData)
.where(and(eq(contacts.id, params.id), eq(contacts.listId, params.listId)))
.returning()
updated = rows[0]
} catch (err) {
// Changing the email to one another contact in this list already has.
if (isUniqueViolation(err)) {
return NextResponse.json(
{ error: 'A contact with this email already exists in this list', data: null, meta: {} },
{ status: 409 },
)
}
throw err
}

if (!updated) {
return NextResponse.json({ error: 'Contact not found', data: null, meta: {} }, { status: 404 })
Expand Down
52 changes: 41 additions & 11 deletions app/api/v1/lists/[listId]/contacts/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ export async function POST(
}
const requireDoubleOptIn = list.requireDoubleOptIn

// A contact already in this list is a conflict, not a server error. Integrations
// retry on 5xx, so answering a duplicate with 500 makes them retry forever.
const [created] = await db
.insert(contacts)
.values({
Expand All @@ -98,26 +100,54 @@ export async function POST(
? { status: 'pending', confirmationToken: randomUUID() }
: {}),
})
.onConflictDoNothing({ target: [contacts.listId, contacts.email] })
.returning()

if (requireDoubleOptIn && created) {
try {
if (!created) {
const [existing] = await db
.select({ id: contacts.id, email: contacts.email, status: contacts.status })
.from(contacts)
.where(and(eq(contacts.listId, params.listId), eq(contacts.email, parsed.data.email)))
return NextResponse.json(
{
data: existing ?? null,
meta: {},
error: 'A contact with this email already exists in this list',
},
{ status: 409 },
)
}

// The contact exists from here on, so nothing below may fail the request. A 5xx
// would make the caller retry, and every retry would then be a duplicate.
if (requireDoubleOptIn) {
await runSideEffect('enqueue confirmation job', created.id, async () => {
const queue = await getQueue()
await queue.send(JOBS.SEND_CONFIRMATION, { contactId: created.id })
} catch (err) {
logger.error({ err, contactId: created.id }, 'Failed to enqueue confirmation job')
}
})
}

await enqueueContactEmailVerification([created.id])
await runSideEffect('enqueue email verification', created.id, () =>
enqueueContactEmailVerification([created.id]),
)

await logAudit(
auditFromApiKey(req, auth),
'contact.create',
{ type: 'contact', id: created.id },
{ listId: params.listId, email: created.email, requireDoubleOptIn },
await runSideEffect('write audit log', created.id, () =>
logAudit(
auditFromApiKey(req, auth),
'contact.create',
{ type: 'contact', id: created.id },
{ listId: params.listId, email: created.email, requireDoubleOptIn },
),
)

return NextResponse.json({ data: created, meta: {}, error: null }, { status: 201 })
})
}

async function runSideEffect(label: string, contactId: string, fn: () => Promise<unknown>) {
try {
await fn()
} catch (err) {
logger.error({ err, contactId }, `Failed to ${label} after creating contact`)
}
}
18 changes: 18 additions & 0 deletions lib/api-auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { apiKeys } from '@/lib/db/schema'
import { eq } from 'drizzle-orm'
import bcrypt from 'bcryptjs'
import { consumeApiKeyToken, type RateLimitResult } from '@/lib/rate-limit'
import { logger } from '@/lib/logger'

export interface ApiAuthContext {
keyId: string
Expand Down Expand Up @@ -44,10 +45,27 @@ function v1ErrorResponse(status: number, error: string, headers?: Record<string,
/**
* Wraps a v1 API route handler with API key auth + per-key rate limiting.
* On success, calls the handler with `auth` and decorates the response with X-RateLimit-* headers.
* Any error thrown by auth, rate limiting or the handler is logged and answered with a JSON 500
* in the v1 envelope, instead of the empty-body 500 Next.js sends for an uncaught error.
*/
export async function withApiAuth(
req: NextRequest,
handler: (auth: ApiAuthContext) => Promise<NextResponse>,
): Promise<NextResponse> {
try {
return await authenticateAndRun(req, handler)
} catch (err) {
logger.error(
{ err, method: req.method, path: req.nextUrl.pathname },
'Unhandled error in v1 API route',
)
return v1ErrorResponse(500, 'Internal server error')
}
}

async function authenticateAndRun(
req: NextRequest,
handler: (auth: ApiAuthContext) => Promise<NextResponse>,
): Promise<NextResponse> {
const auth = await authenticateApiKey(req)
if (!auth) {
Expand Down
16 changes: 16 additions & 0 deletions lib/db/errors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/** Postgres SQLSTATE for a unique constraint violation. */
export const PG_UNIQUE_VIOLATION = '23505'

/**
* True when err is a Postgres unique violation. drizzle-orm wraps driver errors in a
* DrizzleQueryError whose message is the failed SQL, so the SQLSTATE is on `cause`.
* Walks a few levels of `cause` to find it.
*/
export function isUniqueViolation(err: unknown): boolean {
let current: unknown = err
for (let depth = 0; depth < 5 && current && typeof current === 'object'; depth++) {
if ((current as { code?: unknown }).code === PG_UNIQUE_VIOLATION) return true
current = (current as { cause?: unknown }).cause
}
return false
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"build": "next build",
"start": "next start",
"lint": "next lint",
"test": "tsx --test test/*.test.ts",
"test": "tsx --test --test-concurrency=1 test/*.test.ts",
"db:generate": "drizzle-kit generate:pg",
"db:migrate": "tsx --env-file=.env.local lib/db/migrate.ts",
"db:migrate:prod": "tsx lib/db/migrate.ts",
Expand Down
4 changes: 2 additions & 2 deletions test/email-checker.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
*
* TEST_DATABASE_URL=postgresql://postgres:postgres@localhost:5432/hedwig_test npm test
*
* Node runs test files in parallel, and every file would share this database, so
* keep database tests in this file (or run them serially) to avoid clobbering.
* Every database test file shares this database, so `npm test` runs test files
* one at a time to avoid clobbering.
*/
// Must be the first import: it points DATABASE_URL at the test database before lib/db loads.
import { skipWithoutDatabase } from './helpers/test-env'
Expand Down
Loading
Loading