@@ -13,10 +13,7 @@ public function getCurrentPolicies(): PoliciesCollection {
1313 $ now = CarbonImmutable::now ();
1414
1515 // This works based on the assumption that the latest policy has the highest id given that id is AUTO_INCREMENT
16- $ latestPolicyIds = Policy::where ('active_from ' , '< ' , $ now )
17- ->selectRaw ('MAX(id) as id ' )
18- ->groupBy ('policy_type ' )
19- ->pluck ('id ' );
16+ $ latestPolicyIds = $ this ->getLatestPolicies ();
2017
2118 $ currentPolicies = Policy::whereIn ('id ' , $ latestPolicyIds )->get ();
2219
@@ -26,19 +23,27 @@ public function getCurrentPolicies(): PoliciesCollection {
2623 public function getMissingPolicies (Request $ request ): PoliciesCollection {
2724 $ now = CarbonImmutable::now ();
2825
29- $ activePolicyIds = Policy::where ('active_from ' , '<= ' , $ now )
30- ->selectRaw ('MAX(id) as id ' )
31- ->groupBy ('policy_type ' )
32- ->pluck ('id ' );
26+ $ activePolicyIds = $ this ->getLatestPolicies ();
3327
34- $ acceptedPolicyIds = PolicyAcceptance::where ('user_id ' , $ request ->user ()->id )
28+ $ userAcceptedPolicyIds = PolicyAcceptance::where ('user_id ' , $ request ->user ()->id )
3529 ->whereIn ('policy_id ' , $ activePolicyIds )
3630 ->pluck ('policy_id ' );
3731
3832 $ missingPolicies = Policy::whereIn ('id ' , $ activePolicyIds )
39- ->whereNotIn ('id ' , $ acceptedPolicyIds )
33+ ->whereNotIn ('id ' , $ userAcceptedPolicyIds )
4034 ->get ();
4135
4236 return new PoliciesCollection ($ missingPolicies );
4337 }
38+
39+ private function getLatestPolicies () {
40+ $ now = CarbonImmutable::now ();
41+
42+ $ latestPolicyIds = Policy::where ('active_from ' , '< ' , $ now )
43+ ->selectRaw ('MAX(id) as id ' )
44+ ->groupBy ('policy_type ' )
45+ ->pluck ('id ' );
46+
47+ return $ latestPolicyIds ;
48+ }
4449}
0 commit comments