feat: Phase 4 - Mailwizz integration with subscriber sync and retry
This commit is contained in:
46
app/Services/DispatchUnsyncedMailwizzSyncJobsService.php
Normal file
46
app/Services/DispatchUnsyncedMailwizzSyncJobsService.php
Normal file
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use App\Jobs\SyncSubscriberToMailwizz;
|
||||
use App\Models\Subscriber;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
|
||||
final class DispatchUnsyncedMailwizzSyncJobsService
|
||||
{
|
||||
/**
|
||||
* Dispatch a Mailwizz sync job for each subscriber that is not synced yet and whose page has Mailwizz configured.
|
||||
* Duplicate jobs for the same subscriber are skipped while a job is already pending or running (unique lock on SyncSubscriberToMailwizz).
|
||||
*
|
||||
* @return int Number of eligible subscribers (dispatch was attempted for each; some attempts may be skipped as duplicates)
|
||||
*/
|
||||
public function dispatch(?int $preregistrationPageId = null): int
|
||||
{
|
||||
$query = $this->eligibleSubscribersQuery();
|
||||
if ($preregistrationPageId !== null) {
|
||||
$query->where('preregistration_page_id', $preregistrationPageId);
|
||||
}
|
||||
|
||||
$count = 0;
|
||||
$query->chunkById(200, function ($subscribers) use (&$count): void {
|
||||
foreach ($subscribers as $subscriber) {
|
||||
SyncSubscriberToMailwizz::dispatch($subscriber);
|
||||
$count++;
|
||||
}
|
||||
});
|
||||
|
||||
return $count;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Builder<Subscriber>
|
||||
*/
|
||||
public function eligibleSubscribersQuery(): Builder
|
||||
{
|
||||
return Subscriber::query()
|
||||
->where('synced_to_mailwizz', false)
|
||||
->whereHas('preregistrationPage.mailwizzConfig');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user