From 0bdb029729eee1631801539342911c9ae5181842 Mon Sep 17 00:00:00 2001 From: JoshuaMicallefYBSU <91457812+JoshuaMicallefYBSU@users.noreply.github.com> Date: Fri, 25 Oct 2024 23:22:11 +1000 Subject: [PATCH 1/2] RosterInactivity Fixup --- app/Console/Kernel.php | 2 +- .../Controllers/DiscordTestController.php | 4 +- app/Jobs/DiscordAccountCheck.php | 5 + app/Jobs/ProcessRosterInactivity.php | 103 +++++++++--------- .../Roster/TwoMonthFromRemoval.php | 64 +++++++++++ .../roster/twomonthstillremoval.blade.php | 23 ++++ resources/views/layouts/email.blade.php | 2 +- 7 files changed, 149 insertions(+), 54 deletions(-) create mode 100644 app/Notifications/Roster/TwoMonthFromRemoval.php create mode 100644 resources/views/emails/roster/twomonthstillremoval.blade.php diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php index cd4ff6b8..69ec73c9 100644 --- a/app/Console/Kernel.php +++ b/app/Console/Kernel.php @@ -46,7 +46,7 @@ protected function schedule(Schedule $schedule) $schedule->job(new DiscordAccountCheck())->dailyAt('04:00'); //Roster Inactivity checks - $schedule->job(new ProcessRosterInactivity())->daily(); + $schedule->job(new ProcessRosterInactivity())->dailyAt('23:59'); // Shanwick Controller Roster Update $schedule->job(new ProcessShanwickControllers())->daily(); diff --git a/app/Http/Controllers/DiscordTestController.php b/app/Http/Controllers/DiscordTestController.php index 97d8e665..4ea4db07 100644 --- a/app/Http/Controllers/DiscordTestController.php +++ b/app/Http/Controllers/DiscordTestController.php @@ -5,7 +5,7 @@ use Illuminate\Http\Request; use App\Services\DiscordClient; use Illuminate\Support\Facades\Http; -use App\Jobs\DiscordAccountCheck; +use App\Jobs\ProcessRosterInactivity; use App\Jobs\ProcessShanwickController; class DiscordTestController extends Controller @@ -28,7 +28,7 @@ public function EditTagTest() public function Job() { // Dispatch the job - $job = DiscordAccountCheck::dispatch(); + $job = ProcessRosterInactivity::dispatch(); // Call the handle method directly to get the result synchronously $result = $job->handle(); diff --git a/app/Jobs/DiscordAccountCheck.php b/app/Jobs/DiscordAccountCheck.php index 42053a1f..799135dd 100644 --- a/app/Jobs/DiscordAccountCheck.php +++ b/app/Jobs/DiscordAccountCheck.php @@ -325,6 +325,11 @@ public function handle() continue; } + // Skip Server Owner (Gary) + if($discord_uid == 350995372627197954){ + continue; + } + $accounts_not_linked++; //records that Account Not Linked Role Assigned sleep(1); diff --git a/app/Jobs/ProcessRosterInactivity.php b/app/Jobs/ProcessRosterInactivity.php index 78240eed..b952fa64 100644 --- a/app/Jobs/ProcessRosterInactivity.php +++ b/app/Jobs/ProcessRosterInactivity.php @@ -11,10 +11,7 @@ use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\SerializesModels; use Illuminate\Support\Facades\Notification; -use App\Notifications\Network\TwoMonthInactivityReminder; -use App\Notifications\Network\OneMonthInactivityReminder; -use App\Notifications\Network\OneWeekInactivityReminder; -use App\Notifications\Network\ControllerTerminated; +use App\Notifications\Roster\TwoMonthFromRemoval; use App\Services\DiscordClient; class ProcessRosterInactivity implements ShouldQueue @@ -42,10 +39,10 @@ public function __construct() public function handle() { // Counter Variables (For Message at End) - $first_notice = 0; //305 Days without controlling - $second_notice = 0; //335 Days without controlling - $third_notice = 0; //358 Days without controlling - $termination_notice = 0; //365 Days without controlling (one year) + $first_notice = 0; //1NOV message sent out + $second_notice = 0; //1DEC message sent out + $third_notice = 0; //24DEC message + $termination_notice = 0; //31DEC Message Sent Out $first_names = []; $second_names = []; @@ -53,20 +50,24 @@ public function handle() $termination_names = []; $roster_controllers = RosterMember::all(); + // dd($roster_controllers); // Lets now go through each RosterMember (gotta update dem all) foreach($roster_controllers as $roster){ - // get sessions within the last 12 months - $sessions = SessionLog::where('cid', $roster->cid)->where('created_at', '>=', Carbon::now()->subMonths(12))->orderBy('created_at', 'asc')->get(); - $last_session = SessionLog::where('cid', $roster->cid)->where('created_at', '>=', Carbon::now()->subMonths(12))->orderBy('created_at', 'desc')->first(); + // get sessions since start of year + $sessions = SessionLog::where('cid', $roster->cid)->where('created_at', '>=', Carbon::now()->startOfYear())->orderBy('created_at', 'asc')->get(); + + // Name of user being checked - Will link to the Users Roster Profile + $name = "[" . $roster->user->fullName('FLC') . "](" . route('training.admin.roster.viewcontroller', $roster->cid) . ")"; - // user being checked. - $name = $roster->user->discord_user_id; - // Set some variables (default values) $currency = 0; //Assume no connections - $active_status = 1; //Assume roster member is active + if($roster->active) { //Assume based off current set + $active_status = 1; + } else { + $active_status = 0; + } // Go through each session to get some information foreach($sessions as $s){ @@ -80,59 +81,61 @@ public function handle() $currency += $s->duration; } + // If Currency is greater than 1, set status active + if($currency > 0.99){ + $active_status = 1; + } + + // 1NOV - 2 Month Activity Check + if($roster->active && Carbon::now()->format('d/m') == "11/11" && $roster->currency < 1) { + $active_status = 0; + $first_names[] = $name; - // Check if there was a last session - if($last_session != null && $roster->certification === "certified"){ + $first_notice++; - // Currency is less than 1 hour and last connection was 305 days ago. - if($roster->active && $currency < 1 && $last_session->created_at->diffInDays(now()) == 305){ - $active_status = 0; + Notification::send($roster->user, new TwoMonthFromRemoval($roster->user)); + } - $first_names[] = "<@".$name.">"; + // 1DEC - 1 Month till Removal + if(Carbon::now()->format('d/m') == "1/12" && $roster->currency < 1){ + $second_names[] = $name; - $first_notice++; - }; + $second_notice++; + } - // User in inactive, has < 1hr of activity, and has not controlled for 335 days - if($currency < 1 && $last_session->created_at->diffInDays(now()) == 335){ - $second_names[] = "<@".$name.">"; + // 24DEC - 7 Days Till Removal + if(Carbon::now()->format('d/m') == "24/12" && $roster->currency < 1){ + $third_names[] = $name; - $second_notice++; - } + $third_notice++; + } + // End Of Year - Reset Time + if(Carbon::now()->format('d/m') == "31/12"){ - // User is inactive, has <1hr of activity and has not controlled for 358 Days - if($currency < 1 && $last_session->created_at->diffInDays(now()) == 358){ - $third_names[] = "<@".$name.">"; + // User to be terminated + if($roster->currency < 1){ + $termination_names[] = $name; - $third_notice++; + $termination_notice++; } - // Save Roster Information based of above if statements - $roster->active = $active_status; - $roster->currency = $currency; - $roster->save(); - - - // No Session was returned within the last 365 Days. - } else { - if($roster->certification === "certified"){ - $termination_notice++; - $termination_names[] = "<@".$name.">"; - - $roster->user->removeRole('Certified Controller'); - $roster->user->assignRole('Guest'); - $roster->delete(); - } + // Set Total Currency back to Zero (New Year Begins) + $currency = 0; } - } + + // Save Roster Information based of above if statements + $roster->active = $active_status; + $roster->currency = $currency; + $roster->save(); + } #Generate Discord Message $message_contents = "The following changes have been made to the Controller Roster."; if($first_notice != 0){ - $message_contents .= "\n\n**60 Days Until Removed (Set as Inactive)**"; + $message_contents .= "\n\n**60 Days To Complete Activity (Set as Inactive)**"; foreach($first_names as $n){ $message_contents .= "\n- ".$n; diff --git a/app/Notifications/Roster/TwoMonthFromRemoval.php b/app/Notifications/Roster/TwoMonthFromRemoval.php new file mode 100644 index 00000000..05513ee7 --- /dev/null +++ b/app/Notifications/Roster/TwoMonthFromRemoval.php @@ -0,0 +1,64 @@ +user = $user; + } + + /** + * Get the notification's delivery channels. + * + * @param mixed $notifiable + * + * @return array + */ + public function via($notifiable) + { + return ['mail']; + } + + /** + * Get the mail representation of the notification. + * + * @param mixed $notifiable + * + * @return \Illuminate\Notifications\Messages\MailMessage + */ + public function toMail($notifiable) + { + return (new MailMessage())->view( + 'emails.roster.twomonthstillremoval', + ['user' => $this->user] + )->subject('Two Months to Fufill Activity Requirements'); + } + + /** + * Get the array representation of the notification. + * + * @param mixed $notifiable + * + * @return array + */ + public function toArray($notifiable) + { + return [ + // + ]; + } +} diff --git a/resources/views/emails/roster/twomonthstillremoval.blade.php b/resources/views/emails/roster/twomonthstillremoval.blade.php new file mode 100644 index 00000000..2cf585e3 --- /dev/null +++ b/resources/views/emails/roster/twomonthstillremoval.blade.php @@ -0,0 +1,23 @@ +@extends('layouts.email') + +@section('to-line', 'Dear '. $user->fullName('F') . ',') + +@section('message-content') +

Our records indicate that you currently do not meet the currency requirements for your endorsement with Gander Oceanic

+ +

This is a friendly reminder that you must control a minimum of 1 hour per calendar year. You currently have {{$user->currency}} minutes recorded on Gander & Shanwick Positions.

+ +

+ +

If you believe this is a mistake or have any questions, please email the Chief Instructor with your query.

+@endsection + +@section('from-line') +Sent by Gander Oceanic OCA +@endsection + +@section('footer-to-line', $user->fullName('FLC').' ('.$user->email.')') + +@section('footer-reason-line') +of Currency Policy Requirements +@endsection diff --git a/resources/views/layouts/email.blade.php b/resources/views/layouts/email.blade.php index e71ca7f1..c0967697 100644 --- a/resources/views/layouts/email.blade.php +++ b/resources/views/layouts/email.blade.php @@ -134,7 +134,7 @@
-Image +Image
From 9f354709e1120d13d0b4d287edb30fdf7dc0d852 Mon Sep 17 00:00:00 2001 From: JoshuaMicallefYBSU <91457812+JoshuaMicallefYBSU@users.noreply.github.com> Date: Fri, 25 Oct 2024 23:23:21 +1000 Subject: [PATCH 2/2] Update Kernel.php --- app/Console/Kernel.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php index 69ec73c9..072bc094 100644 --- a/app/Console/Kernel.php +++ b/app/Console/Kernel.php @@ -46,7 +46,7 @@ protected function schedule(Schedule $schedule) $schedule->job(new DiscordAccountCheck())->dailyAt('04:00'); //Roster Inactivity checks - $schedule->job(new ProcessRosterInactivity())->dailyAt('23:59'); + $schedule->job(new ProcessRosterInactivity())->dailyAt('23:55'); // Shanwick Controller Roster Update $schedule->job(new ProcessShanwickControllers())->daily();