Skip to content

Commit

Permalink
Merge pull request #26 from palladiumkenya/feature/data-cleaning-algo…
Browse files Browse the repository at this point in the history
…rithm

Enhancement/Resend sms missed out during normal scheduling
  • Loading branch information
kevlanyo authored Apr 30, 2024
2 parents 1640d4d + e859724 commit 35313c7
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 4 deletions.
51 changes: 47 additions & 4 deletions app/Http/Controllers/ScheduleSMSController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use App\Models\ClientOutgoing;
use App\Models\OutgoingSms;
use App\Models\Wellness;
use App\Models\UnsentSMS;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Http;
Expand Down Expand Up @@ -502,6 +503,48 @@ public function ltfuScheduler()

}

public function resend() //resend all outgoing sms that were not sent during normal scheduling
{
$messages = UnsentSMS::all();
foreach($messages as $message)
{
$clnt_outgoing_id = $message->id;
$destination = $message->destination;
$msg = $message->msg;
$source = $message->source;

//call sms service
$result = $this->send_message($source,$destination,$clnt_outgoing_id,$msg);

$status = $result['status'];
$messageid = '';
$cost = 0;

foreach($result as $row)
{
if($status == 'success')
{
foreach($result['data'] as $data)
{
foreach($data['Recipients'] as $Recipient)
{
$messageid = $Recipient['messageId'];
$cost = $Recipient['cost'];
}
}
}

//update the sent message with the sms cost and send status
$sms = ClientOutgoing::find($clnt_outgoing_id);
$sms->status = $status;
$sms->cost = $cost;
$sms->message_id = $messageid;
$sms->save();

}
}
}

public function sender()
{
try{
Expand Down Expand Up @@ -530,7 +573,7 @@ public function sender()
->where('msg','like', '%'.$msg.'%')
->where('destination', $destination)
->where('status', 'Sent')
->whereRaw('created_at between (CURDATE() - INTERVAL 1 DAY) AND (CURDATE() + INTERVAL 1 DAY) ')
->whereRaw('DATE(created_at) between (CURDATE() - INTERVAL 1 DAY) AND (CURDATE() + INTERVAL 1 DAY) ')
->doesntExist()) //Message has not been sent, send the current message
{
// $queries = DB::getQueryLog();
Expand All @@ -544,7 +587,7 @@ public function sender()
}

//call sms service
$result = $this->send_message($source,$destination , $msg);
$result = $this->send_message($source,$destination,$clnt_outgoing_id,$msg);

$status = $result['status'];
$messageid = '';
Expand Down Expand Up @@ -585,7 +628,7 @@ public function sender()
}
}

private function send_message($source, $destination, $msg)
private function send_message($source, $destination,$sender_id, $msg)
{
$key = env('SMS_SERVICE_KEY', '');
$host = env('SMS_SERVICE_HOST', '');
Expand All @@ -596,7 +639,7 @@ private function send_message($source, $destination, $msg)
->post("$host", [
'destination' => $destination,
'msg' => $msg,
'sender_id' => $destination,
'sender_id' => $sender_id,
'gateway' => $source,
]);

Expand Down
13 changes: 13 additions & 0 deletions app/Models/UnsentSMS.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class UnsentSMS extends Model
{
use HasFactory;

public $table = "vw_unsent_sms";
}
3 changes: 3 additions & 0 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@
//send sms notifications
Route::get('/sms/sender', ['uses' => 'App\Http\Controllers\ScheduleSMSController@sender', 'as' => 'sms-sender']);

//resend sms notifications
Route::get('/sms/resend', ['uses' => 'App\Http\Controllers\ScheduleSMSController@resend', 'as' => 'sms-resend']);

//get KHIS TxCurr
Route::get('/txcurr', ['uses' => 'App\Http\Controllers\TxCurrController@txcurr', 'as' => 'txcurr']);

Expand Down

0 comments on commit 35313c7

Please sign in to comment.