-
Notifications
You must be signed in to change notification settings - Fork 3
Create endpoint to get polices that should be accepted #1198
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
d2ebfb7
Create endpoint to get polices that should be accepted
rosalieper 14506fd
Added unit tests
rosalieper ee33c8c
Fix tests
rosalieper 68af187
Merge branch 'main' into T429591
rosalieper a259661
Fix linting
rosalieper ecc8953
Improve tests
rosalieper 91dc8bd
refactor test
rosalieper 035956a
fix nitpick
rosalieper 5d6e358
add v1 to route
rosalieper 07bbf50
Remove key from the api response
rosalieper bfbd3c3
fix linting
rosalieper 3fa75d1
adjust test to spec
deer-wmde b55b0db
unwrap `data` key
deer-wmde a44deea
add PolicyResource class
deer-wmde 3b27d1d
wrap in "items"
deer-wmde ddde8d1
map to PolicyResource
deer-wmde f023a67
adjust date format
deer-wmde 62c9d8b
formatting
deer-wmde d9a750f
Merge branch 'main' into T429591
deer-wmde File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| <?php | ||
|
|
||
| namespace App\Http\Controllers; | ||
|
|
||
| use App\Http\Resources\PoliciesCollection; | ||
| use App\Policy; | ||
| use Carbon\CarbonImmutable; | ||
|
|
||
| class PoliciesController extends Controller { | ||
| public function getCurrentPolicies(): PoliciesCollection { | ||
| $now = CarbonImmutable::now(); | ||
|
|
||
| // This works based on the assumption that the latest policy has the highest id given that id is AUTO_INCREMENT | ||
| $latestPolicyIds = Policy::where('active_from', '<', $now) | ||
|
rosalieper marked this conversation as resolved.
|
||
| ->selectRaw('MAX(id) as id') | ||
| ->groupBy('policy_type') | ||
| ->pluck('id'); | ||
|
|
||
| $currentPolicies = Policy::whereIn('id', $latestPolicyIds)->get(); | ||
|
|
||
| return new PoliciesCollection($currentPolicies); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| <?php | ||
|
|
||
| namespace App\Http\Resources; | ||
|
|
||
| use Illuminate\Http\Request; | ||
| use Illuminate\Http\Resources\Json\ResourceCollection; | ||
| use Illuminate\Support\Collection; | ||
|
|
||
| class PoliciesCollection extends ResourceCollection { | ||
| // per default Laravel wraps ResourceCollections in a `data` key: https://laravel.com/docs/11.x/eloquent-resources#data-wrapping | ||
| // which is not wanted in this case: https://phabricator.wikimedia.org/T429591 | ||
| public static $wrap = 'items'; | ||
|
|
||
| /** | ||
| * Transform the resource collection into an array. | ||
| */ | ||
| public function toArray(Request $request): array { | ||
| return $this->collection->mapInto(PolicyResource::class)->toArray(); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| <?php | ||
|
|
||
| namespace App\Http\Resources; | ||
|
|
||
| use Illuminate\Http\Request; | ||
| use Illuminate\Http\Resources\Json\JsonResource; | ||
| use Illuminate\Support\Carbon; | ||
|
|
||
| class PolicyResource extends JsonResource { | ||
| /** | ||
| * Transform the resource into an array. | ||
| * | ||
| * @return array<string, mixed> | ||
| */ | ||
| public function toArray(Request $request): array { | ||
| return [ | ||
| 'metadata' => [ | ||
| 'policy_id' => $this->id, | ||
| 'type' => $this->policy_type, | ||
| 'active_from' => Carbon::parse($this->active_from)->format('Y-m-d'), | ||
| 'content_vue_file' => $this->content_vue_file, | ||
| ], | ||
| ]; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| <?php | ||
|
|
||
| namespace Database\Factories; | ||
|
|
||
| use App\Policy; | ||
| use Illuminate\Database\Eloquent\Factories\Factory; | ||
|
|
||
| /** | ||
| * @extends Factory<Policy> | ||
| */ | ||
| class PolicyFactory extends Factory { | ||
| protected $model = Policy::class; | ||
|
|
||
| /** | ||
| * Define the model's default state. | ||
| * | ||
| * @return array<string, mixed> | ||
| */ | ||
| public function definition(): array { | ||
| return [ | ||
| 'policy_type' => $this->faker->randomElement(['terms-of-use', 'hosting-policy']), | ||
| 'active_from' => now(), | ||
| 'content_vue_file' => fake()->slug() . '.vue', | ||
| ]; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
|
deer-wmde marked this conversation as resolved.
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| <?php | ||
|
|
||
| namespace Tests\Http\Controllers; | ||
|
|
||
| use App\Policy; | ||
| use Illuminate\Foundation\Testing\DatabaseTransactions; | ||
| use Tests\TestCase; | ||
|
|
||
| class PoliciesControllerTest extends TestCase { | ||
| use DatabaseTransactions; | ||
|
|
||
| public function testGetCurrentPolicies(): void { | ||
| $currentTime = now(); | ||
|
|
||
| // Future policy | ||
| Policy::factory()->create([ | ||
| 'policy_type' => 'terms-of-use', | ||
| 'active_from' => $currentTime->addDay(), | ||
| ]); | ||
| // Old policy | ||
| Policy::factory()->create([ | ||
| 'policy_type' => 'hosting-policy', | ||
| 'active_from' => $currentTime->subMonth(), | ||
| ]); | ||
| // Active policies | ||
| $latestActiveToUPolicy = Policy::factory()->create([ | ||
| 'policy_type' => 'terms-of-use', | ||
| 'active_from' => $currentTime->subMonth(), | ||
| ]); | ||
| $latestActiveHostingPolicy = Policy::factory()->create([ | ||
| 'policy_type' => 'hosting-policy', | ||
| 'active_from' => $currentTime->subWeek(), | ||
| ]); | ||
|
|
||
| $response = $this->getJson('/v1/policies/current'); | ||
|
|
||
| $response->assertOk(); | ||
| $response->assertJsonStructure([ | ||
| 'items' => [ | ||
| '*' => [ | ||
| 'metadata' => [ | ||
| 'policy_id', | ||
| 'active_from', | ||
| 'content_vue_file', | ||
| 'type', | ||
| ], | ||
| ], | ||
| ], | ||
| ]); | ||
|
|
||
| $response->assertJsonFragment([ | ||
| 'policy_id' => $latestActiveToUPolicy->id, | ||
| 'active_from' => $latestActiveToUPolicy->active_from->format('Y-m-d'), | ||
| ]); | ||
| $response->assertJsonFragment([ | ||
| 'policy_id' => $latestActiveHostingPolicy->id, | ||
| 'active_from' => $latestActiveHostingPolicy->active_from->format('Y-m-d'), | ||
| ]); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.