| | |
| | | |
| | | 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; |
| | |
| | | use JVBase\base\Site; |
| | | use WP_REST_Request; |
| | | use WP_REST_Response; |
| | | use Exception; |
| | | |
| | | if (!defined('ABSPATH')) { |
| | | exit; // Exit if accessed directly |
| | |
| | | |
| | | class ApprovalRoutes extends Rest |
| | | { |
| | | use Instance; |
| | | protected array $userTypes; |
| | | protected array $termTypes; |
| | | protected array $allTypes; |
| | |
| | | $this->cacheName = 'approvals'; |
| | | $this->hasMemberApproval = Site::membership() && Site::membership()->has('member_verified'); |
| | | parent::__construct(); |
| | | $this->cache = Cache::for('approvals', 'custom', $this->cacheTtl); |
| | | |
| | | $this->initTypes(); |
| | | } |
| | |
| | | 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']; |
| | | } |
| | | |
| | |
| | | */ |
| | | 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) { |
| | |
| | | int $required_approvals = 3 |
| | | ): int|false { |
| | | |
| | | $result = JVB()->approvals()->createApproval( |
| | | $result = Approvals::getInstance()->createApproval( |
| | | $user_id, |
| | | $taxonomy, |
| | | $name, |
| | |
| | | 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 = get_current_user_id(); |
| | | $type = sanitize_text_field($request->get_param('type') ?? 'all'); |
| | | $status = sanitize_text_field($request->get_param('status') ?? 'pending'); |
| | | |
| | | $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); |
| | | } |
| | | } |