From e0075ab68c8fbe7952c715b74c298955d673d4b4 Mon Sep 17 00:00:00 2001 From: Noah Lancien Date: Thu, 10 Apr 2025 09:53:29 +0200 Subject: [PATCH] fix: Put all database actions in a transaction for the DeleteCrmCustomer job --- app/Jobs/DeleteCrmCustomerJob.php | 59 ++++++++++++++++--------------- 1 file changed, 31 insertions(+), 28 deletions(-) diff --git a/app/Jobs/DeleteCrmCustomerJob.php b/app/Jobs/DeleteCrmCustomerJob.php index b342131..d65296f 100644 --- a/app/Jobs/DeleteCrmCustomerJob.php +++ b/app/Jobs/DeleteCrmCustomerJob.php @@ -9,6 +9,7 @@ use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Foundation\Bus\Dispatchable; use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\SerializesModels; +use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Log; class DeleteCrmCustomerJob implements ShouldQueue @@ -44,36 +45,38 @@ class DeleteCrmCustomerJob implements ShouldQueue $hashchode = $this->customer['hashcode']; $email = $this->customer['email']; - // For baskets and old walls. - $contact = Contact::where('contact_ref', $hashchode)->first(); - if ($contact) { - $contact->murs()->delete(); - $contact->paniers()->delete(); - $contact->logs()->delete(); - $contact->delete(); + DB::transaction(function () use ($hashchode, $email) { + // For baskets and old walls. + $contact = Contact::where('contact_ref', $hashchode)->first(); + if ($contact) { + $contact->murs()->delete(); + $contact->paniers()->delete(); + $contact->logs()->delete(); + $contact->delete(); - Log::channel('crm')->info( - sprintf( - "Hashcode : %s - Email : %s - Contact deleted.", - $hashchode, - $email - ) - ); - } + Log::channel('crm')->info( + sprintf( + "Hashcode : %s - Email : %s - Contact deleted.", + $hashchode, + $email + ) + ); + } - // For walls. - $customer = Customers::where('hashcode', $hashchode)->first(); - if ($customer) { - $customer->printedWalls()->delete(); - $customer->delete(); + // For walls. + $customer = Customers::where('hashcode', $hashchode)->first(); + if ($customer) { + $customer->printedWalls()->delete(); + $customer->delete(); - Log::channel('crm')->info( - sprintf( - "Hashcode : %s - Email : %s - Customer deleted.", - $hashchode, - $email - ) - ); - } + Log::channel('crm')->info( + sprintf( + "Hashcode : %s - Email : %s - Customer deleted.", + $hashchode, + $email + ) + ); + } + }); } }