ldc-crm/app/Console/Commands/DeleteUnconfirmedCustomers.php
Romain Bonhomme 328e38ce38 Init
2020-12-01 17:02:10 +01:00

60 lines
1.8 KiB
PHP

<?php
namespace App\Console\Commands;
use App\Models\Customer;
use App\Models\CustomerLogin;
use Carbon\Carbon;
use Illuminate\Console\Command;
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.";
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
$customersLogins = CustomerLogin::where([['status', 0], ['created_at', '<', Carbon::now()->subDay(1)]])->get();
foreach($customersLogins as $customerLogin) {
// Si non activé et créer il y a plus d'un jour : Suppression
$customerLogin->customer->infos()->where('brand_id', $customerLogin->brand_id)->first()->delete();
// On récupère son id pour le cas ou on doit le supprimer complètement après
$customerId = $customerLogin->customer->id;
// Suppression de ses infos de login pour la marque
$customerLogin->delete();
// Si le customer n'a plus d'info il n'a donc plus besoin de compte en crm
if (count($customerLogin->customer->infos) === 0) {
// Suppression du customer une fois tout le reste supprimé
$customer = Customer::find($customerId);
$customer->optInHistory()->delete();
$customer->delete();
}
}
}
}