Jake Vanderwerf
2026-08-26 18e7e415339ccc9adf8ade562f93abef606da16d
inc/rest/routes/ApprovalRoutes.php
@@ -2,7 +2,10 @@
namespace JVBase\rest\routes;
use JVBase\managers\CustomTable;
use JVBase\Instance;
use JVBase\managers\Approvals;
use JVBase\managers\Cache;
use JVBase\registrar\Registrar;
use JVBase\rest\PermissionHandler;
use JVBase\rest\Rest;
@@ -11,7 +14,6 @@
use JVBase\base\Site;
use WP_REST_Request;
use WP_REST_Response;
use Exception;
if (!defined('ABSPATH')) {
    exit; // Exit if accessed directly
@@ -19,6 +21,7 @@
class ApprovalRoutes extends Rest
{
   use Instance;
    protected array $userTypes;
    protected array $termTypes;
    protected array $allTypes;
@@ -31,6 +34,7 @@
        $this->cacheName = 'approvals';
      $this->hasMemberApproval = Site::membership() && Site::membership()->has('member_verified');
        parent::__construct();
      $this->cache = Cache::for('approvals', 'custom', $this->cacheTtl);
        $this->initTypes();
    }
@@ -55,7 +59,6 @@
      Route::for('approvals')
         ->get([$this, 'getApprovals'])
         ->args([
            'user' => 'integer|required',
            'type' => 'string',
            'status' => 'string|enum:pending,approved,rejected,expired',
         ])
@@ -63,7 +66,6 @@
         ->rateLimit(30)
         ->post([$this, 'handleAction'])
         ->args([
            'user' => 'integer|required',
            'request_id' => 'integer|required',
            'action' => 'string|required|enum:approve,reject',
            'type' => 'string|required',
@@ -84,7 +86,7 @@
    {
      $data = $request->get_params();
      $request_id = absint($data['request_id']);
      $user_id = absint($data['user']);
      $user_id = get_current_user_id();
      $action = sanitize_text_field($data['action']);
      $type = sanitize_text_field($data['type']);
      $notes = sanitize_text_field($data['notes'] ?? '');
@@ -108,7 +110,7 @@
      if (!in_array($vote, ['approve', 'reject', 'dismiss'])) {
         return false;
      }
      $result = JVB()->approvals()->markApproval($request_id, $user_id, $type, $vote, $notes);
      $result = Approvals::getInstance()->markApproval($request_id, $user_id, $type, $vote, $notes);
      return $result['success'];
   }
@@ -149,11 +151,11 @@
     */
   public function getVerificationDetails(int $requestID, string $type): array|false
   {
      $request = JVB()->approvals()->getRequest($requestID, $type);
      $request = Approvals::getInstance()->getRequest($requestID, $type);
      if (!$request) {
         return false;
      }
      $votes = JVB()->approvals()->getVotes($requestID, $type);
      $votes = Approvals::getInstance()->getVotes($requestID, $type);
      // Join with user data for display names
      foreach ($votes as &$vote) {
@@ -189,7 +191,7 @@
      int $required_approvals = 3
   ): int|false {
      $result = JVB()->approvals()->createApproval(
      $result = Approvals::getInstance()->createApproval(
         $user_id,
         $taxonomy,
         $name,
@@ -198,68 +200,16 @@
      return $result['success'];
   }
   protected function getTableName(string $type, string $suffix): string
   {
      return match ($type) {
         'term' => "approval_term_{$suffix}",
         default => "approval_{$type}_{$suffix}",
      };
   }
   public function getApprovals(WP_REST_Request $request): WP_REST_Response
   {
      $user_id = absint($request->get_param('user'));
      $user_id = get_current_user_id();
      $type = sanitize_text_field($request->get_param('type') ?? 'all');
      $status = sanitize_text_field($request->get_param('status') ?? 'pending');
      if (!$this->checkUser($user_id)) {
         return $this->unauthorized();
      }
      $cacheKey = compact('user_id', 'type', 'status');
      $result = $this->cache->remember($cacheKey, function() use ($type, $status) {
         $data = [];
         if ($type === 'user' || $type === 'all') {
            $data['user_approvals'] = $this->getUserApprovals($status);
         }
         if ($type === 'term' || $type === 'all') {
            $data['term_approvals'] = $this->getTermApprovals($status);
         }
         return $data;
      });
      $result = Approvals::getInstance()->getApprovalRequests($user_id, $type === 'all' ? null : $type);
      return $this->success($result);
   }
   private function getUserApprovals(string $status = 'pending'): array
   {
      $table = CustomTable::for($this->getTableName('artist', 'requests'));
      $query = $table;
      if ($status !== 'all') {
         $query = $query->where(['status' => $status]);
      }
      return $query->orderBy('created_at', 'DESC')->getResults(ARRAY_A);
   }
   private function getTermApprovals(string $status = 'pending'): array
   {
      $table = CustomTable::for($this->getTableName('term', 'requests'));
      if ($status === 'all') {
         return $table->orderBy('created_at', 'DESC')->getResults(ARRAY_A);
      }
      return $table
         ->where(['status' => $status])
         ->orderBy('created_at', 'DESC')
         ->getResults(ARRAY_A);
   }
}