From 8b143b1bda041164df27ba4c70d1a697d8e69b27 Mon Sep 17 00:00:00 2001 From: Perside Rosalie Date: Mon, 20 Jul 2026 12:41:33 +0200 Subject: [PATCH 1/2] Create endpoint to get specific policy by type and active_from date Bug: T432339 --- app/Http/Controllers/PoliciesController.php | 29 ++++++++++++++++++ routes/api.php | 1 + .../Controllers/PoliciesControllerTest.php | 30 +++++++++++++++++++ 3 files changed, 60 insertions(+) diff --git a/app/Http/Controllers/PoliciesController.php b/app/Http/Controllers/PoliciesController.php index 1a3dfd65..fb816a62 100644 --- a/app/Http/Controllers/PoliciesController.php +++ b/app/Http/Controllers/PoliciesController.php @@ -3,8 +3,11 @@ namespace App\Http\Controllers; use App\Http\Resources\PoliciesCollection; +use App\Http\Resources\PolicyResource; use App\Policy; use Carbon\CarbonImmutable; +use Illuminate\Support\Facades\Validator; +use Illuminate\Validation\Rule; class PoliciesController extends Controller { public function getCurrentPolicies(): PoliciesCollection { @@ -20,4 +23,30 @@ public function getCurrentPolicies(): PoliciesCollection { return new PoliciesCollection($currentPolicies); } + + public function getPolicyByTypeAndActiveFrom($policyType, $activeFrom): PolicyResource { + $validator = Validator::make( + [ + 'policy_type' => $policyType, + 'active_from' => $activeFrom, + ], + [ + 'policy_type' => ['required', 'string', Rule::in(['terms-of-use', 'hosting-policy'])], + 'active_from' => ['required', 'date', 'date_format:Y-m-d'], + ] + + ); + $validator->validate(); + $validated = $validator->safe(); + + $validatedActiveFrom = CarbonImmutable::parse($validated['active_from']); + + $policy = Policy::where('policy_type', $validated['policy_type'])->where('active_from', '=', $validatedActiveFrom)->first(); + + if (!$policy) { + abort(404, 'Policy not found.'); + } + + return new PolicyResource($policy); + } } diff --git a/routes/api.php b/routes/api.php index 125bc994..53d972b3 100644 --- a/routes/api.php +++ b/routes/api.php @@ -22,6 +22,7 @@ $router->post('contact/sendMessage', ['uses' => 'ContactController@sendMessage']); $router->post('complaint/sendMessage', ['uses' => 'ComplaintController@sendMessage']); $router->get('v1/policies/current', ['uses' => 'PoliciesController@getCurrentPolicies']); + $router->get('v1/policies/{policy_type}/by_active_from/{active_from}', ['uses' => 'PoliciesController@getPolicyByTypeAndActiveFrom']); $router->post('auth/login', ['uses' => 'Auth\LoginController@postLogin'])->name('login'); // Authed diff --git a/tests/Http/Controllers/PoliciesControllerTest.php b/tests/Http/Controllers/PoliciesControllerTest.php index 7c9e030b..58468e85 100644 --- a/tests/Http/Controllers/PoliciesControllerTest.php +++ b/tests/Http/Controllers/PoliciesControllerTest.php @@ -57,4 +57,34 @@ public function testGetCurrentPolicies(): void { 'active_from' => $latestActiveHostingPolicy->active_from->format('Y-m-d'), ]); } + + public function testGetPolicyByTypeAndActiveFrom(): void { + Policy::factory()->create([ + 'policy_type' => 'hosting-policy', + 'active_from' => '2026-07-01', + ]); + + Policy::factory()->create([ + 'policy_type' => 'hosting-policy', + 'active_from' => '2026-07-02', + ]); + + $request = $this->getJson('v1/policies/hosting-policy/by_active_from/2026-07-01'); + + $request->assertOk(); + $request->assertJsonFragment([ + 'active_from' => '2026-07-01', + 'type' => 'hosting-policy', + ]); + } + + public function testGetPolicyByTypeAndActiveFromReturns422WithInvalidParams(): void { + $request = $this->getJson('v1/policies/fake-policy/by_active_from/not-a-date'); + $request->assertUnprocessable(); + } + + public function testMissingPolicyByTypeAndActiveFromReturns404(): void { + $request = $this->getJson('v1/policies/hosting-policy/by_active_from/2026-07-01'); + $request->assertNotFound(); + } } From f5fe748c68b2ad6d193dfcb35ce49c536dfda6d6 Mon Sep 17 00:00:00 2001 From: Perside Rosalie Date: Mon, 20 Jul 2026 12:52:48 +0200 Subject: [PATCH 2/2] Moving the logic for getting a single policy in a different controller --- app/Http/Controllers/PoliciesController.php | 29 ------------- app/Http/Controllers/PolicyController.php | 37 +++++++++++++++++ routes/api.php | 2 +- .../Controllers/PoliciesControllerTest.php | 30 -------------- .../Http/Controllers/PolicyControllerTest.php | 41 +++++++++++++++++++ 5 files changed, 79 insertions(+), 60 deletions(-) create mode 100644 app/Http/Controllers/PolicyController.php create mode 100644 tests/Http/Controllers/PolicyControllerTest.php diff --git a/app/Http/Controllers/PoliciesController.php b/app/Http/Controllers/PoliciesController.php index fb816a62..1a3dfd65 100644 --- a/app/Http/Controllers/PoliciesController.php +++ b/app/Http/Controllers/PoliciesController.php @@ -3,11 +3,8 @@ namespace App\Http\Controllers; use App\Http\Resources\PoliciesCollection; -use App\Http\Resources\PolicyResource; use App\Policy; use Carbon\CarbonImmutable; -use Illuminate\Support\Facades\Validator; -use Illuminate\Validation\Rule; class PoliciesController extends Controller { public function getCurrentPolicies(): PoliciesCollection { @@ -23,30 +20,4 @@ public function getCurrentPolicies(): PoliciesCollection { return new PoliciesCollection($currentPolicies); } - - public function getPolicyByTypeAndActiveFrom($policyType, $activeFrom): PolicyResource { - $validator = Validator::make( - [ - 'policy_type' => $policyType, - 'active_from' => $activeFrom, - ], - [ - 'policy_type' => ['required', 'string', Rule::in(['terms-of-use', 'hosting-policy'])], - 'active_from' => ['required', 'date', 'date_format:Y-m-d'], - ] - - ); - $validator->validate(); - $validated = $validator->safe(); - - $validatedActiveFrom = CarbonImmutable::parse($validated['active_from']); - - $policy = Policy::where('policy_type', $validated['policy_type'])->where('active_from', '=', $validatedActiveFrom)->first(); - - if (!$policy) { - abort(404, 'Policy not found.'); - } - - return new PolicyResource($policy); - } } diff --git a/app/Http/Controllers/PolicyController.php b/app/Http/Controllers/PolicyController.php new file mode 100644 index 00000000..8527fdaf --- /dev/null +++ b/app/Http/Controllers/PolicyController.php @@ -0,0 +1,37 @@ + $policyType, + 'active_from' => $activeFrom, + ], + [ + 'policy_type' => ['required', 'string', Rule::in(['terms-of-use', 'hosting-policy'])], + 'active_from' => ['required', 'date', 'date_format:Y-m-d'], + ] + + ); + $validator->validate(); + $validated = $validator->safe(); + + $validatedActiveFrom = CarbonImmutable::parse($validated['active_from']); + + $policy = Policy::where('policy_type', $validated['policy_type'])->where('active_from', '=', $validatedActiveFrom)->first(); + + if (!$policy) { + abort(404, 'Policy not found.'); + } + + return new PolicyResource($policy); + } +} diff --git a/routes/api.php b/routes/api.php index 53d972b3..d6dc19fa 100644 --- a/routes/api.php +++ b/routes/api.php @@ -22,7 +22,7 @@ $router->post('contact/sendMessage', ['uses' => 'ContactController@sendMessage']); $router->post('complaint/sendMessage', ['uses' => 'ComplaintController@sendMessage']); $router->get('v1/policies/current', ['uses' => 'PoliciesController@getCurrentPolicies']); - $router->get('v1/policies/{policy_type}/by_active_from/{active_from}', ['uses' => 'PoliciesController@getPolicyByTypeAndActiveFrom']); + $router->get('v1/policies/{policy_type}/by_active_from/{active_from}', ['uses' => 'PolicyController@getPolicyByTypeAndActiveFrom']); $router->post('auth/login', ['uses' => 'Auth\LoginController@postLogin'])->name('login'); // Authed diff --git a/tests/Http/Controllers/PoliciesControllerTest.php b/tests/Http/Controllers/PoliciesControllerTest.php index 58468e85..7c9e030b 100644 --- a/tests/Http/Controllers/PoliciesControllerTest.php +++ b/tests/Http/Controllers/PoliciesControllerTest.php @@ -57,34 +57,4 @@ public function testGetCurrentPolicies(): void { 'active_from' => $latestActiveHostingPolicy->active_from->format('Y-m-d'), ]); } - - public function testGetPolicyByTypeAndActiveFrom(): void { - Policy::factory()->create([ - 'policy_type' => 'hosting-policy', - 'active_from' => '2026-07-01', - ]); - - Policy::factory()->create([ - 'policy_type' => 'hosting-policy', - 'active_from' => '2026-07-02', - ]); - - $request = $this->getJson('v1/policies/hosting-policy/by_active_from/2026-07-01'); - - $request->assertOk(); - $request->assertJsonFragment([ - 'active_from' => '2026-07-01', - 'type' => 'hosting-policy', - ]); - } - - public function testGetPolicyByTypeAndActiveFromReturns422WithInvalidParams(): void { - $request = $this->getJson('v1/policies/fake-policy/by_active_from/not-a-date'); - $request->assertUnprocessable(); - } - - public function testMissingPolicyByTypeAndActiveFromReturns404(): void { - $request = $this->getJson('v1/policies/hosting-policy/by_active_from/2026-07-01'); - $request->assertNotFound(); - } } diff --git a/tests/Http/Controllers/PolicyControllerTest.php b/tests/Http/Controllers/PolicyControllerTest.php new file mode 100644 index 00000000..b3527248 --- /dev/null +++ b/tests/Http/Controllers/PolicyControllerTest.php @@ -0,0 +1,41 @@ +create([ + 'policy_type' => 'hosting-policy', + 'active_from' => '2026-07-01', + ]); + + Policy::factory()->create([ + 'policy_type' => 'hosting-policy', + 'active_from' => '2026-07-02', + ]); + + $request = $this->getJson('v1/policies/hosting-policy/by_active_from/2026-07-01'); + + $request->assertOk(); + $request->assertJsonFragment([ + 'active_from' => '2026-07-01', + 'type' => 'hosting-policy', + ]); + } + + public function testGetPolicyByTypeAndActiveFromReturns422WithInvalidParams(): void { + $request = $this->getJson('v1/policies/fake-policy/by_active_from/not-a-date'); + $request->assertUnprocessable(); + } + + public function testMissingPolicyByTypeAndActiveFromReturns404(): void { + $request = $this->getJson('v1/policies/hosting-policy/by_active_from/2026-07-01'); + $request->assertNotFound(); + } +}