76 lines
2.8 KiB
PHP
76 lines
2.8 KiB
PHP
<?php
|
|
|
|
namespace App\Filament\Resources\Manage\Cooperations\Actions\Cooperation;
|
|
|
|
use App\Enums\ApprovalStatus;
|
|
use App\Enums\RoleEnum;
|
|
use App\Filament\Support\CheerfulNotification;
|
|
use App\Models\Cooperation;
|
|
use App\Models\User;
|
|
use App\Notifications\BroadcastNotification;
|
|
use Filament\Actions\Action;
|
|
use Filament\Support\Icons\Heroicon;
|
|
|
|
class RejectAction extends Action
|
|
{
|
|
public static function getDefaultName(): ?string
|
|
{
|
|
return 'reject';
|
|
}
|
|
|
|
protected function setUp(): void
|
|
{
|
|
parent::setUp();
|
|
|
|
$this->label('Tolak')
|
|
->icon(Heroicon::OutlinedXMark)
|
|
->color('danger')
|
|
->action(function (Cooperation $record): void {
|
|
$cooperationMedia = $record->cooperationMedia()
|
|
->whereHas('partnerMedia.company', function ($query) {
|
|
$query->where('user_id', auth()->id());
|
|
})
|
|
->first();
|
|
|
|
if ($cooperationMedia) {
|
|
$cooperationMedia->update(['status' => ApprovalStatus::REJECTED]);
|
|
|
|
CheerfulNotification::info(
|
|
CheerfulNotification::getByKey('cooperation.offer_rejected'),
|
|
CheerfulNotification::getByKey('cooperation.offer_rejection_sent')
|
|
)->send();
|
|
|
|
// Notify Admins
|
|
User::query()
|
|
->superAdmin()
|
|
->get()
|
|
->each(function ($admin) use ($record, $cooperationMedia): void {
|
|
$admin->notify(new BroadcastNotification([
|
|
'title' => CheerfulNotification::getByKey('cooperation.offer_rejected_by_media'),
|
|
'body' => CheerfulNotification::getByKey('notification.media_rejected_offer', ['cooperationMedia__partnerMedia__name' => $cooperationMedia->partnerMedia->name, 'record__title' => $record->title]),
|
|
]));
|
|
});
|
|
}
|
|
})
|
|
->visible(function (Cooperation $record): bool {
|
|
$user = auth()->user();
|
|
|
|
if (! $user || ! $user->hasRole(RoleEnum::PERUSAHAAN->value)) {
|
|
return false;
|
|
}
|
|
|
|
if (! $record->taskAssignment()->exists()) {
|
|
return false;
|
|
}
|
|
|
|
$cooperationMedia = $record->cooperationMedia()
|
|
->whereHas('partnerMedia.company', function ($query) {
|
|
$query->where('user_id', auth()->id());
|
|
})
|
|
->first();
|
|
|
|
return $cooperationMedia && $cooperationMedia->status === ApprovalStatus::PENDING;
|
|
});
|
|
}
|
|
}
|