update controllers logic
This commit is contained in:
@@ -2,7 +2,7 @@
|
||||
|
||||
namespace App\Http\Controllers\Api\Staff;
|
||||
|
||||
use App\Http\Controllers\Api\BaseApiController;
|
||||
use App\Http\Controllers\Api\Core\BaseApiController;
|
||||
use App\Http\Requests\Staff\StaffIndexRequest;
|
||||
use App\Http\Requests\Staff\StaffStoreRequest;
|
||||
use App\Http\Requests\Staff\StaffUpdateRequest;
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
namespace App\Http\Controllers\Api\Staff;
|
||||
|
||||
use App\Http\Controllers\Api\BaseApiController;
|
||||
use App\Http\Controllers\Api\Core\BaseApiController;
|
||||
use App\Http\Requests\Teachers\TeacherAbsenceSubmitRequest;
|
||||
use App\Http\Requests\Teachers\TeacherClassViewRequest;
|
||||
use App\Http\Requests\Teachers\TeacherSwitchClassRequest;
|
||||
@@ -25,21 +25,21 @@ class TeacherController extends BaseApiController
|
||||
|
||||
public function classes(TeacherClassViewRequest $request): JsonResponse
|
||||
{
|
||||
$userId = (int) (auth()->id() ?? 0);
|
||||
if ($userId <= 0) {
|
||||
return response()->json(['ok' => false, 'message' => 'Unauthorized.'], 401);
|
||||
$guard = $this->authenticatedUserIdOrUnauthorized();
|
||||
if ($guard instanceof JsonResponse) {
|
||||
return $guard;
|
||||
}
|
||||
|
||||
try {
|
||||
$data = $this->dashboardService->classView(
|
||||
$userId,
|
||||
$guard,
|
||||
$request->validated()['class_section_id'] ?? null
|
||||
);
|
||||
} catch (\RuntimeException $e) {
|
||||
return response()->json(['ok' => false, 'message' => $e->getMessage()], 403);
|
||||
}
|
||||
|
||||
$teacher = $this->teacherPayload($data['teacher'] ?? null, $userId);
|
||||
$teacher = $this->teacherPayload($data['teacher'] ?? null, $guard);
|
||||
$studentsBySection = [];
|
||||
|
||||
foreach (($data['studentsBySection'] ?? []) as $sectionId => $students) {
|
||||
@@ -59,15 +59,15 @@ class TeacherController extends BaseApiController
|
||||
|
||||
public function switchClass(TeacherSwitchClassRequest $request): JsonResponse
|
||||
{
|
||||
$userId = (int) (auth()->id() ?? 0);
|
||||
if ($userId <= 0) {
|
||||
return response()->json(['ok' => false, 'message' => 'Unauthorized.'], 401);
|
||||
$guard = $this->authenticatedUserIdOrUnauthorized();
|
||||
if ($guard instanceof JsonResponse) {
|
||||
return $guard;
|
||||
}
|
||||
|
||||
$requested = (int) $request->validated()['class_section_id'];
|
||||
|
||||
try {
|
||||
$data = $this->dashboardService->classView($userId, $requested);
|
||||
$data = $this->dashboardService->classView($guard, $requested);
|
||||
} catch (\RuntimeException $e) {
|
||||
return response()->json(['ok' => false, 'message' => $e->getMessage()], 403);
|
||||
}
|
||||
@@ -87,12 +87,12 @@ class TeacherController extends BaseApiController
|
||||
|
||||
public function absenceForm(): JsonResponse
|
||||
{
|
||||
$userId = (int) (auth()->id() ?? 0);
|
||||
if ($userId <= 0) {
|
||||
return response()->json(['ok' => false, 'message' => 'Unauthorized.'], 401);
|
||||
$guard = $this->authenticatedUserIdOrUnauthorized();
|
||||
if ($guard instanceof JsonResponse) {
|
||||
return $guard;
|
||||
}
|
||||
|
||||
$data = $this->absenceService->formData($userId);
|
||||
$data = $this->absenceService->formData($guard);
|
||||
|
||||
return response()->json([
|
||||
'ok' => true,
|
||||
@@ -106,12 +106,12 @@ class TeacherController extends BaseApiController
|
||||
|
||||
public function submitAbsence(TeacherAbsenceSubmitRequest $request): JsonResponse
|
||||
{
|
||||
$userId = (int) (auth()->id() ?? 0);
|
||||
if ($userId <= 0) {
|
||||
return response()->json(['ok' => false, 'message' => 'Unauthorized.'], 401);
|
||||
$guard = $this->authenticatedUserIdOrUnauthorized();
|
||||
if ($guard instanceof JsonResponse) {
|
||||
return $guard;
|
||||
}
|
||||
|
||||
$result = $this->absenceService->submit($userId, $request->validated());
|
||||
$result = $this->absenceService->submit($guard, $request->validated());
|
||||
|
||||
return response()->json([
|
||||
'ok' => (bool) ($result['ok'] ?? false),
|
||||
@@ -140,4 +140,17 @@ class TeacherController extends BaseApiController
|
||||
'role' => User::getUserRoleName($userId),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int|JsonResponse
|
||||
*/
|
||||
private function authenticatedUserIdOrUnauthorized(): int|JsonResponse
|
||||
{
|
||||
$userId = (int) (auth()->id() ?? 0);
|
||||
if ($userId <= 0) {
|
||||
return response()->json(['ok' => false, 'message' => 'Unauthorized.'], 401);
|
||||
}
|
||||
|
||||
return $userId;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,83 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api\Staff;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Services\Email\EmailDispatchService;
|
||||
use App\Services\Staff\StaffTimeOffLinkService;
|
||||
use Illuminate\Http\Response;
|
||||
|
||||
/**
|
||||
* Public principal acknowledgment link — sends courtesy confirmation to the requester.
|
||||
*/
|
||||
class TimeOffNotificationController extends Controller
|
||||
{
|
||||
public function __construct(
|
||||
private StaffTimeOffLinkService $tokens,
|
||||
private EmailDispatchService $mail,
|
||||
) {}
|
||||
|
||||
public function notify(string $token): Response
|
||||
{
|
||||
$payload = $this->tokens->parseToken($token);
|
||||
|
||||
if (! $payload) {
|
||||
return $this->respondHtml('Sorry, this link is invalid or has expired.', 400);
|
||||
}
|
||||
|
||||
$email = trim((string) ($payload['email'] ?? ''));
|
||||
$fullName = trim((string) ($payload['name'] ?? ''));
|
||||
|
||||
if ($email === '') {
|
||||
return $this->respondHtml('Unable to notify the requester because their email was not included.', 400);
|
||||
}
|
||||
|
||||
$dates = (string) ($payload['dates'] ?? '-');
|
||||
$role = (string) ($payload['role'] ?? 'staff');
|
||||
$reason = (string) ($payload['reason'] ?? '');
|
||||
$reasonType = (string) ($payload['reason_type'] ?? '');
|
||||
$submittedAt = (string) ($payload['submitted_at'] ?? '');
|
||||
$origin = (string) ($payload['origin'] ?? 'staff portal');
|
||||
|
||||
try {
|
||||
$subject = 'TimeOff Request - Principal Acknowledgment';
|
||||
$body = '<div style="font-family:Arial,Helvetica,sans-serif;font-size:14px;line-height:1.5;">'
|
||||
.'<p>Dear '.e($fullName !== '' ? $fullName : 'Staff Member').',</p>'
|
||||
.'<p>This is a courtesy confirmation that your time-off request submitted via the '
|
||||
.e($origin).' has been reviewed by the principal.</p>'
|
||||
.'<table cellpadding="6" cellspacing="0" style="border-collapse:collapse;">'
|
||||
.'<tr><td><strong>Role</strong></td><td>'.e($role).'</td></tr>'
|
||||
.'<tr><td><strong>Dates</strong></td><td>'.e($dates !== '' ? $dates : '-').'</td></tr>'
|
||||
.'<tr><td><strong>Reason Type</strong></td><td>'.e($reasonType !== '' ? $reasonType : '-').'</td></tr>'
|
||||
.'<tr><td><strong>Reason</strong></td><td>'.e($reason !== '' ? $reason : '-').'</td></tr>'
|
||||
.($submittedAt !== '' ? '<tr><td><strong>Submitted At</strong></td><td>'.e($submittedAt).'</td></tr>' : '')
|
||||
.'</table>'
|
||||
.'<p>If you have any follow-up questions, please contact the principal\'s office directly.</p>'
|
||||
.'<p style="margin-top:16px;">Thank you,<br>Al Rahma School Administration</p>'
|
||||
.'</div>';
|
||||
|
||||
$this->mail->send($email, $subject, $body, 'notifications');
|
||||
} catch (\Throwable $e) {
|
||||
logger()->error('Failed to send TimeOff confirmation to requester: '.$e->getMessage());
|
||||
|
||||
return $this->respondHtml(
|
||||
'Something went wrong while sending the confirmation email. Please contact IT for help.',
|
||||
500
|
||||
);
|
||||
}
|
||||
|
||||
return $this->respondHtml('Confirmation email sent to the requester. You may close this window.');
|
||||
}
|
||||
|
||||
private function respondHtml(string $message, int $statusCode = 200): Response
|
||||
{
|
||||
$html = '<!DOCTYPE html><html><head><meta charset="utf-8"><title>TimeOff</title></head>'
|
||||
.'<body style="font-family:Arial,Helvetica,sans-serif;padding:24px;">'
|
||||
.'<p>'.e($message).'</p>'
|
||||
.'</body></html>';
|
||||
|
||||
return response($html, $statusCode, [
|
||||
'Content-Type' => 'text/html; charset=UTF-8',
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user