-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implementation of agent notifications resource and API
- Loading branch information
Showing
10 changed files
with
346 additions
and
1 deletion.
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,121 @@ | ||
<?php | ||
|
||
namespace App\Filament\Resources; | ||
|
||
use App\Filament\Resources\AgentNotificationResource\Pages; | ||
use App\Filament\Resources\AgentNotificationResource\RelationManagers; | ||
use App\Models\Agent; | ||
use App\Models\AgentNotification; | ||
use Filament\Forms; | ||
use Filament\Forms\Form; | ||
use Filament\Resources\Resource; | ||
use Filament\Tables; | ||
use Filament\Tables\Table; | ||
use Illuminate\Database\Eloquent\Builder; | ||
use Illuminate\Database\Eloquent\SoftDeletingScope; | ||
|
||
class AgentNotificationResource extends Resource | ||
{ | ||
protected static ?string $model = AgentNotification::class; | ||
|
||
protected static ?string $navigationIcon = 'heroicon-o-bell-alert'; | ||
|
||
protected static ?string $navigationGroup = 'Operations'; | ||
|
||
protected static ?int $navigationSort = 2; | ||
|
||
public static function form(Form $form): Form | ||
{ | ||
return $form | ||
->schema([ | ||
// Forms\Components\Select::make('agent_id') | ||
// ->searchable() | ||
// ->relationship('agent', 'name') | ||
// ->preload() | ||
// ->required(), | ||
Forms\Components\Select::make('agent_id') | ||
->label('Agent') | ||
->searchable() | ||
->getSearchResultsUsing( | ||
fn (string $search) => Agent::active() | ||
->where('name', 'like', '%' . $search . '%') | ||
->orWhere('phone_number', 'like', '%' . $search . '%') | ||
->limit(10)->pluck('name', 'id')->toArray() | ||
) | ||
->getOptionLabelUsing(fn ($value) => | ||
Agent::find($value)->name . ' - ' . Agent::find($value)->phone_number | ||
) | ||
->helperText('Search for active agents with name or phone number(256...).'), | ||
Forms\Components\TextInput::make('title') | ||
->required() | ||
->maxLength(255) | ||
->columnSpanFull(), | ||
Forms\Components\Textarea::make('message') | ||
->required() | ||
->columnSpanFull(), | ||
|
||
]); | ||
} | ||
|
||
public static function table(Table $table): Table | ||
{ | ||
return $table | ||
->columns([ | ||
Tables\Columns\TextColumn::make('agent.name') | ||
->numeric() | ||
->sortable(), | ||
Tables\Columns\TextColumn::make('title') | ||
->label('Content') | ||
->description( | ||
fn (AgentNotification $record): string => $record->message | ||
) | ||
->searchable(), | ||
Tables\Columns\TextColumn::make('user.name') | ||
->label('Sent by') | ||
->numeric() | ||
->sortable(), | ||
Tables\Columns\TextColumn::make('read_at') | ||
->dateTime() | ||
->sortable(), | ||
Tables\Columns\TextColumn::make('deleted_at') | ||
->dateTime() | ||
->sortable() | ||
->toggleable(isToggledHiddenByDefault: true), | ||
Tables\Columns\TextColumn::make('created_at') | ||
->dateTime() | ||
->sortable() | ||
->toggleable(isToggledHiddenByDefault: true), | ||
Tables\Columns\TextColumn::make('updated_at') | ||
->dateTime() | ||
->sortable() | ||
->toggleable(isToggledHiddenByDefault: true), | ||
])->defaultSort('created_at', 'desc') | ||
->filters([ | ||
// | ||
]) | ||
->actions([ | ||
Tables\Actions\EditAction::make(), | ||
]) | ||
->bulkActions([ | ||
Tables\Actions\BulkActionGroup::make([ | ||
Tables\Actions\DeleteBulkAction::make(), | ||
]), | ||
]); | ||
} | ||
|
||
public static function getRelations(): array | ||
{ | ||
return [ | ||
// | ||
]; | ||
} | ||
|
||
public static function getPages(): array | ||
{ | ||
return [ | ||
'index' => Pages\ListAgentNotifications::route('/'), | ||
'create' => Pages\CreateAgentNotification::route('/create'), | ||
'edit' => Pages\EditAgentNotification::route('/{record}/edit'), | ||
]; | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
app/Filament/Resources/AgentNotificationResource/Pages/CreateAgentNotification.php
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,12 @@ | ||
<?php | ||
|
||
namespace App\Filament\Resources\AgentNotificationResource\Pages; | ||
|
||
use App\Filament\Resources\AgentNotificationResource; | ||
use Filament\Actions; | ||
use Filament\Resources\Pages\CreateRecord; | ||
|
||
class CreateAgentNotification extends CreateRecord | ||
{ | ||
protected static string $resource = AgentNotificationResource::class; | ||
} |
19 changes: 19 additions & 0 deletions
19
app/Filament/Resources/AgentNotificationResource/Pages/EditAgentNotification.php
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,19 @@ | ||
<?php | ||
|
||
namespace App\Filament\Resources\AgentNotificationResource\Pages; | ||
|
||
use App\Filament\Resources\AgentNotificationResource; | ||
use Filament\Actions; | ||
use Filament\Resources\Pages\EditRecord; | ||
|
||
class EditAgentNotification extends EditRecord | ||
{ | ||
protected static string $resource = AgentNotificationResource::class; | ||
|
||
protected function getHeaderActions(): array | ||
{ | ||
return [ | ||
Actions\DeleteAction::make(), | ||
]; | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
app/Filament/Resources/AgentNotificationResource/Pages/ListAgentNotifications.php
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,19 @@ | ||
<?php | ||
|
||
namespace App\Filament\Resources\AgentNotificationResource\Pages; | ||
|
||
use App\Filament\Resources\AgentNotificationResource; | ||
use Filament\Actions; | ||
use Filament\Resources\Pages\ListRecords; | ||
|
||
class ListAgentNotifications extends ListRecords | ||
{ | ||
protected static string $resource = AgentNotificationResource::class; | ||
|
||
protected function getHeaderActions(): array | ||
{ | ||
return [ | ||
Actions\CreateAction::make(), | ||
]; | ||
} | ||
} |
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,56 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers; | ||
|
||
use Illuminate\Http\Request; | ||
use App\Models\AgentNotification; | ||
use App\Models\Agent; | ||
|
||
class AgentNotificationController extends Controller | ||
{ | ||
// | ||
|
||
public function agentNotifications(Request $request) | ||
{ | ||
$agent = Agent::find($request->user()->agent_id); | ||
|
||
if ($agent) { | ||
return response()->json([ | ||
'notifications' => $agent->notifications() | ||
->latest() | ||
->get(), | ||
'unread' => $agent->notifications() | ||
->unread() | ||
->count(), | ||
]); | ||
} else { | ||
return response()->json([ | ||
'message' => 'Agent not found' | ||
], 404); | ||
} | ||
} | ||
|
||
public function markAsRead(Request $request) | ||
{ | ||
$request->validate([ | ||
'ids' => 'required|array', | ||
]); | ||
|
||
$agent = Agent::find($request->user()->agent_id); | ||
|
||
if ($agent) { | ||
$agent->notifications() | ||
->whereIn('id', $request->ids) | ||
->update(['read_at' => now()]); | ||
|
||
return response()->json([ | ||
'message' => 'Notifications marked as read' | ||
]); | ||
} else { | ||
return response()->json([ | ||
'message' => 'Agent not found' | ||
], 404); | ||
} | ||
|
||
} | ||
} |
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,69 @@ | ||
<?php | ||
|
||
namespace App\Models; | ||
|
||
use App\Models\Agent; | ||
use App\Models\User; | ||
use Illuminate\Database\Eloquent\Factories\HasFactory; | ||
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\Database\Eloquent\SoftDeletes; | ||
|
||
class AgentNotification extends Model | ||
{ | ||
use HasFactory, SoftDeletes; | ||
|
||
//Set user_id on creating | ||
public static function boot() | ||
{ | ||
parent::boot(); | ||
static::creating(function ($notification) { | ||
$notification->user_id = auth()->id(); | ||
}); | ||
} | ||
|
||
protected $fillable = [ | ||
'agent_id', | ||
'user_id', | ||
'title', | ||
'message', | ||
'read_at', | ||
]; | ||
|
||
protected $hidden = [ | ||
'created_at', | ||
'updated_at', | ||
'deleted_at', | ||
'user_id', | ||
// 'agent_id', | ||
]; | ||
|
||
protected $casts = [ | ||
'data' => 'array', | ||
'read_at' => 'datetime', | ||
'created_at' => 'datetime', | ||
]; | ||
|
||
protected $appends = [ | ||
'created_at_human', | ||
]; | ||
|
||
public function getCreatedAtHumanAttribute() | ||
{ | ||
return $this->created_at->diffForHumans(); | ||
} | ||
|
||
public function agent() | ||
{ | ||
return $this->belongsTo(Agent::class); | ||
} | ||
|
||
public function user() | ||
{ | ||
return $this->belongsTo(User::class); | ||
} | ||
|
||
public function scopeUnread($query) | ||
{ | ||
return $query->whereNull('read_at'); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
database/migrations/2024_03_14_154415_create_agent_notifications_table.php
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 | ||
|
||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
return new class extends Migration | ||
{ | ||
/** | ||
* Run the migrations. | ||
*/ | ||
public function up(): void | ||
{ | ||
Schema::create('agent_notifications', function (Blueprint $table) { | ||
$table->id(); | ||
$table->foreignId('agent_id')->constrained('agents'); | ||
$table->foreignId('user_id')->nullable()->constrained('users'); | ||
$table->string('title'); | ||
$table->text('message'); | ||
$table->timestamp('read_at')->nullable(); | ||
$table->softDeletes($column = 'deleted_at', $precision = 0); | ||
$table->timestamps(); | ||
}); | ||
} | ||
|
||
/** | ||
* Reverse the migrations. | ||
*/ | ||
public function down(): void | ||
{ | ||
Schema::dropIfExists('agent_notifications'); | ||
} | ||
}; |
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