66use Devsrv \ScheduledAction \Enums \Status ;
77use Illuminate \Database \Eloquent \Factories \HasFactory ;
88use Carbon \Carbon ;
9+ use Illuminate \Support \Arr ;
10+ use Devsrv \ScheduledAction \Traits \{ActionStatus , FluentUpdate , FluentCreate };
911
1012class ModelAction extends Model
1113{
12- use HasFactory;
14+ use HasFactory, ActionStatus, FluentUpdate, FluentCreate ;
1315
1416 protected $ table = 'model_actions ' ;
1517
@@ -31,6 +33,11 @@ public function actionable()
3133 return $ this ->morphTo ();
3234 }
3335
36+ public function getExtraProperty (string $ propertyName ): mixed
37+ {
38+ return Arr::get ($ this ->properties ->toArray (), $ propertyName );
39+ }
40+
3441 public function scopeFinished ($ query )
3542 {
3643 return $ query ->where ('status ' , Status::FINISHED );
@@ -61,16 +68,36 @@ public function scopeToActBetween($query, $start, $end)
6168 return $ query ->whereBetween ('act_at ' , [$ start , $ end ]);
6269 }
6370
64- public function scopeWhereProperty ($ query , $ prop , $ value )
71+ public function scopeWhereExtraProperty ($ query , $ prop , $ value )
6572 {
6673 return $ query ->whereJsonContains ('properties-> ' . $ prop , $ value );
6774 }
6875
69- public function scopeWhereProperties ($ query , array $ properties )
76+ public function scopeWhereExtraProperties ($ query , array $ properties )
7077 {
7178 collect ($ properties )
7279 ->each (fn ($ value , $ key ) => $ query ->whereJsonContains ('properties-> ' . $ key , $ value ));
7380
7481 return $ query ;
7582 }
83+
84+ public function scopeOrderByWhenToAct ($ query , string $ direction = 'asc ' )
85+ {
86+ return $ query ->orderBy ('act_at ' , $ direction !== 'asc ' ? 'desc ' : 'asc ' );
87+ }
88+
89+ public function scopeFor ($ query , Model $ model )
90+ {
91+ return $ query
92+ ->where (function ($ query ) use ($ model ) {
93+ $ query
94+ ->where ('actionable_type ' , $ model ->getMorphClass ())
95+ ->where ('actionable_id ' , $ model ->getKey ());
96+ });
97+ }
98+
99+ public function scopeForClass ($ query , string $ model )
100+ {
101+ return $ query ->whereHasMorph ('actionable ' , $ model );
102+ }
76103}
0 commit comments