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..7808ff24a --- /dev/null +++ b/app/Http/Resources/PoliciesCollection.php @@ -0,0 +1,20 @@ +collection->mapInto(PolicyResource::class)->toArray(); + } +} diff --git a/app/Http/Resources/PolicyResource.php b/app/Http/Resources/PolicyResource.php new file mode 100644 index 000000000..a3061ec3e --- /dev/null +++ b/app/Http/Resources/PolicyResource.php @@ -0,0 +1,25 @@ + + */ + 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, + ], + ]; + } +} 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..62f7aa499 --- /dev/null +++ b/database/factories/PolicyFactory.php @@ -0,0 +1,26 @@ + + */ +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..e4e98fc2d 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('v1/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..7c9e030b9 --- /dev/null +++ b/tests/Http/Controllers/PoliciesControllerTest.php @@ -0,0 +1,60 @@ +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'), + ]); + } +}