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 125bc994..d6dc19fa 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' => 'PolicyController@getPolicyByTypeAndActiveFrom']); $router->post('auth/login', ['uses' => 'Auth\LoginController@postLogin'])->name('login'); // Authed 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(); + } +}