Fix multiple notifications send to PIC at one time

- fix by removing previous scheduled notifications job before updating the notification configurations
This commit is contained in:
Afiq Hamzah
2024-06-04 18:48:06 +08:00
parent 052f65307d
commit cbf6bd5f1f
7 changed files with 65 additions and 98 deletions
+24 -8
View File
@@ -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();
}
}
@@ -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,
]);
}
}
+5 -25
View File
@@ -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,
]);
}
}
+5
View File
@@ -0,0 +1,5 @@
<?php
return [
'url' => env('APP_URL', url('/'))
];
+12
View File
@@ -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'),
+3
View File
@@ -0,0 +1,3 @@
{
"window.title": "[ ${rootNameShort} ] ➡️ ${dirty}${activeEditorMedium} ${separator}[Branch: test-simulator-nisha]"
}
@@ -1,34 +0,0 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateJobStatuses extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('job_statuses', function (Blueprint $table) {
$table->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');
}
}