0

Laravel 9

  1. When I send an sms without JOB, elequent inserts the data into the database

  2. Otherwise (when using JOB) the sms is sent but eloquent does not add anything to the database

I tried to do this without success Laravel queue job doesn't updates to the latest code and Laravel not updating job and service code, but it will update other code

My code

A. SMS Dispatch

SendSMSJob::dispatch($canal->canal_nom_gateway,
                        $phones,
                        $projet->projet_nom,
                        $request->message);
 

B. My JOB

 public function handle()
{
    switch ($this->canal) {
        case 'Twilio':
            foreach ($this->phones as $phone) {
                    app(TwilioSmsService::class)->sendMessage($phone, $this->projet->projet_nom, $this->message);
            }
            return;

            break;

        
    }
}

C. SMS Service (This code saves data when I am not using LARAVEL JOB)

 public function sendMessage($to, $from, $body) :array {
    //Select Canal Twilio Canal
    $canal_twilio = DB::table('canals')->where('canal_nom_gateway', 'Twilio')->first();
    $result = ['success' => false, 'data' => [], 'message' => '', 'twilio_sms_id' => null];

    try{

        $options = array();
        $options['body'] = $body;
        $options['from'] = $from ?? $this->from_number;
        $options['statusCallback'] = $this->status_callback_url;

        $apiResponse = $this->client->messages->create($to, $options);

        $result['data'] = $apiResponse->toArray();

        if(!empty($result['data']['errorCode'])) {
            throw new Exception('Send sms request failed');
        }
        $result['success'] = true;

        $createdSms = TwilioSms::create([
            'sid' => $result['data']['sid'],
            'direction' => 'sent',
            'from' => $result['data']['from'],
            'to' => $result['data']['to'],
            'status' => $result['data']['status'],
            'body' => $result['data']['body'],
            'num_segments' => $result['data']['numSegments'],
            'price_segments' => $canal_twilio->canal_price * $result['data']['numSegments'],  //Price unitaire * segment de message
            'user_id' => Auth::id(),
        ]);

        // Decrementer le credit de l'utilisateur apres envois du sms
        $this->user_compte->decrement('compte_credit', $createdSms['price_segments']);

        $result['twilio_sms_id'] = $createdSms->id ?? null;

        $this->log([
            'twilio_sms_id' => $createdSms->id ?? null,
            'sms_sid' => $result['data']['sid'] ?? null,
            'event' => 'send_sms_request',
            'new_status' => $result['data']['status'] ?? null,
            'details' => $result['data'],
            'user_id' => Auth::id(),
        ]);
        // // Notify Me
        // toast('Message envoyé !', 'success');

    }catch(Exception $ex){
        $result['success'] = false;
        $result['message'] = $ex->getMessage();
        $result['data']['error_message'] = $result['message'];
        $this->log([
            'twilio_sms_id' => null,
            'sms_sid' => $result['data']['sid'] ?? null,
            'event' => 'send_sms_request_error',
            'new_status' => $result['data']['status'] ?? null,
            'details' => $result['data'] ?? [],
            'user_id' => Auth::id(),
        ]);
        // toast($result['message'], 'danger');

    }
    return $result;
}

private function log($data) {
    try {
        if(empty($data)) {
            throw new Exception('Invalid log data');
        }

        $logData = [
            'twilio_sms_id' => $data['twilio_sms_id'] ?? null,
            'sms_sid' => $data['sms_sid'] ?? null,
            'sms_message_sid' => $data['sms_sid'] ?? null,
            'event' => $data['event'] ?? 'generic_error',
            'new_status' => $data['new_status'] ?? null,
            'details' => json_encode(($data['details'] ?? [])),
            'user_id' => Auth::id(),
        ];

        TwilioSmsLog::create($logData);

    }catch (Exception $ex) {
        // NOTICE: Should probably create a log channel just for Twilio
        Log::channel('single')->error($ex->getFile().' :: '.$ex->getLine().' :: '.$ex->getMessage());
    }

}

0 Answers0