Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
28 / 28 |
|
100.00% |
4 / 4 |
CRAP | |
100.00% |
1 / 1 |
| PlanHubspotConfigController | |
100.00% |
28 / 28 |
|
100.00% |
4 / 4 |
7 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| show | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
2 | |||
| upsert | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
2 | |||
| destroy | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Http\Controllers\v2\Admin; |
| 4 | |
| 5 | use App\Http\Controllers\Controller; |
| 6 | use App\Http\Models\Plans; |
| 7 | use App\Http\Requests\v2\PlanHubspotConfig\DestroyPlanHubspotConfigRequest; |
| 8 | use App\Http\Requests\v2\PlanHubspotConfig\ShowPlanHubspotConfigRequest; |
| 9 | use App\Http\Requests\v2\PlanHubspotConfig\UpsertPlanHubspotConfigRequest; |
| 10 | use App\Http\Resources\v2\PlanHubspotConfigResource; |
| 11 | use App\Http\Services\PlanHubspotConfigService; |
| 12 | use Illuminate\Http\JsonResponse; |
| 13 | |
| 14 | /** |
| 15 | * Controller for managing plan HubSpot configurations (admin). |
| 16 | * |
| 17 | * Only accessible by users with the VENGRESO_ADMIN role. |
| 18 | * Provides operations for managing HubSpot CRM property mappings for plans. |
| 19 | */ |
| 20 | class PlanHubspotConfigController extends Controller |
| 21 | { |
| 22 | public function __construct( |
| 23 | private PlanHubspotConfigService $hubspotConfigService |
| 24 | ) {} |
| 25 | |
| 26 | /** |
| 27 | * Get the HubSpot config for a plan. |
| 28 | * |
| 29 | * @param ShowPlanHubspotConfigRequest $request Validated request (authorization only) |
| 30 | * @param Plans $plan The plan (route model binding) |
| 31 | * |
| 32 | * @response 200 { |
| 33 | * "result": { |
| 34 | * "data": { |
| 35 | * "id": "507f1f77bcf86cd799439011", |
| 36 | * "plan_id": "507f1f77bcf86cd799439012", |
| 37 | * "name": "Freemium", |
| 38 | * "last_product": "Freemium", |
| 39 | * "cancel_subscription_date": "freemium_cancel_subscription_date", |
| 40 | * "payment_status": null, |
| 41 | * "subscription_annual_recurring_revenue": null, |
| 42 | * "subscription_churn_date": "freemium_subscription_churn_date", |
| 43 | * "subscription_expiration_date": null, |
| 44 | * "subscription_frequency": null, |
| 45 | * "subscription_monthly_recurring_revenue": null, |
| 46 | * "subscription_plan_type": null, |
| 47 | * "subscription_start_date": "freemium_subscription_start_date", |
| 48 | * "subscription_status": "flymsg_freemium_subscription_status", |
| 49 | * "subscription_status_updated_on": "freemium_subscription_status_updated_on", |
| 50 | * "user_type": null, |
| 51 | * "created_at": 1642521600, |
| 52 | * "updated_at": 1642521600 |
| 53 | * } |
| 54 | * } |
| 55 | * } |
| 56 | * @response 404 { |
| 57 | * "error": { |
| 58 | * "message": "HubSpot config not found for this plan." |
| 59 | * } |
| 60 | * } |
| 61 | */ |
| 62 | public function show(ShowPlanHubspotConfigRequest $request, Plans $plan): PlanHubspotConfigResource|JsonResponse |
| 63 | { |
| 64 | $config = $this->hubspotConfigService->getByPlanId((string) $plan->_id); |
| 65 | |
| 66 | if (! $config) { |
| 67 | return response()->json([ |
| 68 | 'error' => [ |
| 69 | 'message' => 'HubSpot config not found for this plan.', |
| 70 | ], |
| 71 | ], 404); |
| 72 | } |
| 73 | |
| 74 | return new PlanHubspotConfigResource($config); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Create or update the HubSpot config for a plan. |
| 79 | * |
| 80 | * If a config exists for this plan, it will be updated. |
| 81 | * If no config exists, a new one will be created. |
| 82 | * |
| 83 | * @param UpsertPlanHubspotConfigRequest $request Validated request with config data |
| 84 | * @param Plans $plan The plan (route model binding) |
| 85 | * |
| 86 | * @response 200 { |
| 87 | * "result": { |
| 88 | * "data": { |
| 89 | * "id": "507f1f77bcf86cd799439011", |
| 90 | * "plan_id": "507f1f77bcf86cd799439012", |
| 91 | * "name": "Freemium", |
| 92 | * "last_product": "Freemium", |
| 93 | * "cancel_subscription_date": "freemium_cancel_subscription_date", |
| 94 | * "payment_status": null, |
| 95 | * "subscription_status": "flymsg_freemium_subscription_status", |
| 96 | * "created_at": 1642521600, |
| 97 | * "updated_at": 1642525200 |
| 98 | * } |
| 99 | * } |
| 100 | * } |
| 101 | */ |
| 102 | public function upsert(UpsertPlanHubspotConfigRequest $request, Plans $plan): JsonResponse|PlanHubspotConfigResource |
| 103 | { |
| 104 | // Check if config already exists to determine response code |
| 105 | $existingConfig = $this->hubspotConfigService->getByPlanId((string) $plan->_id); |
| 106 | |
| 107 | $config = $this->hubspotConfigService->upsert( |
| 108 | (string) $plan->_id, |
| 109 | $request->validated() |
| 110 | ); |
| 111 | |
| 112 | // Return 201 if created, 200 if updated |
| 113 | if (! $existingConfig) { |
| 114 | return (new PlanHubspotConfigResource($config)) |
| 115 | ->response() |
| 116 | ->setStatusCode(201); |
| 117 | } |
| 118 | |
| 119 | return new PlanHubspotConfigResource($config); |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Delete the HubSpot config for a plan. |
| 124 | * |
| 125 | * @param DestroyPlanHubspotConfigRequest $request Validated request (authorization only) |
| 126 | * @param Plans $plan The plan (route model binding) |
| 127 | * |
| 128 | * @response 204 No content |
| 129 | * @response 404 { |
| 130 | * "error": { |
| 131 | * "message": "HubSpot config not found for this plan." |
| 132 | * } |
| 133 | * } |
| 134 | */ |
| 135 | public function destroy(DestroyPlanHubspotConfigRequest $request, Plans $plan): JsonResponse |
| 136 | { |
| 137 | $config = $this->hubspotConfigService->getByPlanId((string) $plan->_id); |
| 138 | |
| 139 | if (! $config) { |
| 140 | return response()->json([ |
| 141 | 'error' => [ |
| 142 | 'message' => 'HubSpot config not found for this plan.', |
| 143 | ], |
| 144 | ], 404); |
| 145 | } |
| 146 | |
| 147 | $this->hubspotConfigService->delete($config); |
| 148 | |
| 149 | return response()->json(null, 204); |
| 150 | } |
| 151 | } |