fix: 🐛 save only updated models in planificated status job
This commit is contained in:
parent
a50ca7f133
commit
e6c0fe65e2
1 changed files with 22 additions and 25 deletions
|
|
@ -20,37 +20,34 @@ class UpdatePlanificatedStatus
|
|||
*/
|
||||
public function handle(): void
|
||||
{
|
||||
// Set all models to apply status modifications.
|
||||
collect([News::class, Strip::class])
|
||||
->each(function (string $model) {
|
||||
// Get all published model with an unpublish_at date passed.
|
||||
/** @var \Illuminate\Database\Eloquent\Collection<array-key,\App\Models\News|\App\Models\Strip> $modelListToUnPublish */
|
||||
$modelListToUnPublish = $model::query()
|
||||
/** @var \Illuminate\Database\Eloquent\Collection<array-key,\App\Models\News|\App\Models\Strip> */
|
||||
$modelListToPublish = $model::query()
|
||||
->whereNotNull('publish_at')
|
||||
->where('publish_at', '<=', Carbon::now())
|
||||
->where(function ($query) {
|
||||
$query->whereNull('unpublish_at')
|
||||
->orWhere('unpublish_at', '>', Carbon::now());
|
||||
})
|
||||
->where('published', false)
|
||||
->get();
|
||||
Log::debug(__CLASS__ . " models to publish " . $modelListToPublish->count());
|
||||
$modelListToPublish->each(function ($currentModel) {
|
||||
$currentModel->published = true;
|
||||
$currentModel->saveOrFail();
|
||||
});
|
||||
|
||||
/** @var \Illuminate\Database\Eloquent\Collection<array-key,\App\Models\News|\App\Models\Strip> */
|
||||
$modelListToUnpublish = $model::query()
|
||||
->whereNotNull('unpublish_at')
|
||||
->where('unpublish_at', '<=', Carbon::now())
|
||||
->where('published', true)
|
||||
->get();
|
||||
Log::debug(__CLASS__ . " model to unpublish " . $modelListToUnPublish->count());
|
||||
|
||||
// Unpublish all models with an unpublish_at date passed.
|
||||
$modelListToUnPublish->each(function (News|Strip $modelToUnpublish) {
|
||||
$modelToUnpublish->published = false;
|
||||
$modelToUnpublish->saveOrFail();
|
||||
});
|
||||
|
||||
// Get all unpublished model with a publish_at date passed.
|
||||
/** @var \Illuminate\Database\Eloquent\Collection<array-key,\App\Models\News|\App\Models\Strip> $modelListToPublish */
|
||||
$modelListToPublish = $model::query()
|
||||
->where('publish_at', '<=', Carbon::now())
|
||||
->whereNotNull('publish_at')
|
||||
->where('published', false)
|
||||
->get();
|
||||
Log::debug(__CLASS__ . " model to publish " . $modelListToPublish->count());
|
||||
|
||||
// Publish all models with a publish_at date passed.
|
||||
$modelListToPublish->each(function (News|Strip $modelToPublish) {
|
||||
$modelToPublish->published = true;
|
||||
$modelToPublish->saveOrFail();
|
||||
Log::debug(__CLASS__ . " models to unpublish " . $modelListToUnpublish->count());
|
||||
$modelListToUnpublish->each(function ($currentModel) {
|
||||
$currentModel->published = false;
|
||||
$currentModel->saveOrFail();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue