reconstruction of the project
This commit is contained in:
@@ -0,0 +1,101 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\AttendanceTracking;
|
||||
|
||||
use App\Models\ParentNotification;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Throwable;
|
||||
|
||||
class AttendanceNotificationLogService
|
||||
{
|
||||
public function __construct(
|
||||
protected ParentNotification $notificationModel
|
||||
) {
|
||||
}
|
||||
|
||||
public function notificationAlreadySent(
|
||||
int $studentId,
|
||||
string $code,
|
||||
string $ymd,
|
||||
string $channel = 'email',
|
||||
?string $to = null
|
||||
): bool {
|
||||
try {
|
||||
if (method_exists($this->notificationModel, 'hasSent')) {
|
||||
return (bool) $this->notificationModel->hasSent($studentId, $code, $ymd, $channel, $to);
|
||||
}
|
||||
|
||||
$query = $this->notificationModel->query()
|
||||
->where('student_id', $studentId)
|
||||
->where('code', $code)
|
||||
->where('incident_date', $ymd)
|
||||
->where('channel', $channel);
|
||||
|
||||
if (!empty($to)) {
|
||||
$query->where('to_address', $to);
|
||||
}
|
||||
|
||||
return $query->exists();
|
||||
} catch (Throwable $e) {
|
||||
Log::debug('notificationAlreadySent(): ' . $e->getMessage());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public function logNotification(
|
||||
int $studentId,
|
||||
string $code,
|
||||
string $ymd,
|
||||
string $channel,
|
||||
?string $to,
|
||||
string $subject,
|
||||
string $status,
|
||||
string $response = '',
|
||||
?string $semester = null,
|
||||
?string $schoolYear = null
|
||||
): void {
|
||||
try {
|
||||
$where = [
|
||||
'student_id' => $studentId,
|
||||
'code' => $code,
|
||||
'incident_date' => $ymd,
|
||||
'channel' => $channel,
|
||||
];
|
||||
|
||||
if (!empty($to)) {
|
||||
$where['to_address'] = $to;
|
||||
}
|
||||
|
||||
$existing = $this->notificationModel->query()
|
||||
->where($where)
|
||||
->orderByDesc('id')
|
||||
->first();
|
||||
|
||||
if ($existing && !empty($existing->id)) {
|
||||
$existing->update([
|
||||
'subject' => $subject,
|
||||
'status' => $status,
|
||||
'response' => $response,
|
||||
'semester' => $semester,
|
||||
'school_year' => $schoolYear,
|
||||
'updated_at' => now(),
|
||||
]);
|
||||
} else {
|
||||
$this->notificationModel->query()->create([
|
||||
'student_id' => $studentId,
|
||||
'code' => $code,
|
||||
'incident_date' => $ymd,
|
||||
'channel' => $channel,
|
||||
'to_address' => $to,
|
||||
'subject' => $subject,
|
||||
'status' => $status,
|
||||
'response' => $response,
|
||||
'semester' => $semester,
|
||||
'school_year' => $schoolYear,
|
||||
]);
|
||||
}
|
||||
} catch (Throwable $e) {
|
||||
Log::debug('logNotification(): ' . $e->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user