From 5128c7489213abeb595eeaec4a076bedec653efd Mon Sep 17 00:00:00 2001 From: root Date: Thu, 23 Apr 2026 15:30:50 -0400 Subject: [PATCH] fix login issues --- app/Providers/AppServiceProvider.php | 3 ++ .../AttendanceTrackingService.php | 1 + .../DefaultAttendanceMailerService.php | 50 +++++++++++++++++++ app/Services/Auth/ApiLoginSecurityService.php | 6 +++ 4 files changed, 60 insertions(+) create mode 100644 app/Services/AttendanceTracking/DefaultAttendanceMailerService.php diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index 5fbd23f8..2b03f011 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -8,6 +8,8 @@ use App\Services\Attendance\SemesterRangeService; use App\Services\Attendance\StudentAttendanceWriterService; use App\Services\Attendance\TeacherAttendanceSubmissionService; use App\Services\Attendance\AttendanceAutoPublishService; +use App\Services\AttendanceTracking\AttendanceMailerService; +use App\Services\AttendanceTracking\DefaultAttendanceMailerService; use App\Services\Invoices\InvoiceConfigService; use App\Services\Invoices\InvoiceGradeService; use App\Services\Invoices\InvoiceTuitionService; @@ -50,6 +52,7 @@ class AppServiceProvider extends ServiceProvider $this->app->singleton(StudentAttendanceWriterService::class); $this->app->singleton(TeacherAttendanceSubmissionService::class); $this->app->singleton(AttendanceAutoPublishService::class); + $this->app->bind(AttendanceMailerService::class, DefaultAttendanceMailerService::class); $this->app->singleton(StaffTimeOffLinkService::class); $this->app->singleton(InvoiceConfigService::class); diff --git a/app/Services/AttendanceTracking/AttendanceTrackingService.php b/app/Services/AttendanceTracking/AttendanceTrackingService.php index 91396598..0f312550 100644 --- a/app/Services/AttendanceTracking/AttendanceTrackingService.php +++ b/app/Services/AttendanceTracking/AttendanceTrackingService.php @@ -2,6 +2,7 @@ namespace App\Services\AttendanceTracking; +use App\Models\Configuration; use Illuminate\Support\Facades\Validator; /** diff --git a/app/Services/AttendanceTracking/DefaultAttendanceMailerService.php b/app/Services/AttendanceTracking/DefaultAttendanceMailerService.php new file mode 100644 index 00000000..0cb9946a --- /dev/null +++ b/app/Services/AttendanceTracking/DefaultAttendanceMailerService.php @@ -0,0 +1,50 @@ +emailService->send($to, $subject, $html, 'attendance'); + } + + public function queueAttendanceEvent(array $payload): void + { + $to = trim((string) data_get($payload, 'parent.email', '')); + if ($to === '') { + throw new \InvalidArgumentException('No parent email found for attendance notification.'); + } + + $student = (array) ($payload['student'] ?? []); + $studentName = trim((string) ($student['firstname'] ?? '') . ' ' . (string) ($student['lastname'] ?? '')); + $subjectType = (string) ($payload['subject'] ?? 'Attendance'); + $date = substr((string) ($payload['date'] ?? now()->toDateString()), 0, 10); + + $subject = "Attendance Notice: {$subjectType}"; + $body = '

Dear ' . e((string) data_get($payload, 'parent.name', 'Parent/Guardian')) . ',

' + . '

This is an attendance notification for your student.

' + . '' + . '

If you have any questions, please contact the school office.

'; + + $html = $this->emailComposerService->renderWithEmailLayout($subject, $body); + + if (!$this->send($to, $subject, $html)) { + Log::error('Attendance notification email failed for ' . $to); + throw new \RuntimeException('Failed to send attendance notification email.'); + } + } +} diff --git a/app/Services/Auth/ApiLoginSecurityService.php b/app/Services/Auth/ApiLoginSecurityService.php index a785ab0e..3626a907 100644 --- a/app/Services/Auth/ApiLoginSecurityService.php +++ b/app/Services/Auth/ApiLoginSecurityService.php @@ -4,6 +4,7 @@ namespace App\Services\Auth; use App\Models\IpAttempt; use App\Models\LoginActivity; +use App\Models\Configuration; use App\Models\User; use Illuminate\Http\Request; use Illuminate\Support\Facades\Log; @@ -83,12 +84,17 @@ class ApiLoginSecurityService public function logSuccessfulLogin(User $user, Request $request): void { + $schoolYear = trim((string) (Configuration::getConfig('school_year') ?? date('Y'))); + $semester = trim((string) (Configuration::getConfig('semester') ?? 'Fall')); + LoginActivity::query()->create([ 'user_id' => $user->id, 'email' => $user->email, 'login_time' => utc_now(), 'ip_address' => $request->ip(), 'user_agent' => (string) ($request->userAgent() ?? ''), + 'school_year' => $schoolYear, + 'semester' => $semester !== '' ? $semester : 'Fall', ]); }