completed the send SMS which uses the first client App API key

This commit is contained in:
Kwesi Banson Jnr
2026-04-05 09:09:04 +00:00
parent 595e7a6531
commit 2064c2d6e5
4 changed files with 39 additions and 34 deletions

View File

@@ -53,7 +53,6 @@ class ClientsTrafficController extends Controller
public function indexTabulator(Request $request){
$client = new Client();
// Construct the URL with pagination parameters from the request
$page = $request->input('page', 0); // Note: This API seems 0-indexed
$size = $request->input('size', 20);
/*
@@ -71,7 +70,7 @@ class ClientsTrafficController extends Controller
*/
$client_id = session('current_user.org_id');
$clients_url = "messages/client/1?page=0&size=20&sort=createdAt,desc";
$clients_url = "messages/client/$client_id?page=0&size=20&sort=createdAt,desc";
$result = ApiCalls::CurlGet($clients_url);
// $content_only = collect($result['content'])->toArray();
$data = json_decode($result, true);
@@ -86,8 +85,8 @@ class ClientsTrafficController extends Controller
// ]);
}
public function getClientApps(){
//dump(session('current_user'));
$client_id = session('current_user.org_id');
$apps_url = "applications/client/$client_id?page=0&size=20&sort=createdAt,desc";
$result = ApiCalls::CurlGet($apps_url);
$result_arr = json_decode($result);
@@ -109,28 +108,19 @@ class ClientsTrafficController extends Controller
}
public function store(Request $request){
//233244982690,233553175995,233204040805
$this->validate($request, [
'message' => 'required',
'sender_id' => 'required|max:11',
'recipients_file'=> 'mimes:csv,txt|max:4096',
'pasted' => 'sometimes',
// 'status' => 'required',
]);
$recipients_arr = [];
// $create_client_url = "clients";
// $result = ApiCalls::CurlPost(json_encode($client_arr), $create_client_url);
if ($request->recipient_file && $request->pasted) {
// code...
}
if ($request->filled('pasted')) {
// code...
$pasted_arr = explode(", ", $request->pasted);
$pasted_arr = explode(",", $request->pasted);
foreach ($pasted_arr as $value) {
// code...
if (ctype_digit($value) && (strlen($value) == 11 || strlen($value) == 12)) {
// Valid
$recipients_arr[] = trim($value);
}
else{
@@ -148,21 +138,21 @@ class ClientsTrafficController extends Controller
fclose($file);
}
$recipients_arr = array_unique($recipients_arr);
$client_id = session('current_user.org_id');
foreach ($recipients_arr as $phone) {
$payload = [
'from' => $request->sender_id,
'to' => $phone,
'refId' => uniqid(),
'refId' => uniqid('cmlportal_'),
'message' => $request->message
];
\Log::info('Payload details ' . json_encode($payload));
$send_sms_url = "sms/send";
#$result = ApiCalls::CurlPost($send_sms_url, $payload);
#\Log::info('Message Successfully submitted ' . $result);
$result = ApiCalls::CurlPost(json_encode($payload), $send_sms_url);
\Log::info('Message Successfully submitted ' . $result);
}
// $result_arr = json_decode($result);
Session::flash('success_message', 'Message successfully submitted for delivery!');
return redirect("send-sms");
}

View File

@@ -31,6 +31,16 @@ class ApiCalls
}
public static function CurlPost($data, $url){
$post_url = env('APIBASEURL') . $url;
$header_part = '';
$client_id = session('current_user.org_id');
if ($url == 'sms/send') {
// code...
$apps_url = "applications/client/$client_id?page=0&size=20&sort=createdAt,desc";
$result = static::CurlGet($apps_url);
$result_arr = json_decode($result);
$api_key = $result_arr->content[0]->apiKey;
$header_part = "Authorization: Bearer " . $api_key;
}
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $post_url,
@@ -43,7 +53,7 @@ class ApiCalls
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => $data,
CURLOPT_HTTPHEADER => array(
'Content-Type: application/json'
'Content-Type: application/json', $header_part
),
));
$response = curl_exec($curl);
@@ -53,6 +63,8 @@ class ApiCalls
public static function CurlPatch($data, $url){
$patch_url = env('APIBASEURL') . $url;
$header_part = '';
$curl = curl_init();
curl_setopt_array($curl, array(
@@ -66,7 +78,7 @@ class ApiCalls
CURLOPT_CUSTOMREQUEST => 'PATCH',
CURLOPT_POSTFIELDS => $data,
CURLOPT_HTTPHEADER => array(
'Content-Type: application/json'
'Content-Type: application/json', $header_part
),
));

View File

@@ -33,7 +33,7 @@
<tr>
<td class="fw-semibold">{{ $row->name}}</td>
<td class="recipient-cellss">
<div class="fw-semibold">{{ $row->type}}</div>
<div class="fw-semibold">{{ $row->type }}</div>
</td>
<td class="text-wrap text-break apiKeyCell" style="max-width: 100px;">
<i class="bi bi-eye viewApiKeyIcon"></i>

View File

@@ -57,34 +57,37 @@
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz" crossorigin="anonymous"></script>
<script src="{{ url('public/libs/jquery-3.2.1.min.js') }}"></script>
<script src="{{ url('public/libs/jquery-confirm/jquery-confirm.min.js') }}"></script>
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
@yield('page-js')
<script>
const myForm = document.getElementById('clientNewAppForm');
myForm.addEventListener('submit', function(e) {
// 2. Stop the page reload
e.preventDefault();
// 3. Extract data from the form
const formData = new FormData(this);
const formAction = $(this).attr("action");
// Convert to JSON if your API requires it (common for modern backends)
const data = Object.fromEntries(formData.entries());
// 4. Execute the AJAX (Fetch) request
fetch(formAction, {
method: 'POST',
headers: {
'Content-Type': 'application/json', // Inform server of JSON content
'Accept': 'application/json' // Expect JSON response
'Content-Type': 'application/json',
'Accept': 'application/json'
},
body: JSON.stringify(data)
})
.then(response => response.json())
.then(result => {
console.log('Success:', result);
// 5. Optional: Close modal or show success message
alert('Data submitted successfully!');
// $.alert('Data submitted successfully!');
Swal.fire({
title: "Sucess",
text: "New App successfully created",
icon: "success"
});
this.reset();
setTimeout(() => { location.reload(); }, 2000);
})
.catch(error => {
console.error('Error:', error);