Viewing file: CampaignCreateChunk.php (7.89 KB) -rw-r--r-- Select action/file-type: (+) | (+) | (+) | Code (+) | Session (+) | (+) | SDB (+) | (+) | (+) | (+) | (+) | (+) |
<?php
namespace App\Jobs;
use App\Models\Campaign; use App\Models\Contact; use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldBeUnique; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Foundation\Bus\Dispatchable; use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\SerializesModels; use Illuminate\Support\Carbon; use Illuminate\Support\Facades\Log;
class CampaignCreateChunk implements ShouldQueue { use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public $daily_sent_limit; public $send_speed; public $campaign_running_date; public $user; public $offset; public $take_count = 5000; public $message; public $to_numbers; public $from_number; public $sms_queue; /** * @var mixed */ public $dynamic_gateway_id; public $other_data;
/** * Create a new job instance. * * @return void */ public function __construct($message,$to_numbers, $dailySentLimit, $campaignRunningDate, $sendSpeed, $user, $offset,$dynamic_gateway_id,$other_data) { $this->message = $message; $this->daily_sent_limit = $dailySentLimit; $this->campaign_running_date = $campaignRunningDate; $this->send_speed = $sendSpeed; $this->user = $user; $this->offset = $offset; $numberArray = json_decode($message->numbers); $this->to_numbers = $to_numbers ?? []; $this->from_number = $numberArray->from ?? []; $this->sms_queue = []; sort($this->to_numbers); $this->dynamic_gateway_id=$dynamic_gateway_id; $this->other_data=$other_data; }
/** * Execute the job. * * @return void */
public function handle() { $campaignRunningDate=Carbon::parse($this->campaign_running_date); $toNumber = array_slice($this->to_numbers, $this->offset, ($this->daily_sent_limit)); if ($toNumber) { $generatedToNumbers = []; $all_to_numbers_without_country_code=[]; foreach ($toNumber as $to){ $all_to_numbers_without_country_code[]=getPhoneNumberWithoutDialCode($to); }
$contacts = Contact::where('customer_id', $this->user->id)->whereIn('number', $all_to_numbers_without_country_code)->orderBy('number')->get()->unique('number'); foreach ($contacts as $contact) { if ($this->dynamic_gateway_id) { $templates = json_decode($this->message->body); $templateBody = $templates; $templateBody = str_replace('contact__id', $contact->id, $templateBody); if ($contact->first_name) { $templateBody = str_replace('{first_name}', $contact->first_name, $templateBody); } else { $templateBody = str_replace('{first_name}', ' ', $templateBody); } if ($contact->last_name) { $templateBody = str_replace('{last_name}', $contact->last_name, $templateBody); } else { $templateBody = str_replace('{last_name}', ' ', $templateBody); } if ($contact->address) { $templateBody = str_replace('{address}', $contact->address, $templateBody); } else { $templateBody = str_replace('{address}', ' ', $templateBody); } if ($contact->city) { $templateBody = str_replace('{city}', $contact->city, $templateBody); } else { $templateBody = str_replace('{city}', ' ', $templateBody); } if ($contact->state) { $templateBody = str_replace('{state}', $contact->state, $templateBody); } else { $templateBody = str_replace('{state}', ' ', $templateBody); } if ($contact->zip_code) { $templateBody = str_replace('{zip_code}', $contact->zip_code, $templateBody); } else { $templateBody = str_replace('{zip_code}', ' ', $templateBody); } if ($contact->email) { $templateBody = str_replace('{email}', $contact->email, $templateBody); } else { $templateBody = str_replace('{email}', ' ', $templateBody); } if (!in_array($contact->number, $generatedToNumbers)) {
$toNumber = $contact->contact_dial_code.$contact->number; $others = ''; if(isset($this->other_data) && isset($this->other_data['dlt_template_id']) && isset($this->other_data['dlt_entity_id']) && $this->other_data['dlt_template_id'] && $this->other_data['dlt_entity_id']){ $others = [ 'dlt_template_id' => $this->other_data['dlt_template_id'], 'entity_id' => $this->other_data['dlt_entity_id'] ]; } $this->sms_queue[] = [ 'message_id' => $this->message->id, 'campaign_id' => $this->message->campaign_id, 'message_files' => $this->message->message_files, 'from_type' => $this->message->sender_type, 'from' => $this->from_number, 'to' => $toNumber, 'dynamic_gateway_id' => $this->dynamic_gateway_id, 'schedule_datetime' => null, 'body' => $templateBody, 'created_at' => now(), 'updated_at' => now(), 'dlt_data' => json_encode($others), ]; $generatedToNumbers[] = $contact->number; } } }
foreach (array_chunk($this->sms_queue, $this->take_count) as $key => $daily_sms_queues) { $final_sms_queue = [];
foreach ($daily_sms_queues as $queue) { $addSeconds=floor($this->send_speed) < 1 ? 1 : floor($this->send_speed); $final_sms_queue[] = [ 'message_id' => $queue['message_id'], 'campaign_id' => $queue['campaign_id'], 'message_files' => $queue['message_files'], 'from_type' => $queue['from_type'], 'from' => $queue['from'], 'to' => $queue['to'], 'dynamic_gateway_id' => $queue['dynamic_gateway_id'], 'schedule_datetime' => $campaignRunningDate->addSeconds($addSeconds)->toDateTimeString(), 'body' => $queue['body'], 'created_at' => now(), 'updated_at' => now(), 'type'=> 'sent', 'sms_gateway_value'=> $queue['dlt_data'], ]; } if ($final_sms_queue) { $this->user->sms_queues()->createMany($final_sms_queue); $this->user->message_logs()->createMany($final_sms_queue); } } }else{ Log::info("there is no to Numbers"); } }
public function failed(\Throwable $exception) { Campaign::where('id',$this->message->campaign_id)->update(['import_fail_message'=>substr($exception->getMessage(), 0, 191)]); }
/** * The number of seconds the job can run before timing out. * * @var int */ public $timeout = 300; }
|