diff --git a/app/Http/Controllers/PoliciesController.php b/app/Http/Controllers/PoliciesController.php new file mode 100644 index 000000000..1a3dfd65f --- /dev/null +++ b/app/Http/Controllers/PoliciesController.php @@ -0,0 +1,23 @@ +selectRaw('MAX(id) as id') + ->groupBy('policy_type') + ->pluck('id'); + + $currentPolicies = Policy::whereIn('id', $latestPolicyIds)->get(); + + return new PoliciesCollection($currentPolicies); + } +} diff --git a/app/Http/Resources/PoliciesCollection.php b/app/Http/Resources/PoliciesCollection.php new file mode 100644 index 000000000..d233312bd --- /dev/null +++ b/app/Http/Resources/PoliciesCollection.php @@ -0,0 +1,19 @@ + + */ + public function toArray(Request $request): array { + return [ + 'items' => $this->collection, + ]; + } +} diff --git a/app/Policy.php b/app/Policy.php index 44933192c..d9eb662a7 100644 --- a/app/Policy.php +++ b/app/Policy.php @@ -5,6 +5,7 @@ use Carbon\CarbonImmutable; use Eloquent; use Illuminate\Database\Eloquent\Builder; +use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; /** @@ -24,10 +25,13 @@ * @method static Builder|Policy whereId($value) * @method static Builder|Policy wherePolicyType($value) * @method static Builder|Policy whereUpdatedAt($value) + * @method static \Database\Factories\PolicyFactory factory(...$parameters) * * @mixin Eloquent */ class Policy extends Model { + use HasFactory; + // define which attributes are mass assignable protected $fillable = [ 'policy_type', diff --git a/database/factories/PolicyFactory.php b/database/factories/PolicyFactory.php new file mode 100644 index 000000000..b53aa93b2 --- /dev/null +++ b/database/factories/PolicyFactory.php @@ -0,0 +1,27 @@ + + */ +class PolicyFactory extends Factory { + protected $model = Policy::class; + + /** + * Define the model's default state. + * + * @return array + */ + public function definition(): array { + return [ + 'policy_type' => $this->faker->randomElement(['terms-of-use', 'hosting-policy']), + 'active_from' => now(), + 'content_vue_file' => fake()->slug() . '.vue', + ]; + } +} diff --git a/routes/api.php b/routes/api.php index f014ffbcb..e88422c69 100644 --- a/routes/api.php +++ b/routes/api.php @@ -21,6 +21,7 @@ $router->post('user/resetPassword', ['uses' => 'Auth\ResetPasswordController@reset']); $router->post('contact/sendMessage', ['uses' => 'ContactController@sendMessage']); $router->post('complaint/sendMessage', ['uses' => 'ComplaintController@sendMessage']); + $router->get('policies/current', ['uses' => 'PoliciesController@getCurrentPolicies']); $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 new file mode 100644 index 000000000..fbfdbb8ca --- /dev/null +++ b/tests/Http/Controllers/PoliciesControllerTest.php @@ -0,0 +1,47 @@ +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 + $latestToUPolicy = Policy::factory()->create([ + 'policy_type' => 'terms-of-use', + 'active_from' => $currentTime->subMonth(), + ]); + $latestHostingPolicy = Policy::factory()->create([ + 'policy_type' => 'hosting-policy', + 'active_from' => $currentTime->subWeek(), + ]); + + $response = $this->getJson('/policies/current'); + + $response->assertOk(); + $response->assertJsonCount(2, 'data.items'); + $response->assertJsonFragment([ + 'id' => $latestToUPolicy->id, + 'active_from' => $latestToUPolicy->active_from, + ]); + $response->assertJsonFragment([ + 'id' => $latestHostingPolicy->id, + 'active_from' => $latestHostingPolicy->active_from, + ]); + } +}