diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php index ad1e238..5c7d714 100644 --- a/app/Console/Kernel.php +++ b/app/Console/Kernel.php @@ -5,9 +5,7 @@ namespace App\Console; use Illuminate\Console\Scheduling\Schedule; use Laravel\Lumen\Console\Kernel as ConsoleKernel; -use App\Cawangan; -use App\Gadai; -use Carbon\Carbon; +use App\Jobs\WhatsappUnsoldCommodityScanner; class Kernel extends ConsoleKernel { @@ -34,5 +32,7 @@ class Kernel extends ConsoleKernel // $schedule->command('gadai:daily')->daily()->timezone('Asia/Kuala_Lumpur')->at('00:00'); + + $schedule->job(new WhatsappUnsoldCommodityScanner)->daily()->timezone('Asia/Kuala_Lumpur')->at('00:00'); } } diff --git a/app/Http/Controllers/SettingController.php b/app/Http/Controllers/SettingController.php new file mode 100644 index 0000000..3f0e58f --- /dev/null +++ b/app/Http/Controllers/SettingController.php @@ -0,0 +1,97 @@ +get(); + + return response()->json($settings); + } + + public function updateUnsoldCommodityNotifications(Request $request) + { + $request_array = $request->input(); + + $general_inputs = array_filter( + $request_array, + function ($value, $key) { + return strpos($key, 'pic_') === false; + }, + ARRAY_FILTER_USE_BOTH + ); + + foreach ($general_inputs as $key => $value) { + Setting::where([ + ['group', 'unsold_commodity_notifications'], + ['key', $key] + ]) + ->first() + ->update(['value' => $value]); + } + + $pic_inputs = array_filter( + $request_array, + function ($value, $key) { + return Str::startsWith($key, 'pic_'); + }, + ARRAY_FILTER_USE_BOTH + ); + + $unique_pic_input = array_map(function ($array, $key) { + return $this->filterUniqueById($array, $key); + }, $pic_inputs, array_keys($pic_inputs)); + + $transformed_unique_pic_input = []; + + array_walk($unique_pic_input, function ($item) use (&$transformed_unique_pic_input) { + foreach ($item as $key => $value) { + $transformed_unique_pic_input[$key] = $value; + } + }); + + + foreach ($transformed_unique_pic_input as $key => $value) { + Setting::where([ + ['group', 'unsold_commodity_notifications'], + ['key', $key] + ]) + ->first() + ->update(['value' => json_encode($value)]); + } + + dispatch((new WhatsappUnsoldCommodityScanner())); + + return response()->json('success'); + } + + public function filterUniqueById($array, $key) + { + $uniqueIds = []; + $filteredArray = []; + + if($array !== [] ){ + foreach ($array as $item) { + if (!in_array($item['id'], $uniqueIds)) { + $uniqueIds[] = $item['id']; + $filteredArray[] = $item; + } + } + } + + return [ + $key => $filteredArray + ]; + } +} diff --git a/app/Jobs/WhatsappUnsoldCommodityNotification.php b/app/Jobs/WhatsappUnsoldCommodityNotification.php new file mode 100644 index 0000000..2a8763a --- /dev/null +++ b/app/Jobs/WhatsappUnsoldCommodityNotification.php @@ -0,0 +1,87 @@ +where('group', 'unsold_commodity_notifications')->get(); + + $this->status = $settings->where('key', 'status')->first()->value; + + $this->start_time = $settings->where('key', 'start_time')->first()->value; + + $this->end_time = $settings->where('key', 'end_time')->first()->value; + + $this->message = $settings->where('key', 'message')->first()->value; + + $isKomoditiStillAvailable = KomuditiPurchase::on('mysql7')->latest()->first()->unit_komuditi === 0; + + $pic_key = 'pic_' . strtolower(Carbon::now()->englishDayOfWeek); + $pic = json_decode($settings->where('key', $pic_key)->first()->value); + + $phone_numbers = array_map(function ($p) { + if(isset($p->no_telefon)){ + return $p->no_telefon; + } + }, $pic); + + + if ($phone_numbers[0] === NULL) { + return; + } + + if ($this->status === '0') { + return; + } + + if (Carbon::parse($this->start_time) > Carbon::now()) { + return; + } + + if (Carbon::parse($this->end_time) < Carbon::now()) { + return; + } + + if (!$isKomoditiStillAvailable) { + return; + } + + foreach ($phone_numbers as $phone_number) { + Http::get(env('WABOT_API'), [ + 'number' => $phone_number, + 'type' => 'text', + 'message' => $this->message, + 'instance_id' => env('WABOT_INSTANCES_ID'), + 'access_token' => env('WABOT_TOKEN_KEYS') + ]); + } + + dispatch((new WhatsappUnsoldCommodityNotification($this->message))->delay(Carbon::now()->addMinutes(5))); + } +} diff --git a/app/Jobs/WhatsappUnsoldCommodityScanner.php b/app/Jobs/WhatsappUnsoldCommodityScanner.php new file mode 100644 index 0000000..d64df49 --- /dev/null +++ b/app/Jobs/WhatsappUnsoldCommodityScanner.php @@ -0,0 +1,55 @@ +where('group', 'unsold_commodity_notifications')->get(); + + $this->status = $settings->where('key', 'status')->first()->value; + + $this->start_time = $settings->where('key', 'start_time')->first()->value; + + $this->end_time = $settings->where('key', 'end_time')->first()->value; + + $this->message = $settings->where('key', 'message')->first()->value; + + if ($this->status === '0') { + return; + } + + if (Carbon::parse($this->start_time) > Carbon::now()) { + return; + } + + if (Carbon::parse($this->end_time) < Carbon::now()) { + return; + } + + dispatch((new WhatsappUnsoldCommodityNotification($this->message))->delay(Carbon::parse($this->start_time))); + } +} diff --git a/app/Setting.php b/app/Setting.php new file mode 100644 index 0000000..7bc5e32 --- /dev/null +++ b/app/Setting.php @@ -0,0 +1,19 @@ +router->group([ ], function ($router) { require __DIR__.'/../routes/web.php'; require __DIR__.'/../routes/report.php'; + require __DIR__.'/../routes/setting.php'; }); $app->middleware([ diff --git a/database/migrations/2024_05_27_093206_create_settings_table.php b/database/migrations/2024_05_27_093206_create_settings_table.php new file mode 100644 index 0000000..62b182b --- /dev/null +++ b/database/migrations/2024_05_27_093206_create_settings_table.php @@ -0,0 +1,35 @@ +id(); + $table->string('key')->unique(); + $table->text('value')->nullable(); + $table->string('group')->nullable(); + $table->text('description')->nullable(); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('settings'); + } +} diff --git a/database/migrations/2024_05_27_093207_add_unsold_commodities_settings.php b/database/migrations/2024_05_27_093207_add_unsold_commodities_settings.php new file mode 100644 index 0000000..70c983d --- /dev/null +++ b/database/migrations/2024_05_27_093207_add_unsold_commodities_settings.php @@ -0,0 +1,100 @@ + 'status', + 'value' => 'true', + 'group' => 'unsold_commodity_notifications', + 'description' => 'Turn on or turn off notification for unsold commodity notifications', + ], + [ + 'key' => 'start_time', + 'value' => '20:00', + 'group' => 'unsold_commodity_notifications', + 'description' => 'The time the notifications start', + ], + [ + 'key' => 'end_time', + 'value' => '23:00', + 'group' => 'unsold_commodity_notifications', + 'description' => 'The time the notifications end', + ], + [ + 'key' => 'message', + 'value' => 'Baki komoditi masih lagi berbaki dan belum habis dijual', + 'group' => 'unsold_commodity_notifications', + 'description' => 'Turn on or turn off notification for unsold commodity notifications', + ], + [ + 'key' => 'pic_sunday', + 'value' => '', + 'group' => 'unsold_commodity_notifications', + 'description' => 'Person Incharge on Sunday', + ], + [ + 'key' => 'pic_monday', + 'value' => '', + 'group' => 'unsold_commodity_notifications', + 'description' => 'Person Incharge on Monday', + ], + [ + 'key' => 'pic_tuesday', + 'value' => '', + 'group' => 'unsold_commodity_notifications', + 'description' => 'Person Incharge on Tuesday', + ], + [ + 'key' => 'pic_wednesday', + 'value' => '', + 'group' => 'unsold_commodity_notifications', + 'description' => 'Person Incharge on Wednesday', + ], + [ + 'key' => 'pic_thursday', + 'value' => '', + 'group' => 'unsold_commodity_notifications', + 'description' => 'Person Incharge on Thursday', + ], + [ + 'key' => 'pic_friday', + 'value' => '', + 'group' => 'unsold_commodity_notifications', + 'description' => 'Person Incharge on Friday', + ], + [ + 'key' => 'pic_saturday', + 'value' => '', + 'group' => 'unsold_commodity_notifications', + 'description' => 'Person Incharge on Saturday', + ], + + ]; + + array_walk($data, function($setting){ + Setting::create($setting); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('settings'); + } +} diff --git a/routes/setting.php b/routes/setting.php new file mode 100644 index 0000000..e4e7a2f --- /dev/null +++ b/routes/setting.php @@ -0,0 +1,6 @@ +group(['prefix' => 'api/settings'], function () use ($router) { + $router->get('unsold-commodity-notifications', ['uses' => 'SettingController@getUnsoldCommodityNotifications']); + $router->post('unsold-commodity-notifications', ['uses' => 'SettingController@updateUnsoldCommodityNotifications']); +});