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
This commit is contained in:
Afiq Hamzah
2024-06-10 18:32:02 +08:00
parent 3686fb93de
commit 119f6d6428
6 changed files with 176 additions and 58 deletions
@@ -57,6 +57,11 @@ class SettingController extends Controller
array_walk($unique_pic_input, function ($item) use (&$transformed_unique_pic_input) { array_walk($unique_pic_input, function ($item) use (&$transformed_unique_pic_input) {
foreach ($item as $key => $value) { foreach ($item as $key => $value) {
if(count($value) === 0){
$value = [new \stdClass()];
}
$transformed_unique_pic_input[$key] = $value; $transformed_unique_pic_input[$key] = $value;
} }
}); });
@@ -37,7 +37,8 @@ class WhatsappUnsoldCommodityNotification extends Job
$start_time = $settings->where('key', 'start_time')->first()->value; $start_time = $settings->where('key', 'start_time')->first()->value;
$end_time = $settings->where('key', 'end_time')->first()->value; $end_time = $settings->where('key', 'end_time')->first()->value;
$message = $settings->where('key', 'message')->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_key = 'pic_' . strtolower(Carbon::now()->englishDayOfWeek);
$pic = json_decode($settings->where('key', $pic_key)->first()->value); $pic = json_decode($settings->where('key', $pic_key)->first()->value);
@@ -48,12 +49,18 @@ class WhatsappUnsoldCommodityNotification extends Job
} }
}, $pic); }, $pic);
if ($isWabotCredentialNotComplete) {
Log::error('Wabot credential not complete');
return false;
}
if ($phone_numbers[0] === NULL) { if ($phone_numbers[0] === NULL) {
Log::error('No PIC phone numbers detected');
return false; return false;
} }
if ($status === '0') { if ($status === '0') {
Log::info('Unsold commodity notifications are turned off, no notification will be sent');
return false; return false;
} }
@@ -65,10 +72,12 @@ class WhatsappUnsoldCommodityNotification extends Job
return false; return false;
} }
if ($isKomoditiStillSold) { if ($isAllKomoditiSold) {
Log::info('All komoditi is sold');
return false; return false;
} }
foreach ($phone_numbers as $phone_number) { foreach ($phone_numbers as $phone_number) {
$response = Http::get(env('WABOT_API'), [ $response = Http::get(env('WABOT_API'), [
'number' => $phone_number, 'number' => $phone_number,
@@ -89,12 +98,11 @@ class WhatsappUnsoldCommodityNotification extends Job
$logblastwasap->updated_at = Carbon::now(); $logblastwasap->updated_at = Carbon::now();
$logblastwasap->save(); $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))); dispatch((new WhatsappUnsoldCommodityNotification($this->message))->delay(Carbon::now()->addMinutes(5)));
} }
} }
@@ -13,18 +13,19 @@ class CreateSettingsTable extends Migration
*/ */
public function up() public function up()
{ {
Schema::create('settings', function (Blueprint $table) { if (!Schema::hasTable('settings')) {
$table->id(); Schema::create('settings', function (Blueprint $table) {
$table->string('key')->unique(); $table->id();
$table->text('value')->nullable(); $table->string('group')->nullable();
$table->string('group')->nullable(); $table->string('key')->unique();
$table->text('description')->nullable(); $table->text('value')->nullable();
$table->timestamps(); $table->text('description')->nullable();
}); $table->timestamps();
});
}
// Call the seeder // Call the seeder
(new UnsoldCommodityNotificationSettingSeeders())->run(); (new UnsoldCommodityNotificationSettingSeeders())->run();
(new UnsoldCommodityNotificationPICSeeders())->run();
} }
/** /**
@@ -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"
}
]}
@@ -1,37 +0,0 @@
<?php
use App\Setting;
use Illuminate\Database\Seeder;
class UnsoldCommodityNotificationPICSeeders extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
$data_pic = [
'pic_sunday' => [[]],
'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]);
}
}
}
@@ -12,6 +12,8 @@ class UnsoldCommodityNotificationSettingSeeders extends Seeder
*/ */
public function run() public function run()
{ {
Setting::where('group', 'unsold_commodity_notifications')->each(fn($x) => $x->delete());
$data = [ $data = [
[ [
'key' => 'status', 'key' => 'status',
@@ -39,43 +41,43 @@ class UnsoldCommodityNotificationSettingSeeders extends Seeder
], ],
[ [
'key' => 'pic_sunday', 'key' => 'pic_sunday',
'value' => '', 'value' => '[{}]',
'group' => 'unsold_commodity_notifications', 'group' => 'unsold_commodity_notifications',
'description' => 'Person Incharge on Sunday', 'description' => 'Person Incharge on Sunday',
], ],
[ [
'key' => 'pic_monday', 'key' => 'pic_monday',
'value' => '', 'value' => '[{}]',
'group' => 'unsold_commodity_notifications', 'group' => 'unsold_commodity_notifications',
'description' => 'Person Incharge on Monday', 'description' => 'Person Incharge on Monday',
], ],
[ [
'key' => 'pic_tuesday', 'key' => 'pic_tuesday',
'value' => '', 'value' => '[{}]',
'group' => 'unsold_commodity_notifications', 'group' => 'unsold_commodity_notifications',
'description' => 'Person Incharge on Tuesday', 'description' => 'Person Incharge on Tuesday',
], ],
[ [
'key' => 'pic_wednesday', 'key' => 'pic_wednesday',
'value' => '', 'value' => '[{}]',
'group' => 'unsold_commodity_notifications', 'group' => 'unsold_commodity_notifications',
'description' => 'Person Incharge on Wednesday', 'description' => 'Person Incharge on Wednesday',
], ],
[ [
'key' => 'pic_thursday', 'key' => 'pic_thursday',
'value' => '', 'value' => '[{}]',
'group' => 'unsold_commodity_notifications', 'group' => 'unsold_commodity_notifications',
'description' => 'Person Incharge on Thursday', 'description' => 'Person Incharge on Thursday',
], ],
[ [
'key' => 'pic_friday', 'key' => 'pic_friday',
'value' => '', 'value' => '[{}]',
'group' => 'unsold_commodity_notifications', 'group' => 'unsold_commodity_notifications',
'description' => 'Person Incharge on Friday', 'description' => 'Person Incharge on Friday',
], ],
[ [
'key' => 'pic_saturday', 'key' => 'pic_saturday',
'value' => '', 'value' => '[{}]',
'group' => 'unsold_commodity_notifications', 'group' => 'unsold_commodity_notifications',
'description' => 'Person Incharge on Saturday', 'description' => 'Person Incharge on Saturday',
], ],