ldc-crm/app/Console/Commands/DeleteUnconfirmedCustomers.php
2022-08-29 10:55:54 +02:00

71 lines
2.2 KiB
PHP

<?php
namespace App\Console\Commands;
use App\Models\Customer;
use App\Models\CustomerLogin;
use App\Models\CustomerOptInHistory;
use Carbon\Carbon;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
/**
* @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))
->with(['brand', 'customer'])
->get();
$customersLogins->map(function (CustomerLogin $customerLogin) {
Log::info($customerLogin->customer_id);
DB::transaction(function () use ($customerLogin) {
/** @var \App\Models\Customer $customer */
$customer = $customerLogin->customer;
/** @var \App\Models\Brand */
$brand = $customerLogin->brand;
// * Suppression de ses infos de login pour la marque.
$customer->info($brand, Customer::FIRST_OR_FAIL)->deleteOrFail();
$customer->login($brand, Customer::FIRST_OR_FAIL)->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;
}
}