Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[AMC & Coordonator/ Home] display alert banners for lack of activity on the Intervenții section. #334

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

declare(strict_types=1);

namespace App\Filament\Widgets\DashboardNotifications;

use App\Models\Activity;
use App\Models\User;
use Filament\Widgets\Widget;

class CoordDashboardActivityNotification extends Widget
{
protected static string $view = 'filament.widgets.dashboard-notification';

protected int|string|array $columnSpan = 'full';


private ?string $pluralNotificationText = ' utilizatori nu au introdus activitate pentru luna precedentă!';
private ?string $singleNotificationText = '1 utilizator nu a introdus activitate pentru luna precedentă!';

public static function canView(): bool
{

return auth()->user()->isCoordinator();
}

public function isCoordinator(): bool
{
return auth()->user()->isCoordinator();
}

public function getNotification(): string|null
{

$countOfUsersWithoutActivity = User::query()
->onlyNurses()
->activatesInCounty(auth()->user()->county_id)
->whereNotIn('id', function ($query) {
$query->select('subject_id')
->from('activity_log')
->where('subject_type', 'intervention')
->whereBetween('created_at', [now()->subMonth()->startOfMonth(), now()->subMonth()->endOfMonth()])
->whereNot('event', 'dismissed');
})
->count();

if ($countOfUsersWithoutActivity === 1) {
return $this->singleNotificationText;
}

if ($countOfUsersWithoutActivity > 1) {
return $countOfUsersWithoutActivity . $this->pluralNotificationText;
}

return null;

}

public function getNotificationIcon(): string
{
return 'resources/svg/danger.svg';

}

public function dismissNotification(): void
{
// NOT sure about this part
Activity::create([
'subject_id' => auth()->id(),
'causer_id' => auth()->id(),
'event' => 'dismissed',
'log_name' => 'default',
'subject_type' => 'intervention',
'causer_type' => 'user',
'description' => 'dismissed notification',
'properties' => ['notification' => $this->notificationText]
]);

}

}


Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php

declare(strict_types=1);

namespace App\Filament\Widgets\DashboardNotifications;

use App\Models\Activity;
use Filament\Widgets\Widget;

class NurseDashboardActivityNotification extends Widget
{
protected static string $view = 'filament.widgets.dashboard-notification';

protected int|string|array $columnSpan = 'full';
private ?string $oneWeekNotification = 'Nu ați introdus activitate în platformă de mai mult de o săptămână!';
private ?string $oneMonthNotification = 'Mai aveți 5 zile pentru a introduce activitatea pentru luna precedentă! Dacă nu introduceți la timp activitatea, nu veți mai avea posibilitatea să o introduceți și veți suferi consecințe!';
public bool $hideDismissButton = false;

public static function canView(): bool
{

return auth()->user()->isNurse();
}


public function getNotification(): string|null
{

$lastMonthActivityCount = Activity::query()
->where('subject_id', auth()->id())
->where('subject_type', 'intervention')
->whereBetween('created_at', [now()->subMonth()->startOfMonth(), now()->subMonth()->endOfMonth()])
->whereNot('event', 'dismissed')
->count();

/**
* Checks if the user has no activity in the last month, and it's within the first 5 days of the current month.
* If true, hides the dismiss button and returns the one-month notification.
*/
if ($lastMonthActivityCount < 1 && now()->day <= 5) {
$this->hideDismissButton = true;
return $this->oneMonthNotification;
}


$latestActivity = Activity::latest()
->where('subject_id', auth()->id())
->where('subject_type', 'intervention')
->whereNot('event', 'dismissed')
->first();

$notificationDismissed = Activity::latest()
->where('subject_id', auth()->id())
->where('subject_type', 'intervention')
->where('event', 'dismissed')
->first();


// Notification was already dismissed
if ($notificationDismissed && $notificationDismissed->created_at->gt($latestActivity->created_at)) {
return null;
}

// No activity in the last 7 days
if ($latestActivity && $latestActivity->created_at->lt(now()->subDays(7))) {
return $this->oneWeekNotification;
}

return null;
}

public function getNotificationIcon(): string
{
return 'resources/svg/warning.svg';

}

public function dismissNotification(): void
{
// NOT sure about this part
Activity::create([
'subject_id' => auth()->id(),
'causer_id' => auth()->id(),
'event' => 'dismissed',
'log_name' => 'default',
'subject_type' => 'intervention',
'causer_type' => 'user',
'description' => 'dismissed notification',
'properties' => ['notification' => $this->oneWeekNotification]
]);

}

}


4 changes: 3 additions & 1 deletion resources/js/app.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import.meta.glob([
'../images/**'
'../images/**',
'../svg/**',
]);

window.setEventContents = ({ event, timeText }) => {
Expand All @@ -15,3 +16,4 @@ window.setEventContents = ({ event, timeText }) => {

return { html };
};

9 changes: 9 additions & 0 deletions resources/svg/danger.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions resources/svg/dismiss.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 9 additions & 0 deletions resources/svg/warning.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

@if($this->getNotification() !== null)
<x-filament::widget class="dashboard-notification-widget" id="dashboard-notification">
<div id="dashboard-notification" class="py-3 px-8 bg-[#FEFAEA] border border-[#FBBF24] rounded-lg">
<div class="flex justify-between items-center">
<div class="flex flex-row gap-3 items-center">
<img src="{{Vite::asset($this->getNotificationIcon())}}" alt="warning icon" class="w-10 h-10">
<p class="text-base font-medium">{{$this->getNotification()}}</p>
@if(auth()->user()->isCoordinator())
<a href="{{route('filament.resources.users.index')}}" class="underline font-bold">Vezi lista →</a>
@endif
</div>
@if(!auth()->user()->isCoordinator() && !$this->hideDismissButton)
<div class="flex items-center">
<button wire:click="dismissNotification" class="focus:outline-none" onclick="hideNotification()">
<img src="{{Vite::asset('resources/svg/dismiss.svg')}}" alt="dismiss icon" class="w-6 h-6">
</button>
</div>
@endif
</div>
</div>
</x-filament::widget>
@else
<div class="hidden"></div>
@endif