Files
fewo-stommer/app/Actions/AnonymizeGuests.php
T

62 lines
2.6 KiB
PHP

<?php
namespace App\Actions;
use App\Models\Booking;
use App\Models\Guest;
use App\Models\PortalEmail;
use App\Models\ProcessTestRun;
/**
* GDPR storage limitation: anonymises guests whose last stay ended longer ago than the retention period, and
* guests who only ever sent requests/offers that never became binding (shorter period), and removes
* operational extras that are no longer needed (portal e-mail excerpts, marketing attribution).
* Booking records (dates, prices, payments) stay as accounting documents.
*/
class AnonymizeGuests
{
public function handle(): int
{
$cutoff = today()->subYears(config('fewo.guest_retention_years'));
$guests = Guest::query()
->whereNull('anonymized_at')
->whereDate('created_at', '<', $cutoff)
->whereDoesntHave('bookings', fn ($query) => $query->whereDate('departure', '>=', $cutoff))
->whereDoesntHave('portalStays', fn ($query) => $query->withTrashed()->whereDate('ends_on', '>=', $cutoff))
->get();
$inquiryCutoff = now()->subMonths(config('fewo.retention.inquiry_months'));
$inquiriesOnly = Guest::query()
->whereNull('anonymized_at')
->whereDoesntHave('bookings', fn ($query) => $query->whereNotNull('confirmed_at'))
->whereDoesntHave('portalStays', fn ($query) => $query->withTrashed())
->whereDoesntHave('bookings', fn ($query) => $query->where('updated_at', '>=', $inquiryCutoff))
->where('updated_at', '<', $inquiryCutoff)
->get();
$all = $guests->merge($inquiriesOnly)->unique('id');
$all->each->anonymize();
$this->purgeOperationalData();
return $all->count();
}
private function purgeOperationalData(): void
{
// Process tester reports (synthetic data only, decision O3).
ProcessTestRun::query()->where('created_at', '<', now()->subDays(config('fewo.retention.process_test_run_days')))->delete();
PortalEmail::query()
->whereNotNull('excerpt')
->where('received_at', '<', now()->subDays(config('fewo.retention.portal_email_excerpt_days')))
->update(['excerpt' => null]);
Booking::query()
->where('created_at', '<', now()->subMonths(config('fewo.retention.attribution_months')))
->where(fn ($query) => $query->whereNotNull('referral_source')->orWhereNotNull('utm_source')->orWhereNotNull('utm_medium')->orWhereNotNull('utm_campaign'))
->update(['referral_source' => null, 'utm_source' => null, 'utm_medium' => null, 'utm_campaign' => null]);
}
}