Files
Global-Jain/app/Jobs/Notifications/Sangh/SendUserJoinedSangh.php
2025-11-05 10:37:10 +05:30

123 lines
4.8 KiB
PHP

<?php
namespace App\Jobs\Notifications\Sangh;
use App\Constant\Constant;
use Illuminate\Bus\Queueable;
use App\Models\UserDeviceToken;
use Illuminate\Support\Facades\Log;
use App\Traits\PushNotificationTraits;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
class SendUserJoinedSangh implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels, PushNotificationTraits;
/**
* @var
*/
protected $user;
/**
* @var
*/
protected $sangh;
/**
* @var
*/
protected $sanghMemberId;
/**
* @var
*/
protected $coreMember;
/**
* @var
*/
protected $roleName;
/**
*
*/
public function __construct($user, $sangh, $sanghMemberId, $coreMember, $roleName)
{
$this->user = $user;
$this->sangh = $sangh;
$this->sanghMemberId = $sanghMemberId;
$this->coreMember = $coreMember;
$this->roleName = $roleName;
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
try {
//Push Notification to admin when user send join request
$tokens = UserDeviceToken::where('user_id', $this->sanghMemberId)->with('user')->get()->toArray();
$pushData = [];
$users = [];
if($tokens && $tokens > 0) {
foreach ($tokens as $token) {
$pushData['userToken'] = $token['token'] ?? [];
$pushData['title'] = trans('notifications.notification_title.user_sent_join_request', ['sangh' => $this->sangh->name]);
$pushData['body'] = trans('notifications.push_sangh.user_sent_join_request', ['user' => $this->user->name, 'sangh' => $this->sangh->name]);
$pushData['image'] = ($this->user->avatar) ? $this->user->avatar : "";
$pushData['from_id'] = $this->sangh->id;
$extraData = [];
$extraData['from_id'] = $this->sangh->id;
$extraData['user_id'] = $this->sanghMemberId ?? '';
$extraData['user_name'] = $this->user->name ?? '';
$extraData['user_type'] = 'sangh';
$extraData['type'] = trans('notifications.sangh_notification_type.user-sent-member-request');
$extraData['created_at'] = date('Y/m/d H:i:s');
$extraData['os'] = $token['os'] ? getPlatform($token['os']) : [];
$pushData['extraData'] = $extraData ?? [];
$this->pushNotification($pushData, $extraData);
}
}
//Store Notification
$notificationData = [];
$notificationData['body'] = ($this->roleName) ? trans('notifications.sangh.user_sent_join_request_with_role', ['sangh' => $this->sangh->name, 'role' => $this->roleName]) : trans('notifications.sangh.user_sent_join_request', ['sangh' => $this->sangh->name]);
$notificationData['user_id'] = $this->sanghMemberId;
$notificationData['type'] = trans('notifications.sangh_notification_type.user-sent-member-request');
$notificationData['title'] = trans('notifications.notification_title.sangh_added_member', ['sangh' => $this->sangh->name]);
$extraNotificationData = [];
$extraNotificationData['from_id'] = $this->user->id ?? '';
$extraNotificationData['user_id'] = $this->user->id ?? '';
$extraNotificationData['name'] = $this->user->name ?? '';
$extraNotificationData['avatar'] = ($this->user->avatar) ? $this->user->avatar : "";
$extraNotificationData['sangh_id'] = $this->sangh->id ?? '';
$extraNotificationData['sangh_name'] = $this->sangh->name ?? '';
$extraNotificationData['add_member'] = $this->coreMember;
$extraNotificationData['role'] = $this->roleName ?? '';
$extraNotificationData['notification_type'] = Constant::STATUS_TWO;
$extraNotificationData['user_type'] = 'sangh';
$extraNotificationData['created_at'] = date('Y/m/d H:i:s');
$notificationData['extraNotificationData'] = $extraNotificationData ?? [];
if (!in_array($this->sanghMemberId, $users)) {
$users[$this->sanghMemberId] = $this->sanghMemberId;
$this->storeNotification($notificationData);
//Unread count increament
unreadNotificationCounter($this->sanghMemberId);
}
} catch (\Exception $ex) {
Log::error($ex);
}
}
}