add test batches
This commit is contained in:
@@ -7,6 +7,7 @@ use App\Models\AttendanceTracking;
|
||||
use App\Models\Configuration;
|
||||
use App\Models\Student;
|
||||
use App\Models\StudentClass;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Throwable;
|
||||
@@ -14,7 +15,6 @@ use Throwable;
|
||||
class AttendanceNotificationWorkflowService
|
||||
{
|
||||
protected string $semester;
|
||||
|
||||
protected string $schoolYear;
|
||||
|
||||
public function __construct(
|
||||
@@ -29,33 +29,33 @@ class AttendanceNotificationWorkflowService
|
||||
protected AttendanceEmailComposerService $emailComposerService,
|
||||
protected AttendanceNotificationLogService $notificationLogService
|
||||
) {
|
||||
$this->semester = (string) ($this->configModel->getConfig('semester') ?? '');
|
||||
$this->semester = (string) ($this->configModel->getConfig('semester') ?? '');
|
||||
$this->schoolYear = (string) ($this->configModel->getConfig('school_year') ?? '');
|
||||
}
|
||||
|
||||
public function record(array $data): array
|
||||
{
|
||||
$sid = (int) $data['student_id'];
|
||||
$ymd = substr((string) $data['date'], 0, 10);
|
||||
$semester = $data['semester'] ?? $this->semester;
|
||||
$sid = (int) $data['student_id'];
|
||||
$ymd = substr((string) $data['date'], 0, 10);
|
||||
$semester = $data['semester'] ?? $this->semester;
|
||||
$schoolYear = $data['school_year'] ?? $this->schoolYear;
|
||||
$subject = $data['subject_type'] ?? 'Absent';
|
||||
$subject = $data['subject_type'] ?? 'Absent';
|
||||
|
||||
$student = $this->studentModel->query()->find($sid)?->toArray() ?? [];
|
||||
$parentEmail = $data['parent_email'] ?? '';
|
||||
$parentName = $data['parent_name'] ?? '';
|
||||
$parentName = $data['parent_name'] ?? '';
|
||||
|
||||
if (! $parentEmail) {
|
||||
if (!$parentEmail) {
|
||||
$parent = $this->parentLookupService->getPrimaryParentForStudent($sid) ?? [];
|
||||
$parentEmail = $parent['email'] ?? '';
|
||||
$parentName = $parent['parent_name'] ?? '';
|
||||
$parentName = $parent['parent_name'] ?? '';
|
||||
}
|
||||
|
||||
if (! $parentEmail) {
|
||||
if (!$parentEmail) {
|
||||
return [
|
||||
'success' => false,
|
||||
'message' => 'No parent email found for this student.',
|
||||
'status' => 422,
|
||||
'status' => 422,
|
||||
];
|
||||
}
|
||||
|
||||
@@ -87,37 +87,37 @@ class AttendanceNotificationWorkflowService
|
||||
: [];
|
||||
|
||||
$payload = [
|
||||
'student_id' => $sid,
|
||||
'student' => $student,
|
||||
'parent' => ['email' => $parentEmail, 'name' => $parentName],
|
||||
'semester' => $semester,
|
||||
'student_id' => $sid,
|
||||
'student' => $student,
|
||||
'parent' => ['email' => $parentEmail, 'name' => $parentName],
|
||||
'semester' => $semester,
|
||||
'school_year' => $schoolYear,
|
||||
'class' => $class,
|
||||
'now' => now()->toDateTimeString(),
|
||||
'date' => $ymd,
|
||||
'subject' => $subject,
|
||||
'class' => $class,
|
||||
'now' => now()->toDateTimeString(),
|
||||
'date' => $ymd,
|
||||
'subject' => $subject,
|
||||
'consequence' => $consequence ?: 'follow_up',
|
||||
];
|
||||
|
||||
try {
|
||||
$this->attendanceMailerService->queueAttendanceEvent($payload);
|
||||
|
||||
if ($row && ! empty($row['id']) && method_exists($this->attendanceTrackingModel, 'markAsNotified')) {
|
||||
if ($row && !empty($row['id']) && method_exists($this->attendanceTrackingModel, 'markAsNotified')) {
|
||||
$this->attendanceTrackingModel->markAsNotified((int) $row['id']);
|
||||
}
|
||||
|
||||
return [
|
||||
'success' => true,
|
||||
'message' => 'Notification queued.',
|
||||
'data' => $payload,
|
||||
'data' => $payload,
|
||||
];
|
||||
} catch (Throwable $e) {
|
||||
Log::error('Attendance record queue failed: '.$e->getMessage());
|
||||
Log::error('Attendance record queue failed: ' . $e->getMessage());
|
||||
|
||||
return [
|
||||
'success' => false,
|
||||
'message' => $e->getMessage(),
|
||||
'status' => 500,
|
||||
'status' => 500,
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -132,32 +132,30 @@ class AttendanceNotificationWorkflowService
|
||||
$details = [];
|
||||
|
||||
foreach ($toSend as $v) {
|
||||
$sid = (int) ($v['id'] ?? 0);
|
||||
$code = (string) ($v['violation_code'] ?? $v['code'] ?? '');
|
||||
$date = (string) ($v['last_date'] ?? '');
|
||||
$to = trim((string) ($v['parent_email'] ?? ''));
|
||||
$sid = (int) ($v['id'] ?? 0);
|
||||
$code = (string) ($v['violation_code'] ?? $v['code'] ?? '');
|
||||
$date = (string) ($v['last_date'] ?? '');
|
||||
$to = trim((string) ($v['parent_email'] ?? ''));
|
||||
$pname = (string) ($v['parent_name'] ?? '');
|
||||
|
||||
if ($sid <= 0 || ! $code || ! $date || ! $to) {
|
||||
if ($sid <= 0 || !$code || !$date || !$to) {
|
||||
$errors++;
|
||||
$details[] = ['student_id' => $sid, 'code' => $code, 'date' => $date, 'result' => 'missing data'];
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($this->notificationLogService->notificationAlreadySent($sid, $code, $date, 'email', $to)) {
|
||||
$skipped++;
|
||||
$details[] = ['student_id' => $sid, 'code' => $code, 'date' => $date, 'result' => 'duplicate'];
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
$parent = $this->parentLookupService->getPrimaryParentForStudent($sid);
|
||||
$parent = $this->parentLookupService->getPrimaryParentForStudent($sid);
|
||||
$context = $this->emailComposerService->buildTemplateContext($v, $parent);
|
||||
$variant = $this->emailComposerService->pickVariant($code, $variantOverride);
|
||||
$tpl = $this->emailComposerService->renderTemplate($code, $variant, $context);
|
||||
$tpl = $this->emailComposerService->renderTemplate($code, $variant, $context);
|
||||
|
||||
if (! $tpl) {
|
||||
if (!$tpl) {
|
||||
[$subject, $body] = $this->emailComposerService->buildFallbackAutoEmail($code, $v, $pname);
|
||||
} else {
|
||||
[$subject, $body] = $tpl;
|
||||
@@ -214,7 +212,7 @@ class AttendanceNotificationWorkflowService
|
||||
$this->semester,
|
||||
$this->schoolYear
|
||||
);
|
||||
$details[] = ['student_id' => $sid, 'code' => $code, 'date' => $date, 'result' => 'error: '.$e->getMessage()];
|
||||
$details[] = ['student_id' => $sid, 'code' => $code, 'date' => $date, 'result' => 'error: ' . $e->getMessage()];
|
||||
}
|
||||
|
||||
try {
|
||||
@@ -262,7 +260,7 @@ class AttendanceNotificationWorkflowService
|
||||
}
|
||||
} catch (Throwable $e) {
|
||||
$errors++;
|
||||
$details[] = ['student_id' => $sid, 'code' => $code, 'date' => $date, 'result' => 'cc_error: '.$e->getMessage()];
|
||||
$details[] = ['student_id' => $sid, 'code' => $code, 'date' => $date, 'result' => 'cc_error: ' . $e->getMessage()];
|
||||
}
|
||||
|
||||
if ($anyOk) {
|
||||
@@ -279,13 +277,13 @@ class AttendanceNotificationWorkflowService
|
||||
}
|
||||
|
||||
$allDates = [];
|
||||
$absDates = array_map(fn ($d) => substr((string) $d, 0, 10), (array) ($v['absences'] ?? []));
|
||||
$absDates = array_map(fn ($d) => substr((string) $d, 0, 10), (array) ($v['absences'] ?? []));
|
||||
$lateDates = array_map(fn ($d) => substr((string) $d, 0, 10), (array) ($v['lates'] ?? []));
|
||||
|
||||
if (! empty($absDates)) {
|
||||
if (!empty($absDates)) {
|
||||
$allDates = array_merge($allDates, $absDates);
|
||||
}
|
||||
if (! empty($lateDates)) {
|
||||
if (!empty($lateDates)) {
|
||||
$allDates = array_merge($allDates, $lateDates);
|
||||
}
|
||||
if (empty($allDates)) {
|
||||
@@ -305,29 +303,29 @@ class AttendanceNotificationWorkflowService
|
||||
|
||||
return [
|
||||
'success' => true,
|
||||
'sent' => $sent,
|
||||
'sent' => $sent,
|
||||
'skipped' => $skipped,
|
||||
'errors' => $errors,
|
||||
'errors' => $errors,
|
||||
'details' => $details,
|
||||
];
|
||||
}
|
||||
|
||||
public function sendEmailManual(array $data, array $violationDates = []): array
|
||||
{
|
||||
$sid = (int) $data['student_id'];
|
||||
$to = trim((string) $data['to']);
|
||||
$subject = (string) $data['subject'];
|
||||
$sid = (int) $data['student_id'];
|
||||
$to = trim((string) $data['to']);
|
||||
$subject = (string) $data['subject'];
|
||||
$bodyInput = (string) $data['body_html'];
|
||||
$code = (string) $data['code'];
|
||||
$variant = (string) ($data['variant'] ?? 'default');
|
||||
$code = (string) $data['code'];
|
||||
$variant = (string) ($data['variant'] ?? 'default');
|
||||
|
||||
$ymdRaw = (string) ($data['incident_date'] ?? '');
|
||||
$ymd = $ymdRaw !== '' ? substr($ymdRaw, 0, 10) : '';
|
||||
$ymd = $ymdRaw !== '' ? substr($ymdRaw, 0, 10) : '';
|
||||
|
||||
$safeHtmlBody = $this->emailComposerService->normalizeBodyToHtml($bodyInput);
|
||||
$wrapped = $this->emailComposerService->renderWithEmailLayout($subject, $safeHtmlBody);
|
||||
|
||||
$semester = $this->semester ?: (string) ($this->configModel->getConfig('semester') ?? '');
|
||||
$semester = $this->semester ?: (string) ($this->configModel->getConfig('semester') ?? '');
|
||||
$schoolYear = $this->schoolYear ?: (string) ($this->configModel->getConfig('school_year') ?? '');
|
||||
|
||||
try {
|
||||
@@ -356,7 +354,7 @@ class AttendanceNotificationWorkflowService
|
||||
$to2 = trim((string) ($sec['email'] ?? ''));
|
||||
|
||||
if ($to2 !== '' && strcasecmp($to2, $to) !== 0) {
|
||||
if (! $this->notificationLogService->notificationAlreadySent($sid, $code, $ymd, 'email', $to2)) {
|
||||
if (!$this->notificationLogService->notificationAlreadySent($sid, $code, $ymd, 'email', $to2)) {
|
||||
$ok2 = $this->attendanceMailerService->send($to2, $subject, $wrapped);
|
||||
|
||||
$this->notificationLogService->logNotification(
|
||||
@@ -378,11 +376,11 @@ class AttendanceNotificationWorkflowService
|
||||
}
|
||||
}
|
||||
|
||||
if (! $anyOk) {
|
||||
if (!$anyOk) {
|
||||
return [
|
||||
'success' => false,
|
||||
'message' => 'Failed to send email.',
|
||||
'status' => 500,
|
||||
'status' => 500,
|
||||
];
|
||||
}
|
||||
|
||||
@@ -396,7 +394,7 @@ class AttendanceNotificationWorkflowService
|
||||
);
|
||||
}
|
||||
|
||||
if (! empty($violationDates) && method_exists($this->attendanceDataModel, 'markReportedAndNotified')) {
|
||||
if (!empty($violationDates) && method_exists($this->attendanceDataModel, 'markReportedAndNotified')) {
|
||||
$this->attendanceDataModel->markReportedAndNotified(
|
||||
$sid,
|
||||
$violationDates,
|
||||
@@ -408,12 +406,12 @@ class AttendanceNotificationWorkflowService
|
||||
return [
|
||||
'success' => true,
|
||||
'message' => 'Email sent successfully.',
|
||||
'data' => [
|
||||
'student_id' => $sid,
|
||||
'to' => $to,
|
||||
'secondary_to' => $to2 ?? null,
|
||||
'subject' => $subject,
|
||||
'variant' => $variant,
|
||||
'data' => [
|
||||
'student_id' => $sid,
|
||||
'to' => $to,
|
||||
'secondary_to' => $to2 ?? null,
|
||||
'subject' => $subject,
|
||||
'variant' => $variant,
|
||||
'incident_date' => $ymd,
|
||||
],
|
||||
];
|
||||
@@ -433,19 +431,19 @@ class AttendanceNotificationWorkflowService
|
||||
|
||||
return [
|
||||
'success' => false,
|
||||
'message' => 'Error: '.$e->getMessage(),
|
||||
'status' => 500,
|
||||
'message' => 'Error: ' . $e->getMessage(),
|
||||
'status' => 500,
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
public function saveNotificationNote(array $data): array
|
||||
{
|
||||
$sid = (int) $data['student_id'];
|
||||
$code = (string) $data['code'];
|
||||
$note = trim((string) $data['note']);
|
||||
$sid = (int) $data['student_id'];
|
||||
$code = (string) $data['code'];
|
||||
$note = trim((string) $data['note']);
|
||||
$ymdRaw = (string) ($data['incident_date'] ?? '');
|
||||
$ymd = $ymdRaw !== '' ? substr($ymdRaw, 0, 10) : '';
|
||||
$ymd = $ymdRaw !== '' ? substr($ymdRaw, 0, 10) : '';
|
||||
|
||||
try {
|
||||
$day = $ymd !== '' ? $ymd : date('Y-m-d');
|
||||
@@ -457,11 +455,11 @@ class AttendanceNotificationWorkflowService
|
||||
->where('school_year', $this->schoolYear)
|
||||
->where('date', '>=', $start)
|
||||
->where('date', '<', $end)
|
||||
->where('reason', 'like', '%'.$code.'%')
|
||||
->where('reason', 'like', '%' . $code . '%')
|
||||
->orderByDesc('date')
|
||||
->first();
|
||||
|
||||
if (! $row) {
|
||||
if (!$row) {
|
||||
$row = $this->attendanceTrackingModel->query()
|
||||
->where('student_id', $sid)
|
||||
->where('semester', $this->semester)
|
||||
@@ -472,28 +470,28 @@ class AttendanceNotificationWorkflowService
|
||||
->first();
|
||||
}
|
||||
|
||||
if ($row && ! empty($row->id)) {
|
||||
if ($row && !empty($row->id)) {
|
||||
DB::table($this->attendanceTrackingModel->getTable())
|
||||
->where('id', (int) $row->id)
|
||||
->update([
|
||||
'notif_counter' => DB::raw('COALESCE(notif_counter,0)+1'),
|
||||
'note' => $note,
|
||||
'is_notified' => 1,
|
||||
'updated_at' => now(),
|
||||
'note' => $note,
|
||||
'is_notified' => 1,
|
||||
'updated_at' => now(),
|
||||
]);
|
||||
} else {
|
||||
$this->attendanceTrackingModel->query()->create([
|
||||
'student_id' => $sid,
|
||||
'date' => $start,
|
||||
'is_reported' => 0,
|
||||
'reason' => $code,
|
||||
'is_notified' => 1,
|
||||
'student_id' => $sid,
|
||||
'date' => $start,
|
||||
'is_reported' => 0,
|
||||
'reason' => $code,
|
||||
'is_notified' => 1,
|
||||
'notif_counter' => 1,
|
||||
'semester' => $this->semester,
|
||||
'school_year' => $this->schoolYear,
|
||||
'note' => $note,
|
||||
'created_at' => now(),
|
||||
'updated_at' => now(),
|
||||
'semester' => $this->semester,
|
||||
'school_year' => $this->schoolYear,
|
||||
'note' => $note,
|
||||
'created_at' => now(),
|
||||
'updated_at' => now(),
|
||||
]);
|
||||
}
|
||||
|
||||
@@ -504,9 +502,9 @@ class AttendanceNotificationWorkflowService
|
||||
} catch (Throwable $e) {
|
||||
return [
|
||||
'success' => false,
|
||||
'message' => 'Failed to save note: '.$e->getMessage(),
|
||||
'status' => 500,
|
||||
'message' => 'Failed to save note: ' . $e->getMessage(),
|
||||
'status' => 500,
|
||||
];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user