From e0075ab68c8fbe7952c715b74c298955d673d4b4 Mon Sep 17 00:00:00 2001 From: Noah Lancien Date: Thu, 10 Apr 2025 09:53:29 +0200 Subject: [PATCH 1/2] 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 + ) + ); + } + }); } } From 5cf4fad3816377f9600d4f442db9fea9dd8a2413 Mon Sep 17 00:00:00 2001 From: Noah Lancien Date: Thu, 10 Apr 2025 15:20:37 +0200 Subject: [PATCH 2/2] fix: write logs in csv for crm customer deletion --- app/Jobs/DeleteCrmCustomerJob.php | 86 ++++++++++++++++++++----------- 1 file changed, 56 insertions(+), 30 deletions(-) diff --git a/app/Jobs/DeleteCrmCustomerJob.php b/app/Jobs/DeleteCrmCustomerJob.php index d65296f..0729dec 100644 --- a/app/Jobs/DeleteCrmCustomerJob.php +++ b/app/Jobs/DeleteCrmCustomerJob.php @@ -45,38 +45,64 @@ class DeleteCrmCustomerJob implements ShouldQueue $hashchode = $this->customer['hashcode']; $email = $this->customer['email']; - 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(); + try { + DB::transaction(function () use ($hashchode, $email) { + // For baskets and old walls. + $contact = Contact::query()->where('contact_ref', $hashchode)->first(); + if ($contact) { + $contact->deleteOrFail(); - Log::channel('crm')->info( - sprintf( - "Hashcode : %s - Email : %s - Contact deleted.", - $hashchode, - $email - ) - ); - } + $this->writeCsvLog('Contact deleted'); + } - // For walls. - $customer = Customers::where('hashcode', $hashchode)->first(); - if ($customer) { - $customer->printedWalls()->delete(); - $customer->delete(); + // For walls. + $customer = Customers::query()->where('hashcode', $hashchode)->first(); + if ($customer) { + $customer->deleteOrFail(); - Log::channel('crm')->info( - sprintf( - "Hashcode : %s - Email : %s - Customer deleted.", - $hashchode, - $email - ) - ); - } - }); + $this->writeCsvLog('Customer deleted'); + } + }); + } catch (\Exception $e) { + Log::info( + sprintf( + "DeleteCrmCustomerJob failed - Code : %s - %s", + $e->getCode(), + $e->getMessage(), + ) + ); + \report($e); + } //end try + } + + /** + * Write logs in /logs/crm.csv. + * + * @param string $message + * + * @return void + */ + private function writeCsvLog(string $message) + { + + $csvPath = \storage_path('/logs/crm.csv'); + if (!\file_exists($csvPath)) { + $logFile = fopen($csvPath, 'w'); + \fputcsv($logFile, ['date', 'hashcode', 'email', 'action'], ';'); + \fclose($logFile); + } + + $logFile = fopen($csvPath, 'a'); + \fputcsv( + $logFile, + [ + \now()->toAtomString(), + $this->customer['hashcode'], + $this->customer['email'], + $message + ], + ';' + ); + \fclose($logFile); } }