Viewing File: /home/ubuntu/vedadeals-backend-base/app/Jobs/ProductPriceUpdateNotificationJob.php

<?php

namespace App\Jobs;

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 App\Models\{ User, BellNotification, Wishlist };
use App\Notifications\{ BellNotification as BellNotify};
use App\Services\{EmailService};

use DB, Setting, Exception, stdClass, Notification;

use Illuminate\Support\Str;

class ProductPriceUpdateNotificationJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    protected $product, $old_price;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($product, $old_price)
    {
        $this->product = $product;

        $this->old_price = $old_price;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        try {

            $old_price = formatted_amount($this->old_price);

            $new_price = formatted_amount($this->product->price);

            Wishlist::with(['user'])->where(['product_id' => $this->product->id])
                ->chunkById(100, function ($wishlists) use($old_price, $new_price) {

                foreach ($wishlists as $wishlist) {

                    $user = $wishlist->user;

                    if (Setting::get('is_push_notification') == YES && $user) {

                        $data = new stdClass;

                        $data->title = $data->body = tr('wishlist_product_price_changed', Str::limit($this->product->name, 30)).$old_price.' To '.$new_price;

                        if($user->push_notification && $user->is_logged_in && $user->device_token && in_array($user->device_type, [DEVICE_ANDROID, DEVICE_IOS]) ) {

                            $data->push_data = [
                                'content_id' =>$this->product->id ? : '',
                                'notification_type' => BELL_NOTIFICATION_WISHLIST,
                                'content_unique_id' => $this->product->unique_id ? : '',
                            ];

                            $data->device_token = $user->device_token;

                            $data->action = BELL_NOTIFICATION_WISHLIST;

                            Notification::send($user, new BellNotify($data));
                        }

                        BellNotification::Create([
                            'title' => $data->title,
                            'description' => $data->body,
                            'to_user_id' => $user->id,
                            'action' => BELL_NOTIFICATION_WISHLIST,
                            'model_unique_id' => $this->product->unique_id ? : '',
                        ]);
                    }
                }
            });

        } catch (Exception $e) {

            info("Product Price Update Notification Job Failed : ".$e->getMessage());
        }
    }
}
Back to Directory File Manager