Ticket 738 : Rewrite DeleteUnconfirmedCustomers

This commit is contained in:
Jérémy Munsch 2022-03-29 18:09:29 +02:00
parent f4c60bf6f7
commit 84018f9d5b
No known key found for this signature in database
GPG key ID: 55196BEE445F6049
2 changed files with 32 additions and 17 deletions

View file

@ -2,10 +2,12 @@
namespace App\Console\Commands;
use App\Models\Customer;
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
@ -34,22 +36,33 @@ class DeleteUnconfirmedCustomers extends Command
*/
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();
}
}
/** @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;
}
}

View file

@ -2,6 +2,7 @@
namespace App\Console;
use App\Console\Commands\DeleteUnconfirmedCustomers;
use App\Jobs\StartStatJobs;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
@ -27,6 +28,7 @@ class Kernel extends ConsoleKernel
protected function schedule(Schedule $schedule)
{
$schedule->job(StartStatJobs::class)->dailyAt('03:30');
$schedule->command(DeleteUnconfirmedCustomers::class)->dailyAt('05:30');
}
/**