From 119f6d642800f5f14195cfec41a082ce795a6d67 Mon Sep 17 00:00:00 2001 From: Afiq Hamzah Date: Mon, 10 Jun 2024 18:32:02 +0800 Subject: [PATCH] Codebase update - Remove default pic assignments for unsold commodity notifications - Check wabot credential complete first before notification blast, if incomplete no notification be sent - Change migration column order for settings table, column group first - Resolve error of not showing select after updating any PIC for selected day --- app/Http/Controllers/SettingController.php | 5 + .../WhatsappUnsoldCommodityNotification.php | 16 +- ...024_05_27_093206_create_settings_table.php | 21 +-- .../arrahn_master_db_202405160953.json | 139 ++++++++++++++++++ .../UnsoldCommodityNotificationPICSeeders.php | 37 ----- ...ldCommodityNotificationSettingsSeeders.php | 16 +- 6 files changed, 176 insertions(+), 58 deletions(-) create mode 100644 database/migrations/arrahn_master_db_202405160953.json delete mode 100644 database/seeds/UnsoldCommodityNotificationPICSeeders.php diff --git a/app/Http/Controllers/SettingController.php b/app/Http/Controllers/SettingController.php index d743cea..bf9476a 100644 --- a/app/Http/Controllers/SettingController.php +++ b/app/Http/Controllers/SettingController.php @@ -57,6 +57,11 @@ class SettingController extends Controller array_walk($unique_pic_input, function ($item) use (&$transformed_unique_pic_input) { foreach ($item as $key => $value) { + + if(count($value) === 0){ + $value = [new \stdClass()]; + } + $transformed_unique_pic_input[$key] = $value; } }); diff --git a/app/Jobs/WhatsappUnsoldCommodityNotification.php b/app/Jobs/WhatsappUnsoldCommodityNotification.php index 60ee602..fe0dd51 100644 --- a/app/Jobs/WhatsappUnsoldCommodityNotification.php +++ b/app/Jobs/WhatsappUnsoldCommodityNotification.php @@ -37,7 +37,8 @@ class WhatsappUnsoldCommodityNotification extends Job $start_time = $settings->where('key', 'start_time')->first()->value; $end_time = $settings->where('key', 'end_time')->first()->value; $message = $settings->where('key', 'message')->first()->value; - $isKomoditiStillSold = KomuditiPurchase::on('mysql7')->latest()->first()->unit_komuditi === 0; + $isAllKomoditiSold = KomuditiPurchase::on('mysql7')->latest()->first()->unit_komuditi === 0; + $isWabotCredentialNotComplete = env('WABOT_API') === null || env('WABOT_INSTANCES_ID') === null || env('WABOT_TOKEN_KEYS') === null; $pic_key = 'pic_' . strtolower(Carbon::now()->englishDayOfWeek); $pic = json_decode($settings->where('key', $pic_key)->first()->value); @@ -48,12 +49,18 @@ class WhatsappUnsoldCommodityNotification extends Job } }, $pic); + if ($isWabotCredentialNotComplete) { + Log::error('Wabot credential not complete'); + return false; + } if ($phone_numbers[0] === NULL) { + Log::error('No PIC phone numbers detected'); return false; } if ($status === '0') { + Log::info('Unsold commodity notifications are turned off, no notification will be sent'); return false; } @@ -65,10 +72,12 @@ class WhatsappUnsoldCommodityNotification extends Job return false; } - if ($isKomoditiStillSold) { + if ($isAllKomoditiSold) { + Log::info('All komoditi is sold'); return false; } + foreach ($phone_numbers as $phone_number) { $response = Http::get(env('WABOT_API'), [ 'number' => $phone_number, @@ -89,12 +98,11 @@ class WhatsappUnsoldCommodityNotification extends Job $logblastwasap->updated_at = Carbon::now(); $logblastwasap->save(); - Log::info("WABOT - Notifikasi Komoditi Tidak Terjual".$response); + Log::info("WABOT - Notifikasi Komoditi Tidak Terjual" . $response); } dispatch((new WhatsappUnsoldCommodityNotification($this->message))->delay(Carbon::now()->addMinutes(5))); - } } diff --git a/database/migrations/2024_05_27_093206_create_settings_table.php b/database/migrations/2024_05_27_093206_create_settings_table.php index c836094..f1edbaa 100644 --- a/database/migrations/2024_05_27_093206_create_settings_table.php +++ b/database/migrations/2024_05_27_093206_create_settings_table.php @@ -13,18 +13,19 @@ class CreateSettingsTable extends Migration */ public function up() { - Schema::create('settings', function (Blueprint $table) { - $table->id(); - $table->string('key')->unique(); - $table->text('value')->nullable(); - $table->string('group')->nullable(); - $table->text('description')->nullable(); - $table->timestamps(); - }); + if (!Schema::hasTable('settings')) { + Schema::create('settings', function (Blueprint $table) { + $table->id(); + $table->string('group')->nullable(); + $table->string('key')->unique(); + $table->text('value')->nullable(); + $table->text('description')->nullable(); + $table->timestamps(); + }); + } // Call the seeder - (new UnsoldCommodityNotificationSettingSeeders())->run(); - (new UnsoldCommodityNotificationPICSeeders())->run(); + (new UnsoldCommodityNotificationSettingSeeders())->run(); } /** diff --git a/database/migrations/arrahn_master_db_202405160953.json b/database/migrations/arrahn_master_db_202405160953.json new file mode 100644 index 0000000..ab3e6ca --- /dev/null +++ b/database/migrations/arrahn_master_db_202405160953.json @@ -0,0 +1,139 @@ +{ +"arrahn_master_db": [ + { + "details_caw" : "Kota Bharu 2", + "kodcaw" : "KB2" + }, + { + "details_caw" : "Wakaf Che Yeh", + "kodcaw" : "WCY" + }, + { + "details_caw" : "1 Stop", + "kodcaw" : "1STOP" + }, + { + "details_caw" : "Jelawat", + "kodcaw" : "020301" + }, + { + "details_caw" : "Wakaf Bharu", + "kodcaw" : "WB" + }, + { + "details_caw" : "Kok Lanas", + "kodcaw" : "ALS" + }, + { + "details_caw" : "Pasir Puteh", + "kodcaw" : "020306" + }, + { + "details_caw" : "Machang", + "kodcaw" : "ARM" + }, + { + "details_caw" : "Pasir Mas", + "kodcaw" : "APM" + }, + { + "details_caw" : "Gua Musang", + "kodcaw" : "010311" + }, + { + "details_caw" : "Guchil", + "kodcaw" : "GC" + }, + { + "details_caw" : "Jeli", + "kodcaw" : "JLI" + }, + { + "details_caw" : "Rantau Panjang", + "kodcaw" : "ARP" + }, + { + "details_caw" : "Kuantan", + "kodcaw" : "010621" + }, + { + "details_caw" : "Pasir Gudang", + "kodcaw" : "010136" + }, + { + "details_caw" : "Skudai", + "kodcaw" : "011235" + }, + { + "details_caw" : "Putatan", + "kodcaw" : "011227" + }, + { + "details_caw" : "Bentong", + "kodcaw" : "010620" + }, + { + "details_caw" : "Raub", + "kodcaw" : "010617" + }, + { + "details_caw" : "Kangar", + "kodcaw" : "010915" + }, + { + "details_caw" : "Sandakan", + "kodcaw" : "011228" + }, + { + "details_caw" : "Sungai Petani", + "kodcaw" : "010219" + }, + { + "details_caw" : "Tawau", + "kodcaw" : "011229" + }, + { + "details_caw" : "Api-Api", + "kodcaw" : "011230" + }, + { + "details_caw" : "Kajang", + "kodcaw" : "011013" + }, + { + "details_caw" : "Raja Laut", + "kodcaw" : "011412" + }, + { + "details_caw" : "Beaufort", + "kodcaw" : "011231" + }, + { + "details_caw" : "Kota Belud", + "kodcaw" : "011232" + }, + { + "details_caw" : "Tuaran", + "kodcaw" : "011234" + }, + { + "details_caw" : "Keningau", + "kodcaw" : "011233" + }, + { + "details_caw" : "Tanah Merah", + "kodcaw" : "020303" + }, + { + "details_caw" : "Temerloh", + "kodcaw" : "010618" + }, + { + "details_caw" : "Semporna", + "kodcaw" : "011237" + }, + { + "details_caw" : "Kota Bharu 1", + "kodcaw" : "KB1" + } +]} diff --git a/database/seeds/UnsoldCommodityNotificationPICSeeders.php b/database/seeds/UnsoldCommodityNotificationPICSeeders.php deleted file mode 100644 index 3092409..0000000 --- a/database/seeds/UnsoldCommodityNotificationPICSeeders.php +++ /dev/null @@ -1,37 +0,0 @@ - [[]], - 'pic_monday' => [[]], - 'pic_tuesday' => [[]], - 'pic_wednesday' => [[]], - 'pic_thursday' => [[]], - 'pic_friday' => [[]], - 'pic_saturday' => [[]], - ]; - - $transformed_data = array_map(function($pic){ - return json_encode($pic); - }, $data_pic); - - $outputs = array_combine(array_keys($data_pic), $transformed_data); - - foreach ($outputs as $key => $data) { - Setting::where('group', 'unsold_commodity_notifications') - ->where('key', $key) - ->update(['value' => $data]); - } - } -} diff --git a/database/seeds/UnsoldCommodityNotificationSettingsSeeders.php b/database/seeds/UnsoldCommodityNotificationSettingsSeeders.php index f284669..9bfab95 100644 --- a/database/seeds/UnsoldCommodityNotificationSettingsSeeders.php +++ b/database/seeds/UnsoldCommodityNotificationSettingsSeeders.php @@ -12,6 +12,8 @@ class UnsoldCommodityNotificationSettingSeeders extends Seeder */ public function run() { + Setting::where('group', 'unsold_commodity_notifications')->each(fn($x) => $x->delete()); + $data = [ [ 'key' => 'status', @@ -39,43 +41,43 @@ class UnsoldCommodityNotificationSettingSeeders extends Seeder ], [ 'key' => 'pic_sunday', - 'value' => '', + 'value' => '[{}]', 'group' => 'unsold_commodity_notifications', 'description' => 'Person Incharge on Sunday', ], [ 'key' => 'pic_monday', - 'value' => '', + 'value' => '[{}]', 'group' => 'unsold_commodity_notifications', 'description' => 'Person Incharge on Monday', ], [ 'key' => 'pic_tuesday', - 'value' => '', + 'value' => '[{}]', 'group' => 'unsold_commodity_notifications', 'description' => 'Person Incharge on Tuesday', ], [ 'key' => 'pic_wednesday', - 'value' => '', + 'value' => '[{}]', 'group' => 'unsold_commodity_notifications', 'description' => 'Person Incharge on Wednesday', ], [ 'key' => 'pic_thursday', - 'value' => '', + 'value' => '[{}]', 'group' => 'unsold_commodity_notifications', 'description' => 'Person Incharge on Thursday', ], [ 'key' => 'pic_friday', - 'value' => '', + 'value' => '[{}]', 'group' => 'unsold_commodity_notifications', 'description' => 'Person Incharge on Friday', ], [ 'key' => 'pic_saturday', - 'value' => '', + 'value' => '[{}]', 'group' => 'unsold_commodity_notifications', 'description' => 'Person Incharge on Saturday', ],