Files
api_arrahn/app/Jobs/WhatsappUnsoldCommodityNotification.php
T
Afiq Hamzah 119f6d6428 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
2024-06-11 08:41:12 +08:00

109 lines
3.2 KiB
PHP

<?php
namespace App\Jobs;
use App\BlastWasap;
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();
$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;
$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 = 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 ($isWabotCredentialNotComplete) {
Log::error('Wabot credential not complete');
return false;
}
if ($phone_numbers[0] === NULL) {
Log::error('No PIC phone numbers detected');
return false;
}
if ($status === '0') {
Log::info('Unsold commodity notifications are turned off, no notification will be sent');
return false;
}
if (Carbon::parse($start_time) > Carbon::now()) {
return false;
}
if (Carbon::parse($end_time) < Carbon::now()) {
return false;
}
if ($isAllKomoditiSold) {
Log::info('All komoditi is sold');
return false;
}
foreach ($phone_numbers as $phone_number) {
$response = Http::get(env('WABOT_API'), [
'number' => $phone_number,
'type' => 'text',
'message' => $message,
'instance_id' => env('WABOT_INSTANCES_ID'),
'access_token' => env('WABOT_TOKEN_KEYS')
]);
//insert log blast wasap
$logblastwasap = new BlastWasap();
$logblastwasap->kodcaw = 'OPERASI';
$logblastwasap->notelefon = $phone_number;
$logblastwasap->norujukan = 'Notifikasi Komoditi Tidak Terjual';
$logblastwasap->status = $response;
$logblastwasap->created_at = Carbon::now();
$logblastwasap->updated_at = Carbon::now();
$logblastwasap->save();
Log::info("WABOT - Notifikasi Komoditi Tidak Terjual" . $response);
}
dispatch((new WhatsappUnsoldCommodityNotification($this->message))->delay(Carbon::now()->addMinutes(5)));
}
}