From f7362dbc5af033060a3d7ff98e80fc63d5e5344f Mon Sep 17 00:00:00 2001 From: Daeng Deni Mardaeni Date: Wed, 29 Jan 2025 19:42:28 +0700 Subject: [PATCH] feat(transactions): tambahkan job dan model untuk memproses data transaksi - Menambahkan job `ProcessTransactionDataJob` untuk memproses file CSV transaksi. - Membuat model `TempTransaction` untuk menyimpan data transaksi sementara. - Menambahkan migrasi untuk tabel `temp_transactions` dengan atribut yang diperlukan. --- app/Jobs/ProcessTransactionDataJob.php | 68 +++++++++++++++++++ app/Models/TempTransaction.php | 23 +++++++ ..._043114_create_temp_transactions_table.php | 35 ++++++++++ 3 files changed, 126 insertions(+) create mode 100644 app/Jobs/ProcessTransactionDataJob.php create mode 100644 app/Models/TempTransaction.php create mode 100644 database/migrations/2025_01_28_043114_create_temp_transactions_table.php diff --git a/app/Jobs/ProcessTransactionDataJob.php b/app/Jobs/ProcessTransactionDataJob.php new file mode 100644 index 0000000..3601c9b --- /dev/null +++ b/app/Jobs/ProcessTransactionDataJob.php @@ -0,0 +1,68 @@ +getFillable(); + while (($row = fgetcsv($handle, 0, ";")) !== false) { + if (count($headers) === count($row)) { + $data = array_combine($headers, $row); + + try { + TempTransaction::updateOrCreate(['_id' => $data['_id']], $data); + } catch (Exception $e) { + Log::error('Error processing transactions: ' . $e->getMessage()); + } + } + } + fclose($handle); + } else { + throw new Exception("Unable to open file: {$filePath}"); + } + } catch (Exception $e) { + Log::error('Error in ProcessTransctionDataJob: ' . $e->getMessage()); + throw $e; + } + } + } diff --git a/app/Models/TempTransaction.php b/app/Models/TempTransaction.php new file mode 100644 index 0000000..72e3aa7 --- /dev/null +++ b/app/Models/TempTransaction.php @@ -0,0 +1,23 @@ +id(); + foreach ($fieldsArray as $field) { + $field = trim($field); + $table->text($field)->nullable(); + } + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('temp_transactions'); + } +};