#!/bin/bash # # COMPREHENSIVE Ordrestyring Data Sync Script # Imports ALL data from Ordrestyring API v2 for advanced tilbudsberegning # Includes: Cases, Materials, Hours, Calendar, Employees, Financial data # Suitable for running as hourly cron job for fresh data # set -e # Colors for output RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' PURPLE='\033[0;35m' CYAN='\033[0;36m' NC='\033[0m' # Configuration SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" LOG_FILE="$SCRIPT_DIR/ordrestyring_comprehensive_sync.log" LOCK_FILE="$SCRIPT_DIR/ordrestyring_comprehensive_sync.lock" # Function to log with timestamp log() { echo -e "$(date '+%Y-%m-%d %H:%M:%S') - $1" | tee -a "$LOG_FILE" } # Function to cleanup on exit cleanup() { if [ -f "$LOCK_FILE" ]; then rm -f "$LOCK_FILE" fi } trap cleanup EXIT # Check if another sync is already running if [ -f "$LOCK_FILE" ]; then log "${RED}❌ Another comprehensive sync process is already running (lock file exists)${NC}" exit 1 fi # Create lock file echo $$ > "$LOCK_FILE" log "${CYAN}🚀 Starting COMPREHENSIVE Ordrestyring sync for Tilbudsberegning${NC}" log "${BLUE}📊 Importing: Cases, Materials, Hours, Calendar, Employees, Financial Data${NC}" # Export API token (using the working v2 API key) export ORDRESTYRING_TOKEN="" # Change to script directory cd "$SCRIPT_DIR" # Check if comprehensive import script exists if [ ! -f "comprehensive_ordrestyring_import.js" ]; then log "${RED}❌ Comprehensive import script not found: comprehensive_ordrestyring_import.js${NC}" exit 1 fi # Check database connection log "${YELLOW}🔍 Checking database connection...${NC}" if ! command -v mysql &> /dev/null; then log "${RED}❌ MySQL client not found${NC}" exit 1 fi # Test database connection if ! mysql -u ordrestyring_user -p"${ORDRESTYRING_DB_PASSWORD}" -h localhost ordrestyring_local -e "SELECT 1;" &> /dev/null; then log "${RED}❌ Database connection failed${NC}" exit 1 fi log "${GREEN}✅ Database connection verified${NC}" # Run comprehensive sync log "${PURPLE}📥 Running comprehensive Ordrestyring data sync...${NC}" log "${CYAN} - All Cases with historical data${NC}" log "${CYAN} - Customer database (Debtors)${NC}" log "${CYAN} - Employee data and types${NC}" log "${CYAN} - Time tracking (Hours)${NC}" log "${CYAN} - Materials and cost data${NC}" log "${CYAN} - Calendar/planning data${NC}" log "${CYAN} - Financial data (Invoices)${NC}" log "${CYAN} - Analytics generation${NC}" # Run the comprehensive import START_TIME=$(date +%s) node comprehensive_ordrestyring_import.js 2>&1 | tee -a "$LOG_FILE" # Check exit status IMPORT_EXIT_CODE=${PIPESTATUS[0]} END_TIME=$(date +%s) DURATION=$((END_TIME - START_TIME)) if [ $IMPORT_EXIT_CODE -eq 0 ]; then log "${GREEN}✅ Comprehensive sync completed successfully in ${DURATION} seconds${NC}" # Log success metrics TOTAL_RECORDS=$(mysql -u ordrestyring_user -p"${ORDRESTYRING_DB_PASSWORD}" -h localhost ordrestyring_local -se " SELECT (SELECT COUNT(*) FROM ordrestyring_cases) + (SELECT COUNT(*) FROM ordrestyring_debtors) + (SELECT COUNT(*) FROM ordrestyring_users) + (SELECT COUNT(*) FROM ordrestyring_hours) + (SELECT COUNT(*) FROM ordrestyring_case_materials) + (SELECT COUNT(*) FROM ordrestyring_calendar) as total_records " 2>/dev/null || echo "0") log "${GREEN}📊 Total records in local database: ${TOTAL_RECORDS}${NC}" # Check for active employees ACTIVE_EMPLOYEES=$(mysql -u ordrestyring_user -p"${ORDRESTYRING_DB_PASSWORD}" -h localhost ordrestyring_local -se " SELECT COUNT(*) FROM ordrestyring_users WHERE full_name IS NOT NULL AND email IS NOT NULL " 2>/dev/null || echo "0") log "${GREEN}👥 Active employees found: ${ACTIVE_EMPLOYEES}${NC}" # Check calendar entries for planning CALENDAR_ENTRIES=$(mysql -u ordrestyring_user -p"${ORDRESTYRING_DB_PASSWORD}" -h localhost ordrestyring_local -se " SELECT COUNT(*) FROM ordrestyring_calendar WHERE start_time > UNIX_TIMESTAMP(CURDATE()) AND start_time < UNIX_TIMESTAMP(DATE_ADD(CURDATE(), INTERVAL 7 DAY)) " 2>/dev/null || echo "0") log "${GREEN}📅 Calendar entries for next 7 days: ${CALENDAR_ENTRIES}${NC}" # Update sync status for tilbudsberegning app echo "{ \"last_sync\": \"$(date -Iseconds)\", \"status\": \"success\", \"duration_seconds\": $DURATION, \"total_records\": $TOTAL_RECORDS, \"active_employees\": $ACTIVE_EMPLOYEES, \"calendar_entries_next_7_days\": $CALENDAR_ENTRIES }" > "$SCRIPT_DIR/last_sync_status.json" else log "${RED}❌ Comprehensive sync failed with exit code: $IMPORT_EXIT_CODE${NC}" # Update error status echo "{ \"last_sync\": \"$(date -Iseconds)\", \"status\": \"failed\", \"error_code\": $IMPORT_EXIT_CODE, \"duration_seconds\": $DURATION }" > "$SCRIPT_DIR/last_sync_status.json" exit 1 fi log "${PURPLE}🎉 COMPREHENSIVE Ordrestyring sync completed successfully${NC}" log "${BLUE}💡 Data is now ready for advanced tilbudsberegning with historical analysis${NC}" log "${CYAN}📋 Calendar data available for tilbudsbereger.alw.dk planning integration${NC}"