# adimyy/google_script architecture

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

- Interactive diagram: https://gitdiagram.com/adimyy/google_script
- Repository: https://github.com/adimyy/google_script

## Overview

##### Project type and purpose
- This is a single-service Node.js API (no separate frontend) designed to manage medication data and orders via Google Sheets and Google Drive.
- It exposes OAuth2 endpoints and a REST-style `/api` endpoint that accepts actions like “createsheet,” “storemedication,” and “storeorder.”

##### File structure
- Top-level files
- app.js → the entire Express-style HTTP server
- package.json / package-lock.json → defines Node.js dependencies (express, googleapis, dotenv, etc.)
- vercel.json → deployment configuration for Vercel serverless
- .gitignore, README.md → ancillary
- No “src/,” “services/,” or “controllers/” directories; everything lives in app.js, making this effectively a small monolith.

##### Key insights from the README
- Uses Google OAuth2 for authentication.
- Integrates two Google Services: Sheets API and Drive API.
- Manages a folder structure:
NAQQAS/
├── SHEETS_FOLDER/
│   └── [user_email]/
└── ORDER_FOLDER/
└── [user_email]/
- Hosted locally (node app.js) or on Vercel (serverless functions).

##### Main components
a. Client (web browser or other HTTP client)
b. API Server (Node.js/Express in app.js)
- OAuth2 module
- Sheets-management module
- Drive-management module
c. Google OAuth2 Authorization Server
d. Google Sheets API
e. Google Drive API
g. Environment Config (CLIENT_ID, etc.) shown as a secure vault

##### Relationships and interactions
- Client → GET /auth → API Server → Google OAuth2 → redirects back to `/oauth2callback`
- Client → GET/POST /api?action=… → API Server → calls Google Sheets API or Google Drive API
- API Server reads credentials from environment vault, refreshes tokens with OAuth2 server
- Google Drive API persists folder structure (NAQQAS/SHEETS_FOLDER & ORDER_FOLDER)
- Google Sheets API reads/writes rows for medication and order data

##### Architectural patterns and principles
- Monolithic service (single codebase) but functionally decomposed into:
- Authentication flow
- Resource management (Sheets vs. Drive)
- External service integration pattern
- Configuration-driven (dotenv + Vercel env vars)
- Stateless, REST-style HTTP API

##### Relevant technologies and libraries
- Express (HTTP routing)
- googleapis (Sheets & Drive clients + OAuth2 client)
- dotenv (local config)
- Vercel (serverless hosting)
- Node.js v14+

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% External Entities
    Client["Client\n(Web Browser / HTTP Client)"]:::external
    AuthServer["Google OAuth2\nAuthorization Server"]:::external
    SheetsAPI["Google Sheets API"]:::external
    DriveAPI["Google Drive API"]:::external

    %% Deployment Infrastructure
    subgraph "Vercel Functions" 
        direction TB
        APIServer["API Server\n(Node.js/Express)"]:::internal
        subgraph "Modules"
            direction TB
            OAuthModule["OAuth2 Module"]:::internal
            SheetsModule["Sheets-Management Module"]:::internal
            DriveModule["Drive-Management Module"]:::internal
        end
        EnvConfig["Environment Configuration"]:::config
    end

    %% Additional Components
    Deployment["Deployment Configuration\n(Vercel)"]:::infra
    Dependencies["Dependency Definitions"]:::config
    Metadata["Project Metadata"]:::doc

    %% Interactions
    Client -->|"GET /auth"| OAuthModule
    OAuthModule -->|"Auth Request"| AuthServer
    AuthServer -->|"Redirect /oauth2callback"| OAuthModule
    Client -->|"GET/POST /api?action=..."| APIServer
    APIServer -->|"Token Exchange"| AuthServer
    APIServer -->|"calls Sheets API"| SheetsAPI
    APIServer -->|"calls Drive API"| DriveAPI
    EnvConfig -->|provides creds| OAuthModule
    EnvConfig -->|provides creds| SheetsModule
    EnvConfig -->|provides creds| DriveModule

    %% Click Events
    click APIServer "https://github.com/adimyy/google_script/blob/master/app.js"
    click OAuthModule "https://github.com/adimyy/google_script/blob/master/app.js"
    click SheetsModule "https://github.com/adimyy/google_script/blob/master/app.js"
    click DriveModule "https://github.com/adimyy/google_script/blob/master/app.js"
    click EnvConfig "https://github.com/adimyy/google_script/blob/master/package.json"
    click EnvConfig "https://github.com/adimyy/google_script/blob/master/.gitignore"
    click Deployment "https://github.com/adimyy/google_script/blob/master/vercel.json"
    click Dependencies "https://github.com/adimyy/google_script/blob/master/package.json"
    click Metadata "https://github.com/adimyy/google_script/blob/master/README.md"

    %% Styles
    classDef internal fill:#D0E8FF,stroke:#0366D6,stroke-width:1px
    classDef external fill:#E6FFEA,stroke:#28A745,stroke-width:1px
    classDef infra fill:#F0F0F0,stroke:#6A737D,stroke-width:1px
    classDef config fill:#FFF5D1,stroke:#D29922,stroke-width:1px
    classDef doc fill:#F3E8FF,stroke:#8250DF,stroke-width:1px
```
