# saadtw/craftit architecture

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

- Interactive diagram: https://gitdiagram.com/saadtw/craftit
- Repository: https://github.com/saadtw/craftit

## Overview

CraftIt is a single Next.js App Router monolith that combines the UI, API, domain logic, and database access in one repository. The architecture is centered around role-based experiences for customers, manufacturers, and admins, with shared backend models and route handlers underneath.

The top-level UI is organized by persona:
- `app/customer`, `app/manufacturer`, and `app/admin` contain the main dashboard shells and feature pages.
- `app/auth` contains signup/login/password recovery/verification flows.
- `app/manufacturers`, `app/bids`, and `app/custom-orders` expose shared public or cross-role pages.
This separation is important for diagramming because the same backend services and models support multiple frontend surfaces.

All server-side HTTP endpoints live under `app/api` and are grouped by domain rather than by role. The main business domains are orders, products, RFQs, bids, group buys, disputes, support tickets, notifications, chat, payments, users, uploads, and verification documents. These route handlers are the main boundary between the browser and the backend domain logic.

MongoDB with Mongoose is the system of record. The `models/` directory defines the persistent entities that drive the marketplace: `User`, `Product`, `Order`, `RFQ`, `Bid`, `GroupBuy`, `Chat`, `ChatMessage`, `Dispute`, `SupportTicket`, `Notification`, `Review`, `AdminLog`, `VerificationDocument`, and related sub-entities like product questions and support messages. Most workflows are multi-entity: for example, an RFQ can spawn bids, an accepted bid becomes an order, and the order can later generate milestones, reviews, disputes, and notifications.

Authentication is built around NextAuth.js with JWT credentials sessions, plus separate auth utilities in `lib/auth.js`, `lib/requestAuth.js`, `lib/token.js`, and `lib/mobileAuth.js`. The app supports browser sessions and a distinct mobile auth surface under `app/api/mobile/auth/*`. Session state is shared into the client through `app/SessionProvider.js`.

The marketplace’s core transaction flow runs through RFQs, bids, and orders. Customers create RFQs or direct custom orders, manufacturers respond with bids, and a selected bid can be accepted into an order. Orders then progress through status updates, milestones, shipping, cancellation, and review endpoints. This is the central commercial workflow the architecture should emphasize.

Chat is its own subsystem and is tied to orders and bids. The API includes inbox, per-order chat, read tracking, and a stream endpoint, while `lib/chatEmitter.js` and `lib/sessionEmitter.js` indicate real-time event propagation. In the diagram, treat chat as an asynchronous collaboration layer attached to commercial transactions rather than a standalone feature.

Payments are handled as a Stripe-backed escrow-like flow. The API exposes create-intent, capture, refund, and webhook handlers, and the README describes an authorize-then-capture lifecycle. Stripe is an external dependency that affects order state transitions, especially when an order is accepted, cancelled, or disputed.

File and document handling is another distinct subsystem. `app/api/upload/*` manages uploads, and the README specifies AWS S3 for images, 3D models, and verification documents. This supports product listings, customer order attachments, and manufacturer verification files. In the architecture diagram, S3 should sit outside the app as object storage, with uploads initiated through API routes.

Admin operations are isolated both in UI and in API routes under `app/api/admin/*`. The admin backend covers manufacturer verification, user suspension, platform stats, activity logs, disputes, and order oversight. `models/AdminLog.js` plus the admin activity-log API indicate a strong audit trail boundary for sensitive moderation actions.

Manufacturer verification is a dedicated workflow with its own document model and approval endpoints. Manufacturers start unverified, upload business documents, and admins review or verify them. This is a distinct compliance path that gates access to manufacturer capabilities like bidding, publishing products, and participating in platform commerce.

Notifications, support, and reviews form the post-transaction and operational support layer. Notifications are system-generated events, support tickets allow issue escalation with threaded messages, and reviews are linked to completed orders. These features depend on the core order and user domains but have their own routes and models.

The codebase also includes service modules in `services/` for non-trivial business logic such as bid comparison, bid handling, matching, and notification generation. These services sit between API routes and Mongoose models, acting as the domain logic layer that keeps route handlers thin.

From an infrastructure perspective, the important external dependencies are MongoDB, AWS S3, Stripe, and NextAuth. The runtime is a Next.js server/client hybrid using React and Tailwind CSS v4 on the frontend, but the diagram should focus less on styling and more on the boundaries between role-based pages, domain APIs, service logic, persistence, and external integrations.

Main entry points: [`models`](https://github.com/saadtw/craftit/tree/main/models) (Mongoose models), [`app/api`](https://github.com/saadtw/craftit/tree/main/app/api) (Marketplace API), [`app/api/auth`](https://github.com/saadtw/craftit/tree/main/app/api/auth) (Auth API), [`app/api/admin`](https://github.com/saadtw/craftit/tree/main/app/api/admin) (Admin API)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_ui["UI surfaces"]
  node_app_customer["Customer UI<br/>Next.js pages"]
  node_app_manufacturer["Manufacturer UI<br/>Next.js pages"]
  node_app_admin["Admin UI<br/>Next.js pages"]
  node_app_auth["Auth UI<br/>Next.js pages"]
  node_app_public["Public pages<br/>Next.js pages"]
end

subgraph group_api["API routes"]
  node_api_auth["Auth API<br/>route handlers"]
  node_api_market["Marketplace API<br/>route handlers"]
  node_api_orders["Orders API<br/>route handlers"]
  node_api_chat["Chat API<br/>route handlers"]
  node_api_payments["Payments API<br/>route handlers"]
  node_api_admin["Admin API<br/>route handlers"]
  node_api_support["Support API<br/>route handlers"]
  node_api_upload["Upload API<br/>route handlers"]
  node_api_mobile["Mobile Auth API<br/>route handlers"]
end

subgraph group_core["Core domain"]
  node_services["Domain services<br/>business logic"]
  node_models[("Mongoose models<br/>persistence models")]
end

subgraph group_infra["Infrastructure"]
  node_auth_lib["Auth helpers<br/>auth utils<br/>[auth.js]"]
  node_db[("MongoDB<br/>database")]
  node_stripe["Stripe<br/>payments"]
  node_s3[("S3 storage<br/>object storage")]
  node_nextauth["NextAuth<br/>auth provider"]
  node_emitters["Event emitters<br/>realtime infra<br/>[chatEmitter.js]"]
end

node_app_customer -->|"uses"| node_api_market
node_app_customer -->|"uses"| node_api_orders
node_app_customer -->|"uses"| node_api_chat
node_app_manufacturer -->|"uses"| node_api_market
node_app_manufacturer -->|"uses"| node_api_orders
node_app_manufacturer -->|"uses"| node_api_chat
node_app_admin -->|"uses"| node_api_admin
node_app_auth -->|"uses"| node_api_auth
node_app_public -->|"uses"| node_api_market
node_api_auth -->|"auth logic"| node_auth_lib
node_api_auth -->|"session"| node_nextauth
node_api_mobile -->|"auth logic"| node_auth_lib
node_api_market -->|"domain logic"| node_services
node_api_orders -->|"domain logic"| node_services
node_api_chat -->|"events"| node_emitters
node_api_admin -->|"admin data"| node_models
node_api_support -->|"support data"| node_models
node_api_upload -->|"stores"| node_s3
node_api_payments -->|"payments"| node_stripe
node_api_payments -->|"domain logic"| node_services
node_services -->|"persists"| node_models
node_models -->|"backed by"| node_db
node_api_auth -->|"users"| node_models
node_api_market -->|"market data"| node_models
node_api_orders -->|"orders"| node_models
node_api_chat -->|"chat data"| node_models
node_api_admin -.->|"oversight"| node_stripe
node_api_admin -.->|"verification files"| node_s3

click node_app_customer "https://github.com/saadtw/craftit/tree/main/app/customer"
click node_app_manufacturer "https://github.com/saadtw/craftit/tree/main/app/manufacturer"
click node_app_admin "https://github.com/saadtw/craftit/tree/main/app/admin"
click node_app_auth "https://github.com/saadtw/craftit/tree/main/app/auth"
click node_app_public "https://github.com/saadtw/craftit/tree/main/app"
click node_api_auth "https://github.com/saadtw/craftit/tree/main/app/api/auth"
click node_api_market "https://github.com/saadtw/craftit/tree/main/app/api"
click node_api_orders "https://github.com/saadtw/craftit/tree/main/app/api/orders"
click node_api_chat "https://github.com/saadtw/craftit/tree/main/app/api/chat"
click node_api_payments "https://github.com/saadtw/craftit/tree/main/app/api/payments"
click node_api_admin "https://github.com/saadtw/craftit/tree/main/app/api/admin"
click node_api_support "https://github.com/saadtw/craftit/tree/main/app/api/support-tickets"
click node_api_upload "https://github.com/saadtw/craftit/tree/main/app/api/upload"
click node_api_mobile "https://github.com/saadtw/craftit/tree/main/app/api/mobile/auth"
click node_services "https://github.com/saadtw/craftit/tree/main/services"
click node_models "https://github.com/saadtw/craftit/tree/main/models"
click node_auth_lib "https://github.com/saadtw/craftit/blob/main/lib/auth.js"
click node_emitters "https://github.com/saadtw/craftit/blob/main/lib/chatEmitter.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_app_customer,node_app_manufacturer,node_app_admin,node_app_auth,node_app_public toneBlue
class node_api_auth,node_api_market,node_api_orders,node_api_chat,node_api_payments,node_api_admin,node_api_support,node_api_upload,node_api_mobile toneAmber
class node_services,node_models toneMint
class node_auth_lib,node_db,node_stripe,node_s3,node_nextauth,node_emitters toneRose
```

## Components

### UI surfaces

Role-based App Router pages and shared public screens.

- [Customer UI](https://github.com/saadtw/craftit/tree/main/app/customer): Next.js pages · Customer dashboard, marketplace, orders, RFQs, support, and payments. · `app/customer`
- [Manufacturer UI](https://github.com/saadtw/craftit/tree/main/app/manufacturer): Next.js pages · Manufacturer dashboard, bids, products, orders, group buys, and support. · `app/manufacturer`
- [Admin UI](https://github.com/saadtw/craftit/tree/main/app/admin): Next.js pages · Admin dashboards for users, manufacturers, disputes, orders, support, and logs. · `app/admin`
- [Auth UI](https://github.com/saadtw/craftit/tree/main/app/auth): Next.js pages · Login, signup, password recovery, and email verification flows. · `app/auth`
- [Public pages](https://github.com/saadtw/craftit/tree/main/app): Next.js pages · Shared marketplace pages for manufacturers, bids, custom orders, and home. · `app`

### API routes

HTTP boundaries grouped by domain and role.

- [Auth API](https://github.com/saadtw/craftit/tree/main/app/api/auth): route handlers · NextAuth, registration, password reset, verification, and session endpoints. · `app/api/auth`
- [Marketplace API](https://github.com/saadtw/craftit/tree/main/app/api): route handlers · Products, RFQs, bids, custom orders, group buys, and manufacturer discovery endpoints. · `app/api`
- [Orders API](https://github.com/saadtw/craftit/tree/main/app/api/orders): route handlers · Order lifecycle endpoints for creation, status, milestones, shipping, cancellation, and reviews. · `app/api/orders`
- [Chat API](https://github.com/saadtw/craftit/tree/main/app/api/chat): route handlers · Order chat, inbox, read tracking, and streaming endpoints. · `app/api/chat`
- [Payments API](https://github.com/saadtw/craftit/tree/main/app/api/payments): route handlers · Stripe payment intent, capture, refund, and webhook endpoints. · `app/api/payments`
- [Admin API](https://github.com/saadtw/craftit/tree/main/app/api/admin): route handlers · Moderation, verification, stats, user management, disputes, and order oversight endpoints. · `app/api/admin`
- [Support API](https://github.com/saadtw/craftit/tree/main/app/api/support-tickets): route handlers · Support ticket and threaded message endpoints. · `app/api/support-tickets`
- [Upload API](https://github.com/saadtw/craftit/tree/main/app/api/upload): route handlers · Single and batch upload endpoints for media and verification files. · `app/api/upload`
- [Mobile Auth API](https://github.com/saadtw/craftit/tree/main/app/api/mobile/auth): route handlers · Mobile login, logout, session lookup, and refresh endpoints. · `app/api/mobile/auth`

### Core domain

Shared models and business logic used across roles and workflows.

- [Domain services](https://github.com/saadtw/craftit/tree/main/services): business logic · Bid comparison, bid handling, matching, and notification orchestration. · `services`
- [Mongoose models](https://github.com/saadtw/craftit/tree/main/models): persistence models · Marketplace entities such as users, products, RFQs, bids, orders, chats, disputes, tickets, reviews, and logs. · `models`

### Infrastructure

Authentication, persistence, messaging, payments, and storage integrations.

- [Auth helpers](https://github.com/saadtw/craftit/blob/main/lib/auth.js): auth utils · Shared browser/session auth utilities, token handling, and request authorization. · `lib/auth.js`
- **MongoDB**: database · System of record accessed through Mongoose.
- **Stripe**: payments · External escrow-like payment provider for authorize, capture, refund, and webhook flows.
- **S3 storage**: object storage · External storage for product images, 3D models, and verification documents.
- **NextAuth**: auth provider · Session and credential authentication layer for browser and API access.
- [Event emitters](https://github.com/saadtw/craftit/blob/main/lib/chatEmitter.js): realtime infra · Chat and session event propagation for asynchronous updates. · `lib/chatEmitter.js`

## Connections

- Customer UI → Marketplace API: uses · Customer screens load and mutate marketplace data through domain APIs.
- Customer UI → Orders API: uses · Customer order, dispute, payment, and review actions.
- Customer UI → Chat API: uses · Customer inbox and order chat interactions.
- Manufacturer UI → Marketplace API: uses · Manufacturer product, RFQ, bid, and group-buy actions.
- Manufacturer UI → Orders API: uses · Manufacturer order progress and milestone updates.
- Manufacturer UI → Chat API: uses · Manufacturer messaging around bids and orders.
- Admin UI → Admin API: uses · Admin moderation and oversight screens.
- Auth UI → Auth API: uses · Signup, login, recovery, and verification flows.
- Public pages → Marketplace API: uses · Public manufacturer, bid, custom order, and product pages.
- Auth API → Auth helpers: auth logic · Shared auth helpers and token/session utilities.
- Auth API → NextAuth: session · NextAuth credential session handling.
- Mobile Auth API → Auth helpers: auth logic · Mobile auth shares request and token utilities.
- Marketplace API → Domain services: domain logic · Route handlers delegate matching, bid comparison, and notifications.
- Orders API → Domain services: domain logic · Order lifecycle endpoints use shared business rules.
- Chat API → Event emitters: events · Chat routes publish realtime updates.
- Admin API → Mongoose models: admin data · Moderation and audit routes persist admin actions and reviews.
- Support API → Mongoose models: support data · Support tickets and threaded messages are stored in MongoDB.
- Upload API → S3 storage: stores · Uploads are written to external object storage.
- Payments API → Stripe: payments · Payment lifecycle and webhook processing.
- Payments API → Domain services: domain logic · Payment handlers coordinate state transitions and notifications.
- Domain services → Mongoose models: persists · Business logic reads and updates marketplace entities.
- Mongoose models → MongoDB: backed by · Mongoose models map to MongoDB collections.
- Auth API → Mongoose models: users · Registration, verification, and session flows use user records.
- Marketplace API → Mongoose models: market data · Products, RFQs, bids, group buys, and custom orders.
- Orders API → Mongoose models: orders · Orders, milestones, disputes, reviews, and notifications.
- Chat API → Mongoose models: chat data · Chats and chat messages are persisted through models.
- Admin API → Stripe: oversight · Admin order and dispute actions can affect payment state.
- Admin API → S3 storage: verification files · Manufacturer verification depends on uploaded documents.
