# letsgettechnical/elecretanta architecture

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

- Interactive diagram: https://gitdiagram.com/letsgettechnical/elecretanta
- Repository: https://github.com/letsgettechnical/elecretanta

## Overview

1. **High-level shape**
   - This is a **Next.js App Router** application with a mix of server-rendered pages and route handlers under `app/`.
   - The product domain is a **Secret Santa / gift exchange platform** centered on user profiles, exchange groups, recipient matching, and AI-generated gift ideas.

2. **Primary user-facing surfaces**
   - Public/auth pages live under `app/auth`, `app/page.tsx`, and `app/gift-exchanges/[id]`-style routes.
   - Core authenticated flows include:
     - onboarding/profile completion
     - creating or joining a group
     - viewing an exchange dashboard
     - editing an exchange
     - waiting for matching/suggestions
     - reviewing profile and gift exchange details
   - The route structure suggests a strongly page-driven app rather than a single SPA shell.

3. **Authentication and session boundary**
   - Auth is built around **Supabase Auth** with a Google login flow.
   - `app/auth/callback/route.ts` handles the OAuth callback.
   - `context/AuthContextProvider.tsx` and `lib/supabase/{client,server,middleware}.ts` indicate a split between:
     - browser client access
     - server-side session-aware access
     - middleware enforcement for protected routes
   - `middleware.ts` is the central boundary for guarding authenticated pages and keeping sessions in sync.

4. **Domain model and persistence**
   - The repository defines explicit types in `app/types/` for the main entities:
     - `profile`
     - `giftExchange`
     - `giftExchangeMember`
     - `giftSuggestion`
   - These types map directly to the core architecture: users have profiles, profiles belong to exchange membership records, exchanges hold budget and state, and suggestions are generated per recipient/exchange.

5. **Gift exchange lifecycle**
   - Exchange CRUD is exposed through `app/api/gift-exchanges/route.ts` and nested routes under `app/api/gift-exchanges/[id]/`.
   - Important sub-flows:
     - manage exchange metadata
     - manage members via `/members`
     - trigger a draw via `/draw`
     - fetch suggestions for an exchange via `/giftSuggestions`
   - The page components under `app/gift-exchanges/[id]/` mirror those backend capabilities and likely act as the main orchestration layer for the exchange lifecycle.

6. **AI gift suggestion pipeline**
   - AI is not just a UI feature; it has a dedicated backend path:
     - `lib/generateAndStoreSuggestions.ts`
     - `lib/generateAndUpdateNewGiftSuggestion.ts`
     - `app/api/giftSuggestions/route.ts`
     - `app/api/gift-exchanges/[id]/giftSuggestions/route.ts`
   - The README and library names indicate suggestions are computed from:
     - recipient profile data
     - interests/hobbies
     - gift preferences
     - budget constraints
     - “avoid” items
   - `app/api/openaiConfig/config.ts` and `OPENAI_API_KEY` show OpenAI as the core external inference service.
   - There is also a `lib/getAmazonImage.ts`, suggesting enrichment of suggestions with product imagery or search results.

7. **Background/cron processing**
   - The repo includes a dedicated cron subsystem under `app/api/cron/`.
   - It is split into small functions:
     - `checkAuthorization`
     - `fetchGiftExchanges`
     - `processGiftExchanges`
   - This implies a scheduled job flow where Vercel or an external scheduler hits `/api/cron`, the request is authorized, active exchanges are fetched, and pending exchanges are processed in batch.
   - This is the main asynchronous backend path separate from user-triggered interactions.

8. **Profile and media handling**
   - Profile management has both page and API layers:
     - `app/profile/[id]/page.tsx`
     - `app/api/profile/route.ts`
     - `app/api/profile/[id]/route.ts`
   - `app/api/getUserAvatar/route.ts` and `lib/getUserAvatar.ts` indicate external avatar retrieval or profile-image lookup.
   - Onboarding includes a dedicated form flow under `app/onboarding/onboarding-form`, and the README notes users can provide preferences via **text or image**, so profile capture is part structured form, part media-assisted input.

9. **UI composition and design system**
   - The `components/` directory is a broad reusable component system, heavily aligned with **shadcn/ui / Radix-style primitives** and Tailwind styling.
   - Higher-level domain components exist alongside primitives:
     - `GiftExchangeHeader`
     - `GroupCard`
     - `InviteCard`
     - `MembersList`
     - `GiftSuggestionCard`
     - `CompletedExchangeCard`
     - `ProfileCard`
   - This separation suggests the UI is composed from a shared primitive layer plus domain-specific screens rather than page-local ad hoc components.

10. **State, feedback, and client-side UX**
   - There is local client state support via `hooks/useLocalStorage.ts` and a toast system under `components/Toast*`, `components/Toaster`, and `hooks/use-toast.ts`.
   - Loading and waiting states are explicitly modeled with components like:
     - `LoadingCard`
     - `LoadingSkeleton`
     - `LoadingSpinner`
     - `GlobalGiftSpinner`
     - `WaitingForSuggestions`
   - This matters architecturally because the exchange/suggestion flow appears asynchronous and the UI is built to handle in-progress states cleanly.

11. **Infrastructure and runtime dependencies**
   - **Supabase** is the persistence/auth backbone.
   - **OpenAI** provides AI suggestion generation.
   - **Google** credentials in the README suggest external Google services are used for login and/or image/search-related functionality.
   - The app is designed for **Vercel** deployment, reinforced by `vercel.json` and the cron route pattern.

12. **Quality gates and development tooling**
   - The repo uses **Jest** for unit/component tests and **Playwright** for browser automation.
   - **Storybook** is present for isolated UI development.
   - ESLint, Prettier, Tailwind, and TypeScript define the front-end engineering baseline.
   - This indicates the project is component-heavy and UI correctness is a major concern, alongside domain logic in the gift-exchange backend.

13. **Key architectural boundaries to draw**
   - A good diagram should separate:
     - browser/UI pages and reusable components
     - auth/session middleware
     - Supabase-backed data access
     - AI suggestion generation
     - scheduled cron processing
   - The main data flow is: user auth → profile onboarding → exchange creation/joining → draw/matching → suggestion generation → recipient/member views, with cron jobs handling deferred processing in the background.

Main entry points: [`lib/generateAndStoreSuggestions.ts`](https://github.com/letsgettechnical/elecretanta/blob/develop/lib/generateAndStoreSuggestions.ts) (Suggestion Engine), [`app/api/gift-exchanges/[id]/route.ts`](https://github.com/letsgettechnical/elecretanta/blob/develop/app/api/gift-exchanges/[id]/route.ts) (Exchange Actions), [`lib/supabase/server.ts`](https://github.com/letsgettechnical/elecretanta/blob/develop/lib/supabase/server.ts) (Supabase Server), [`app/api/cron/route.ts`](https://github.com/letsgettechnical/elecretanta/blob/develop/app/api/cron/route.ts) (Cron Job)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_g_public["Public & Auth"]
  node_n_home["Home<br/>landing page<br/>[page.tsx]"]
  node_n_login["Login<br/>auth page<br/>[page.tsx]"]
  node_n_callback["OAuth Callback<br/>auth route<br/>[route.ts]"]
end

subgraph group_g_core["Core App"]
  node_n_onboarding["Onboarding<br/>profile setup<br/>[page.tsx]"]
  node_n_dashboard["Dashboard<br/>overview page<br/>[page.tsx]"]
  node_n_create_group["Create Group<br/>group page<br/>[page.tsx]"]
  node_n_exchange_page["Exchange View<br/>exchange page<br/>[page.tsx]"]
  node_n_exchange_edit["Edit Exchange<br/>exchange edit page<br/>[page.tsx]"]
  node_n_profile_page["Profile View<br/>profile page<br/>[page.tsx]"]
end

subgraph group_g_api["API Backend"]
  node_n_gift_api["Exchange API<br/>exchange routes<br/>[route.ts]"]
  node_n_exchange_subapi["Exchange Actions<br/>exchange subroutes<br/>[route.ts]"]
  node_n_profile_api["Profile API<br/>profile routes<br/>[route.ts]"]
  node_n_gift_suggestions_api["Suggestions API<br/>suggestion routes<br/>[route.ts]"]
  node_n_cron_api["Cron Job<br/>scheduled processing<br/>[route.ts]"]
end

subgraph group_g_services["Services & Data"]
  node_n_supabase_client[("Supabase Client<br/>db client<br/>[client.ts]")]
  node_n_supabase_server[("Supabase Server<br/>server db<br/>[server.ts]")]
  node_n_middleware{{"Route Guard<br/>middleware<br/>[middleware.ts]"}}
  node_n_suggestion_engine{{"Suggestion Engine<br/>AI pipeline"}}
  node_n_draw_engine{{"Draw Engine<br/>matching logic"}}
  node_n_ai_config["OpenAI Config<br/>[config.ts]"]
  node_n_avatar["Avatar Lookup<br/>image helper<br/>[getUserAvatar.ts]"]
  node_n_amazon["Amazon Image<br/>enrichment helper<br/>[getAmazonImage.ts]"]
end

subgraph group_g_ui["Shared UI"]
  node_n_auth_ctx["Auth Context<br/>client session"]
  node_n_components["Shared UI<br/>component library<br/>[button.tsx]"]
  node_n_toasts["Toast System<br/>feedback state<br/>[Toaster.tsx]"]
end

node_n_home -->|"start auth"| node_n_login
node_n_login -->|"oauth"| node_n_callback
node_n_callback -->|"session"| node_n_auth_ctx
node_n_middleware -->|"protect"| node_n_dashboard
node_n_middleware -->|"protect"| node_n_onboarding
node_n_auth_ctx -->|"client auth"| node_n_supabase_client
node_n_dashboard -->|"create"| node_n_create_group
node_n_dashboard -->|"open"| node_n_exchange_page
node_n_onboarding -->|"save profile"| node_n_profile_api
node_n_profile_page -->|"load profile"| node_n_profile_api
node_n_create_group -->|"create exchange"| node_n_gift_api
node_n_exchange_page -->|"manage exchange"| node_n_exchange_subapi
node_n_exchange_edit -->|"edit members"| node_n_exchange_subapi
node_n_exchange_subapi -->|"draw"| node_n_draw_engine
node_n_exchange_subapi -->|"generate"| node_n_suggestion_engine
node_n_gift_suggestions_api -->|"compute"| node_n_suggestion_engine
node_n_suggestion_engine -->|"openai"| node_n_ai_config
node_n_suggestion_engine -->|"enrich"| node_n_amazon
node_n_cron_api -->|"batch read/write"| node_n_supabase_server
node_n_cron_api -->|"defer work"| node_n_suggestion_engine
node_n_cron_api -->|"process draws"| node_n_draw_engine
node_n_gift_api -->|"persist"| node_n_supabase_server
node_n_profile_api -->|"persist"| node_n_supabase_server
node_n_exchange_subapi -->|"persist"| node_n_supabase_server
node_n_suggestion_engine -->|"store"| node_n_supabase_server
node_n_components -->|"compose ui"| node_n_exchange_page
node_n_components -->|"compose ui"| node_n_onboarding
node_n_toasts -->|"feedback"| node_n_components
node_n_avatar -->|"images"| node_n_profile_page

click node_n_home "https://github.com/letsgettechnical/elecretanta/blob/develop/app/page.tsx"
click node_n_login "https://github.com/letsgettechnical/elecretanta/blob/develop/app/auth/login/page.tsx"
click node_n_callback "https://github.com/letsgettechnical/elecretanta/blob/develop/app/auth/callback/route.ts"
click node_n_onboarding "https://github.com/letsgettechnical/elecretanta/blob/develop/app/onboarding/page.tsx"
click node_n_dashboard "https://github.com/letsgettechnical/elecretanta/blob/develop/app/dashboard/page.tsx"
click node_n_create_group "https://github.com/letsgettechnical/elecretanta/blob/develop/app/create-group-page/page.tsx"
click node_n_exchange_page "https://github.com/letsgettechnical/elecretanta/blob/develop/app/gift-exchanges/[id]/page.tsx"
click node_n_exchange_edit "https://github.com/letsgettechnical/elecretanta/blob/develop/app/gift-exchanges/[id]/edit/page.tsx"
click node_n_profile_page "https://github.com/letsgettechnical/elecretanta/blob/develop/app/profile/[id]/page.tsx"
click node_n_auth_ctx "https://github.com/letsgettechnical/elecretanta/blob/develop/context/AuthContextProvider.tsx"
click node_n_supabase_client "https://github.com/letsgettechnical/elecretanta/blob/develop/lib/supabase/client.ts"
click node_n_supabase_server "https://github.com/letsgettechnical/elecretanta/blob/develop/lib/supabase/server.ts"
click node_n_middleware "https://github.com/letsgettechnical/elecretanta/blob/develop/middleware.ts"
click node_n_gift_api "https://github.com/letsgettechnical/elecretanta/blob/develop/app/api/gift-exchanges/route.ts"
click node_n_exchange_subapi "https://github.com/letsgettechnical/elecretanta/blob/develop/app/api/gift-exchanges/[id]/route.ts"
click node_n_profile_api "https://github.com/letsgettechnical/elecretanta/blob/develop/app/api/profile/route.ts"
click node_n_gift_suggestions_api "https://github.com/letsgettechnical/elecretanta/blob/develop/app/api/giftSuggestions/route.ts"
click node_n_cron_api "https://github.com/letsgettechnical/elecretanta/blob/develop/app/api/cron/route.ts"
click node_n_suggestion_engine "https://github.com/letsgettechnical/elecretanta/blob/develop/lib/generateAndStoreSuggestions.ts"
click node_n_draw_engine "https://github.com/letsgettechnical/elecretanta/blob/develop/lib/drawGiftExchange.ts"
click node_n_ai_config "https://github.com/letsgettechnical/elecretanta/blob/develop/app/api/openaiConfig/config.ts"
click node_n_avatar "https://github.com/letsgettechnical/elecretanta/blob/develop/lib/getUserAvatar.ts"
click node_n_amazon "https://github.com/letsgettechnical/elecretanta/blob/develop/lib/getAmazonImage.ts"
click node_n_components "https://github.com/letsgettechnical/elecretanta/blob/develop/components/Button/button.tsx"
click node_n_toasts "https://github.com/letsgettechnical/elecretanta/blob/develop/components/Toaster/Toaster.tsx"

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_n_home,node_n_login,node_n_callback toneBlue
class node_n_onboarding,node_n_dashboard,node_n_create_group,node_n_exchange_page,node_n_exchange_edit,node_n_profile_page toneAmber
class node_n_gift_api,node_n_exchange_subapi,node_n_profile_api,node_n_gift_suggestions_api,node_n_cron_api toneMint
class node_n_supabase_client,node_n_supabase_server,node_n_middleware,node_n_suggestion_engine,node_n_draw_engine,node_n_ai_config,node_n_avatar,node_n_amazon toneRose
class node_n_auth_ctx,node_n_components,node_n_toasts toneIndigo
```

## Components

### Public & Auth

Entry pages and authentication flow.

- [Home](https://github.com/letsgettechnical/elecretanta/blob/develop/app/page.tsx): landing page · App entry and public landing route. · `app/page.tsx`
- [Login](https://github.com/letsgettechnical/elecretanta/blob/develop/app/auth/login/page.tsx): auth page · Sign-in surface for authenticated access. · `app/auth/login/page.tsx`
- [OAuth Callback](https://github.com/letsgettechnical/elecretanta/blob/develop/app/auth/callback/route.ts): auth route · Supabase Google OAuth callback handler. · `app/auth/callback/route.ts`

### Core App

Authenticated product surfaces for onboarding, dashboard, profiles, and exchanges.

- [Onboarding](https://github.com/letsgettechnical/elecretanta/blob/develop/app/onboarding/page.tsx): profile setup · Profile completion and preference capture flow. · `app/onboarding/page.tsx`
- [Dashboard](https://github.com/letsgettechnical/elecretanta/blob/develop/app/dashboard/page.tsx): overview page · Authenticated landing area for current exchanges and actions. · `app/dashboard/page.tsx`
- [Create Group](https://github.com/letsgettechnical/elecretanta/blob/develop/app/create-group-page/page.tsx): group page · Flow for creating a new gift exchange group. · `app/create-group-page/page.tsx`
- [Exchange View](https://github.com/letsgettechnical/elecretanta/blob/develop/app/gift-exchanges/[id]/page.tsx): exchange page · Main gift exchange detail and orchestration page. · `app/gift-exchanges/[id]/page.tsx`
- [Edit Exchange](https://github.com/letsgettechnical/elecretanta/blob/develop/app/gift-exchanges/[id]/edit/page.tsx): exchange edit page · Editing surface for exchange metadata and members. · `app/gift-exchanges/[id]/edit/page.tsx`
- [Profile View](https://github.com/letsgettechnical/elecretanta/blob/develop/app/profile/[id]/page.tsx): profile page · Public or member profile view by id. · `app/profile/[id]/page.tsx`

### API Backend

Route handlers for persistence, exchange orchestration, and async processing.

- [Exchange API](https://github.com/letsgettechnical/elecretanta/blob/develop/app/api/gift-exchanges/route.ts): exchange routes · CRUD and member management for gift exchanges. · `app/api/gift-exchanges/route.ts`
- [Exchange Actions](https://github.com/letsgettechnical/elecretanta/blob/develop/app/api/gift-exchanges/[id]/route.ts): exchange subroutes · Per-exchange draw, member, and suggestion route handlers. · `app/api/gift-exchanges/[id]/route.ts`
- [Profile API](https://github.com/letsgettechnical/elecretanta/blob/develop/app/api/profile/route.ts): profile routes · Profile lookup and update handlers. · `app/api/profile/route.ts`
- [Suggestions API](https://github.com/letsgettechnical/elecretanta/blob/develop/app/api/giftSuggestions/route.ts): suggestion routes · Endpoints for generating and reading gift suggestions. · `app/api/giftSuggestions/route.ts`
- [Cron Job](https://github.com/letsgettechnical/elecretanta/blob/develop/app/api/cron/route.ts): scheduled processing · Authorized batch processor for active exchanges. · `app/api/cron/route.ts`

### Services & Data

Supabase, AI, and other server-side helpers.

- [Supabase Client](https://github.com/letsgettechnical/elecretanta/blob/develop/lib/supabase/client.ts): db client · Browser-side Supabase access layer. · `lib/supabase/client.ts`
- [Supabase Server](https://github.com/letsgettechnical/elecretanta/blob/develop/lib/supabase/server.ts): server db · Server-side Supabase access for pages and routes. · `lib/supabase/server.ts`
- [Route Guard](https://github.com/letsgettechnical/elecretanta/blob/develop/middleware.ts): middleware · Session sync and protection boundary for app routes. · `middleware.ts`
- [Suggestion Engine](https://github.com/letsgettechnical/elecretanta/blob/develop/lib/generateAndStoreSuggestions.ts): AI pipeline · Generates and stores AI gift suggestions. · `lib/generateAndStoreSuggestions.ts`
- [Draw Engine](https://github.com/letsgettechnical/elecretanta/blob/develop/lib/drawGiftExchange.ts): matching logic · Executes recipient assignment for an exchange. · `lib/drawGiftExchange.ts`
- [OpenAI Config](https://github.com/letsgettechnical/elecretanta/blob/develop/app/api/openaiConfig/config.ts): ai config · OpenAI API configuration used by suggestion generation. · `app/api/openaiConfig/config.ts`
- [Avatar Lookup](https://github.com/letsgettechnical/elecretanta/blob/develop/lib/getUserAvatar.ts): image helper · Retrieves or resolves user avatar imagery. · `lib/getUserAvatar.ts`
- [Amazon Image](https://github.com/letsgettechnical/elecretanta/blob/develop/lib/getAmazonImage.ts): enrichment helper · Enriches gift ideas with product imagery. · `lib/getAmazonImage.ts`

### Shared UI

Reusable components and client-side state used across pages.

- [Auth Context](https://github.com/letsgettechnical/elecretanta/blob/develop/context/AuthContextProvider.tsx): client session · Browser session state and auth-aware client context. · `context/AuthContextProvider.tsx`
- [Shared UI](https://github.com/letsgettechnical/elecretanta/blob/develop/components/Button/button.tsx): component library · Reusable primitives and domain-specific UI building blocks. · `components/Button/button.tsx`
- [Toast System](https://github.com/letsgettechnical/elecretanta/blob/develop/components/Toaster/Toaster.tsx): feedback state · Global notification and feedback UI state. · `components/Toaster/Toaster.tsx`

## Connections

- Home → Login: start auth · Public users move into the login flow.
- Login → OAuth Callback: oauth · Google/Supabase callback completes sign-in.
- OAuth Callback → Auth Context: session · Established session is consumed by the client auth context.
- Route Guard → Dashboard: protect · Guards authenticated pages and session state.
- Route Guard → Onboarding: protect · Allows or redirects onboarding based on session state.
- Auth Context → Supabase Client: client auth · Browser context wraps client-side Supabase usage.
- Dashboard → Create Group: create · Dashboard links into new exchange creation.
- Dashboard → Exchange View: open · Dashboard navigates to existing exchanges.
- Onboarding → Profile API: save profile · Profile completion persists through profile endpoints.
- Profile View → Profile API: load profile · Profile views fetch profile data from the API.
- Create Group → Exchange API: create exchange · Group creation persists as a gift exchange record.
- Exchange View → Exchange Actions: manage exchange · Exchange detail page drives draw, members, and suggestions.
- Edit Exchange → Exchange Actions: edit members · Edit screen updates exchange state and membership.
- Exchange Actions → Draw Engine: draw · Recipient assignment uses matching logic.
- Exchange Actions → Suggestion Engine: generate · Exchange-specific suggestions are computed on demand.
- Suggestions API → Suggestion Engine: compute · Standalone suggestion endpoints invoke the AI pipeline.
- Suggestion Engine → OpenAI Config: openai · Pipeline is configured by OpenAI credentials and settings.
- Suggestion Engine → Amazon Image: enrich · Gift ideas may be enriched with external product imagery.
- Cron Job → Supabase Server: batch read/write · Cron processing uses server-side data access.
- Cron Job → Suggestion Engine: defer work · Scheduled jobs process pending exchanges and suggestion generation.
- Cron Job → Draw Engine: process draws · Background processing can finalize exchange matching.
- Exchange API → Supabase Server: persist · Exchange CRUD uses server-side Supabase access.
- Profile API → Supabase Server: persist · Profile handlers use server-side Supabase access.
- Exchange Actions → Supabase Server: persist · Per-exchange actions read and write through Supabase.
- Suggestion Engine → Supabase Server: store · Generated suggestions are written back to persistence.
- Shared UI → Exchange View: compose ui · Shared components form the main exchange screens.
- Shared UI → Onboarding: compose ui · Reusable UI powers onboarding forms and selectors.
- Toast System → Shared UI: feedback · Toast primitives are used across the shared component layer.
- Avatar Lookup → Profile View: images · Profile screens can resolve user avatars.
