ldc-crm/app/Console/Commands/DeleteUnconfirmedCustomers.php
2022-03-29 18:09:29 +02:00

68 lines
2.2 KiB
PHP

<?php
namespace App\Console\Commands;
use App\Models\CustomerInfo;
use App\Models\CustomerLogin;
use App\Models\CustomerOptInHistory;
use Carbon\Carbon;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
/**
* @codeCoverageIgnore
*/
class DeleteUnconfirmedCustomers extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'customer:deleteuc';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Delete the customers that haven\'t
confirmed their acocunt within 24 hours after the registeration.';
/**
* Execute the console command.
*
* @return integer
*/
public function handle()
{
/** @var \Illuminate\Database\Eloquent\Collection $customersLogins */
$customersLogins = CustomerLogin::whereStatus(false)->where('created_at', '<', Carbon::now()->subDay(1))->get();
$customersLogins->map(function (CustomerLogin $customerLogin) {
DB::transaction(function () use ($customerLogin) {
/** @var \App\Models\Customer $customer */
$customer = $customerLogin->customer;
// * Si non activé et créer il y a plus d'un jour : Suppression.
$customerLogin->customer->infos()->where('brand_id', $customerLogin->brand_id)
->get()->map(function (CustomerInfo $customerInfo) {
$customerInfo->deleteOrFail();
});
// * Suppression de ses infos de login pour la marque.
$customerLogin->deleteOrFail();
// * Si le customer n'a plus d'info il n'a donc plus besoin de compte en crm.
if (!$customer->infos()->count()) {
// * Suppression du customer une fois tout le reste supprimé.
$customer->optInHistory()->get()
->map(function (CustomerOptInHistory $customerOptInHistory) {
$customerOptInHistory->deleteOrFail();
});
$customer->deleteOrFail();
}
});
});
return 0;
}
}