use App\Types\Job;
use App\Models\Tip;
use Illuminate\Database\Eloquent\Collection;
class ProcessTipsJob extends Job
{
protected array $fields = [
'tips.id',
'tips.user_id',
'tips.slug',
'tips.summary',
'tips.first_tag',
];
public int $tries = 1;
public function handle() : void
{
Tip::query()
->select($this->fields)
->join('users', 'tips.user_id', '=', 'users.id')
->where('shared', false)
->whereBetween('published_at', [now()->subDay(), now()])
->chunkById(50, fn($tips) => $this->process($tips));
}
public function process(Collection $tips) : void
{
$query = Tip::whereIn('id', $tips->pluck('id'));
attempt(fn() => $query->update(['shared' => true]));
$tips->each(fn($tip) => PostToSocialMediaJob::dispatch($tip));
}
}