Skip to content

Commit 365a84e

Browse files
authored
Create endpoint to get specific policy by type and active_from date (#1210)
Bug: T432339
1 parent 2b02541 commit 365a84e

3 files changed

Lines changed: 79 additions & 0 deletions

File tree

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace App\Http\Controllers;
4+
5+
use App\Http\Resources\PolicyResource;
6+
use App\Policy;
7+
use Carbon\CarbonImmutable;
8+
use Illuminate\Support\Facades\Validator;
9+
use Illuminate\Validation\Rule;
10+
11+
class PolicyController extends Controller {
12+
public function getPolicyByTypeAndActiveFrom($policyType, $activeFrom): PolicyResource {
13+
$validator = Validator::make(
14+
[
15+
'policy_type' => $policyType,
16+
'active_from' => $activeFrom,
17+
],
18+
[
19+
'policy_type' => ['required', 'string', Rule::in(['terms-of-use', 'hosting-policy'])],
20+
'active_from' => ['required', 'date', 'date_format:Y-m-d'],
21+
]
22+
23+
);
24+
$validator->validate();
25+
$validated = $validator->safe();
26+
27+
$validatedActiveFrom = CarbonImmutable::parse($validated['active_from']);
28+
29+
$policy = Policy::where('policy_type', $validated['policy_type'])->where('active_from', '=', $validatedActiveFrom)->first();
30+
31+
if (!$policy) {
32+
abort(404, 'Policy not found.');
33+
}
34+
35+
return new PolicyResource($policy);
36+
}
37+
}

routes/api.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
$router->post('contact/sendMessage', ['uses' => 'ContactController@sendMessage']);
2323
$router->post('complaint/sendMessage', ['uses' => 'ComplaintController@sendMessage']);
2424
$router->get('v1/policies/current', ['uses' => 'PoliciesController@getCurrentPolicies']);
25+
$router->get('v1/policies/{policy_type}/by_active_from/{active_from}', ['uses' => 'PolicyController@getPolicyByTypeAndActiveFrom']);
2526

2627
$router->post('auth/login', ['uses' => 'Auth\LoginController@postLogin'])->name('login');
2728
// Authed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace Http\Controllers;
4+
5+
use App\Policy;
6+
use Illuminate\Foundation\Testing\DatabaseTransactions;
7+
use Tests\TestCase;
8+
9+
class PolicyControllerTest extends TestCase {
10+
use DatabaseTransactions;
11+
12+
public function testGetPolicyByTypeAndActiveFrom(): void {
13+
Policy::factory()->create([
14+
'policy_type' => 'hosting-policy',
15+
'active_from' => '2026-07-01',
16+
]);
17+
18+
Policy::factory()->create([
19+
'policy_type' => 'hosting-policy',
20+
'active_from' => '2026-07-02',
21+
]);
22+
23+
$request = $this->getJson('v1/policies/hosting-policy/by_active_from/2026-07-01');
24+
25+
$request->assertOk();
26+
$request->assertJsonFragment([
27+
'active_from' => '2026-07-01',
28+
'type' => 'hosting-policy',
29+
]);
30+
}
31+
32+
public function testGetPolicyByTypeAndActiveFromReturns422WithInvalidParams(): void {
33+
$request = $this->getJson('v1/policies/fake-policy/by_active_from/not-a-date');
34+
$request->assertUnprocessable();
35+
}
36+
37+
public function testMissingPolicyByTypeAndActiveFromReturns404(): void {
38+
$request = $this->getJson('v1/policies/hosting-policy/by_active_from/2026-07-01');
39+
$request->assertNotFound();
40+
}
41+
}

0 commit comments

Comments
 (0)