#!/bin/bash # Directus Collection Setup Script # This script creates collections for our custom database tables in Directus DIRECTUS_URL="http://localhost:8055" ADMIN_EMAIL="admin@assetmanagement.com" ADMIN_PASSWORD="AssetAdmin2024!" echo "🚀 Setting up Directus collections..." echo # Authenticate and get token echo "🔑 Authenticating with Directus..." AUTH_RESPONSE=$(curl -s -X POST "$DIRECTUS_URL/auth/login" \ -H "Content-Type: application/json" \ -d "{\"email\":\"$ADMIN_EMAIL\",\"password\":\"$ADMIN_PASSWORD\"}") # Extract access token ACCESS_TOKEN=$(echo $AUTH_RESPONSE | grep -o '"access_token":"[^"]*"' | cut -d'"' -f4) if [ -z "$ACCESS_TOKEN" ]; then echo "❌ Authentication failed" echo "Response: $AUTH_RESPONSE" exit 1 fi echo "✅ Authentication successful" echo # Function to create a collection create_collection() { local collection_name=$1 local display_name=$2 local icon=$3 local color=$4 local note=$5 echo "📁 Creating collection: $collection_name" # Check if collection exists EXISTING=$(curl -s -H "Authorization: Bearer $ACCESS_TOKEN" \ "$DIRECTUS_URL/collections/$collection_name" | grep -c "collection") if [ "$EXISTING" -gt 0 ]; then echo "â„šī¸ Collection $collection_name already exists, skipping..." return fi # Create collection RESPONSE=$(curl -s -X POST "$DIRECTUS_URL/collections" \ -H "Authorization: Bearer $ACCESS_TOKEN" \ -H "Content-Type: application/json" \ -d "{ \"collection\": \"$collection_name\", \"meta\": { \"accountability\": \"all\", \"archive_app_filter\": true, \"collapse\": \"open\", \"collection\": \"$collection_name\", \"color\": \"$color\", \"display_template\": null, \"hidden\": false, \"icon\": \"$icon\", \"note\": \"$note\", \"singleton\": false, \"sort\": null, \"versioning\": false } }") if echo "$RESPONSE" | grep -q "error"; then echo "âš ī¸ Warning: $RESPONSE" else echo "✅ Collection $collection_name created successfully" fi } echo "📁 Creating collections..." echo # Create all collections create_collection "organizations" "Organizations" "business" "#6644FF" "SaaS tenant organizations" create_collection "subscription_plans" "Subscription Plans" "paid" "#00C897" "Available subscription plans" create_collection "asset_categories" "Asset Categories" "category" "#FF6B35" "Asset categorization" create_collection "locations" "Locations" "place" "#4CAF50" "Asset locations and facilities" create_collection "vendors" "Vendors" "store" "#9C27B0" "Vendors and suppliers" create_collection "assets" "Assets" "inventory" "#0078D4" "Main asset registry" create_collection "asset_components" "Asset Components" "construction" "#FF9800" "Asset components and parts" create_collection "asset_qr_codes" "Asset QR Codes" "qr_code" "#795548" "QR codes for assets" create_collection "work_order_types" "Work Order Types" "assignment" "#E91E63" "Work order categories" create_collection "work_orders" "Work Orders" "build" "#2196F3" "Work orders and maintenance requests" create_collection "asset_reminders" "Asset Reminders" "notifications" "#FF5722" "Maintenance reminders and alerts" echo echo "🎉 Collection setup completed successfully!" echo echo "📋 Next steps:" echo "1. Visit http://localhost:8055/admin to configure field permissions" echo "2. Set up relationships between collections" echo "3. Test the frontend connection" echo "4. Create some test assets" echo