# ranasamiafzal/brandy-backend architecture

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

- Interactive diagram: https://gitdiagram.com/ranasamiafzal/brandy-backend
- Repository: https://github.com/ranasamiafzal/brandy-backend

## Overview

#### Purpose and Entry Points
Brandly is a backend API for a brand–influencer marketplace: discovery/matching, collaboration agreements and deliverables, escrow payments, messaging, notifications, and activity tracking. The runtime starts in `server/src/index.js`, which initializes the Express application from `server/src/app.js`; deployment configuration is in `vercel.json`.

#### Application Composition
`server/src/app.js` is the HTTP composition boundary: it should mount versioned module routes and shared middleware. Feature modules consistently separate route/controller/service/model layers, keeping HTTP concerns at route/controller boundaries and MongoDB business persistence in services/models. Shared failures and response envelopes are centralized in `server/src/middleware/errorMiddleware.js`, `server/src/utils/ApiError.js`, and `server/src/utils/ApiResponse.js`.

#### Identity, Access, and Onboarding
Authentication supports JWT-based sessions, OTP/email-related flows, and Google OAuth. Core implementation is in `server/src/modules/auth/auth.service.js`, `server/src/modules/oauth/oauth.controller.js`, and `server/src/config/passport.js`. Authorization and account-state gates are cross-cutting boundaries through `server/src/middleware/authMiddleware.js`, `server/src/middleware/roleMiddleware.js`, and `server/src/middleware/profileGate.js`. User, brand, and influencer profiles are distinct persisted domain entities in `server/src/modules/user/user.model.js`, `server/src/modules/brand/brand.model.js`, and `server/src/modules/influencer/influencer.model.js`.

#### Discovery and AI Matching
Brands and influencers are managed through their respective module services, while the AI matching subsystem scores and formats candidate matches. Its control flow is route → controller → matching service/scorer → response formatting, centered on `server/src/modules/aiMatch/aiMatch.service.js`, `server/src/modules/aiMatch/aiMatch.scorer.js`, and `server/src/modules/aiMatch/aiMatch.formatter.js`. This is an internal matching layer rather than a separately represented external AI provider in the repository tree.

#### Campaign and Collaboration Lifecycle
Campaigns establish the work context (`server/src/modules/campaign/campaign.model.js`). Collaborations then model the mutually agreed engagement and break execution into request, action, and deliverable operations via `server/src/modules/collaboration/collaboration.service.js`, `server/src/modules/collaboration/request.service.js`, and `server/src/modules/collaboration/deliverable.service.js`. Reviews are persisted separately in `server/src/modules/collaboration/review.model.js`. This lifecycle is the key business boundary between discovery and payment/release decisions.

#### Payments and External Financial Boundary
Stripe is isolated behind the payment module, which persists payment and escrow state and exposes Stripe-specific endpoints. The main boundary is `server/src/modules/payment/stripe.service.js`, with state in `server/src/modules/payment/payment.model.js` and `server/src/modules/payment/escrow.model.js`. Collaboration deliverable status likely drives escrow protection and staged payouts; Stripe remains the external payment authority.

#### Real-Time Communication and Supporting Services
Socket.io provides project-based real-time synchronization, configured through `server/src/config/socket.js` and managed in `server/src/config/socketManager.js`. Persistent direct communication is represented by `server/src/modules/message/conversation.model.js` and `server/src/modules/message/message.service.js`; user-facing alerts use `server/src/modules/notification/notification.service.js`. Activity history is a separate audit/feed concern in `server/src/modules/activity/activity.service.js`.

#### Runtime Infrastructure
The server runs on Node.js/Express with MongoDB via the database configuration in `server/src/config/db.js`. Cloudinary is the external media store (`server/src/config/cloudinary.js`) accessed alongside upload middleware (`server/src/middleware/multerMiddleware.js`). SMTP/Nodemailer supports email/OTP communication through `server/src/utils/email.js`. Required credentials and deployment-time configuration are documented by `server/.env.example`.

Main entry points: [`server/src/app.js`](https://github.com/ranasamiafzal/brandy-backend/blob/main/server/src/app.js) (Express app), [`server/src/modules/collaboration/collaboration.service.js`](https://github.com/ranasamiafzal/brandy-backend/blob/main/server/src/modules/collaboration/collaboration.service.js) (Collaborations), [`server/src/modules/aiMatch/aiMatch.service.js`](https://github.com/ranasamiafzal/brandy-backend/blob/main/server/src/modules/aiMatch/aiMatch.service.js) (AI matching), [`server/src/modules/payment/stripe.service.js`](https://github.com/ranasamiafzal/brandy-backend/blob/main/server/src/modules/payment/stripe.service.js) (Stripe payments & escrow)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_runtime["Runtime"]
  node_entry["Server entry<br/>Node.js bootstrap<br/>[index.js]"]
  node_app["Express app<br/>HTTP composition<br/>[app.js]"]
  node_database[("MongoDB<br/>database<br/>[db.js]")]
  node_api_errors["API error envelopes<br/>error handling<br/>[errorMiddleware.js]"]
end

subgraph group_identity["Identity &amp; access"]
  node_auth["Auth sessions &amp; OTP<br/>auth service<br/>[auth.service.js]"]
  node_oauth["Google OAuth<br/>OAuth controller"]
  node_passport["Passport config<br/>OAuth strategy<br/>[passport.js]"]
  node_access_gates["Access gates<br/>auth middleware<br/>[authMiddleware.js]"]
  node_profiles[("User profiles<br/>MongoDB models<br/>[user.model.js]")]
end

subgraph group_marketplace["Marketplace lifecycle"]
  node_matching["AI matching<br/>matching service<br/>[aiMatch.service.js]"]
  node_scorer["Match scorer<br/>scoring layer<br/>[aiMatch.scorer.js]"]
  node_formatter["Match formatter<br/>response formatter"]
  node_campaigns[("Campaigns<br/>MongoDB model<br/>[campaign.model.js]")]
  node_collaboration["Collaborations<br/>collaboration service"]
  node_requests["Collaboration requests<br/>request service<br/>[request.service.js]"]
  node_deliverables["Deliverables<br/>deliverable service"]
  node_reviews[("Reviews<br/>MongoDB model<br/>[review.model.js]")]
end

subgraph group_realtime["Communication"]
  node_messaging["Direct messaging<br/>message service<br/>[message.service.js]"]
  node_sockets["Socket.io<br/>real-time transport<br/>[socket.js]"]
  node_notifications["Notifications<br/>notification service"]
  node_activity["Activity history<br/>activity service"]
end

subgraph group_integrations["External services"]
  node_payments["Stripe payments &amp; escrow<br/>payment service<br/>[stripe.service.js]"]
  node_payment_state[("Payment state<br/>MongoDB models<br/>[payment.model.js]")]
  node_stripe{{"Stripe<br/>payment authority"}}
  node_media{{"Cloudinary uploads<br/>media integration<br/>[cloudinary.js]"}}
  node_email{{"SMTP email<br/>email utility<br/>[email.js]"}}
end

node_entry -->|"starts"| node_app
node_app -->|"applies"| node_access_gates
node_app -->|"handles errors with"| node_api_errors
node_app -->|"routes to"| node_auth
node_app -->|"routes to"| node_matching
node_app -->|"routes to"| node_collaboration
node_app -->|"routes to"| node_payments
node_access_gates -->|"checks account state"| node_profiles
node_auth -->|"manages identities"| node_profiles
node_auth -->|"sends OTP and email"| node_email
node_oauth -->|"uses"| node_passport
node_passport -->|"resolves identity"| node_profiles
node_matching -->|"scores candidates"| node_scorer
node_matching -->|"formats results"| node_formatter
node_matching -->|"matches brands and influencers"| node_profiles
node_collaboration -->|"belongs to campaign"| node_campaigns
node_collaboration -->|"coordinates"| node_requests
node_collaboration -->|"coordinates"| node_deliverables
node_collaboration -->|"records"| node_reviews
node_deliverables -->|"drives staged payout"| node_payments
node_payments -->|"persists"| node_payment_state
node_payments -->|"charges and releases via"| node_stripe
node_messaging -->|"synchronizes through"| node_sockets
node_collaboration -->|"enables engagement chat"| node_messaging
node_collaboration -->|"emits alerts"| node_notifications
node_collaboration -->|"records events"| node_activity
node_app -->|"uploads media through"| node_media
node_app -->|"connects to"| node_database

click node_entry "https://github.com/ranasamiafzal/brandy-backend/blob/main/server/src/index.js"
click node_app "https://github.com/ranasamiafzal/brandy-backend/blob/main/server/src/app.js"
click node_database "https://github.com/ranasamiafzal/brandy-backend/blob/main/server/src/config/db.js"
click node_api_errors "https://github.com/ranasamiafzal/brandy-backend/blob/main/server/src/middleware/errorMiddleware.js"
click node_auth "https://github.com/ranasamiafzal/brandy-backend/blob/main/server/src/modules/auth/auth.service.js"
click node_oauth "https://github.com/ranasamiafzal/brandy-backend/blob/main/server/src/modules/oauth/oauth.controller.js"
click node_passport "https://github.com/ranasamiafzal/brandy-backend/blob/main/server/src/config/passport.js"
click node_access_gates "https://github.com/ranasamiafzal/brandy-backend/blob/main/server/src/middleware/authMiddleware.js"
click node_profiles "https://github.com/ranasamiafzal/brandy-backend/blob/main/server/src/modules/user/user.model.js"
click node_matching "https://github.com/ranasamiafzal/brandy-backend/blob/main/server/src/modules/aiMatch/aiMatch.service.js"
click node_scorer "https://github.com/ranasamiafzal/brandy-backend/blob/main/server/src/modules/aiMatch/aiMatch.scorer.js"
click node_formatter "https://github.com/ranasamiafzal/brandy-backend/blob/main/server/src/modules/aiMatch/aiMatch.formatter.js"
click node_campaigns "https://github.com/ranasamiafzal/brandy-backend/blob/main/server/src/modules/campaign/campaign.model.js"
click node_collaboration "https://github.com/ranasamiafzal/brandy-backend/blob/main/server/src/modules/collaboration/collaboration.service.js"
click node_requests "https://github.com/ranasamiafzal/brandy-backend/blob/main/server/src/modules/collaboration/request.service.js"
click node_deliverables "https://github.com/ranasamiafzal/brandy-backend/blob/main/server/src/modules/collaboration/deliverable.service.js"
click node_reviews "https://github.com/ranasamiafzal/brandy-backend/blob/main/server/src/modules/collaboration/review.model.js"
click node_payments "https://github.com/ranasamiafzal/brandy-backend/blob/main/server/src/modules/payment/stripe.service.js"
click node_payment_state "https://github.com/ranasamiafzal/brandy-backend/blob/main/server/src/modules/payment/payment.model.js"
click node_messaging "https://github.com/ranasamiafzal/brandy-backend/blob/main/server/src/modules/message/message.service.js"
click node_sockets "https://github.com/ranasamiafzal/brandy-backend/blob/main/server/src/config/socket.js"
click node_notifications "https://github.com/ranasamiafzal/brandy-backend/blob/main/server/src/modules/notification/notification.service.js"
click node_activity "https://github.com/ranasamiafzal/brandy-backend/blob/main/server/src/modules/activity/activity.service.js"
click node_media "https://github.com/ranasamiafzal/brandy-backend/blob/main/server/src/config/cloudinary.js"
click node_email "https://github.com/ranasamiafzal/brandy-backend/blob/main/server/src/utils/email.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_entry,node_app,node_database,node_api_errors toneBlue
class node_auth,node_oauth,node_passport,node_access_gates,node_profiles toneAmber
class node_matching,node_scorer,node_formatter,node_campaigns,node_collaboration,node_requests,node_deliverables,node_reviews toneMint
class node_messaging,node_sockets,node_notifications,node_activity toneRose
class node_payments,node_payment_state,node_stripe,node_media,node_email toneIndigo
```

## Components

### Runtime

- [Server entry](https://github.com/ranasamiafzal/brandy-backend/blob/main/server/src/index.js): Node.js bootstrap · `server/src/index.js`
- [Express app](https://github.com/ranasamiafzal/brandy-backend/blob/main/server/src/app.js): HTTP composition · Mounts versioned modules and shared middleware. · `server/src/app.js`
- [MongoDB](https://github.com/ranasamiafzal/brandy-backend/blob/main/server/src/config/db.js): database · `server/src/config/db.js`
- [API error envelopes](https://github.com/ranasamiafzal/brandy-backend/blob/main/server/src/middleware/errorMiddleware.js): error handling · Centralized failures and response envelopes. · `server/src/middleware/errorMiddleware.js`

### Identity & access

- [Auth sessions & OTP](https://github.com/ranasamiafzal/brandy-backend/blob/main/server/src/modules/auth/auth.service.js): auth service · `server/src/modules/auth/auth.service.js`
- [Google OAuth](https://github.com/ranasamiafzal/brandy-backend/blob/main/server/src/modules/oauth/oauth.controller.js): OAuth controller · `server/src/modules/oauth/oauth.controller.js`
- [Passport config](https://github.com/ranasamiafzal/brandy-backend/blob/main/server/src/config/passport.js): OAuth strategy · `server/src/config/passport.js`
- [Access gates](https://github.com/ranasamiafzal/brandy-backend/blob/main/server/src/middleware/authMiddleware.js): auth middleware · JWT, role, and profile-state enforcement. · `server/src/middleware/authMiddleware.js`
- [User profiles](https://github.com/ranasamiafzal/brandy-backend/blob/main/server/src/modules/user/user.model.js): MongoDB models · User, brand, and influencer identities. · `server/src/modules/user/user.model.js`

### Marketplace lifecycle

- [AI matching](https://github.com/ranasamiafzal/brandy-backend/blob/main/server/src/modules/aiMatch/aiMatch.service.js): matching service · `server/src/modules/aiMatch/aiMatch.service.js`
- [Match scorer](https://github.com/ranasamiafzal/brandy-backend/blob/main/server/src/modules/aiMatch/aiMatch.scorer.js): scoring layer · `server/src/modules/aiMatch/aiMatch.scorer.js`
- [Match formatter](https://github.com/ranasamiafzal/brandy-backend/blob/main/server/src/modules/aiMatch/aiMatch.formatter.js): response formatter · `server/src/modules/aiMatch/aiMatch.formatter.js`
- [Campaigns](https://github.com/ranasamiafzal/brandy-backend/blob/main/server/src/modules/campaign/campaign.model.js): MongoDB model · Work context for engagements. · `server/src/modules/campaign/campaign.model.js`
- [Collaborations](https://github.com/ranasamiafzal/brandy-backend/blob/main/server/src/modules/collaboration/collaboration.service.js): collaboration service · Agreed engagement lifecycle. · `server/src/modules/collaboration/collaboration.service.js`
- [Collaboration requests](https://github.com/ranasamiafzal/brandy-backend/blob/main/server/src/modules/collaboration/request.service.js): request service · `server/src/modules/collaboration/request.service.js`
- [Deliverables](https://github.com/ranasamiafzal/brandy-backend/blob/main/server/src/modules/collaboration/deliverable.service.js): deliverable service · Execution status drives release decisions. · `server/src/modules/collaboration/deliverable.service.js`
- [Reviews](https://github.com/ranasamiafzal/brandy-backend/blob/main/server/src/modules/collaboration/review.model.js): MongoDB model · `server/src/modules/collaboration/review.model.js`

### Communication

- [Direct messaging](https://github.com/ranasamiafzal/brandy-backend/blob/main/server/src/modules/message/message.service.js): message service · Persistent conversations and messages. · `server/src/modules/message/message.service.js`
- [Socket.io](https://github.com/ranasamiafzal/brandy-backend/blob/main/server/src/config/socket.js): real-time transport · Project-based synchronization. · `server/src/config/socket.js`
- [Notifications](https://github.com/ranasamiafzal/brandy-backend/blob/main/server/src/modules/notification/notification.service.js): notification service · `server/src/modules/notification/notification.service.js`
- [Activity history](https://github.com/ranasamiafzal/brandy-backend/blob/main/server/src/modules/activity/activity.service.js): activity service · Audit and feed events. · `server/src/modules/activity/activity.service.js`

### External services

- [Stripe payments & escrow](https://github.com/ranasamiafzal/brandy-backend/blob/main/server/src/modules/payment/stripe.service.js): payment service · Persists payment and escrow state around Stripe operations. · `server/src/modules/payment/stripe.service.js`
- [Payment state](https://github.com/ranasamiafzal/brandy-backend/blob/main/server/src/modules/payment/payment.model.js): MongoDB models · Payment and escrow records. · `server/src/modules/payment/payment.model.js`
- **Stripe**: payment authority
- [Cloudinary uploads](https://github.com/ranasamiafzal/brandy-backend/blob/main/server/src/config/cloudinary.js): media integration · `server/src/config/cloudinary.js`
- [SMTP email](https://github.com/ranasamiafzal/brandy-backend/blob/main/server/src/utils/email.js): email utility · Email and OTP delivery. · `server/src/utils/email.js`

## Connections

- Server entry → Express app: starts
- Express app → Access gates: applies
- Express app → API error envelopes: handles errors with
- Express app → Auth sessions & OTP: routes to
- Express app → AI matching: routes to
- Express app → Collaborations: routes to
- Express app → Stripe payments & escrow: routes to
- Access gates → User profiles: checks account state
- Auth sessions & OTP → User profiles: manages identities
- Auth sessions & OTP → SMTP email: sends OTP and email
- Google OAuth → Passport config: uses
- Passport config → User profiles: resolves identity
- AI matching → Match scorer: scores candidates
- AI matching → Match formatter: formats results
- AI matching → User profiles: matches brands and influencers
- Collaborations → Campaigns: belongs to campaign
- Collaborations → Collaboration requests: coordinates
- Collaborations → Deliverables: coordinates
- Collaborations → Reviews: records
- Deliverables → Stripe payments & escrow: drives staged payout
- Stripe payments & escrow → Payment state: persists
- Stripe payments & escrow → Stripe: charges and releases via
- Direct messaging → Socket.io: synchronizes through
- Collaborations → Direct messaging: enables engagement chat
- Collaborations → Notifications: emits alerts
- Collaborations → Activity history: records events
- Express app → Cloudinary uploads: uploads media through
- Express app → MongoDB: connects to
