schneespur/release/schneespur-1.0.2/app/Services/NotificationLogService.php
Michael 7288b93500 Release v1.0.2: diagnostic infrastructure core
Add neutral diagnostic framework for future reporting modules:
- DiagnosticReporterInterface, Registry, Manager, PayloadSanitizer
- Laravel exception hook in bootstrap/app.php
- Module permission declarations (requires_permissions in module.json)
- Core diagnostic report points (module boot/install/update failures)
- Module documentation update (moduldoku.md)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-05-18 16:54:11 +00:00

105 lines
3.5 KiB
PHP

<?php
namespace App\Services;
use App\Models\Customer;
use App\Models\Job;
use App\Models\NotificationLog;
use Carbon\Carbon;
class NotificationLogService
{
public function logSent(Job $job, string $type, string $recipient, array $metadata = []): NotificationLog
{
return $job->notificationLogs()->create([
'channel' => 'email',
'type' => $type,
'recipient' => $recipient,
'status' => 'sent',
'metadata' => $metadata ?: null,
]);
}
public function logFailed(Job $job, string $type, string $recipient, string $error, array $metadata = []): NotificationLog
{
return $job->notificationLogs()->create([
'channel' => 'email',
'type' => $type,
'recipient' => $recipient,
'status' => 'failed',
'error_message' => $error,
'metadata' => $metadata ?: null,
]);
}
public function logSkipped(Job $job, string $type, string $status, array $metadata = []): NotificationLog
{
return $job->notificationLogs()->create([
'channel' => 'email',
'type' => $type,
'status' => $status,
'metadata' => $metadata ?: null,
]);
}
public function anonymizeForCustomer(Customer $customer): int
{
$jobCount = NotificationLog::query()
->where('notifiable_type', Job::class)
->whereIn('notifiable_id', function ($query) use ($customer) {
$query->select('id')
->from('service_jobs')
->where('customer_id', $customer->id);
})
->update(['recipient' => null]);
$customerCount = NotificationLog::query()
->where('notifiable_type', Customer::class)
->where('notifiable_id', $customer->id)
->update(['recipient' => null]);
return $jobCount + $customerCount;
}
public function hasBeenNotified(Job $job, string $type): bool
{
return $job->notificationLogs()
->where('type', $type)
->where('status', 'sent')
->exists();
}
public function logSentForCustomer(Customer $customer, string $type, string $recipient, array $metadata = []): NotificationLog
{
return $customer->notificationLogs()->create([
'channel' => 'email',
'type' => $type,
'recipient' => $recipient,
'status' => 'sent',
'metadata' => $metadata ?: null,
]);
}
public function logFailedForCustomer(Customer $customer, string $type, string $recipient, string $error, array $metadata = []): NotificationLog
{
return $customer->notificationLogs()->create([
'channel' => 'email',
'type' => $type,
'recipient' => $recipient,
'status' => 'failed',
'error_message' => $error,
'metadata' => $metadata ?: null,
]);
}
public function wasRecentlySentToCustomer(Customer $customer, string $type, Carbon $from, Carbon $to, int $minutes = 5): bool
{
return $customer->notificationLogs()
->where('type', $type)
->where('status', 'sent')
->where('created_at', '>=', now()->subMinutes($minutes))
->whereJsonContains('metadata->period_from', $from->toDateString())
->whereJsonContains('metadata->period_to', $to->toDateString())
->exists();
}
}