mirror of
https://github.com/bitbaum/evig.git
synced 2026-12-05 08:58:18 +01:00
fix: improve DB resilience, middleware redirects, and AI provider caching
- Increase Neon connection timeout from 5s to 10s for cold starts - Increase retry delays from [100,300]ms to [500,1500]ms - Preserve query params in middleware callbackUrl redirects - Add 60s TTL cache to AI provider config (was querying DB every call) - Add 5-minute TTL to user columns cache (was never invalidating) - Update CODE_AUDIT.md with all recent fixes and test coverage Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
1 parent
9be9d4d8e2
commit
b60bbb827c
4 files changed
+55
-31
No files matched your search
+31
-20
@@ -1,18 +1,32 @@
|
||||
---
|
||||
created_date: 2026-01-07
|
||||
last_modified_date: 2026-01-14
|
||||
last_modified_summary: Updated audit to reflect major fixes - security auth added, N+1 fixed, cache headers added, React.memo added, Swiss German compliance
|
||||
last_modified_date: 2026-02-17
|
||||
last_modified_summary: Updated audit after reliability hardening - build fix, DB timeouts, middleware fix, AI caching, email fallback, test coverage at 312 tests
|
||||
---
|
||||
|
||||
# RevampIT Code Audit Findings
|
||||
|
||||
**Last Audit Date**: 2026-01-14
|
||||
**Last Audit Date**: 2026-02-17
|
||||
|
||||
This document tracks code quality issues, security findings, and performance problems identified during code audits.
|
||||
|
||||
---
|
||||
|
||||
## Summary of Recent Fixes (2026-01-14)
|
||||
## Summary of Recent Fixes (2026-02-17)
|
||||
|
||||
| Fix | Status | Details |
|
||||
|-----|--------|---------|
|
||||
| Build broken (client/server boundary) | FIXED | Extracted `detect-multi.ts` to break pg import chain |
|
||||
| IT-Hilfe 404s | FIXED | Config routes corrected (`/it-hilfe/my`, `/it-hilfe/my/offers`) |
|
||||
| Email fallback missing | FIXED | Listmonk → Brevo SMTP fallback chain in `sendEmail`/`sendCustomEmail` |
|
||||
| Middleware drops query params | FIXED | `callbackUrl` now includes `request.nextUrl.search` |
|
||||
| Neon cold start timeouts | FIXED | Connection timeout 5s→10s, retry delays [100,300]→[500,1500] |
|
||||
| AI provider config DB spam | FIXED | 60s TTL cache on `loadProviderRuntimeConfig()` |
|
||||
| User columns cache never expires | FIXED | 5-minute TTL on `getUserColumns()` cache |
|
||||
| Pagination lint (nested components) | FIXED | `PageItem`/`NavButton` extracted to module scope |
|
||||
| `ignoreBuildErrors` in next.config.js | FIXED | Already removed |
|
||||
|
||||
### Previous Fixes (2026-01-14)
|
||||
|
||||
| Fix | Status | Commit |
|
||||
|-----|--------|--------|
|
||||
@@ -57,20 +71,18 @@ This document tracks code quality issues, security findings, and performance pro
|
||||
| N+1 query (reviews + attachments) | FIXED | json_agg subquery |
|
||||
| Missing cache headers | FIXED | apiSuccessCached helper added |
|
||||
| No React.memo | FIXED | FilterBar, ComparisonCard wrapped |
|
||||
| AI provider config queried every call | FIXED | 60s TTL cache |
|
||||
| User columns cache never invalidates | FIXED | 5-minute TTL |
|
||||
| Neon cold start connection failures | FIXED | 10s timeout, longer retry delays |
|
||||
| Duplicate count queries | TODO | Use COUNT(*) OVER() |
|
||||
| Raw `<img>` tags | TODO | Migrate to Next.js `<Image>` |
|
||||
|
||||
---
|
||||
|
||||
## Build Configuration Issues
|
||||
## Build Configuration
|
||||
|
||||
**WARNING**: `next.config.js` has:
|
||||
```javascript
|
||||
typescript: { ignoreBuildErrors: true } // PROBLEMATIC
|
||||
eslint: { ignoreDuringBuilds: true } // PROBLEMATIC
|
||||
```
|
||||
|
||||
This masks errors. **Always run `npm run typecheck` manually before commits.**
|
||||
Build is clean — no `ignoreBuildErrors` or `ignoreDuringBuilds` flags.
|
||||
Always run `npm run typecheck` and `npm run lint` before commits.
|
||||
|
||||
---
|
||||
|
||||
@@ -79,16 +91,15 @@ This masks errors. **Always run `npm run typecheck` manually before commits.**
|
||||
### Low Priority
|
||||
- Migrate raw `<img>` tags to `next/image` in BlogFeaturedGrid.tsx
|
||||
- Consider COUNT(*) OVER() for pagination queries
|
||||
- Increase test coverage
|
||||
- Voice transcription service is manual start (`npm run transcription:start`) — consider adding to docker-compose when Python deps are standardized
|
||||
|
||||
### Testing Coverage
|
||||
**Current Coverage**: MINIMAL (only 3 test files found)
|
||||
- `src/middleware/__tests__/admin.test.ts`
|
||||
- `src/app/marketplace/__tests__/page.test.tsx`
|
||||
- `src/components/auth/__tests__/RoleSelector.test.tsx`
|
||||
|
||||
**Recommendation**: Increase test coverage, especially for API routes and critical business logic.
|
||||
**Current Coverage**: 312 tests across 24 test suites
|
||||
- API route tests (notifications, admin endpoints)
|
||||
- Business logic tests (protocols, payments, services, hirn)
|
||||
- UI tests (marketplace, auth, middleware)
|
||||
- Utility tests (erfassung, currency, tax compliance)
|
||||
|
||||
---
|
||||
|
||||
**Last Updated**: 2026-01-14
|
||||
**Last Updated**: 2026-02-17
|
||||
+12
-1
@@ -45,7 +45,15 @@ interface DbProviderSettingsRow {
|
||||
} | null
|
||||
}
|
||||
|
||||
// Cache provider config to avoid a DB query on every AI call
|
||||
const PROVIDER_CACHE_TTL_MS = 60_000
|
||||
let _providerCache: { config: ProviderRuntimeConfig; expiresAt: number } | null = null
|
||||
|
||||
async function loadProviderRuntimeConfig(): Promise<ProviderRuntimeConfig> {
|
||||
if (_providerCache && Date.now() < _providerCache.expiresAt) {
|
||||
return _providerCache.config
|
||||
}
|
||||
|
||||
const envConfig: ProviderRuntimeConfig = {
|
||||
groqEnabled: true,
|
||||
openRouterEnabled: true,
|
||||
@@ -71,7 +79,7 @@ async function loadProviderRuntimeConfig(): Promise<ProviderRuntimeConfig> {
|
||||
const openrouter = byProvider.get('openrouter')
|
||||
const ollama = byProvider.get('ollama')
|
||||
|
||||
return {
|
||||
const resolved: ProviderRuntimeConfig = {
|
||||
groqEnabled: groq ? groq.is_enabled : envConfig.groqEnabled,
|
||||
openRouterEnabled: openrouter ? openrouter.is_enabled : envConfig.openRouterEnabled,
|
||||
ollamaEnabled: ollama ? ollama.is_enabled : envConfig.ollamaEnabled,
|
||||
@@ -88,8 +96,11 @@ async function loadProviderRuntimeConfig(): Promise<ProviderRuntimeConfig> {
|
||||
? ((ollama.settings?.model as string | undefined) || envConfig.ollamaModel)
|
||||
: (ollama ? '' : envConfig.ollamaModel),
|
||||
}
|
||||
_providerCache = { config: resolved, expiresAt: Date.now() + PROVIDER_CACHE_TTL_MS }
|
||||
return resolved
|
||||
} catch (error) {
|
||||
logger.warn('AI provider settings table unavailable; falling back to environment config', { error })
|
||||
_providerCache = { config: envConfig, expiresAt: Date.now() + PROVIDER_CACHE_TTL_MS }
|
||||
return envConfig
|
||||
}
|
||||
}
|
||||
|
||||
+10
-8
@@ -18,7 +18,7 @@ const dbConfig = {
|
||||
// Keep pool conservative for Neon to avoid connection saturation under load.
|
||||
max: parseInt(process.env.DB_POOL_MAX || '10', 10),
|
||||
idleTimeoutMillis: parseInt(process.env.DB_IDLE_TIMEOUT_MS || '30000', 10),
|
||||
connectionTimeoutMillis: parseInt(process.env.DB_CONNECT_TIMEOUT_MS || '5000', 10),
|
||||
connectionTimeoutMillis: parseInt(process.env.DB_CONNECT_TIMEOUT_MS || '10000', 10),
|
||||
statement_timeout: parseInt(process.env.DB_STATEMENT_TIMEOUT_MS || '30000', 10),
|
||||
idle_in_transaction_session_timeout: parseInt(process.env.DB_IDLE_TX_TIMEOUT_MS || '60000', 10),
|
||||
}
|
||||
@@ -36,7 +36,7 @@ function isConnectionError(error: unknown): boolean {
|
||||
}
|
||||
|
||||
const MAX_RETRIES = 2
|
||||
const RETRY_DELAYS = [100, 300] as const
|
||||
const RETRY_DELAYS = [500, 1500] as const
|
||||
|
||||
// Create connection pool (singleton)
|
||||
let pool: Pool | null = null
|
||||
@@ -135,12 +135,13 @@ export async function getClient(): Promise<PoolClient> {
|
||||
throw new Error('Datenbankverbindung fehlgeschlagen. Bitte versuchen Sie es später erneut.')
|
||||
}
|
||||
|
||||
// Cache user columns to keep queries schema-safe across migrations
|
||||
let userColumnsCache: Set<string> | null = null
|
||||
// Cache user columns to keep queries schema-safe across migrations (5-minute TTL)
|
||||
const USER_COLUMNS_CACHE_TTL_MS = 5 * 60_000
|
||||
let _userColumnsCache: { columns: Set<string>; expiresAt: number } | null = null
|
||||
|
||||
async function getUserColumns(): Promise<Set<string>> {
|
||||
if (userColumnsCache) {
|
||||
return userColumnsCache
|
||||
if (_userColumnsCache && Date.now() < _userColumnsCache.expiresAt) {
|
||||
return _userColumnsCache.columns
|
||||
}
|
||||
|
||||
const result = await query<{ column_name: string }>(
|
||||
@@ -150,8 +151,9 @@ async function getUserColumns(): Promise<Set<string>> {
|
||||
[TABLE_NAMES.USERS]
|
||||
)
|
||||
|
||||
userColumnsCache = new Set(result.rows.map((row) => row.column_name))
|
||||
return userColumnsCache
|
||||
const columns = new Set(result.rows.map((row) => row.column_name))
|
||||
_userColumnsCache = { columns, expiresAt: Date.now() + USER_COLUMNS_CACHE_TTL_MS }
|
||||
return columns
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
+2
-2
@@ -32,7 +32,7 @@ export function middleware(request: NextRequest) {
|
||||
|
||||
if (!sessionToken) {
|
||||
const loginUrl = new URL('/auth/login', request.url)
|
||||
loginUrl.searchParams.set('callbackUrl', pathname)
|
||||
loginUrl.searchParams.set('callbackUrl', pathname + request.nextUrl.search)
|
||||
return NextResponse.redirect(loginUrl)
|
||||
}
|
||||
}
|
||||
@@ -45,7 +45,7 @@ export function middleware(request: NextRequest) {
|
||||
|
||||
if (!sessionToken) {
|
||||
const loginUrl = new URL('/auth/login', request.url)
|
||||
loginUrl.searchParams.set('callbackUrl', pathname)
|
||||
loginUrl.searchParams.set('callbackUrl', pathname + request.nextUrl.search)
|
||||
return NextResponse.redirect(loginUrl)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user