🛒 Shrinkit Shopify Backend¶
E-commerce automation, Shopify/Odoo integration, and multi-app platform
Overview¶
The Shrinkit Shopify Backend is a comprehensive Flask-based application that handles multiple Shopify apps including Shrinkit, Price Updater, Lens Recommender, and Odoo integration. It provides e-commerce automation, dynamic pricing, product recommendations, and ERP synchronization.
🏗️ Architecture¶
Core Responsibilities¶
- E-commerce Automation: Abandoned cart recovery, customer lifecycle management
- Dynamic Pricing: Exchange rate-based pricing, bulk price management
- Product Recommendations: AI-powered lens recommendations for optical products
- ERP Integration: Bidirectional sync between Shopify and Odoo
- Multi-App Support: Shrinkit, Price Updater, Lens Recommender, Private Apps
- Webhook Processing: Real-time Shopify event processing
Technology Stack¶
- Python Flask: Web application framework
- MongoDB: Primary data store via Parse Server
- Redis: Caching and session management
- Shopify GraphQL/REST APIs: External Shopify store interactions
- Odoo XML-RPC: ERP integration
- AWS S3: File storage and CDN
🔧 Configuration¶
Server Details¶
- Server:
ovh-shopify-backend - Port: 8456 (HTTP), 8457 (HTTPS)
- Environment: Production, Staging, Development
Environment Variables¶
# Shopify Configuration
SHOPIFY_API_KEY=ask your direct manager for the access
SHOPIFY_API_SECRET=ask your direct manager for the access
SHOPIFY_API_VERSION=2023-10
HOST=https://shopify-backend.appgain.io
# Database Configuration
MONGODB_URI=mongodb://ovh-mongo-master:27017/shopify_backend
REDIS_HOST=ovh-redis
REDIS_PORT=6379
REDIS_PASSWORD=ask your direct manager for the access
# External Services
APPGAIN_API=https://api.appgain.io
ODOO_API_URL=https://odoo.appgain.io
AWS_S3_BUCKET=appgain-shopify-files
# Security
SECRET_KEY=ask your direct manager for the access
OTP_SECRET=ask your direct manager for the access
# Monitoring
SENTRY_DSN=ask your direct manager for the access
PROMETHEUS_ENABLED=true
LOG_LEVEL=info
📊 Application Types¶
Shrinkit - Main E-commerce Automation¶
// Core Features
- Abandoned cart recovery (15-30% sales recovery)
- Customer lifecycle management
- Order processing automation
- Smart link generation
- Customer segmentation
- Performance analytics
// Business Value
- Recovers 15-30% of lost sales
- Improves conversion rates
- Reduces manual work
- Improves order accuracy
Price Updater - Dynamic Pricing Management¶
// Core Features
- Exchange rate-based pricing
- Bulk price management
- Competitive pricing analysis
- Test mode for safe updates
- Audit trail of changes
- Rollback capabilities
// Business Value
- Optimizes pricing for international markets
- Maximizes profits
- Saves hours of manual work
- Reduces pricing errors
Lens Recommender - Optical Product Recommendations¶
// Core Features
- Personalized lens recommendations
- Inventory optimization
- Customer consultation support
- AI-powered matching
- Product categorization
- Consultation reports
// Business Value
- Improves customer experience
- Increases conversion rates
- Reduces inventory costs
- Improves consultation quality
Private Apps - Custom Integrations¶
// Core Features
- Custom automation workflows
- Specialized integrations
- Tailored solutions
- Platform-specific features
- Custom webhooks
- Extended functionality
// Business Value
- Tailored solutions for specific business needs
- Platform-specific optimizations
- Custom automation workflows
🔗 Integration Points¶
Shopify Integration¶
// GraphQL API Integration
const SHOPIFY_GRAPHQL_ENDPOINT = 'https://{shop}.myshopify.com/admin/api/2023-10/graphql.json';
// REST API Integration
const SHOPIFY_REST_ENDPOINT = 'https://{shop}.myshopify.com/admin/api/2023-10';
// Webhook Processing
const WEBHOOK_TOPICS = [
'orders/create',
'orders/updated',
'orders/cancelled',
'customers/create',
'customers/updated',
'products/create',
'products/updated',
'checkouts/create',
'checkouts/updated'
];
Odoo ERP Integration¶
// XML-RPC Configuration
const ODOO_CONFIG = {
host: 'odoo.appgain.io',
port: 8069,
database: 'appgain_erp',
username: 'shopify_integration',
password: 'ask your direct manager for the access'
};
// Sync Operations
- Product synchronization (bidirectional)
- Order management integration
- Customer data sync
- Financial reconciliation
- Inventory synchronization
Appgain Platform Integration¶
// Automation Workflows
- Trigger automated campaigns
- Customer data processing
- Analytics integration
- Notification delivery
- Smart link generation
// API Endpoints
- User management
- Campaign creation
- Analytics data
- Notification sending
📁 Project Structure¶
shopify-backend/
├── api/ # Main application code
│ ├── abandoned_checkouts/ # Abandoned cart automation
│ ├── core/ # Core app installation & management
│ ├── gql/ # GraphQL queries & mutations
│ ├── helpers/ # Utility functions
│ ├── lens_recommender/ # Lens recommendation engine
│ ├── odoo/ # Odoo ERP integration
│ ├── price_updater/ # Dynamic pricing system
│ ├── products/ # Product management APIs
│ ├── smartlinks/ # URL shortening & tracking
│ ├── stores/ # Store management
│ ├── webhooks/ # Shopify webhook handlers
│ ├── app.py # Flask app factory
│ ├── config.py # Configuration management
│ ├── errors.py # Error handling classes
│ ├── extensions.py # Flask extensions setup
│ └── logger.py # Logging configuration
├── instance/ # Environment-specific config
├── tests/ # Test suite
├── Dockerfile # Container configuration
├── docker-compose.yml # Multi-service orchestration
├── requirements.txt # Python dependencies
└── README.md # Documentation
🔐 Authentication Models¶
Appgain JWT Authentication¶
// JWT Token Format
Authorization: Bearer <jwt_token>
// Token Validation
- Validated via validate_store_and_auth_token()
- Used for dashboard and admin requests
- Contains user ownership claims
- Account and suit-level permissions
Shopify Bearer JWT¶
// Shopify Bearer Token
Authorization: Bearer <shopify_token>
// Usage
- Short-lived tokens from Shopify Admin UI
- Used only for /graphql proxy endpoint
- Validated against SHOPIFY_API_SECRET
- Platform-specific authentication
Shopify Access Token¶
// Access Token Storage
- Permanent tokens stored after OAuth
- Used for internal Shopify API calls
- Retrieved from active_stores dictionary
- Store-specific authentication
No Authentication¶
// Public Endpoints
- Shopify webhooks (trusted origin)
- Internal cron jobs
- Health check endpoints
- Public API endpoints
📊 Database Schema¶
Store Configuration¶
// shopifyActiveShops Collection
{
"_id": "ObjectId",
"name": "store.myshopify.com",
"type": "shrinkit|price_updater|lens_recommender|private_app",
"accessToken": "shpat_...",
"apiKey": "appgain_api_key",
"appId": "appgain_app_id",
"subdomain": "store_subdomain",
"accountId": "user_account_id",
"authtoken": "jwt_token",
"userEmail": "store_owner@email.com",
"otpEnabled": false,
"v3Status": "enabled|disabled|in-progress|failed",
"odoo": {
"host": "odoo.example.com",
"db": "database_name",
"user": "username",
"password": "ask your direct manager for the access",
"status": "synced|failed|in-progress",
"last_sync_at": "2024-01-01T00:00:00Z"
}
}
Dynamic Collections (per store)¶
// Store-specific Collections
- Users{store_subdomain} - Customer data
- Purchases{store_subdomain} - Order data
- Products{store_subdomain} - Product data
- Checkouts{store_subdomain} - Abandoned checkouts
- Analytics{store_subdomain} - Analytics data
🔄 Business Scenarios¶
Abandoned Cart Recovery¶
// Process Flow
1. System detects abandoned checkout via Shopify webhook
2. Stores checkout data in Parse database
3. Triggers automated email/SMS campaigns via Appgain
4. Tracks recovery success rates and revenue
// Business Value
- Recovers 15-30% of lost sales
- Increases conversion rates
- Improves customer engagement
Dynamic Pricing Management¶
// Process Flow
1. System monitors exchange rates (USD to local currency)
2. Automatically updates product prices based on current rates
3. Maintains profit margins while staying competitive
4. Provides test mode for safe price updates
// Business Value
- Optimizes pricing for international markets
- Maximizes profits
- Reduces manual pricing work
Customer Lifecycle Management¶
// Process Flow
1. Customer data synced to Parse database
2. Segmented based on purchase history and behavior
3. Personalized marketing campaigns triggered
4. Loyalty programs and retention strategies applied
// Business Value
- Improves customer retention
- Increases lifetime value
- Enhances customer experience
ERP Integration (Odoo)¶
// Process Flow
1. Bidirectional sync between Shopify and Odoo
2. Automatic product creation/updates
3. Order management integration
4. Financial reconciliation
// Business Value
- Eliminates data duplication
- Ensures consistency
- Streamlines operations
📈 Performance & Scaling¶
Multi-Store Support¶
// Store Management
- 100+ active Shopify stores
- Per-store data isolation
- Store-specific configurations
- Independent scaling
// Performance Metrics
- 1,000+ webhooks per hour
- Real-time data synchronization
- < 500ms average response time
- 99.9% service availability
Caching Strategy¶
// Redis Caching
- Store configurations
- Product data
- Customer information
- API responses
- Session data
// Cache Invalidation
- Time-based expiration
- Event-based invalidation
- Manual cache clearing
Database Optimization¶
// Indexes
- Store name (unique)
- Access token
- Account ID
- Store type
- Sync timestamps
// Query Optimization
- Aggregation pipelines
- Read replicas
- Connection pooling
- Query caching
🔍 Monitoring & Observability¶
Health Checks¶
# Service Health
GET /health
# Response Format
{
"status": "healthy",
"timestamp": "2024-01-01T00:00:00Z",
"version": "1.0.0",
"services": {
"mongodb": "connected",
"redis": "connected",
"shopify_api": "connected",
"odoo_api": "connected"
},
"stores": {
"active": 100,
"syncing": 5,
"failed": 1
}
}
Prometheus Metrics¶
// Custom Metrics
- webhook_processing_duration
- api_request_duration
- store_sync_status
- odoo_sync_duration
- abandoned_cart_recovery_rate
- price_update_success_rate
Error Tracking¶
// Error Categories
- Shopify API errors
- Odoo sync failures
- Webhook processing errors
- Authentication failures
- Database connection issues
// Error Handling
- Automatic retry mechanisms
- Error logging and alerting
- Graceful degradation
- Circuit breaker patterns
🚀 Deployment¶
Docker Configuration¶
# Dockerfile
FROM python:3.9-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . .
EXPOSE 8456
CMD ["python", "run.py"]
Docker Compose¶
# docker-compose.yml
version: '3.8'
services:
shopify-backend:
build: .
ports:
- "8456:8456"
environment:
- NODE_ENV=production
- MONGODB_URI=mongodb://mongo:27017/shopify_backend
- REDIS_HOST=redis
depends_on:
- mongo
- redis
networks:
- appgain-net
deploy:
replicas: 3
mongo:
image: mongo:6.0
volumes:
- mongo-data:/data/db
networks:
- appgain-net
redis:
image: redis:7-alpine
volumes:
- redis-data:/data
networks:
- appgain-net
volumes:
mongo-data:
redis-data:
networks:
appgain-net:
driver: bridge
Kubernetes Deployment¶
# k8s-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: shopify-backend
spec:
replicas: 3
selector:
matchLabels:
app: shopify-backend
template:
metadata:
labels:
app: shopify-backend
spec:
containers:
- name: shopify-backend
image: appgain/shopify-backend:latest
env:
- name: NODE_ENV
value: "production"
- name: MONGODB_URI
value: "mongodb://mongo-cluster:27017/shopify_backend"
- name: REDIS_HOST
value: "redis-cluster"
resources:
requests:
memory: "512Mi"
cpu: "500m"
limits:
memory: "1Gi"
cpu: "1000m"
livenessProbe:
httpGet:
path: /health
port: 8456
initialDelaySeconds: 30
periodSeconds: 10
readinessProbe:
httpGet:
path: /health
port: 8456
initialDelaySeconds: 5
periodSeconds: 5
🔧 Development¶
Local Development Setup¶
# Clone repository
git clone <repository>
cd shopify-backend
# Create virtual environment
python3 -m venv venv
source venv/bin/activate
# Install dependencies
pip install -r requirements.txt
# Environment setup
cp instance/config.example.py instance/config.py
# Edit config.py with local configuration
# Start development server
./startDev
# Run tests
python -m pytest
# Run linting
flake8 api/
API Testing¶
# Test store installation
curl -X GET "http://localhost:8456/?shop=test-store.myshopify.com"
# Test webhook processing
curl -X POST "http://localhost:8456/webhooks/orders/create" \
-H "X-Shopify-Topic: orders/create" \
-H "X-Shopify-Shop-Domain: test-store.myshopify.com" \
-d '{"order": {"id": 123}}'
# Test Odoo sync
curl -X POST "http://localhost:8456/odoo/configs/sync" \
-H "Authorization: Bearer <token>"
🔒 Security¶
Input Validation¶
# Pydantic Schema Validation
from pydantic import BaseModel, EmailStr
class StoreConfig(BaseModel):
name: str
access_token: str
api_key: str
app_id: str
subdomain: str
account_id: str
user_email: EmailStr
odoo_config: Optional[OdooConfig] = None
Rate Limiting¶
# Flask-Limiter Configuration
from flask_limiter import Limiter
from flask_limiter.util import get_remote_address
limiter = Limiter(
app,
key_func=get_remote_address,
default_limits=["200 per day", "50 per hour"]
)
@app.route("/api/products")
@limiter.limit("100 per minute")
def get_products():
# API logic
pass
Secret Management¶
# Environment-based Configuration
import os
from cryptography.fernet import Fernet
def get_secret(key_name):
"""Get secret from environment or encrypted storage"""
secret = os.getenv(key_name)
if not secret:
# Decrypt from secure storage
secret = decrypt_secret(key_name)
return secret
📞 Support & Resources¶
Documentation¶
Monitoring Tools¶
- Sentry: Error tracking and performance monitoring
- Prometheus: Metrics collection and alerting
- Grafana: Dashboard visualization
- Custom Analytics: Store performance and sync status
Development Resources¶
- GitHub Repository: Source code and issues
- Postman Collection: API testing
- Swagger Documentation: Interactive API docs
- Development Environment: Docker setup
Last updated: January 2024
Ask Chehab GPT