# goasante/mad-buddy architecture

> Architecture diagram of the GitHub repository goasante/mad-buddy, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-07-19). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/goasante/mad-buddy
- Repository: https://github.com/goasante/mad-buddy

## Overview

#### Purpose and Runtime
Mad Buddy is a privacy-first social-proximity product: approved friends receive coarse “glow”/nearby signals, never coordinates, distances, maps, or location history. It is a TypeScript Next.js App Router application, deployed with Vercel configuration and backed by Supabase/Postgres/Auth/Storage. Primary public entry is `app/page.tsx`; application routing begins through `app/layout.tsx` and route groups.

#### Identity, Sessions, and Route Boundaries
Supabase authentication supports signup, login, password recovery, OAuth callback, and protected application access. Server/client/admin Supabase clients isolate browser, request-scoped, and privileged database access. Route protection is centralized rather than trusted to UI state.
Paths: `app/(auth)/actions.ts`, `app/auth/callback/route.ts`, `lib/supabase/server.ts`, `lib/security/route-protection.ts`, `proxy.ts`.

#### Product Application Shell and Server Actions
Authenticated social features are grouped under `(app)`, with a shared shell and page-level server-action modules. Pages compose feature components while actions invoke domain services and Supabase; this forms the main browser-to-server mutation boundary. The dashboard is the primary post-login experience.
Paths: `app/(app)/layout.tsx`, `components/app-shell/app-shell.tsx`, `app/(app)/dashboard/page.tsx`, `app/(app)/profile-actions.ts`.

#### Profile, Friendship, and Privacy-Safe Proximity
Profiles and relationship state govern who can see social signals. Clients submit location updates through a dedicated API; backend proximity code derives safe nearby results and freshness state, while presentation uses glow indicators. This is the critical privacy boundary: raw location must not cross into frontend responses.
Paths: `app/api/location/update/route.ts`, `app/api/friends/nearby/route.ts`, `lib/proximity/backend.ts`, `lib/proximity/nearby-service.ts`, `components/glow/glow-avatar.tsx`.

#### Social Coordination and Community Features
The application expands proximity into friends, circles/groups, events, plans, hangout sessions, invitations, messages, moments/drops, and safety workflows. Feature-specific service/rules layers encode eligibility, permissions, and state transitions, with corresponding app pages/actions as orchestration surfaces.
Paths: `lib/social/plans.ts`, `lib/events/service.ts`, `lib/messaging/service.ts`, `lib/content/service.ts`, `app/(app)/groups/[id]/page.tsx`.

#### Notifications, Engagement, and Scheduled Work
Notifications include in-app APIs and push subscriptions/preferences; engagement provides buddy score, achievements, rankings, and recaps. A cron endpoint drives the job worker/handlers, with idempotency and job-event schema support; GitHub schedules its tick.
Paths: `app/api/cron/tick/route.ts`, `lib/jobs/worker.ts`, `lib/jobs/handlers.ts`, `lib/notifications/server.ts`, `.github/workflows/cron-tick.yml`.

#### Billing, Administration, and Operational Controls
Paystack initializes checkout and receives signed webhooks to synchronize subscriptions and entitlements; premium-access checks gate paid features. Separate admin-auth/admin routes provide governance, report review, enforcement, audit, user, and billing views. Health/readiness, CSP reporting, rate limiting, and structured logging support operation.
Paths: `app/api/paystack/webhook/route.ts`, `lib/paystack/sync.ts`, `lib/billing/entitlements.ts`, `app/(admin)/admin/layout.tsx`, `lib/admin/governance.ts`.

#### Persistence and Schema Evolution
Supabase migrations are the durable contract for profiles, friendships, subscriptions, location/privacy, social features, messaging/realtime, push, safety, administration, jobs, and RLS. Typed database bindings connect application services to this evolving schema.
Paths: `supabase/migrations/20260709100000_initial_schema.sql`, `supabase/migrations/20260717280000_jobs_idempotency_events.sql`, `supabase/migrations/20260717300000_realtime_messages.sql`, `lib/supabase/database.types.ts`.

Main entry points: [`lib/supabase/server.ts`](https://github.com/goasante/mad-buddy/blob/main/lib/supabase/server.ts) (Supabase access), [`lib/proximity/backend.ts`](https://github.com/goasante/mad-buddy/blob/main/lib/proximity/backend.ts) (Proximity backend), [`lib/jobs/worker.ts`](https://github.com/goasante/mad-buddy/blob/main/lib/jobs/worker.ts) (Job worker), [`lib/social/plans.ts`](https://github.com/goasante/mad-buddy/blob/main/lib/social/plans.ts) (Social domain services)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_identity["Identity &amp; Access"]
  node_public["Public entry<br/>Next.js page<br/>[page.tsx]"]
  node_auth_actions["Auth actions<br/>server actions<br/>[actions.ts]"]
  node_oauth_callback["OAuth callback<br/>route handler<br/>[route.ts]"]
  node_route_protection{{"Route protection<br/>security middleware"}}
  node_proxy["Request proxy<br/>Next.js proxy<br/>[proxy.ts]"]
end

subgraph group_social["Social Application"]
  node_app_shell["Authenticated shell<br/>application layout<br/>[layout.tsx]"]
  node_dashboard["Dashboard<br/>application page<br/>[page.tsx]"]
  node_profile_actions["Profile actions<br/>server actions<br/>[profile-actions.ts]"]
  node_social_services["Social domain services<br/>service layer<br/>[plans.ts]"]
  node_group_page["Group page<br/>application page<br/>[page.tsx]"]
end

subgraph group_proximity["Privacy-Safe Proximity"]
  node_location_update["Location update<br/>API route<br/>[route.ts]"]
  node_nearby_api["Nearby signals API<br/>API route<br/>[route.ts]"]
  node_proximity_backend{{"Proximity backend<br/>privacy boundary<br/>[backend.ts]"}}
  node_nearby_service["Nearby service<br/>service layer<br/>[nearby-service.ts]"]
  node_glow_avatar["Glow indicator<br/>UI component<br/>[glow-avatar.tsx]"]
end

subgraph group_operations["Operations &amp; Revenue"]
  node_notifications["Notifications<br/>notification service<br/>[server.ts]"]
  node_cron_tick["Cron tick<br/>API route<br/>[route.ts]"]
  node_job_worker["Job worker<br/>background worker<br/>[worker.ts]"]
  node_github_schedule["GitHub schedule<br/>workflow<br/>[cron-tick.yml]"]
  node_paystack_webhook["Paystack webhook<br/>API route<br/>[route.ts]"]
  node_billing["Entitlements<br/>billing service<br/>[entitlements.ts]"]
  node_admin["Admin governance<br/>admin domain<br/>[governance.ts]"]
end

subgraph group_data["Data Platform"]
  node_supabase_client["Supabase access<br/>server data client<br/>[server.ts]"]
  node_database_types["Typed database contract<br/>TypeScript bindings<br/>[database.types.ts]"]
  node_schema[("Supabase schema &amp; RLS<br/>Postgres migrations")]
end

node_public -->|"signup and login"| node_auth_actions
node_auth_actions -->|"auth operations"| node_supabase_client
node_oauth_callback -->|"OAuth session"| node_supabase_client
node_proxy -->|"enforces access"| node_route_protection
node_route_protection -->|"permits authenticated routes"| node_app_shell
node_app_shell -->|"hosts"| node_dashboard
node_dashboard -->|"mutations"| node_profile_actions
node_profile_actions -->|"orchestrates"| node_social_services
node_group_page -->|"social state"| node_social_services
node_social_services -->|"persists"| node_supabase_client
node_location_update -->|"raw location"| node_proximity_backend
node_proximity_backend -->|"derives coarse state"| node_nearby_service
node_nearby_api -->|"safe results"| node_nearby_service
node_nearby_api -->|"glow state"| node_glow_avatar
node_proximity_backend -->|"stores protected location state"| node_supabase_client
node_github_schedule -->|"scheduled tick"| node_cron_tick
node_cron_tick -->|"dispatches"| node_job_worker
node_job_worker -->|"notification jobs"| node_notifications
node_paystack_webhook -->|"subscription events"| node_billing
node_billing -->|"syncs entitlements"| node_supabase_client
node_admin -->|"governance data"| node_supabase_client
node_supabase_client -->|"typed queries"| node_database_types
node_database_types -->|"matches schema"| node_schema
node_job_worker -->|"job events"| node_schema

click node_public "https://github.com/goasante/mad-buddy/blob/main/app/page.tsx"
click node_auth_actions "https://github.com/goasante/mad-buddy/blob/main/app/(auth)/actions.ts"
click node_oauth_callback "https://github.com/goasante/mad-buddy/blob/main/app/auth/callback/route.ts"
click node_route_protection "https://github.com/goasante/mad-buddy/blob/main/lib/security/route-protection.ts"
click node_proxy "https://github.com/goasante/mad-buddy/blob/main/proxy.ts"
click node_app_shell "https://github.com/goasante/mad-buddy/blob/main/app/(app)/layout.tsx"
click node_dashboard "https://github.com/goasante/mad-buddy/blob/main/app/(app)/dashboard/page.tsx"
click node_profile_actions "https://github.com/goasante/mad-buddy/blob/main/app/(app)/profile-actions.ts"
click node_social_services "https://github.com/goasante/mad-buddy/blob/main/lib/social/plans.ts"
click node_group_page "https://github.com/goasante/mad-buddy/blob/main/app/(app)/groups/%5Bid%5D/page.tsx"
click node_location_update "https://github.com/goasante/mad-buddy/blob/main/app/api/location/update/route.ts"
click node_nearby_api "https://github.com/goasante/mad-buddy/blob/main/app/api/friends/nearby/route.ts"
click node_proximity_backend "https://github.com/goasante/mad-buddy/blob/main/lib/proximity/backend.ts"
click node_nearby_service "https://github.com/goasante/mad-buddy/blob/main/lib/proximity/nearby-service.ts"
click node_glow_avatar "https://github.com/goasante/mad-buddy/blob/main/components/glow/glow-avatar.tsx"
click node_notifications "https://github.com/goasante/mad-buddy/blob/main/lib/notifications/server.ts"
click node_cron_tick "https://github.com/goasante/mad-buddy/blob/main/app/api/cron/tick/route.ts"
click node_job_worker "https://github.com/goasante/mad-buddy/blob/main/lib/jobs/worker.ts"
click node_github_schedule "https://github.com/goasante/mad-buddy/blob/main/.github/workflows/cron-tick.yml"
click node_paystack_webhook "https://github.com/goasante/mad-buddy/blob/main/app/api/paystack/webhook/route.ts"
click node_billing "https://github.com/goasante/mad-buddy/blob/main/lib/billing/entitlements.ts"
click node_admin "https://github.com/goasante/mad-buddy/blob/main/lib/admin/governance.ts"
click node_supabase_client "https://github.com/goasante/mad-buddy/blob/main/lib/supabase/server.ts"
click node_database_types "https://github.com/goasante/mad-buddy/blob/main/lib/supabase/database.types.ts"
click node_schema "https://github.com/goasante/mad-buddy/blob/main/supabase/migrations/20260709100000_initial_schema.sql"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_public,node_auth_actions,node_oauth_callback,node_route_protection,node_proxy toneBlue
class node_app_shell,node_dashboard,node_profile_actions,node_social_services,node_group_page toneAmber
class node_location_update,node_nearby_api,node_proximity_backend,node_nearby_service,node_glow_avatar toneMint
class node_notifications,node_cron_tick,node_job_worker,node_github_schedule,node_paystack_webhook,node_billing,node_admin toneRose
class node_supabase_client,node_database_types,node_schema toneIndigo
```

## Components

### Identity & Access

- [Public entry](https://github.com/goasante/mad-buddy/blob/main/app/page.tsx): Next.js page · Primary public landing experience. · `app/page.tsx`
- [Auth actions](https://github.com/goasante/mad-buddy/blob/main/app/(auth)/actions.ts): server actions · `app/(auth)/actions.ts`
- [OAuth callback](https://github.com/goasante/mad-buddy/blob/main/app/auth/callback/route.ts): route handler · `app/auth/callback/route.ts`
- [Route protection](https://github.com/goasante/mad-buddy/blob/main/lib/security/route-protection.ts): security middleware · Centralizes protected-route access checks. · `lib/security/route-protection.ts`
- [Request proxy](https://github.com/goasante/mad-buddy/blob/main/proxy.ts): Next.js proxy · `proxy.ts`

### Social Application

- [Authenticated shell](https://github.com/goasante/mad-buddy/blob/main/app/(app)/layout.tsx): application layout · `app/(app)/layout.tsx`
- [Dashboard](https://github.com/goasante/mad-buddy/blob/main/app/(app)/dashboard/page.tsx): application page · Primary post-login experience. · `app/(app)/dashboard/page.tsx`
- [Profile actions](https://github.com/goasante/mad-buddy/blob/main/app/(app)/profile-actions.ts): server actions · `app/(app)/profile-actions.ts`
- [Social domain services](https://github.com/goasante/mad-buddy/blob/main/lib/social/plans.ts): service layer · Plans, events, messaging, and content rules. · `lib/social/plans.ts`
- [Group page](https://github.com/goasante/mad-buddy/blob/main/app/(app)/groups/%5Bid%5D/page.tsx): application page · `app/(app)/groups/[id]/page.tsx`

### Privacy-Safe Proximity

- [Location update](https://github.com/goasante/mad-buddy/blob/main/app/api/location/update/route.ts): API route · Accepts client location updates. · `app/api/location/update/route.ts`
- [Nearby signals API](https://github.com/goasante/mad-buddy/blob/main/app/api/friends/nearby/route.ts): API route · Returns privacy-safe nearby state only. · `app/api/friends/nearby/route.ts`
- [Proximity backend](https://github.com/goasante/mad-buddy/blob/main/lib/proximity/backend.ts): privacy boundary · Derives coarse signals without exposing raw coordinates. · `lib/proximity/backend.ts`
- [Nearby service](https://github.com/goasante/mad-buddy/blob/main/lib/proximity/nearby-service.ts): service layer · Computes nearby and freshness state. · `lib/proximity/nearby-service.ts`
- [Glow indicator](https://github.com/goasante/mad-buddy/blob/main/components/glow/glow-avatar.tsx): UI component · Presents coarse social proximity. · `components/glow/glow-avatar.tsx`

### Operations & Revenue

- [Notifications](https://github.com/goasante/mad-buddy/blob/main/lib/notifications/server.ts): notification service · In-app and push notification delivery. · `lib/notifications/server.ts`
- [Cron tick](https://github.com/goasante/mad-buddy/blob/main/app/api/cron/tick/route.ts): API route · `app/api/cron/tick/route.ts`
- [Job worker](https://github.com/goasante/mad-buddy/blob/main/lib/jobs/worker.ts): background worker · Runs idempotent scheduled work. · `lib/jobs/worker.ts`
- [GitHub schedule](https://github.com/goasante/mad-buddy/blob/main/.github/workflows/cron-tick.yml): workflow · Triggers the cron tick. · `.github/workflows/cron-tick.yml`
- [Paystack webhook](https://github.com/goasante/mad-buddy/blob/main/app/api/paystack/webhook/route.ts): API route · Receives signed billing events. · `app/api/paystack/webhook/route.ts`
- [Entitlements](https://github.com/goasante/mad-buddy/blob/main/lib/billing/entitlements.ts): billing service · Synchronizes subscriptions and gates premium access. · `lib/billing/entitlements.ts`
- [Admin governance](https://github.com/goasante/mad-buddy/blob/main/lib/admin/governance.ts): admin domain · Governance, enforcement, audit, and billing controls. · `lib/admin/governance.ts`

### Data Platform

- [Supabase access](https://github.com/goasante/mad-buddy/blob/main/lib/supabase/server.ts): server data client · Request-scoped access to Supabase Auth and Postgres. · `lib/supabase/server.ts`
- [Typed database contract](https://github.com/goasante/mad-buddy/blob/main/lib/supabase/database.types.ts): TypeScript bindings · `lib/supabase/database.types.ts`
- [Supabase schema & RLS](https://github.com/goasante/mad-buddy/blob/main/supabase/migrations/20260709100000_initial_schema.sql): Postgres migrations · Durable contract for social, privacy, jobs, and realtime data. · `supabase/migrations/20260709100000_initial_schema.sql`

## Connections

- Public entry → Auth actions: signup and login
- Auth actions → Supabase access: auth operations
- OAuth callback → Supabase access: OAuth session
- Request proxy → Route protection: enforces access
- Route protection → Authenticated shell: permits authenticated routes
- Authenticated shell → Dashboard: hosts
- Dashboard → Profile actions: mutations
- Profile actions → Social domain services: orchestrates
- Group page → Social domain services: social state
- Social domain services → Supabase access: persists
- Location update → Proximity backend: raw location
- Proximity backend → Nearby service: derives coarse state
- Nearby signals API → Nearby service: safe results
- Nearby signals API → Glow indicator: glow state
- Proximity backend → Supabase access: stores protected location state
- GitHub schedule → Cron tick: scheduled tick
- Cron tick → Job worker: dispatches
- Job worker → Notifications: notification jobs
- Paystack webhook → Entitlements: subscription events
- Entitlements → Supabase access: syncs entitlements
- Admin governance → Supabase access: governance data
- Supabase access → Typed database contract: typed queries
- Typed database contract → Supabase schema & RLS: matches schema
- Job worker → Supabase schema & RLS: job events
