freealberta/influence/scripts/build-nocodb.sh

930 lines
29 KiB
Bash
Executable File

#!/bin/bash
# NocoDB Auto-Setup Script for Alberta Influence Campaign Tool
# Based on the successful map setup script
# This script creates tables in your existing NocoDB project
set -e # Exit on any error
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Function to print colored output
print_status() {
echo -e "${BLUE}[INFO]${NC} $1" >&2
}
print_success() {
echo -e "${GREEN}[SUCCESS]${NC} $1" >&2
}
print_warning() {
echo -e "${YELLOW}[WARNING]${NC} $1" >&2
}
print_error() {
echo -e "${RED}[ERROR]${NC} $1" >&2
}
# Load environment variables
if [ -f ".env" ]; then
set -a
source .env
set +a
print_success "Environment variables loaded from .env"
else
print_error ".env file not found!"
exit 1
fi
# Validate required environment variables
if [ -z "$NOCODB_API_URL" ] || [ -z "$NOCODB_API_TOKEN" ]; then
print_error "Required environment variables NOCODB_API_URL and NOCODB_API_TOKEN not set!"
exit 1
fi
# Extract base URL from API URL and set up v2 API endpoints
BASE_URL=$(echo "$NOCODB_API_URL" | sed 's|/api/v1||')
API_BASE_V1="$NOCODB_API_URL"
API_BASE_V2="${BASE_URL}/api/v2"
# We'll create a new base for the influence campaign
BASE_ID=""
print_status "Using NocoDB instance: $BASE_URL"
print_status "Will create a new base for the Influence Campaign Tool"
# Function to make API calls with proper error handling
make_api_call() {
local method=$1
local endpoint=$2
local data=$3
local description=$4
local api_version=${5:-"v2"} # Default to v2
print_status "$description"
local response
local http_code
local full_url
if [[ "$api_version" == "v1" ]]; then
full_url="$API_BASE_V1$endpoint"
else
full_url="$API_BASE_V2$endpoint"
fi
if [ "$method" = "GET" ]; then
response=$(curl -s -w "%{http_code}" -H "xc-token: $NOCODB_API_TOKEN" \
-H "Content-Type: application/json" \
--max-time 60 \
"$full_url" 2>/dev/null)
curl_exit_code=$?
else
response=$(curl -s -w "%{http_code}" -X "$method" \
-H "xc-token: $NOCODB_API_TOKEN" \
-H "Content-Type: application/json" \
--max-time 60 \
-d "$data" \
"$full_url" 2>/dev/null)
curl_exit_code=$?
fi
if [[ $curl_exit_code -ne 0 ]]; then
print_error "Network error occurred while making API call (curl exit code: $curl_exit_code)"
return 1
fi
if [[ -z "$response" ]]; then
print_error "Empty response from API call"
return 1
fi
http_code="${response: -3}"
response_body="${response%???}"
print_status "HTTP Code: $http_code"
if [[ "$http_code" -ge 200 && "$http_code" -lt 300 ]]; then
print_success "$description completed successfully"
echo "$response_body"
else
print_error "$description failed with HTTP code: $http_code"
print_error "Response: $response_body"
return 1
fi
}
# Function to test API connectivity
test_api_connectivity() {
print_status "Testing API connectivity..."
# Test basic connectivity first
if ! curl -s --max-time 10 -I "$BASE_URL" > /dev/null 2>&1; then
print_error "Cannot reach NocoDB instance at $BASE_URL"
return 1
fi
# Test API with token using v2 endpoint
local test_response
test_response=$(curl -s --max-time 10 -w "%{http_code}" -H "xc-token: $NOCODB_API_TOKEN" \
-H "Content-Type: application/json" \
"$API_BASE_V2/meta/bases" 2>/dev/null || echo "CURL_ERROR")
if [[ "$test_response" == "CURL_ERROR" ]]; then
print_error "Network error when testing API"
return 1
fi
local http_code="${test_response: -3}"
if [[ "$http_code" -ge 200 && "$http_code" -lt 300 ]]; then
print_success "API connectivity test successful"
return 0
else
print_error "API test failed with HTTP code: $http_code"
return 1
fi
}
# Function to create a table
create_table() {
local base_id=$1
local table_name=$2
local table_data=$3
local description=$4
local response
response=$(make_api_call "POST" "/meta/bases/$base_id/tables" "$table_data" "Creating table: $table_name ($description)" "v2")
if [[ $? -eq 0 && -n "$response" ]]; then
local table_id
table_id=$(echo "$response" | grep -o '"id":"[^"]*"' | head -1 | cut -d'"' -f4)
if [[ -n "$table_id" ]]; then
print_success "Table '$table_name' created with ID: $table_id"
echo "$table_id"
else
print_error "Failed to extract table ID from response"
print_error "Response was: $response"
return 1
fi
else
print_error "Failed to create table: $table_name"
return 1
fi
}
# Function to validate table creation
validate_table_ids() {
local tables=("$@")
for table_id in "${tables[@]}"; do
if [[ -z "$table_id" || "$table_id" == "null" ]]; then
print_error "Invalid table ID detected: '$table_id'"
return 1
fi
done
return 0
}
# Function to create the representatives table
create_representatives_table() {
local base_id=$1
local table_data='{
"table_name": "influence_representatives",
"title": "Influence Representatives",
"columns": [
{
"column_name": "id",
"title": "ID",
"uidt": "ID",
"pk": true,
"ai": true,
"rqd": true
},
{
"column_name": "postal_code",
"title": "Postal Code",
"uidt": "SingleLineText",
"rqd": true
},
{
"column_name": "name",
"title": "Name",
"uidt": "SingleLineText",
"rqd": false
},
{
"column_name": "email",
"title": "Email",
"uidt": "Email",
"rqd": false
},
{
"column_name": "district_name",
"title": "District Name",
"uidt": "SingleLineText",
"rqd": false
},
{
"column_name": "elected_office",
"title": "Elected Office",
"uidt": "SingleLineText",
"rqd": false
},
{
"column_name": "party_name",
"title": "Party Name",
"uidt": "SingleLineText",
"rqd": false
},
{
"column_name": "representative_set_name",
"title": "Representative Set Name",
"uidt": "SingleLineText",
"rqd": false
},
{
"column_name": "url",
"title": "Profile URL",
"uidt": "URL",
"rqd": false
},
{
"column_name": "photo_url",
"title": "Photo URL",
"uidt": "URL",
"rqd": false
},
{
"column_name": "cached_at",
"title": "Cached At",
"uidt": "DateTime",
"rqd": false
}
]
}'
create_table "$base_id" "influence_representatives" "$table_data" "Representatives data from Represent API"
}
# Function to create the email logs table
create_email_logs_table() {
local base_id=$1
local table_data='{
"table_name": "influence_email_logs",
"title": "Influence Email Logs",
"columns": [
{
"column_name": "id",
"title": "ID",
"uidt": "ID",
"pk": true,
"ai": true,
"rqd": true
},
{
"column_name": "recipient_email",
"title": "Recipient Email",
"uidt": "Email",
"rqd": true
},
{
"column_name": "sender_name",
"title": "Sender Name",
"uidt": "SingleLineText",
"rqd": true
},
{
"column_name": "sender_email",
"title": "Sender Email",
"uidt": "Email",
"rqd": true
},
{
"column_name": "subject",
"title": "Subject",
"uidt": "SingleLineText",
"rqd": false
},
{
"column_name": "postal_code",
"title": "Postal Code",
"uidt": "SingleLineText",
"rqd": false
},
{
"column_name": "status",
"title": "Status",
"uidt": "SingleSelect",
"rqd": false,
"colOptions": {
"options": [
{"title": "sent", "color": "#00ff00"},
{"title": "failed", "color": "#ff0000"}
]
}
},
{
"column_name": "sent_at",
"title": "Sent At",
"uidt": "DateTime",
"rqd": false
}
]
}'
create_table "$base_id" "influence_email_logs" "$table_data" "Email sending logs"
}
# Function to create the postal codes table
create_postal_codes_table() {
local base_id=$1
local table_data='{
"table_name": "influence_postal_codes",
"title": "Influence Postal Codes",
"columns": [
{
"column_name": "id",
"title": "ID",
"uidt": "ID",
"pk": true,
"ai": true,
"rqd": true
},
{
"column_name": "postal_code",
"title": "Postal Code",
"uidt": "SingleLineText",
"rqd": true
},
{
"column_name": "city",
"title": "City",
"uidt": "SingleLineText",
"rqd": false
},
{
"column_name": "province",
"title": "Province",
"uidt": "SingleLineText",
"rqd": false
},
{
"column_name": "centroid_lat",
"title": "Centroid Latitude",
"uidt": "Decimal",
"rqd": false,
"meta": {
"precision": 10,
"scale": 8
}
},
{
"column_name": "centroid_lng",
"title": "Centroid Longitude",
"uidt": "Decimal",
"rqd": false,
"meta": {
"precision": 11,
"scale": 8
}
},
{
"column_name": "last_updated",
"title": "Last Updated",
"uidt": "DateTime",
"rqd": false
}
]
}'
create_table "$base_id" "influence_postal_codes" "$table_data" "Postal code information cache"
}
# Function to create the campaigns table
create_campaigns_table() {
local base_id="$1"
local table_data='{
"table_name": "influence_campaigns",
"title": "Campaigns",
"columns": [
{
"column_name": "id",
"title": "ID",
"uidt": "ID"
},
{
"column_name": "slug",
"title": "Campaign Slug",
"uidt": "SingleLineText",
"unique": true,
"rqd": true
},
{
"column_name": "title",
"title": "Campaign Title",
"uidt": "SingleLineText",
"rqd": true
},
{
"column_name": "description",
"title": "Description",
"uidt": "LongText"
},
{
"column_name": "email_subject",
"title": "Email Subject",
"uidt": "SingleLineText",
"rqd": true
},
{
"column_name": "email_body",
"title": "Email Body",
"uidt": "LongText",
"rqd": true
},
{
"column_name": "call_to_action",
"title": "Call to Action",
"uidt": "LongText"
},
{
"column_name": "status",
"title": "Status",
"uidt": "SingleSelect",
"rqd": true,
"colOptions": {
"options": [
{"title": "draft", "color": "#d0f1fd"},
{"title": "active", "color": "#c2f5e8"},
{"title": "paused", "color": "#ffdce5"},
{"title": "archived", "color": "#ffeab6"}
]
}
},
{
"column_name": "allow_smtp_email",
"title": "Allow SMTP Email",
"uidt": "Checkbox",
"cdf": "true"
},
{
"column_name": "allow_mailto_link",
"title": "Allow Mailto Link",
"uidt": "Checkbox",
"cdf": "true"
},
{
"column_name": "collect_user_info",
"title": "Collect User Info",
"uidt": "Checkbox",
"cdf": "true"
},
{
"column_name": "show_email_count",
"title": "Show Email Count",
"uidt": "Checkbox",
"cdf": "true"
},
{
"column_name": "target_government_levels",
"title": "Target Government Levels",
"uidt": "MultiSelect",
"colOptions": {
"options": [
{"title": "Federal", "color": "#cfdffe"},
{"title": "Provincial", "color": "#c2f5e8"},
{"title": "Municipal", "color": "#ffdaf6"},
{"title": "School Board", "color": "#ffeab6"}
]
}
}
]
}'
create_table "$base_id" "influence_campaigns" "$table_data" "Campaign definitions and settings"
}
# Function to create the campaign emails table
create_campaign_emails_table() {
local base_id="$1"
local table_data='{
"table_name": "influence_campaign_emails",
"title": "Campaign Emails",
"columns": [
{
"column_name": "id",
"title": "ID",
"uidt": "ID"
},
{
"column_name": "campaign_id",
"title": "Campaign ID",
"uidt": "Number",
"rqd": true
},
{
"column_name": "campaign_slug",
"title": "Campaign Slug",
"uidt": "SingleLineText",
"rqd": true
},
{
"column_name": "user_email",
"title": "User Email",
"uidt": "Email"
},
{
"column_name": "user_name",
"title": "User Name",
"uidt": "SingleLineText"
},
{
"column_name": "user_postal_code",
"title": "User Postal Code",
"uidt": "SingleLineText"
},
{
"column_name": "recipient_email",
"title": "Recipient Email",
"uidt": "Email",
"rqd": true
},
{
"column_name": "recipient_name",
"title": "Recipient Name",
"uidt": "SingleLineText"
},
{
"column_name": "recipient_title",
"title": "Recipient Title",
"uidt": "SingleLineText"
},
{
"column_name": "recipient_level",
"title": "Government Level",
"uidt": "SingleSelect",
"colOptions": {
"options": [
{"title": "Federal", "color": "#cfdffe"},
{"title": "Provincial", "color": "#c2f5e8"},
{"title": "Municipal", "color": "#ffdaf6"},
{"title": "School Board", "color": "#ffeab6"},
{"title": "Other", "color": "#d1f7c4"}
]
}
},
{
"column_name": "email_method",
"title": "Email Method",
"uidt": "SingleSelect",
"rqd": true,
"colOptions": {
"options": [
{"title": "smtp", "color": "#c2f5e8"},
{"title": "mailto", "color": "#ffdaf6"}
]
}
},
{
"column_name": "subject",
"title": "Subject",
"uidt": "SingleLineText",
"rqd": true
},
{
"column_name": "message",
"title": "Message",
"uidt": "LongText",
"rqd": true
},
{
"column_name": "status",
"title": "Status",
"uidt": "SingleSelect",
"rqd": true,
"colOptions": {
"options": [
{"title": "sent", "color": "#c2f5e8"},
{"title": "failed", "color": "#ffdce5"},
{"title": "clicked", "color": "#cfdffe"}
]
}
},
{
"column_name": "sent_at",
"title": "Sent At",
"uidt": "DateTime",
"cdf": "now()",
"rqd": false
}
]
}'
create_table "$base_id" "influence_campaign_emails" "$table_data" "Campaign email tracking"
}
# Function to create the users table
create_users_table() {
local base_id="$1"
local table_data='{
"table_name": "influence_users",
"title": "Users",
"columns": [
{
"column_name": "id",
"title": "ID",
"uidt": "ID",
"pk": true,
"ai": true,
"rqd": true
},
{
"column_name": "email",
"title": "Email",
"uidt": "Email",
"unique": true,
"rqd": true
},
{
"column_name": "name",
"title": "Name",
"uidt": "SingleLineText",
"rqd": true
},
{
"column_name": "password",
"title": "Password",
"uidt": "SingleLineText",
"rqd": true
},
{
"column_name": "admin",
"title": "Admin",
"uidt": "Checkbox",
"cdf": "false"
},
{
"column_name": "last_login",
"title": "Last Login",
"uidt": "DateTime",
"rqd": false
}
]
}'
create_table "$base_id" "influence_users" "$table_data" "User authentication and management"
}
# Function to create a new base
create_base() {
local base_data='{
"title": "Alberta Influence Campaign Tool",
"type": "database"
}'
local response
response=$(make_api_call "POST" "/meta/bases" "$base_data" "Creating new base: Alberta Influence Campaign Tool" "v2")
if [[ $? -eq 0 && -n "$response" ]]; then
local base_id
base_id=$(echo "$response" | grep -o '"id":"[^"]*"' | head -1 | cut -d'"' -f4)
if [[ -n "$base_id" ]]; then
print_success "Base 'Alberta Influence Campaign Tool' created with ID: $base_id"
echo "$base_id"
else
print_error "Failed to extract base ID from response"
return 1
fi
else
print_error "Failed to create base"
return 1
fi
}
# Function to update .env file with table IDs
update_env_with_table_ids() {
local base_id=$1
local representatives_table_id=$2
local email_logs_table_id=$3
local postal_codes_table_id=$4
local campaigns_table_id=$5
local campaign_emails_table_id=$6
local users_table_id=$7
print_status "Updating .env file with NocoDB project and table IDs..."
# Create backup of .env file
if [ -f ".env" ]; then
cp ".env" ".env.backup.$(date +%Y%m%d_%H%M%S)"
print_status "Created backup of .env file"
fi
# Function to update or add environment variable
update_env_var() {
local var_name=$1
local var_value=$2
local env_file=${3:-".env"}
if grep -q "^${var_name}=" "$env_file"; then
# Variable exists, update it
sed -i "s|^${var_name}=.*|${var_name}=${var_value}|" "$env_file"
print_status "Updated ${var_name} in .env"
else
# Variable doesn't exist, add it
echo "${var_name}=${var_value}" >> "$env_file"
print_status "Added ${var_name} to .env"
fi
}
# Update all environment variables
update_env_var "NOCODB_PROJECT_ID" "$base_id"
update_env_var "NOCODB_TABLE_REPRESENTATIVES" "$representatives_table_id"
update_env_var "NOCODB_TABLE_EMAILS" "$email_logs_table_id"
update_env_var "NOCODB_TABLE_POSTAL_CODES" "$postal_codes_table_id"
update_env_var "NOCODB_TABLE_CAMPAIGNS" "$campaigns_table_id"
update_env_var "NOCODB_TABLE_CAMPAIGN_EMAILS" "$campaign_emails_table_id"
update_env_var "NOCODB_TABLE_USERS" "$users_table_id"
print_success "Successfully updated .env file with all table IDs"
# Display the updated values
print_status ""
print_status "Updated .env with the following values:"
print_status "NOCODB_PROJECT_ID=$base_id"
print_status "NOCODB_TABLE_REPRESENTATIVES=$representatives_table_id"
print_status "NOCODB_TABLE_EMAILS=$email_logs_table_id"
print_status "NOCODB_TABLE_POSTAL_CODES=$postal_codes_table_id"
print_status "NOCODB_TABLE_CAMPAIGNS=$campaigns_table_id"
print_status "NOCODB_TABLE_CAMPAIGN_EMAILS=$campaign_emails_table_id"
print_status "NOCODB_TABLE_USERS=$users_table_id"
}
# Main execution
main() {
print_status "Starting NocoDB Setup for Alberta Influence Campaign Tool..."
print_status "============================================================"
# First test API connectivity
if ! test_api_connectivity; then
print_error "API connectivity test failed"
exit 1
fi
print_status ""
print_status "Creating new base for Influence Campaign Tool..."
# Create a new base
BASE_ID=$(create_base)
if [[ $? -ne 0 || -z "$BASE_ID" ]]; then
print_error "Failed to create base"
exit 1
fi
print_status "Created base with ID: $BASE_ID"
print_warning "This created a new NocoDB project for the Influence Campaign Tool"
# Create tables
print_status ""
print_status "Creating tables..."
# Create representatives table
REPRESENTATIVES_TABLE_ID=$(create_representatives_table "$BASE_ID")
if [[ $? -ne 0 ]]; then
print_error "Failed to create representatives table"
exit 1
fi
# Create email logs table
EMAIL_LOGS_TABLE_ID=$(create_email_logs_table "$BASE_ID")
if [[ $? -ne 0 ]]; then
print_error "Failed to create email logs table"
exit 1
fi
# Create postal codes table
POSTAL_CODES_TABLE_ID=$(create_postal_codes_table "$BASE_ID")
if [[ $? -ne 0 ]]; then
print_error "Failed to create postal codes table"
exit 1
fi
# Create campaigns table
CAMPAIGNS_TABLE_ID=$(create_campaigns_table "$BASE_ID")
if [[ $? -ne 0 ]]; then
print_error "Failed to create campaigns table"
exit 1
fi
# Create campaign emails table
CAMPAIGN_EMAILS_TABLE_ID=$(create_campaign_emails_table "$BASE_ID")
if [[ $? -ne 0 ]]; then
print_error "Failed to create campaign emails table"
exit 1
fi
# Create users table
USERS_TABLE_ID=$(create_users_table "$BASE_ID")
if [[ $? -ne 0 ]]; then
print_error "Failed to create users table"
exit 1
fi
# Validate all table IDs were created successfully
if ! validate_table_ids "$REPRESENTATIVES_TABLE_ID" "$EMAIL_LOGS_TABLE_ID" "$POSTAL_CODES_TABLE_ID" "$CAMPAIGNS_TABLE_ID" "$CAMPAIGN_EMAILS_TABLE_ID" "$USERS_TABLE_ID"; then
print_error "One or more table IDs are invalid"
exit 1
fi
# Wait a moment for tables to be fully created
sleep 3
print_status ""
print_status "============================================================"
print_success "NocoDB Setup completed successfully!"
print_status "============================================================"
print_status ""
print_status "Created new base: Alberta Influence Campaign Tool (ID: $BASE_ID)"
print_status "Created tables:"
print_status " - influence_representatives (ID: $REPRESENTATIVES_TABLE_ID)"
print_status " - influence_email_logs (ID: $EMAIL_LOGS_TABLE_ID)"
print_status " - influence_postal_codes (ID: $POSTAL_CODES_TABLE_ID)"
print_status " - influence_campaigns (ID: $CAMPAIGNS_TABLE_ID)"
print_status " - influence_campaign_emails (ID: $CAMPAIGN_EMAILS_TABLE_ID)"
print_status " - influence_users (ID: $USERS_TABLE_ID)"
# Automatically update .env file with new project ID
print_status ""
print_status "Updating .env file with new project ID..."
if [ -f ".env" ]; then
# Update existing .env file
if grep -q "NOCODB_PROJECT_ID=" .env; then
# Replace existing NOCODB_PROJECT_ID
sed -i "s/NOCODB_PROJECT_ID=.*/NOCODB_PROJECT_ID=$BASE_ID/" .env
print_success "Updated NOCODB_PROJECT_ID in .env file"
else
# Add new NOCODB_PROJECT_ID
echo "NOCODB_PROJECT_ID=$BASE_ID" >> .env
print_success "Added NOCODB_PROJECT_ID to .env file"
fi
else
print_error ".env file not found - please create one from .env.example"
exit 1
fi
# Update .env file with table IDs
update_env_with_table_ids "$BASE_ID" "$REPRESENTATIVES_TABLE_ID" "$EMAIL_LOGS_TABLE_ID" "$POSTAL_CODES_TABLE_ID" "$CAMPAIGNS_TABLE_ID" "$CAMPAIGN_EMAILS_TABLE_ID" "$USERS_TABLE_ID"
print_status ""
print_status "============================================================"
print_success "Automated setup completed successfully!"
print_status "============================================================"
print_status ""
print_status "Created new base: Alberta Influence Campaign Tool (ID: $BASE_ID)"
print_status "Updated .env file with project ID and all table IDs"
print_status ""
print_status "Next steps:"
print_status "1. Check your NocoDB instance at: $BASE_URL"
print_status "2. Verify the tables were created successfully"
print_status "3. Start your influence campaign application with: docker compose up"
print_status "4. The application will be available at: http://localhost:3333"
print_status "5. Access the admin panel at: http://localhost:3333/admin.html"
print_status ""
print_success "Your Alberta Influence Campaign Tool is ready to use!"
}
# Check if script is being run directly
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
# Check for command line arguments
if [[ "$1" == "--help" || "$1" == "-h" ]]; then
echo "Usage: $0 [OPTIONS]"
echo ""
echo "Options:"
echo " --help, -h Show this help message"
echo ""
echo "Creates a new NocoDB base with all required tables for the Influence Campaign Tool"
exit 0
else
main "$@"
fi
fi