# lamlai/sync-chative architecture

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

- Interactive diagram: https://gitdiagram.com/lamlai/sync-chative
- Repository: https://github.com/lamlai/sync-chative

## Overview

##### Main Components
- WordPress Plugin Environment: This hosts the entire plugin and ties into the WordPress lifecycle.
- Admin Interface: Represented by the settings page (settings-page.php), logs viewer (logs-page.php), and ancillary admin styles (admin-styles.php) within the “templates” folder.
- Synchronization Logic:
- API Integration: The component responsible for fetching contacts from Chative’s API.
- Sync Manager: The module handling the syncing process (including duplicate checks, sync interval handling, manual trigger logic, and CS validation) likely defined in functions.php.
- Background Processing: The WordPress cron-based scheduling mechanism that automatically triggers synchronization every 5 minutes.
- Logging System: The component dedicated to recording sync attempts, including success/failure status, history, and log cleanup.

##### Relationships and Interactions
- How the WordPress Environment triggers the Plugin’s activation and initializes the sync components.
- Logging System with the Sync Manager, showing that all sync attempts and outcomes are recorded.

##### Important Architectural Patterns and Design Principles
- Separation of Concerns: Clearly separate UI, background processing, API integration, and logging.
- Modular Design: Each component (e.g., admin interface, sync management, logging) can be represented as discrete modules or blocks.
- Integration Layer: Mark the external integrations (Chative API and FluentCRM) as separate entities to emphasize external dependencies.
- Event-driven Triggers: Emphasize using WordPress cron for auto-triggered events versus manual triggers from the admin UI.

##### Incorporate Relevant Technologies and Libraries
- External APIs: Clearly denote the Chative API and the FluentCRM system.
- UI Components: Represent that the admin interface is built using PHP templates (files in the “templates” folder) and potentially some CSS/JS (as hinted by admin-styles.php).

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Top Level Plugin Loader
    WPEnv["WordPress Environment / Plugin Loader"]:::plugin

    %% Admin Interface Subgraph
    subgraph "Admin Interface"
        Settings["Settings Page"]:::admin
        Logs["Logs Viewer"]:::admin
        AdminStyles["Admin Styles"]:::admin
    end

    %% Background Scheduler Subgraph
    subgraph "Background Services"
        Cron["Background Scheduler (WP Cron)"]:::background
    end

    %% Synchronization Module Subgraph
    subgraph "Synchronization Module"
        SyncManager["Sync Manager (Manual & Scheduled)"]:::sync
        APIIntegration["Chative API Integration"]:::sync
        FluentCRM["FluentCRM / Webhook Sender"]:::sync
        Logging["Logging System"]:::sync
    end

    %% External Systems
    AAPI(("Chative API")):::external
    FCRM(("FluentCRM")):::external

    %% Relationships
    WPEnv -->|"Initializes"| Settings
    WPEnv -->|"Initializes"| Cron
    WPEnv -->|"Initializes"| SyncManager

    Settings -->|"TriggerManualSync"| SyncManager
    Settings -->|"AccessLogs"| Logs

    Cron -->|"ScheduledTrigger"| SyncManager

    SyncManager -->|"FetchContacts"| APIIntegration
    APIIntegration -->|"GetData"| AAPI

    SyncManager -->|"UpdateContact"| FluentCRM
    FluentCRM -->|"SendData"| FCRM

    SyncManager -->|"RecordSync"| Logging

    %% Styles
    classDef plugin fill:#d5f5e3,stroke:#82e0aa,stroke-width:2px;
    classDef admin fill:#f2d7d5,stroke:#d98880,stroke-width:2px;
    classDef background fill:#d6eaf8,stroke:#5dade2,stroke-width:2px;
    classDef sync fill:#fcf3cf,stroke:#f7dc6f,stroke-width:2px;
    classDef external fill:#e8daef,stroke:#af7ac5,stroke-width:2px,stroke-dasharray: 5 5;

    %% Click Events
    click WPEnv "https://github.com/lamlai/sync-chative/blob/main/index.php"
    click Settings "https://github.com/lamlai/sync-chative/blob/main/templates/settings-page.php"
    click Logs "https://github.com/lamlai/sync-chative/blob/main/templates/logs-page.php"
    click AdminStyles "https://github.com/lamlai/sync-chative/blob/main/templates/admin-styles.php"
    click SyncManager "https://github.com/lamlai/sync-chative/blob/main/functions.php"
    click APIIntegration "https://github.com/lamlai/sync-chative/blob/main/functions.php"
    click FluentCRM "https://github.com/lamlai/sync-chative/blob/main/functions.php"
    click Logging "https://github.com/lamlai/sync-chative/blob/main/functions.php"
```
