Skip to content

Create endpoint to get polices that should be accepted#1198

Open
rosalieper wants to merge 5 commits into
mainfrom
T429591
Open

Create endpoint to get polices that should be accepted#1198
rosalieper wants to merge 5 commits into
mainfrom
T429591

Conversation

@rosalieper

Copy link
Copy Markdown
Contributor

Bug: T429591

@rosalieper rosalieper marked this pull request as ready for review July 13, 2026 10:29
$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)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I understand, query 1 builds a list of IDs with group-by + max and then pluck, query 2 fetches rows by those IDs.
So it means you do 2 round trip to get IDs and fetch rows. I think for a small set of data it's fine, but I recommend avoiding this pattern for large data set

use Carbon\CarbonImmutable;

class PoliciesController extends Controller {
public function getCurrentPolicies() {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider adding a return type (aka PoliciesCollection in this case)

use App\Http\Resources\PoliciesCollection;
use App\Policy;
use Carbon\CarbonImmutable;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

consider adding PHPDoc for this controller

*/
public function toArray(Request $request): array {
return [
'items' => $this->collection,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the key items sounds "off" for me. ResourceCollection's responses look like { "data": [ // stuff go here] }.

Not a functionality issue but coding style

https://laravel.com/docs/12.x/collections

$response = $this->getJson('/policies/current');

$response->assertOk();
$response->assertJsonCount(1, 'data.items');

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this test only asserts the count, so it can pass even if the endpoint returns the wrong policy.
You should add assertions that the returned item is the expected 'active' policy (by id or active_from value) and that the "future" policies are left out.

public function testGetCurrentPolicies(): void {
// Future policy
Policy::factory()->create([
'active_from' => now()->addDay(),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

real-time now() calls can be fragile when used on different timestamps.
Freeze time with Carbon::setTestNow() or just a plain simple $currentTime = now()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a few suggestions for extra test coverage:

  1. Returns one current policy per policy type
  • Create two terms-of-use rows and two hosting-policy rows, with mixed active dates, then assert exactly 2 results (one per type).
  1. Picks latest active policy for each type
  • For the same type, create old active, newer active and future active
  • Assert the returned row is the newer active, not old and not future.
  1. Returns one current policy per policy type
  • Create two terms-of-use rows and two hosting-policy rows, with mixed active dates, then assert exactly 2 results (one per type).
  1. Current query uses MAX(id) per type (in PoliciesController.php). Add a test that backfills an older active_from with higher id to document expected behavior.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants