Files
evig/docs/ARCHITECTURE_QUICK_START.md
CatoandClaude be2f01859d docs: correct drift — evig host, /opt/evig box paths, email provider, CI smoke target (#573)
- Production host is evig.orangecat.ch everywhere a doc presents the current
  address; revampit.orangecat.ch is the retired 308 redirect.
- Box was renamed /opt/revampit -> /opt/evig (systemd evig-app, evig-cron@*,
  evig-backup, scripts/selfhost-deploy-evig.sh); docs and the Layer B list
  still named the old paths and a deploy script that no longer exists.
- CLAUDE.md claimed auth-smoke/inventory-smoke target the old host; CI now
  defaults PLAYWRIGHT_BASE_URL to evig.orangecat.ch, so the redirect has no
  CI coverage. Say so.
- Local dev DB is the Docker Postgres on 5433, not Neon.
- Email is Resend first (src/config/email.ts), not Listmonk/Brevo.
- README: no "Swiss-compliant donation receipts" while in formation; real
  test layout, eight locales, actual CI jobs.
- CONTRIBUTING / PR template / REVIEW: evig name, pnpm, Node 22.13+,
  messages/ path; no donate link to Revamp-IT.
- ARCHITECTURE_QUICK_START: drop the example that granted staff by e-mail.
- Payment setup docs: canonical webhook URLs plus a note on providers
  configured before the rename.


Claude-Session: https://claude.ai/code/session_01PJkbo18cRNXtgPWZN2S9cD

Co-authored-by: Claude <noreply@anthropic.com>
2026-10-19 12:51:27 +02:00

11 KiB

Architecture Quick Start Guide

New Patterns Implemented: Repository Layer, Service Layer, Unified Permissions


🚀 Quick Start

1. Using Repositories (Data Access)

Problem Solved: N+1 queries killing performance

Before:

// ❌ BAD: N+1 query problem (101 queries!)
const repairers = await query(`SELECT * FROM repairers LIMIT 50`)
for (const repairer of repairers) {
  const ratings = await query(`SELECT AVG(rating) FROM reviews WHERE repairer_id = $1`, [repairer.id])
  const reviews = await query(`SELECT * FROM reviews WHERE repairer_id = $1`, [repairer.id])
}

After:

// ✅ GOOD: Single query with JOIN aggregation (1 query!)
import { RepairerRepository } from '@/lib/repositories'

const repo = new RepairerRepository()
const repairers = await repo.findActiveWithDetails(50)
// Returns: repairers with reviews, avg_rating, review_count, services - all in one query!

Available Repositories:

  • RepairerRepository - Repairer profiles with reviews & services
  • HelperRepository - IT-Hilfe helpers with statistics
  • BaseRepository - Extend this for new repositories

Creating New Repositories:

import { BaseRepository } from '@/lib/repositories'
import { TABLE_NAMES } from '@/config/database'

export class ProductRepository extends BaseRepository {
  async findActiveWithDetails(limit = 50) {
    return this.query(`
      SELECT
        p.*,
        COALESCE(json_agg(i.*) FILTER (WHERE i.id IS NOT NULL), '[]') as images
      FROM ${TABLE_NAMES.INVENTORY_ITEMS} p
      LEFT JOIN ${TABLE_NAMES.PRODUCT_IMAGES} i ON p.id = i.product_id
      WHERE p.status = 'active'
      GROUP BY p.id
      LIMIT $1
    `, [limit])
  }
}

2. Using Services (Business Logic)

Problem Solved: 400-line routes with mixed concerns

Before:

// ❌ BAD: Business logic in route (419 lines!)
export async function POST(request: NextRequest) {
  // ... signature verification

  // Business logic mixed with HTTP handling
  if (event.type === 'payment_intent.succeeded') {
    const paymentIntent = event.data.object
    await query(`UPDATE payments SET status = 'succeeded'...`)
    await query(`UPDATE orders SET payment_status = 'paid'...`)
    await query(`UPDATE appointments SET status = 'confirmed'...`)
    // ... 400 more lines
  }
}

After:

// ✅ GOOD: Thin route + Service layer (50 lines!)
import { PaymentService } from '@/lib/services'

export async function POST(request: NextRequest) {
  const paymentService = new PaymentService()
  const event = await stripe.webhooks.constructEvent(body, sig, secret)

  // Route = orchestration only
  switch (event.type) {
    case 'payment_intent.succeeded':
      await paymentService.handlePaymentIntentSucceeded(event.data.object)
      break
    // ... other cases (one-liners)
  }

  return NextResponse.json({ success: true })
}

Service Layer Benefits:

  • ✅ Testable without HTTP mocking
  • ✅ Reusable across multiple routes
  • ✅ Clear separation of concerns
  • ✅ Easy to maintain and debug

Creating New Services:

import { query } from '@/lib/auth/db'
import { TABLE_NAMES } from '@/config/database'

export class EmailService {
  async sendWelcomeEmail(userId: string): Promise<void> {
    // Business logic here
    const user = await query(`SELECT * FROM ${TABLE_NAMES.USERS} WHERE id = $1`, [userId])
    // ... email sending logic
  }
}

3. Using Unified Permissions (Auth)

Problem Solved: Dual auth system causing staff lockouts

Current Situation:

  • Old system: 11 files using role === 'REVAMPIT_ADMIN'
  • New system: 6 files using is_staff + staff_permissions
  • Problem: New staff with is_staff=true but no role get locked out

Solution: Use unified functions that check BOTH systems

Before:

// ❌ BAD: Only checks old system (locks out new staff!)
if (session.user.role === ROLES.REVAMPIT_ADMIN) {
  return NextResponse.next()
}
return NextResponse.redirect('/unauthorized')

After: check the SECTION, never the e-mail domain and never the bare staff flag. isStaffEmail is cosmetic (welcome-email template) and grants nothing; session.user.isStaff alone is wider than any section. Use withAdmin('<section>') (below) or canAccessSection(user, section) from @/lib/permissions.

For API routes, use the withAdmin middleware with section-level auth:

import { withAdmin } from '@/lib/api/middleware'

// Staff-only (any section):
export const GET = withAdmin(async (request, session) => { ... })

// Section-specific access:
export const GET = withAdmin('products', async (request, session) => { ... })

Available Functions (from @/lib/permissions):

  1. isStaffEmail(email) — Cosmetic only (picks the welcome-email template); never grants staff — staff is users.is_staff
  2. isSuperAdmin(email) — Check if user has full admin access
  3. canAccessSection(user, section) — Check section-specific access
  4. getAccessibleSections(user) — Get all accessible sections

📂 Architecture Overview

┌─────────────────────────────────────────────┐
│                 Route Layer                 │
│         /app/api/*/route.ts (50 lines)      │
│  - Parse request                            │
│  - Validate input                           │
│  - Call service                             │
│  - Format response                          │
└────────────────────┬────────────────────────┘
                     │
                     ▼
┌─────────────────────────────────────────────┐
│              Service Layer                  │
│        /lib/services/*.ts (100-200 lines)   │
│  - Business logic                           │
│  - Workflows                                │
│  - Validation                               │
│  - Call repositories                        │
└────────────────────┬────────────────────────┘
                     │
                     ▼
┌─────────────────────────────────────────────┐
│            Repository Layer                 │
│      /lib/repositories/*.ts (50-100 lines)  │
│  - Data access                              │
│  - SQL queries                              │
│  - JOIN aggregations                        │
│  - Transactions                             │
└─────────────────────────────────────────────┘

🎯 When to Use What

Use Repositories When:

  • ✅ Fetching data from database
  • ✅ Complex queries with JOINs
  • ✅ Aggregating related data
  • ✅ Avoiding N+1 queries

Example: Get repairers with reviews

const repo = new RepairerRepository()
const repairers = await repo.findActiveWithDetails(50)

Use Services When:

  • ✅ Business logic (validations, workflows)
  • ✅ Multi-step operations
  • ✅ Coordinating multiple repositories
  • ✅ Side effects (emails, notifications)

Example: Handle payment success

const service = new PaymentService()
await service.handlePaymentIntentSucceeded(paymentIntent)
// Handles: update payment, update order, send email, etc.

Use Unified Permissions When:

  • ✅ Checking admin access
  • ✅ Section-level permissions
  • ✅ During auth migration period
  • ✅ ANY auth-related check

Example: Protect admin route

if (!hasAdminAccessUnified(session.user)) {
  return NextResponse.redirect('/unauthorized')
}

🚫 Anti-Patterns to Avoid

❌ Don't Query in Loops

// BAD: N+1 query problem
for (const user of users) {
  const profile = await query(`SELECT * FROM profiles WHERE user_id = $1`, [user.id])
}

// GOOD: Single query with JOIN
const usersWithProfiles = await query(`
  SELECT u.*, p.* FROM users u
  LEFT JOIN profiles p ON u.id = p.user_id
`)

❌ Don't Mix Business Logic in Routes

// BAD: Business logic in route
export async function POST(request: NextRequest) {
  // ... 200 lines of business logic
}

// GOOD: Thin route + service
export async function POST(request: NextRequest) {
  const service = new PaymentService()
  await service.handlePayment(data)
  return apiSuccess({ success: true })
}

❌ Don't Use Old Auth Only

// BAD: Locks out new staff
if (user.role === ROLES.REVAMPIT_ADMIN) { ... }

// GOOD: Works with both systems
if (hasAdminAccessUnified(user)) { ... }

📊 Performance Gains

Operation Before After Improvement
List repairers 101 queries 1 query 100x faster
Get repairer details 7 queries 1 query 7x faster
List helpers 76 queries 1 query 76x faster

Response Time Target: < 100ms for all endpoints


🧪 Testing Examples

Repository Tests

describe('RepairerRepository', () => {
  it('finds active repairers with details in single query', async () => {
    const repo = new RepairerRepository()
    const repairers = await repo.findActiveWithDetails(10)

    expect(repairers).toHaveLength(10)
    expect(repairers[0].reviews).toBeDefined()
    expect(repairers[0].avg_rating).toBeDefined()
  })
})

Service Tests

describe('PaymentService', () => {
  it('updates order on payment success', async () => {
    const service = new PaymentService()
    await service.handlePaymentIntentSucceeded({
      id: 'pi_123',
      metadata: { orderId: 'order_123' }
    })

    // Verify order was updated
    const order = await query(`SELECT * FROM orders WHERE id = 'order_123'`)
    expect(order.payment_status).toBe('paid')
  })
})

Auth Tests

describe('Unified Permissions', () => {
  it('grants access to old and new staff', () => {
    const oldUser = { email: 'old@test.com', role: 'REVAMPIT_ADMIN' }
    const newUser = { email: 'new@revamp-it.ch', isStaff: true }

    expect(hasAdminAccessUnified(oldUser)).toBe(true)
    expect(hasAdminAccessUnified(newUser)).toBe(true)
  })
})

📚 Further Reading

  • IMPLEMENTATION_SUMMARY.md - Full implementation details
  • ARCHITECTURE_EVALUATION.md - Original improvement plan
  • docs/SHARED_CONTEXT.md - Project tech stack
  • Global Engineering Standards: ~/.claude/CLAUDE.md

Questions? Check the implementation files for detailed examples and documentation.