107 lines
2.9 KiB
PHP
107 lines
2.9 KiB
PHP
<?php
|
|
|
|
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\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)]);
|
|
}
|
|
|
|
$this->cancelPreviousNotificationJobs();
|
|
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
|
|
];
|
|
}
|
|
|
|
public function cancelPreviousNotificationJobs()
|
|
{
|
|
JobStatus::on('mysql7')->where('type', WhatsappUnsoldCommodityNotification::class)->update([
|
|
'status' => false
|
|
]);
|
|
}
|
|
}
|