-
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #206 from liberu-crm/sweep/Implement-in-app-notifi…
…cations-for-CRM-events Implement in-app notifications for CRM events
- Loading branch information
Showing
9 changed files
with
285 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers; | ||
|
||
use Illuminate\Http\Request; | ||
|
||
class NotificationController extends Controller | ||
{ | ||
public function index() | ||
{ | ||
$user = auth()->user(); | ||
$notifications = $user->inAppNotifications()->paginate(10); | ||
return view('notifications.index', compact('notifications')); | ||
} | ||
|
||
public function markAsRead($id) | ||
{ | ||
$notification = auth()->user()->inAppNotifications()->findOrFail($id); | ||
$notification->markAsRead(); | ||
return response()->json(['success' => true]); | ||
} | ||
|
||
public function markAllAsRead() | ||
{ | ||
auth()->user()->unreadNotifications->markAsRead(); | ||
return response()->json(['success' => true]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
namespace App\Listeners; | ||
|
||
use App\Notifications\CRMEventNotification; | ||
use Illuminate\Contracts\Queue\ShouldQueue; | ||
use Illuminate\Queue\InteractsWithQueue; | ||
|
||
class SendCRMEventNotification implements ShouldQueue | ||
{ | ||
use InteractsWithQueue; | ||
|
||
public function handle($event) | ||
{ | ||
$eventName = class_basename($event); | ||
$notificationConfig = config("crm.notifications.events.{$eventName}"); | ||
|
||
if ($notificationConfig) { | ||
$users = $this->getUsersToNotify($event); | ||
foreach ($users as $user) { | ||
$user->notify(new CRMEventNotification($eventName, $event->toArray())); | ||
} | ||
} | ||
} | ||
|
||
protected function getUsersToNotify($event) | ||
{ | ||
// Implement logic to determine which users should be notified | ||
// This could be based on user roles, team membership, or other criteria | ||
// For now, we'll just notify all users (you should refine this) | ||
return \App\Models\User::all(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
|
||
namespace App\Notifications; | ||
|
||
use Illuminate\Bus\Queueable; | ||
use Illuminate\Contracts\Queue\ShouldQueue; | ||
use Illuminate\Notifications\Messages\MailMessage; | ||
use Illuminate\Notifications\Notification; | ||
|
||
class CRMEventNotification extends Notification implements ShouldQueue | ||
{ | ||
use Queueable; | ||
|
||
protected $event; | ||
protected $data; | ||
|
||
public function __construct($event, $data) | ||
{ | ||
$this->event = $event; | ||
$this->data = $data; | ||
} | ||
|
||
public function via($notifiable) | ||
{ | ||
return ['mail', 'database']; | ||
} | ||
|
||
public function toMail($notifiable) | ||
{ | ||
return (new MailMessage) | ||
->subject("CRM Event: {$this->event}") | ||
->line("A new CRM event has occurred: {$this->event}") | ||
->line("Details: " . $this->getEventDetails()) | ||
->action('View in CRM', url('/dashboard')) | ||
->line('Thank you for using our application!'); | ||
} | ||
|
||
public function toArray($notifiable) | ||
{ | ||
return [ | ||
'event' => $this->event, | ||
'data' => $this->data, | ||
]; | ||
} | ||
|
||
protected function getEventDetails() | ||
{ | ||
// Customize this method based on the event type and data | ||
return json_encode($this->data); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
return [ | ||
'notifications' => [ | ||
'events' => [ | ||
'new_lead' => true, | ||
'deal_closed' => true, | ||
'task_overdue' => true, | ||
'new_comment' => true, | ||
'meeting_scheduled' => true, | ||
], | ||
], | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?php | ||
|
||
namespace Tests\Feature; | ||
|
||
use Illuminate\Foundation\Testing\RefreshDatabase; | ||
use Illuminate\Foundation\Testing\WithFaker; | ||
use Tests\TestCase; | ||
use App\Models\User; | ||
use App\Notifications\CRMEventNotification; | ||
use Illuminate\Support\Facades\Notification; | ||
|
||
class NotificationTest extends TestCase | ||
{ | ||
use RefreshDatabase; | ||
|
||
public function test_notifications_are_created_for_crm_events() | ||
{ | ||
Notification::fake(); | ||
|
||
$user = User::factory()->create(); | ||
$event = 'NewLead'; | ||
$data = ['name' => 'John Doe', 'email' => '[email protected]']; | ||
|
||
$user->notify(new CRMEventNotification($event, $data)); | ||
|
||
Notification::assertSentTo( | ||
$user, | ||
CRMEventNotification::class, | ||
function ($notification) use ($event, $data) { | ||
return $notification->event === $event && $notification->data === $data; | ||
} | ||
); | ||
} | ||
|
||
public function test_users_can_view_in_app_notifications() | ||
{ | ||
$user = User::factory()->create(); | ||
$this->actingAs($user); | ||
|
||
$event = 'NewLead'; | ||
$data = ['name' => 'John Doe', 'email' => '[email protected]']; | ||
|
||
$user->notify(new CRMEventNotification($event, $data)); | ||
|
||
$response = $this->get('/notifications'); | ||
|
||
$response->assertStatus(200); | ||
$response->assertSee($event); | ||
} | ||
|
||
public function test_users_can_mark_notifications_as_read() | ||
{ | ||
$user = User::factory()->create(); | ||
$this->actingAs($user); | ||
|
||
$notification = $user->notifications()->create([ | ||
'type' => CRMEventNotification::class, | ||
'data' => ['event' => 'NewLead', 'data' => ['name' => 'John Doe']], | ||
]); | ||
|
||
$response = $this->post("/notifications/{$notification->id}/mark-as-read"); | ||
|
||
$response->assertStatus(200); | ||
$this->assertNotNull($notification->fresh()->read_at); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
namespace Tests\Unit\Notifications; | ||
|
||
use App\Notifications\CRMEventNotification; | ||
use Illuminate\Foundation\Testing\RefreshDatabase; | ||
use Tests\TestCase; | ||
use App\Models\User; | ||
|
||
class CRMEventNotificationTest extends TestCase | ||
{ | ||
use RefreshDatabase; | ||
|
||
public function test_email_notification_content() | ||
{ | ||
$user = User::factory()->create(); | ||
$event = 'NewLead'; | ||
$data = ['name' => 'John Doe', 'email' => '[email protected]']; | ||
|
||
$notification = new CRMEventNotification($event, $data); | ||
$mailMessage = $notification->toMail($user); | ||
|
||
$this->assertEquals("CRM Event: {$event}", $mailMessage->subject); | ||
$this->assertStringContainsString("A new CRM event has occurred: {$event}", $mailMessage->introLines[0]); | ||
$this->assertStringContainsString("Details: " . json_encode($data), $mailMessage->introLines[1]); | ||
} | ||
|
||
public function test_in_app_notification_data_structure() | ||
{ | ||
$user = User::factory()->create(); | ||
$event = 'DealClosed'; | ||
$data = ['deal_id' => 123, 'amount' => 10000]; | ||
|
||
$notification = new CRMEventNotification($event, $data); | ||
$arrayData = $notification->toArray($user); | ||
|
||
$this->assertEquals($event, $arrayData['event']); | ||
$this->assertEquals($data, $arrayData['data']); | ||
} | ||
} |