From b0194e10a87e16797a568d8a30d53ebecd27d8a4 Mon Sep 17 00:00:00 2001
From: Jake Vanderwerf <get@jakevanderwerf.ca>
Date: Sat, 18 Oct 2025 15:04:51 +0000
Subject: [PATCH] =DataStore.js and UploaderManager.js overhaul

---
 inc/registry/CheckCustomTables.php |   70 +++++++++++++++++++++++++++++++++++
 1 files changed, 70 insertions(+), 0 deletions(-)

diff --git a/inc/registry/CheckCustomTables.php b/inc/registry/CheckCustomTables.php
index 1d4bcaa..57b4795 100644
--- a/inc/registry/CheckCustomTables.php
+++ b/inc/registry/CheckCustomTables.php
@@ -72,6 +72,13 @@
 				error_log("JVB: Error in dashboard section: " . $e->getMessage());
 			}
 
+			try {
+				if (array_key_exists('referrals', $this->JVB_SITE) && $this->JVB_SITE['referrals']) {
+					$tables = array_merge($tables, $this->referralTables());
+				}
+			} catch (Exception $e) {
+				error_log("JVB: Error in referrals section: " . $e->getMessage());
+			}
 			// RE-ENABLE favourites tables
 			try {
 				if ($this->JVB_SITE['favourites']) {
@@ -1146,6 +1153,68 @@
     }
 
 
+	/**
+	 * Create referral tracking tables
+	 *
+	 * Call this from the main table creation method in CheckCustomTables.php:
+	 * $tables = array_merge($tables, $this->referralTables());
+	 */
+	protected function referralTables(): array
+	{
+		$tables = [];
+
+		// Main referrals table
+		$tables['referrals'] = "(
+        `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
+        `referrer_id` bigint(20) unsigned NOT NULL,
+        `referee_id` bigint(20) unsigned NOT NULL,
+        `referee_name` varchar(255) NOT NULL,
+        `referee_email` varchar(255) NOT NULL,
+        `referee_phone` varchar(50) DEFAULT NULL,
+        `referral_code` varchar(50) NOT NULL,
+        `status` enum('pending', 'treated', 'cancelled') DEFAULT 'pending',
+        `referred_at` datetime NOT NULL,
+        `treated_at` datetime DEFAULT NULL,
+        `notes` text DEFAULT NULL,
+        PRIMARY KEY (`id`),
+        UNIQUE KEY `referee_unique` (`referee_id`),
+        KEY `referrer_idx` (`referrer_id`),
+        KEY `status_idx` (`status`),
+        KEY `code_idx` (`referral_code`),
+        KEY `date_idx` (`referred_at`),
+        CONSTRAINT `{$this->base}referral_referrer_fk` FOREIGN KEY (`referrer_id`)
+            REFERENCES {$this->wpdb->users} (`ID`) ON DELETE CASCADE,
+        CONSTRAINT `{$this->base}referral_referee_fk` FOREIGN KEY (`referee_id`)
+            REFERENCES {$this->wpdb->users} (`ID`) ON DELETE CASCADE
+    )";
+
+		// Rewards table
+		$tables['referral_rewards'] = "(
+        `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
+        `referral_id` bigint(20) unsigned NOT NULL,
+        `user_id` bigint(20) unsigned NOT NULL,
+        `reward_type` enum('referrer', 'referee') NOT NULL,
+        `amount` decimal(10,2) NOT NULL,
+        `reward_calculation` varchar(20) DEFAULT NULL COMMENT 'percentage or fixed',
+        `status` enum('available', 'redeemed', 'expired', 'cancelled') DEFAULT 'available',
+        `created_at` datetime NOT NULL,
+        `redeemed_at` datetime DEFAULT NULL,
+        `expires_at` datetime DEFAULT NULL,
+        `notes` text DEFAULT NULL,
+        PRIMARY KEY (`id`),
+        KEY `referral_idx` (`referral_id`),
+        KEY `user_idx` (`user_id`),
+        KEY `status_idx` (`status`),
+        KEY `type_idx` (`reward_type`),
+        CONSTRAINT `{$this->base}reward_referral_fk` FOREIGN KEY (`referral_id`)
+            REFERENCES {$this->wpdb->prefix}" . BASE . "referrals (`id`) ON DELETE CASCADE,
+        CONSTRAINT `{$this->base}reward_user_fk` FOREIGN KEY (`user_id`)
+            REFERENCES {$this->wpdb->users} (`ID`) ON DELETE CASCADE
+    )";
+
+		return $tables;
+	}
+
     /*******************************************************************************************
      * These methods help create a content-type taxonomy, like the tattoo shops in edmonton.ink
      *     To set up, ensure that some fields in the registered taxonomy include 'content_table' => true
@@ -1538,4 +1607,5 @@
 
         return "(\n    " . implode(",\n    ", $allDefinitions) . "\n)";
     }
+
 }

--
Gitblit v1.10.0