Add feat unsold commodity notifications

This commit is contained in:
Afiq Hamzah
2024-05-28 16:52:35 +08:00
parent 19017b8174
commit 931de4471c
9 changed files with 403 additions and 3 deletions
+3 -3
View File
@@ -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');
}
}
@@ -0,0 +1,97 @@
<?php
namespace App\Http\Controllers;
use App\Jobs\WhatsappUnsoldCommodityNotification;
use App\Jobs\WhatsappUnsoldCommodityScanner;
use App\Setting;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Str;
class SettingController extends Controller
{
public function getUnsoldCommodityNotifications()
{
$settings = Setting::where('group', 'unsold_commodity_notifications')->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
];
}
}
@@ -0,0 +1,87 @@
<?php
namespace App\Jobs;
use Illuminate\Support\Facades\Http;
use App\Setting;
use Carbon\Carbon;
use App\KomuditiPurchase;
use Illuminate\Support\Facades\Log;
class WhatsappUnsoldCommodityNotification extends Job
{
public $status;
public $start_time;
public $end_time;
public $message;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct()
{
}
/**
* Execute the job.
*
* @return void
*/
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;
$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)));
}
}
@@ -0,0 +1,55 @@
<?php
namespace App\Jobs;
use App\Setting;
use Carbon\Carbon;
class WhatsappUnsoldCommodityScanner extends Job
{
public $status;
public $start_time;
public $end_time;
public $message;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct()
{
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
$settings = Setting::on()->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)));
}
}
+19
View File
@@ -0,0 +1,19 @@
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Setting extends Model
{
protected $fillable = [
'key',
'value',
'group',
'description',
];
}