CrestRoad/laravel/app/Console/Commands/SendReminder.php

98 lines
3.5 KiB
PHP

<?php
namespace App\Console\Commands;
use App\Http\Requests\LambdaRequest;
use App\Services\SessionCommunicationService;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
class SendReminder extends Command
{
protected $signature = 'reminder:send';
protected $description = 'Queue session reminder messages';
protected $comm;
public function __construct(SessionCommunicationService $comm)
{
parent::__construct();
$this->comm = $comm;
}
public function handle()
{
$sends = DB::table('message_reminder as ms')
->leftJoin('message_template as t', 'ms.template', '=', 't.id')
->where('ms.status', 'active')
->select('ms.*', 't.template_code', 't.template_body', 't.title as template_title', 't.button_url', 't.button_label')
->get();
$sessions = DB::table('sessions')
->where('status', 'active')
->select(
'id',
DB::raw('DATEDIFF(start_date, now()) AS days'),
'number',
'code',
'type',
'start_date',
'venue',
'communication_mode'
)
->get();
if ($sends->count() > 0) {
foreach ($sends as $send) {
foreach ($sessions as $session) {
if (
($session->id == 0 || $session->id == $send->session_id)
&& $session->days == $send->days_before
&& date('H:i', strtotime($send->time)) == date('H:i', strtotime('now') + 19800)
) {
$searchparam = ['session_code' => $session->code];
if ($send->audience == 'attended') {
$searchparam['present'] = 'yes';
}
$response = LambdaRequest::getrequest(
'contact',
'contact/get_all',
[
'source' => 'dynamodb',
'search' => json_encode($searchparam),
'start' => 0,
'length' => 9999,
],
env('LAMBDA_ACCESS_TOKEN', 'default_value')
);
$contacts = SessionCommunicationService::extractContactItems($response);
if (count($contacts) > 0) {
$config = [
'id' => $send->id,
'template_code' => $send->template_code,
'template_body' => $send->template_body,
'template_title' => $send->template_title,
'button_url' => $send->button_url,
'button_label' => $send->button_label,
'header_params' => $send->header_params,
'body_params' => $send->body_params,
'button_params' => $send->button_params,
];
foreach ($contacts as $item) {
$this->comm->dispatchForContact($session, $item, $config);
}
}
}
}
}
}
return 0;
}
}