# izecrypt/fashionton-backend architecture

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

- Interactive diagram: https://gitdiagram.com/izecrypt/fashionton-backend
- Repository: https://github.com/izecrypt/fashionton-backend

## Overview

#### Purpose and Deployment Shape
FashionTON Wardrobe is a Telegram Mini App for managing clothing, assembling outfits, entering social style challenges, and receiving size recommendations. The active deployment appears to be a static JavaScript frontend plus Vercel serverless APIs, configured through `vercel.json`. A separate Express backend deployment option exists under `backend/server.js`, with Railway/Render deployment descriptors.

#### Frontend Application Shell
The client is vanilla HTML/CSS/JavaScript rather than a framework application. `src/index.html` is the source shell; `public/index.html` and `public/` appear to be the deployable built/copy artifact. Application startup, state, and API integration are separated across:
- `src/js/app.js`
- `src/js/store.js`
- `src/js/api-client.js`

The browser client loads screens and shared UI behavior, then calls same-origin `/api/*` endpoints. Telegram embedding and TON wallet integration are product concerns described by the README, though no TON-specific source file is present in this tree.

#### User-Facing Feature Screens
Feature modules divide the primary user journeys:
- Wardrobe browsing and garment organization: `src/screens/wardrobe.js`, `src/components/image-upload.js`
- Outfit composition/try-on-facing experience: `src/screens/outfits.js`, `src/components/item-modal.js`
- Challenge viewing and participation: `src/screens/challenges.js`

These screens consume centralized client state and the API client rather than directly owning persistence. Shared visual system assets are in `src/styles/main.css` and `src/styles/glass.css`.

#### Serverless API Boundary
The current `api/` directory is a flattened Vercel-function surface, with common infrastructure modules:
- Authentication/request identity: `api/_auth.js`
- Database access: `api/_db.js`
- Cloudinary media integration: `api/_cloudinary.js`

Primary handlers are `api/wardrobe.js`, `api/challenges.js`, `api/user.js`, `api/leaderboard.js`, and `api/size-calculator.js`. These functions form the trust boundary: browser requests should be authenticated, data-scoped to the Telegram user, and then persisted or sent to external services.

#### Wardrobe and Media Flow
Users select/upload clothing through the UI component, submit wardrobe mutations to `api/wardrobe.js`, and retrieve the resulting wardrobe for screen rendering. Server-side media handling is intended to use Cloudinary via `api/_cloudinary.js`; database records are managed behind `api/_db.js`. The older, more granular implementation retains an explicit upload-signature endpoint at `api-backup/upload/signature.js`, which suggests direct client-to-Cloudinary upload support may have existed before API consolidation.

#### Challenge and Gamification Flow
Challenge activity is currently consolidated into `api/challenges.js`; its backup counterpart shows the intended stages: challenge lookup, entry submission, voting, and winner selection:
- `api-backup/challenges/index.js`
- `api-backup/challenges/entry.js`
- `api-backup/challenges/vote.js`

Leaderboard data is exposed separately through `api/leaderboard.js`. A scheduled daily challenge/winner process is represented only in the backup structure at `api-backup/cron/challenge.js`; production scheduling must be explicitly restored/configured if automatic challenge lifecycle processing is required.

#### Data, Identity, and External Services
Material infrastructure is Vercel serverless hosting, a database abstracted by `api/_db.js`, and Cloudinary for garment/challenge images. Telegram supplies the Mini App runtime and likely user identity, validated by `api/_auth.js`. TON Connect and TON payments are product-level integrations described in the README, but no current API payment handler or blockchain implementation is present in `api/`; treat payment verification/reward distribution as a missing or external subsystem.

#### Parallel/Legacy Implementations
`api-backup/` contains a fuller route-oriented API and should be treated as a reference or migration source, not assumed live. `backend/` is an alternate Node/Express hosting path with its own package manifest and deployment setup. Maintaining both Vercel functions and Express routes risks behavioral drift; select one authoritative backend surface before expanding features.

Main entry points: [`src/js/app.js`](https://github.com/izecrypt/fashionton-backend/blob/main/src/js/app.js) (App startup), [`api/_db.js`](https://github.com/izecrypt/fashionton-backend/blob/main/api/_db.js) (Database access), [`api/_auth.js`](https://github.com/izecrypt/fashionton-backend/blob/main/api/_auth.js) (Request authentication), [`src/js/api-client.js`](https://github.com/izecrypt/fashionton-backend/blob/main/src/js/api-client.js) (API client)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_client["Mini App Client"]
  node_client_shell["Client shell<br/>vanilla JS app<br/>[index.html]"]
  node_client_app["App startup<br/>application state<br/>[app.js]"]
  node_client_store["Client store<br/>state management<br/>[store.js]"]
  node_api_client["API client<br/>same-origin HTTP<br/>[api-client.js]"]
  node_wardrobe_ui["Wardrobe and upload<br/>feature screen<br/>[wardrobe.js]"]
  node_outfits_ui["Outfit experience<br/>feature screen<br/>[outfits.js]"]
  node_challenges_ui["Challenges<br/>feature screen<br/>[challenges.js]"]
end

subgraph group_serverless["Vercel API"]
  node_vercel{{"Vercel deployment<br/>static host and functions<br/>[vercel.json]"}}
  node_auth["Request authentication<br/>identity boundary<br/>[_auth.js]"]
  node_wardrobe_api["Wardrobe API<br/>serverless handler<br/>[wardrobe.js]"]
  node_challenges_api["Challenges API<br/>serverless handler<br/>[challenges.js]"]
  node_user_api["User API<br/>serverless handler<br/>[user.js]"]
  node_leaderboard_api["Leaderboard API<br/>serverless handler<br/>[leaderboard.js]"]
  node_size_api["Size calculator API<br/>serverless handler<br/>[size-calculator.js]"]
  node_database[("Database access<br/>persistence adapter<br/>[_db.js]")]
  node_cloudinary_adapter["Cloudinary adapter<br/>media integration<br/>[_cloudinary.js]"]
end

subgraph group_services["External Services"]
  node_telegram{{"Telegram Mini App<br/>identity runtime"}}
  node_cloudinary[("Cloudinary<br/>media storage")]
end

subgraph group_legacy["Alternate / Legacy"]
  node_backup_api["Route-oriented API backup<br/>migration reference<br/>[index.js]"]
  node_express_backend["Express backend option<br/>alternate deployment<br/>[server.js]"]
end

node_telegram -->|"embeds"| node_client_shell
node_client_shell -->|"starts"| node_client_app
node_client_app -->|"manages"| node_client_store
node_client_app -->|"loads"| node_wardrobe_ui
node_client_app -->|"loads"| node_outfits_ui
node_client_app -->|"loads"| node_challenges_ui
node_wardrobe_ui -->|"uses"| node_api_client
node_outfits_ui -->|"uses"| node_api_client
node_challenges_ui -->|"uses"| node_api_client
node_api_client -->|"/api requests"| node_vercel
node_vercel -->|"invokes"| node_auth
node_auth -->|"authorizes"| node_wardrobe_api
node_auth -->|"authorizes"| node_challenges_api
node_auth -->|"authorizes"| node_user_api
node_wardrobe_api -->|"stores garments"| node_database
node_wardrobe_api -->|"handles media"| node_cloudinary_adapter
node_challenges_api -->|"stores entries and votes"| node_database
node_user_api -->|"reads and writes users"| node_database
node_leaderboard_api -->|"reads rankings"| node_database
node_size_api -->|"uses user data"| node_database
node_cloudinary_adapter -->|"uploads media"| node_cloudinary
node_backup_api -.->|"migration reference"| node_challenges_api
node_express_backend -.->|"alternative to"| node_vercel

click node_client_shell "https://github.com/izecrypt/fashionton-backend/blob/main/src/index.html"
click node_client_app "https://github.com/izecrypt/fashionton-backend/blob/main/src/js/app.js"
click node_client_store "https://github.com/izecrypt/fashionton-backend/blob/main/src/js/store.js"
click node_api_client "https://github.com/izecrypt/fashionton-backend/blob/main/src/js/api-client.js"
click node_wardrobe_ui "https://github.com/izecrypt/fashionton-backend/blob/main/src/screens/wardrobe.js"
click node_outfits_ui "https://github.com/izecrypt/fashionton-backend/blob/main/src/screens/outfits.js"
click node_challenges_ui "https://github.com/izecrypt/fashionton-backend/blob/main/src/screens/challenges.js"
click node_vercel "https://github.com/izecrypt/fashionton-backend/blob/main/vercel.json"
click node_auth "https://github.com/izecrypt/fashionton-backend/blob/main/api/_auth.js"
click node_wardrobe_api "https://github.com/izecrypt/fashionton-backend/blob/main/api/wardrobe.js"
click node_challenges_api "https://github.com/izecrypt/fashionton-backend/blob/main/api/challenges.js"
click node_user_api "https://github.com/izecrypt/fashionton-backend/blob/main/api/user.js"
click node_leaderboard_api "https://github.com/izecrypt/fashionton-backend/blob/main/api/leaderboard.js"
click node_size_api "https://github.com/izecrypt/fashionton-backend/blob/main/api/size-calculator.js"
click node_database "https://github.com/izecrypt/fashionton-backend/blob/main/api/_db.js"
click node_cloudinary_adapter "https://github.com/izecrypt/fashionton-backend/blob/main/api/_cloudinary.js"
click node_backup_api "https://github.com/izecrypt/fashionton-backend/blob/main/api-backup/challenges/index.js"
click node_express_backend "https://github.com/izecrypt/fashionton-backend/blob/main/backend/server.js"

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_client_shell,node_client_app,node_client_store,node_api_client,node_wardrobe_ui,node_outfits_ui,node_challenges_ui toneBlue
class node_vercel,node_auth,node_wardrobe_api,node_challenges_api,node_user_api,node_leaderboard_api,node_size_api,node_database,node_cloudinary_adapter toneAmber
class node_telegram,node_cloudinary toneMint
class node_backup_api,node_express_backend toneRose
```

## Components

### Mini App Client

- [Client shell](https://github.com/izecrypt/fashionton-backend/blob/main/src/index.html): vanilla JS app · `src/index.html`
- [App startup](https://github.com/izecrypt/fashionton-backend/blob/main/src/js/app.js): application state · `src/js/app.js`
- [Client store](https://github.com/izecrypt/fashionton-backend/blob/main/src/js/store.js): state management · `src/js/store.js`
- [API client](https://github.com/izecrypt/fashionton-backend/blob/main/src/js/api-client.js): same-origin HTTP · `src/js/api-client.js`
- [Wardrobe and upload](https://github.com/izecrypt/fashionton-backend/blob/main/src/screens/wardrobe.js): feature screen · `src/screens/wardrobe.js`
- [Outfit experience](https://github.com/izecrypt/fashionton-backend/blob/main/src/screens/outfits.js): feature screen · `src/screens/outfits.js`
- [Challenges](https://github.com/izecrypt/fashionton-backend/blob/main/src/screens/challenges.js): feature screen · `src/screens/challenges.js`

### Vercel API

- [Vercel deployment](https://github.com/izecrypt/fashionton-backend/blob/main/vercel.json): static host and functions · `vercel.json`
- [Request authentication](https://github.com/izecrypt/fashionton-backend/blob/main/api/_auth.js): identity boundary · Validates and scopes requests to Telegram users. · `api/_auth.js`
- [Wardrobe API](https://github.com/izecrypt/fashionton-backend/blob/main/api/wardrobe.js): serverless handler · `api/wardrobe.js`
- [Challenges API](https://github.com/izecrypt/fashionton-backend/blob/main/api/challenges.js): serverless handler · Consolidates challenge activity. · `api/challenges.js`
- [User API](https://github.com/izecrypt/fashionton-backend/blob/main/api/user.js): serverless handler · `api/user.js`
- [Leaderboard API](https://github.com/izecrypt/fashionton-backend/blob/main/api/leaderboard.js): serverless handler · `api/leaderboard.js`
- [Size calculator API](https://github.com/izecrypt/fashionton-backend/blob/main/api/size-calculator.js): serverless handler · `api/size-calculator.js`
- [Database access](https://github.com/izecrypt/fashionton-backend/blob/main/api/_db.js): persistence adapter · `api/_db.js`
- [Cloudinary adapter](https://github.com/izecrypt/fashionton-backend/blob/main/api/_cloudinary.js): media integration · `api/_cloudinary.js`

### External Services

- **Telegram Mini App**: identity runtime · Supplies the embedded runtime and user identity.
- **Cloudinary**: media storage

### Alternate / Legacy

Reference implementations not assumed live.

- [Route-oriented API backup](https://github.com/izecrypt/fashionton-backend/blob/main/api-backup/challenges/index.js): migration reference · Contains granular challenge and upload routes. · `api-backup/challenges/index.js`
- [Express backend option](https://github.com/izecrypt/fashionton-backend/blob/main/backend/server.js): alternate deployment · Separate backend path that can drift from Vercel functions. · `backend/server.js`

## Connections

- Telegram Mini App → Client shell: embeds
- Client shell → App startup: starts
- App startup → Client store: manages
- App startup → Wardrobe and upload: loads
- App startup → Outfit experience: loads
- App startup → Challenges: loads
- Wardrobe and upload → API client: uses
- Outfit experience → API client: uses
- Challenges → API client: uses
- API client → Vercel deployment: /api requests
- Vercel deployment → Request authentication: invokes
- Request authentication → Wardrobe API: authorizes
- Request authentication → Challenges API: authorizes
- Request authentication → User API: authorizes
- Wardrobe API → Database access: stores garments
- Wardrobe API → Cloudinary adapter: handles media
- Challenges API → Database access: stores entries and votes
- User API → Database access: reads and writes users
- Leaderboard API → Database access: reads rankings
- Size calculator API → Database access: uses user data
- Cloudinary adapter → Cloudinary: uploads media
- Route-oriented API backup → Challenges API: migration reference
- Express backend option → Vercel deployment: alternative to
