Ticket 738 : Rewrite DeleteUnconfirmedCustomers
This commit is contained in:
parent
f4c60bf6f7
commit
84018f9d5b
2 changed files with 32 additions and 17 deletions
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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');
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue