Skip to content

Commit ba5312f

Browse files
committed
more guide with example
1 parent 3bdfd04 commit ba5312f

File tree

1 file changed

+99
-0
lines changed

1 file changed

+99
-0
lines changed

README.md

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,105 @@ $action->syncWithoutDetachingRunsOnEvery([Days::SUNDAY, ..]); // doesnt remove
165165
$action->setNonRecurring()->setActOn($carbon)->save();
166166
```
167167

168+
### Example
169+
170+
###### Step - 1 : some event happend and a task is created to execute on future day & time
171+
```php
172+
ModelAction::actWith('MAIL')
173+
->forModel($application)
174+
->actAt($inThreeDays)
175+
->setExtraProperties([
176+
'mailable' => RejectApplication::class,
177+
'template' => $template,
178+
'role' => $application->job->role
179+
])
180+
->createSchedule();
181+
```
182+
183+
###### Step - 2 : admin decides to alter the task
184+
```php
185+
public function modifyScheduledTask() {
186+
$this->validate();
187+
188+
$this->schedule
189+
->setActDate(Carbon::createFromFormat('m/d/Y', $this->act_date))
190+
->setActTime(Carbon::createFromFormat('H:i:s', $this->act_time))
191+
->mergeExtraProperties([
192+
'template' => $this->templateid,
193+
'extra_data' => $this->role
194+
])
195+
->save();
196+
197+
$this->info('schedule updated');
198+
}
199+
200+
public function cancelSchedule() {
201+
$this->schedule->setCancelled()->save();
202+
$this->info('schedule cancelled');
203+
}
204+
```
205+
206+
###### Step - 3 : receiver class gets task payload & passes the task to classes based on task action (for this example sending email)
207+
```php
208+
<?php
209+
210+
namespace App\Http\AutoAction;
211+
212+
use Facades\App\Http\Services\AutoAction\Mailer;
213+
214+
class ScheduledActionReceiver
215+
{
216+
public function __invoke($tasks)
217+
{
218+
foreach ($tasks as $task) {
219+
match($task->action) {
220+
'MAIL' => MailTaskHandler::handle($task),
221+
...
222+
223+
default => activity()->log('auto action task unhandled')
224+
};
225+
}
226+
}
227+
}
228+
```
229+
###### Step - 4 : email sending task payload gets received via previous receiver class and mail is sent
230+
```php
231+
class MailTaskHandler
232+
{
233+
public function handle($task) {
234+
[$user, $mailable, $extras, $time] = $this->extractData($task);
235+
236+
Mail::to($user)
237+
->later(
238+
Carbon::now()->setTimeFromTimeString($time),
239+
new $mailable($user, $extras)
240+
);
241+
242+
$task->setFinished()->save(); // 👈 marking the task finished here though it is actually not, because for mail there is no easy way to execute this code after that mail is actually sent
243+
}
244+
245+
private function extractData($task) {
246+
$model = $task->actionable;
247+
$user = null;
248+
249+
$mailable = $task->getExtraProperty('mailable');
250+
$template = $task->getExtraProperty('template');
251+
252+
$extras = [];
253+
254+
if($mailable === RejectApplication::class) {
255+
$extras['role'] = $task->getExtraProperty('role');
256+
257+
$user = $model->applicant;
258+
}
259+
260+
$time = $task->act_time;
261+
262+
return [$user, $mailable, $template, $extras, $time];
263+
}
264+
}
265+
```
266+
168267

169268
## Changelog
170269

0 commit comments

Comments
 (0)