Skip to main content

Internal API Service

The Internal API Service is the core business logic engine of the DashClicks platform, handling all proprietary features and internal operations. It serves as the primary service for account management, CRM, billing, content creation, and all platform-specific functionality.

Service Overviewโ€‹

๐Ÿ—๏ธ Architectureโ€‹

  • Service Name: Internal API
  • Port: 5002
  • Entry Point: internal/app.js
  • API Version: v1 (/v1/* routes)
  • Debug Configuration: "API Internal" in VS Code launch.json

๐Ÿ”„ Request Flowโ€‹

Frontend โ†’ Dashboard Gateway (5000) โ†’ API Router (5001) โ†’ Internal API (5002)

All requests to /v1/* routes are proxied to this service by the API Router.

๐Ÿ”— Special Route Handlersโ€‹

GET /:code       # URL shortener redirect handler
GET /v1/:code # Affiliate redirect handler
GET / # Basic health check
GET /status # Service status check

๐Ÿ“ Directory Structureโ€‹

internal/
โ”œโ”€โ”€ app.js # Express application entry point
โ”œโ”€โ”€ package.json # Service dependencies
โ”œโ”€โ”€ jest.config.js # Jest testing configuration
โ”œโ”€โ”€ jest.setup.js # Jest setup configuration
โ”œโ”€โ”€ jest.setup.after.env.js # Jest environment setup
โ”œโ”€โ”€ Dockerfile # Docker container configuration
โ”œโ”€โ”€ README.md # Service documentation
โ”œโ”€โ”€ .dockerignore # Docker ignore patterns
โ””โ”€โ”€ api/
โ”œโ”€โ”€ v1/ # API version 1 implementation
โ”‚ โ”œโ”€โ”€ index.js # Main API router with middleware
โ”‚ โ”œโ”€โ”€ models/ # Copied from /shared/models (Git-ignored)
โ”‚ โ”œโ”€โ”€ utilities/ # Copied from /shared/utilities (Git-ignored)
โ”‚ โ”œโ”€โ”€ __mocks__/ # Jest mock files (e.g., stripe.js)
โ”‚ โ””โ”€โ”€ [24 modules]/ # Business logic modules
โ””โ”€โ”€ v2/ # API version 2 (minimal structure)
โ”œโ”€โ”€ models/ # Copied from /shared/models (Git-ignored)
โ””โ”€โ”€ utilities/ # Copied from /shared/utilities (Git-ignored)

Core Business Modulesโ€‹

The Internal API contains 24 specialized modules handling different aspects of the DashClicks platform:

๐Ÿข Account & User Managementโ€‹

  • accounts/ - Multi-tenant account management, domain configuration, and billing setup
  • users/ - User authentication, profiles, permissions, and workspace management
  • auth/ - JWT authentication, session management, and OAuth integrations

๐Ÿ‘ฅ Customer Relationship Managementโ€‹

  • crm/ - Complete contact management, deal tracking, and pipeline automation
  • activities/ - User activity tracking, audit logs, and engagement analytics
  • forms/ - Lead capture forms, form builders, and submission handling
  • inbound/ - Lead capture automation and inbound marketing workflows

๐Ÿ’ฐ Sales & Revenue Operationsโ€‹

  • billing/ - Subscription management, invoicing, and payment processing
  • store/ - White-label marketplace for digital marketing products
  • onebalance/ - Unified billing and credit management system
  • affiliates/ - Partner and affiliate program management

๐ŸŒ Website & Content Creationโ€‹

  • sites/ - Agency website builder and hosting platform (route: /agencywebsites)
  • instasites/ - Automated website generation and management
  • instareports/ - Automated reporting and analytics generation
  • templates/ - Marketing template management and customization
  • funnels/ - Sales funnel creation and conversion tracking

๐Ÿ’ฌ Communication & Collaborationโ€‹

๐Ÿ› ๏ธ Project & Task Managementโ€‹

  • projects/ - Task management and project collaboration tools
  • reviews/ - Online reputation management and review monitoring (route: /reputation)
  • url-shortener/ - Branded link shortening and click tracking (route: /url)

๐Ÿ”ง System & Integration Servicesโ€‹

  • webhooks/ - External service webhook handling and processing
  • oauth/ - Third-party application authentication (route: /auth/oauth)
  • cors-proxy/ - Cross-origin request handling (route: /corsproxy)
  • filters/ - Data filtering and search functionality
  • shared/ - Shared components and utilities
  • public/ - Public-facing content and landing pages
  • mobile-app/ - Mobile application API endpoints

๐Ÿ” Administrative Functionsโ€‹

  • admin/ - System administration and configuration management (DashClicks internal use only)

Authentication & Authorizationโ€‹

๐Ÿ”‘ JWT Token Authenticationโ€‹

The Internal API uses JWT-based authentication with Bearer tokens:

// All requests require Authorization header
Authorization: Bearer <
jwt_token >
// Token contains user context:
{
userId: 'user_id',
accountId: 'account_id',
permissions: ['read', 'write', 'admin'],
exp: timestamp,
};

๐Ÿ›ก๏ธ Permission Levelsโ€‹

  • User: Basic access to own data and assigned resources
  • Admin: Account-level administration and management
  • Super Admin: Platform-level administration (DashClicks only)
  • API Access: Programmatic access with scoped permissions

๐Ÿ”’ Route Protectionโ€‹

// Middleware chain from /api/v1/index.js
router.use('/accounts', validateLimit, _accounts);

router.use(
'/crm',
verifyAuthorization(), // JWT verification
verifyAccessAndStatus({ accountIDRequired: true }), // Account access validation
validateLimit, // Rate limiting
_crm,
);

// Special route handling
router.use('/auth/oauth', disableLog, _oauth); // OAuth with logging disabled
router.use('/auth', disableLog, _auth); // Auth with logging disabled

// Global middleware chain for all /v1/* routes:
// 1. setStartTime - Request timing
// 2. utilities.validateRequestMethod - HTTP method validation
// 3. Request/response logging middleware

Database Integrationโ€‹

๐Ÿ—„๏ธ MongoDB Connectionโ€‹

The service connects to MongoDB using shared utilities:

const { connect: connectMongo } = require('./api/v1/utilities/db');

// Connection initialized in app.js
const mongoURL = process.env.MONGO_DB_URL;
await connectMongo({
uri: mongoURL,
initiator: 'internal',
});

๐Ÿ“Š Data Modelsโ€‹

Uses 150+ Mongoose models from /shared/models/:

  • Core Models: Account, User, Contact, Deal, Site, Project
  • Integration Models: Service-specific tokens and keys
  • Analytics Models: Campaign data, performance metrics
  • Communication Models: Messages, notifications, conversations
  • Billing Models: Subscriptions, invoices, payments

Service Communicationโ€‹

๐Ÿ”— Inter-Service Communicationโ€‹

The Internal API communicates with other services:

// External API calls
const externalAPI = 'http://localhost:5003';
const response = await axios.get(`${externalAPI}/v1/e/google-ads/campaigns`);

// Socket services
const socketAPI = 'http://localhost:4000';
await axios.post(`${socketAPI}/emit`, { event: 'account_updated', data });

// Queue Manager
const queueAPI = 'http://localhost:6002';
await axios.post(`${queueAPI}/queue/billing`, { action: 'process_subscription' });

๐ŸŒ External Service Integrationโ€‹

Coordinates with external services through the External API:

  • Google Services: Ads, Analytics, Business profiles
  • Facebook/Meta: Advertising and social media
  • Payment Processing: Stripe, PayPal integrations
  • Communication: Twilio, SendGrid, email providers
  • CRM Systems: Salesforce, HubSpot, Pipedrive

Development Workflowโ€‹

๐Ÿš€ Running the Serviceโ€‹

Always use VS Code debugger - never run directly:

# In VS Code:
# 1. Go to Debug Panel (Ctrl+Shift+D)
# 2. Select "API Internal"
# 3. Press F5 or click green play button
# Service starts on http://localhost:5002

๐Ÿ”ง Development Commandsโ€‹

# Install dependencies
cd internal/
npm install

# Run tests
npm test

# Run specific test suites
npm test -- --testPathPattern=accounts
npm test -- --testPathPattern=crm

# Start development server
npm run dev # Uses nodemon for auto-restart
npm start # Production start

# Service available at http://localhost:5002

๐Ÿ“ Adding New Endpointsโ€‹

Module structure varies by implementation pattern:

// Pattern 1: Lowercase folders (newer modules)
// internal/api/v1/[module]/controllers/
// internal/api/v1/[module]/routes/
// internal/api/v1/[module]/services/
// internal/api/v1/[module]/validations/ or schemas/
// internal/api/v1/[module]/utils/
// internal/api/v1/[module]/tests/

// Pattern 2: Capitalized folders (legacy modules)
// internal/api/v1/[module]/Controllers/
// internal/api/v1/[module]/Routes/
// internal/api/v1/[module]/Models/
// internal/api/v1/[module]/Utils/ or utilities/
// internal/api/v1/[module]/validators/

// Steps to add new endpoint:
// 1. Add route handler in routes/ or Routes/
// 2. Implement controller logic in controllers/ or Controllers/
// 3. Add validation in validations/, schemas/, or validators/
// 4. Add business logic in services/ (if available)
// 5. Export route in module index.js
// 6. Register in main /api/v1/index.js with middleware

๐Ÿงช Testing Strategyโ€‹

Tests are located in module-specific tests/ folders:

// Jest configuration files:
// internal/jest.config.js # Main Jest configuration
// internal/jest.setup.js # Global test setup
// internal/jest.setup.after.env.js # Environment-specific setup

// Module test structure:
// internal/api/v1/[module]/tests/ # Module-specific tests
// โ”œโ”€โ”€ controllers/ # Controller unit tests
// โ”œโ”€โ”€ routes/ # Route integration tests
// โ”œโ”€โ”€ services/ # Business logic tests
// โ””โ”€โ”€ utils/ # Utility function tests

// Mock dependencies available:
// internal/api/v1/__mocks__/stripe.js # Stripe service mocks

API Endpoints Overviewโ€‹

๐Ÿ”„ Common Patternsโ€‹

Most modules follow RESTful patterns:

GET    /v1/[module]           # List resources
GET /v1/[module]/:id # Get specific resource
POST /v1/[module] # Create new resource
PUT /v1/[module]/:id # Update resource
DELETE /v1/[module]/:id # Delete resource

# Advanced patterns
GET /v1/[module]/search # Search resources
POST /v1/[module]/bulk # Bulk operations
GET /v1/[module]/export # Export data
POST /v1/[module]/import # Import data

๐Ÿ“Š Response Formatโ€‹

Standard API response structure:

{
"success": true,
"data": {
/* resource data */
},
"message": "Resource retrieved successfully",
"pagination": {
"page": 1,
"limit": 20,
"total": 150,
"totalPages": 8
},
"meta": {
"timestamp": "2025-10-03T10:30:00Z",
"version": "v1"
}
}

โŒ Error Handlingโ€‹

Centralized error handling middleware in app.js:

// Standard error response format
{
"success": false,
"errno": 400, // HTTP status code
"message": "Error description",
"additional_info": "Detailed error information",
"data": null // Optional error-specific data
}

// Special handling for MongoDB duplicate key errors:
// E11000 duplicate key -> "Duplicate document."

// Axios error handling:
// - Uses response status and data from external API calls
// - Strips sensitive config information

// Global error types handled:
// - Validation errors (Joi)
// - MongoDB duplicate key errors
// - Axios HTTP errors
// - Unhandled promise rejections
// - Uncaught exceptions
// - SIGTERM signals for graceful shutdown

Performance & Optimizationโ€‹

โšก Caching Strategyโ€‹

  • Redis Integration: Session caching and temporary data
  • Database Indexing: Optimized queries with proper indexes
  • Connection Pooling: Efficient database connection management
  • Response Caching: Cached responses for frequently accessed data

๐Ÿ“ˆ Monitoring & Metricsโ€‹

  • Health Checks: /v1/health endpoint for service monitoring
  • Performance Logging: Request/response time tracking
  • Error Tracking: Comprehensive error logging and alerting
  • Resource Monitoring: Memory and CPU usage tracking

๐Ÿ” Debugging Featuresโ€‹

  • Request Logging: Detailed request/response logging
  • Error Stack Traces: Full error context in development
  • Database Query Logging: MongoDB query performance monitoring
  • Service Communication Logging: Inter-service call tracking

Configuration & Environmentโ€‹

๐Ÿ”ง Environment Variablesโ€‹

Key configuration variables for the Internal API:

# Database
MONGO_DB_URL=mongodb://localhost:27017/dashclicks

# Service Configuration
PORT=5002 # Service port (default: 5002)

# Authentication & Security
JWT_SECRET=your_jwt_secret_key
JWT_EXPIRES_IN=24h

# Third-party Services (from package.json dependencies)
# Stripe integration
STRIPE_SECRET_KEY=sk_test_...

# Twilio communications
TWILIO_ACCOUNT_SID=AC...
TWILIO_AUTH_TOKEN=...

# SendGrid email
SENDGRID_API_KEY=SG...

# AWS services
AWS_ACCESS_KEY_ID=...
AWS_SECRET_ACCESS_KEY=...

# Google Cloud services
GOOGLE_CLOUD_PROJECT_ID=...

# Firebase Admin SDK
FIREBASE_ADMIN_SDK_JSON=...

๐Ÿ“ฆ Key Dependenciesโ€‹

From package.json - major integrations and frameworks:

{
"express": "^4.17.1", // Web framework
"mongoose": "^8.18.0", // MongoDB ODM
"joi": "^17.2.1", // Input validation
"jsonwebtoken": "^8.5.1", // JWT authentication
"stripe": "^10.0.0", // Payment processing
"twilio": "^4.21.0", // SMS/Voice communications
"aws-sdk": "^2.1031.0", // AWS integrations
"@sendgrid/client": "^7.6.0", // Email services
"firebase-admin": "^12.7.0", // Firebase integration
"axios": "^0.21.1", // HTTP client
"moment": "^2.29.1", // Date/time handling
"lodash": "^4.17.21", // Utility functions
"multer": "^1.4.2", // File upload handling
"puppeteer": "^19.4.0" // Browser automation (dev)
}

๐Ÿ”’ Security Configurationโ€‹

  • CORS Settings: Configurable cross-origin request handling
  • Rate Limiting: API request rate limiting and throttling
  • Input Validation: Comprehensive request validation
  • SQL Injection Protection: Parameterized queries and input sanitization

Deployment & Scalingโ€‹

๐Ÿณ Docker Configurationโ€‹

# internal/Dockerfile
FROM node:18-alpine
WORKDIR /internal
COPY package.json /internal

RUN npm install

COPY . /internal
CMD node app.js
EXPOSE 8080

Note: Container exposes port 8080, but service runs on port 5002 (configurable via PORT environment variable).

๐Ÿ”„ Health Monitoringโ€‹

// Basic health check endpoints
GET / # Returns: { "status": "ok" }
GET /status # Returns: { "status": "ok" }

// Dashboard version header
// All responses include X-Dashboard-Version header
// Retrieved from DashboardMeta collection (type: 'version')

๐Ÿ“Š Load Balancingโ€‹

  • Horizontal Scaling: Multiple service instances
  • Database Connection Pooling: Efficient resource utilization
  • Stateless Design: No server-side session storage
  • Microservice Architecture: Independent scaling per module

Module Documentationโ€‹

Each module has detailed documentation covering:

  • Architecture & Design: Module structure and patterns
  • API Endpoints: Complete endpoint documentation
  • Database Models: Data structures and relationships
  • Business Logic: Core functionality and workflows
  • Integration Points: External service connections
  • Testing Strategy: Unit and integration test coverage
  • Configuration: Module-specific settings
  • Troubleshooting: Common issues and solutions

๐Ÿ“š Available Module Docsโ€‹

Click on any module below for detailed documentation:

Core Business Modulesโ€‹

  • accounts/ - Account management and configuration
  • users/ - User authentication and profiles
  • auth/ - Authentication and authorization
  • crm/ - Customer relationship management
  • billing/ - Subscription and payment processing

Content & Website Managementโ€‹

Communication & Collaborationโ€‹

Sales & Marketingโ€‹

System & Integrationโ€‹

Complete coverage of all 24 internal modules available.


Support & Resourcesโ€‹

๐Ÿ†˜ Getting Helpโ€‹

  • Documentation: Complete module docs in /docs/service/internal/
  • Code Examples: Reference implementations in each module
  • Testing: Comprehensive test suites for guidance
  • Debugging: VS Code debugger with breakpoint support

๐Ÿ“ Development Guidelinesโ€‹

  • Always use VS Code debugger for service development
  • Follow shared code management rules (never edit service-level models/utilities)
  • Implement comprehensive error handling and validation
  • Write tests for new features and endpoints
  • Document API changes and new functionality
  • Use consistent response formats across modules
๐Ÿ’ฌ

Documentation Assistant

Ask me anything about the docs

Hi! I'm your documentation assistant. Ask me anything about the docs!

I can help you with:
- Code examples
- Configuration details
- Troubleshooting
- Best practices

Try asking: How do I configure the API?
09:31 AM