# shareeep/campusg architecture

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

- Interactive diagram: https://gitdiagram.com/shareeep/campusg
- Repository: https://github.com/shareeep/campusg

## Overview

##### Project Type and Purpose
- CampusG is a full‑stack, microservices‑based food‑delivery platform.
- It exposes a React/Vite frontend for customers and runners, multiple Flask microservices for domain logic, and uses Kafka and Temporal for distributed‑transaction orchestration (sagas).
- External dependencies include Clerk for authentication, Stripe for payments, and an Outsystems Timer API.

##### Top‑Level File Structure & Architectural Patterns
- frontend/ → React + TypeScript + Vite + TailwindCSS UI
- services/ → six Flask‑based microservices:
- user‑service, order_service, payment‑service, notification‑service
- three saga‑orchestrators: create‑order (Kafka‑based), accept‑order (Temporal), complete‑order (Temporal)
- kafka/ → topic definitions for event bus
- temporal/ → server configuration for Temporal workflows
- grafana/ → monitoring dashboards provisioning
- docker-compose.yml spins up all services, Kafka, Temporal, databases, Grafana

##### Main Components & Their Responsibilities
A. Client Layer
- Web App (React/Vite): UI for placing, accepting, tracking orders
B. API/Orchestration Layer
- Create‑Order Saga (Flask + Kafka): orchestrates order creation, payment authorization, timer setup
- Accept‑Order Saga (Temporal): orchestrates runner acceptance and timer notifications
- Complete‑Order Saga (Temporal): orchestrates delivery completion, payout, final status update
C. Core Domain Services
- User Service (Flask + PostgreSQL): manages profiles, Clerk auth, Stripe customer/connect accounts
- Order Service (Flask + PostgreSQL): order lifecycle CRUD and status transitions
- Payment Service (Flask + PostgreSQL): Stripe integration for payments/refunds/payouts
- Notification Service (Flask + PostgreSQL + Kafka consumer): logs domain events for observability
D. Infrastructure
- Kafka Cluster: event bus for Create‑Order Saga commands/events and Notification logging
- Temporal Server: durable workflow engine for Accept‑Order & Complete‑Order sagas
- PostgreSQL Databases: one per service / orchestrator for state persistence
- Grafana: monitoring dashboards
- External APIs: Clerk (OAuth), Stripe (payments), Outsystems Timer (HTTP timer API)

##### Component Interactions & Data Flows
- Frontend → HTTP call → Create‑Order Saga API
→ emits Kafka “CreateOrder” command
→ Order Service (create order)
→ User Service (get payment info)
→ Payment Service (authorize payment)
→ Saga updates order status, calls Timer Service to start countdown
- Frontend → HTTP call → Accept‑Order Saga
→ Temporal worker invokes Order Service (assign runner)
→ HTTP → Timer Service (stop timer)
- Frontend → HTTP call → Complete‑Order Saga
→ Temporal worker invokes Order Service (mark delivered/completed)
→ invokes User Service (fetch runner’s connect ID)
→ invokes Payment Service (fetch payment ID & payout)
- All services emit domain events to Kafka; Notification Service consumes and pushes metrics to Grafana.

##### Architectural Patterns & Principles
- Microservices: single‑responsibility services, separate codebases and data stores
- Saga orchestration: reliable distributed transactions via choreography (Kafka) and orchestration (Temporal)
- API‑first design: each service exposes REST/Swagger APIs (api-specs/)
- Database per service: ensures loose coupling and independent scaling
- Event‑driven: Kafka topics as central message bus
- Durable workflows: Temporal for long‑running, compensating workflows

##### Essential Elements
- Component labels (with technology stacks, e.g., “Payment Service (Flask/SQLAlchemy)”)

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "Client Layer"
        WebApp["Web App<br/>(React/Vite)"]:::frontend
    end

    subgraph "Saga Orchestrators"
        direction TB
        CreateOrderSaga["Create‑Order Saga<br/>(Flask/Kafka)"]:::orchestrator
        AcceptOrderSaga["Accept‑Order Saga<br/>(Temporal)"]:::orchestrator
        CompleteOrderSaga["Complete‑Order Saga<br/>(Temporal)"]:::orchestrator
    end

    subgraph "Core Domain Services"
        direction TB
        UserService["User Service<br/>(Flask/PostgreSQL)"]:::domain
        OrderService["Order Service<br/>(Flask/PostgreSQL)"]:::domain
        PaymentService["Payment Service<br/>(Flask/PostgreSQL)"]:::domain
        NotificationService["Notification Service<br/>(Flask/PostgreSQL/Kafka)"]:::domain
    end

    subgraph "Infrastructure"
        direction TB
        KafkaCluster["Kafka Cluster"]:::infra
        TemporalServer["Temporal Server"]:::infra
        Grafana["Grafana"]:::infra
        PostgreUser["User DB<br/>(PostgreSQL)"]:::infra
        PostgreOrder["Order DB<br/>(PostgreSQL)"]:::infra
        PostgrePayment["Payment DB<br/>(PostgreSQL)"]:::infra
        PostgreNotification["Notification DB<br/>(PostgreSQL)"]:::infra
    end

    subgraph "External Dependencies"
        Clerk["Clerk<br/>(OAuth)"]:::external
        StripeExt["Stripe"]:::external
        TimerAPI["Outsystems Timer API"]:::external
    end

    WebApp -->|HTTP| CreateOrderSaga
    CreateOrderSaga -->|Kafka CreateOrder| KafkaCluster
    KafkaCluster -->|Consumes| OrderService
    CreateOrderSaga -->|HTTP| UserService
    CreateOrderSaga -->|HTTP| PaymentService
    CreateOrderSaga -->|HTTP Timer| TimerAPI

    WebApp -->|HTTP| AcceptOrderSaga
    AcceptOrderSaga -->|Temporal Start| TemporalServer
    TemporalServer -->|Activity: Assign Runner| OrderService
    AcceptOrderSaga -->|HTTP Timer Stop| TimerAPI

    WebApp -->|HTTP| CompleteOrderSaga
    CompleteOrderSaga -->|Temporal Start| TemporalServer
    TemporalServer -->|Activity: Mark Delivered| OrderService
    CompleteOrderSaga -->|HTTP| UserService
    CompleteOrderSaga -->|HTTP| PaymentService

    UserService -->|writes| PostgreUser
    OrderService -->|writes| PostgreOrder
    PaymentService -->|writes| PostgrePayment
    NotificationService -->|writes| PostgreNotification

    UserService -->|events| KafkaCluster
    OrderService -->|events| KafkaCluster
    PaymentService -->|events| KafkaCluster
    CreateOrderSaga -->|events| KafkaCluster
    AcceptOrderSaga -->|events| KafkaCluster
    CompleteOrderSaga -->|events| KafkaCluster

    KafkaCluster -->|Consume| NotificationService
    NotificationService -->|metrics| Grafana

    WebApp -->|Auth| Clerk
    CreateOrderSaga -->|Auth| Clerk
    UserService -->|Auth| Clerk
    PaymentService -->|API| StripeExt

    classDef frontend fill:#cce5ff,stroke:#4da3ff,color:#004085
    classDef orchestrator fill:#ffe5cc,stroke:#ff8c1a,color:#663c00
    classDef domain fill:#d4edda,stroke:#28a745,color:#155724
    classDef infra fill:#e2e3e5,stroke:#6c757d,color:#383d41
    classDef external fill:#f3e5ff,stroke:#9b59b6,color:#4a235a

    click WebApp "https://github.com/shareeep/campusg/tree/main/frontend/"
    click CreateOrderSaga "https://github.com/shareeep/campusg/tree/main/services/create-order-saga-orchestrator/"
    click AcceptOrderSaga "https://github.com/shareeep/campusg/tree/main/services/accept-order-saga-orchestrator/"
    click CompleteOrderSaga "https://github.com/shareeep/campusg/tree/main/services/complete-order-saga-orchestrator/"
    click UserService "https://github.com/shareeep/campusg/tree/main/services/user-service/"
    click OrderService "https://github.com/shareeep/campusg/tree/main/services/order_service/"
    click PaymentService "https://github.com/shareeep/campusg/tree/main/services/payment-service/"
    click NotificationService "https://github.com/shareeep/campusg/tree/main/services/notification-service/"
    click KafkaCluster "https://github.com/shareeep/campusg/blob/main/kafka/config/topics.json"
    click TemporalServer "https://github.com/shareeep/campusg/tree/main/temporal/dynamicconfig/"
    click Grafana "https://github.com/shareeep/campusg/blob/main/grafana/provisioning/datasources/datasources.yml"
```
