From cbf6bd5f1facfddb1a6268e2e4cb3b57feb417fe Mon Sep 17 00:00:00 2001 From: Afiq Hamzah Date: Tue, 4 Jun 2024 18:48:06 +0800 Subject: [PATCH] Fix multiple notifications send to PIC at one time - fix by removing previous scheduled notifications job before updating the notification configurations --- app/Http/Controllers/SettingController.php | 32 +++++++++---- .../WhatsappUnsoldCommodityNotification.php | 47 +++++++------------ app/Jobs/WhatsappUnsoldCommodityScanner.php | 30 ++---------- config/app.php | 5 ++ config/database.php | 12 +++++ database/migrations/.vscode/settings.json | 3 ++ .../2024_05_30_150239_create_job_statuses.php | 34 -------------- 7 files changed, 65 insertions(+), 98 deletions(-) create mode 100644 config/app.php create mode 100644 database/migrations/.vscode/settings.json delete mode 100644 database/migrations/2024_05_30_150239_create_job_statuses.php diff --git a/app/Http/Controllers/SettingController.php b/app/Http/Controllers/SettingController.php index 4ede46e..58a58bd 100644 --- a/app/Http/Controllers/SettingController.php +++ b/app/Http/Controllers/SettingController.php @@ -4,11 +4,10 @@ namespace App\Http\Controllers; use App\Jobs\WhatsappUnsoldCommodityNotification; use App\Jobs\WhatsappUnsoldCommodityScanner; -use App\JobStatus; use App\Setting; -use Carbon\Carbon; use Illuminate\Http\Request; -use Illuminate\Support\Facades\Http; +use Illuminate\Support\Facades\Artisan; +use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Log; use Illuminate\Support\Str; @@ -72,12 +71,14 @@ class SettingController extends Controller ->update(['value' => json_encode($value)]); } - $this->cancelPreviousNotificationJobs(); + $this->cancelPreviousCommodityNotificationsJobs(); + dispatch((new WhatsappUnsoldCommodityScanner())); return response()->json('success'); } + public function filterUniqueById($array, $key) { $uniqueIds = []; @@ -97,10 +98,25 @@ class SettingController extends Controller ]; } - public function cancelPreviousNotificationJobs() + + public function cancelPreviousCommodityNotificationsJobs() { - JobStatus::on('mysql7')->where('type', WhatsappUnsoldCommodityNotification::class)->update([ - 'status' => false - ]); + // flushed the fail job in queueu + Artisan::call('queue:flush'); + + $getClassNameString = function($className){ + $ola = explode("\\", $className); + return end($ola); + }; + + DB::connection('mysql7') + ->table('jobs') + ->where('payload', 'like', '%' . $getClassNameString(WhatsappUnsoldCommodityNotification::class) . '%') + ->delete(); + + DB::connection('mysql7') + ->table('jobs') + ->where('payload', 'like', '%' . $getClassNameString(WhatsappUnsoldCommodityScanner::class) . '%') + ->delete(); } } diff --git a/app/Jobs/WhatsappUnsoldCommodityNotification.php b/app/Jobs/WhatsappUnsoldCommodityNotification.php index 04fcad7..cabd941 100644 --- a/app/Jobs/WhatsappUnsoldCommodityNotification.php +++ b/app/Jobs/WhatsappUnsoldCommodityNotification.php @@ -2,7 +2,6 @@ namespace App\Jobs; -use App\JobStatus; use Illuminate\Support\Facades\Http; use App\Setting; use Carbon\Carbon; @@ -31,69 +30,55 @@ class WhatsappUnsoldCommodityNotification extends Job */ public function handle() { + $settings = Setting::on('mysql7')->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; - + $status = $settings->where('key', 'status')->first()->value; + $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; $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)){ + if (isset($p->no_telefon)) { return $p->no_telefon; } }, $pic); if ($phone_numbers[0] === NULL) { - return; + return false; } - if(JobStatus::where('job_id', $this->job->getJobId()) === false){ - return; + if ($status === '0') { + return false; } - if ($this->status === '0') { - return; + if (Carbon::parse($start_time) > Carbon::now()) { + return false; } - if (Carbon::parse($this->start_time) > Carbon::now()) { - return; - } - - if (Carbon::parse($this->end_time) < Carbon::now()) { - return; + if (Carbon::parse($end_time) < Carbon::now()) { + return false; } // if (!$isKomoditiStillAvailable) { - // return; + // return false; // } foreach ($phone_numbers as $phone_number) { Http::get(env('WABOT_API'), [ 'number' => $phone_number, 'type' => 'text', - 'message' => $this->message, + 'message' => $message, 'instance_id' => env('WABOT_INSTANCES_ID'), 'access_token' => env('WABOT_TOKEN_KEYS') ]); } - $job_id = dispatch((new WhatsappUnsoldCommodityNotification($this->message))->delay(Carbon::now()->addMinutes(1))); - Log::info($job_id); + dispatch((new WhatsappUnsoldCommodityNotification($this->message))->delay(Carbon::now()->addMinutes(5))); - JobStatus::on('mysql7')->create([ - 'job_id' => $job_id, - 'type' => WhatsappUnsoldCommodityNotification::class, - 'status' => true, - ]); } } diff --git a/app/Jobs/WhatsappUnsoldCommodityScanner.php b/app/Jobs/WhatsappUnsoldCommodityScanner.php index d7e184e..61e46de 100644 --- a/app/Jobs/WhatsappUnsoldCommodityScanner.php +++ b/app/Jobs/WhatsappUnsoldCommodityScanner.php @@ -2,7 +2,6 @@ namespace App\Jobs; -use App\JobStatus; use App\Setting; use Carbon\Carbon; @@ -10,7 +9,6 @@ class WhatsappUnsoldCommodityScanner extends Job { public $status; public $start_time; - public $end_time; public $message; /** @@ -30,33 +28,15 @@ class WhatsappUnsoldCommodityScanner extends Job public function handle() { $settings = Setting::on('mysql7')->where('group', 'unsold_commodity_notifications')->get(); + $status = $settings->where('key', 'status')->first()->value; + $start_time = $settings->where('key', 'start_time')->first()->value; - $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 ($status === '0') { + return false; } - if (Carbon::parse($this->start_time) > Carbon::now()) { - return; - } + dispatch((new WhatsappUnsoldCommodityNotification($this->message))->delay(Carbon::parse($start_time))); - if (Carbon::parse($this->end_time) < Carbon::now()) { - return; - } - - $job_id = dispatch((new WhatsappUnsoldCommodityNotification($this->message))->delay(Carbon::parse($this->start_time))); - - JobStatus::on('mysql7')->create([ - 'job_id' => $job_id, - 'type' => WhatsappUnsoldCommodityNotification::class, - 'status' => true, - ]); } } diff --git a/config/app.php b/config/app.php new file mode 100644 index 0000000..ca53786 --- /dev/null +++ b/config/app.php @@ -0,0 +1,5 @@ + env('APP_URL', url('/')) +]; diff --git a/config/database.php b/config/database.php index f464311..71542a5 100644 --- a/config/database.php +++ b/config/database.php @@ -5,6 +5,18 @@ return [ 'default' => 'mysql7', 'migrations' => 'migrations', 'connections' => [ + 'mysql' => [ + 'driver' => 'mysql', + 'host' => env('DB_HOST_7'), + 'port' => env('DB_PORT_7'), + 'database' => env('DB_DATABASE_7'), + 'username' => env('DB_USERNAME_7'), + 'password' => env('DB_PASSWORD_7'), + 'charset' => 'utf8', + 'collation' => 'utf8_unicode_ci', + 'prefix' => '', + 'strict' => false, + ], 'lelong' => [ 'driver' => 'mysql', 'host' => env('DB_HOST_LELONG'), diff --git a/database/migrations/.vscode/settings.json b/database/migrations/.vscode/settings.json new file mode 100644 index 0000000..ab4793e --- /dev/null +++ b/database/migrations/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "window.title": "[ ${rootNameShort} ] ➡️ ${dirty}${activeEditorMedium} ${separator}[Branch: test-simulator-nisha]" +} diff --git a/database/migrations/2024_05_30_150239_create_job_statuses.php b/database/migrations/2024_05_30_150239_create_job_statuses.php deleted file mode 100644 index 57da3be..0000000 --- a/database/migrations/2024_05_30_150239_create_job_statuses.php +++ /dev/null @@ -1,34 +0,0 @@ -id(); - $table->string('type'); - $table->unsignedBigInteger('job_id'); - $table->boolean('status'); - $table->timestamps(); - }); - } - - /** - * Reverse the migrations. - * - * @return void - */ - public function down() - { - Schema::dropIfExists('job_statuses'); - } -}