*/ use HasFactory, Notifiable; /** * Get the attributes that should be cast. * * @return array */ protected function casts(): array { return [ 'tags' => 'array', 'marketing_consent_at' => 'immutable_datetime', 'anonymized_at' => 'immutable_datetime', ]; } /** * @return HasMany */ public function bookings(): HasMany { return $this->hasMany(Booking::class)->latest('arrival'); } /** * @return Attribute */ protected function fullName(): Attribute { return Attribute::get(fn (): string => "{$this->first_name} {$this->last_name}"); } /** * Guests who confirmed (double opt-in) that they want to receive offers by e-mail. * * @param Builder $query * @return Builder */ #[Scope] protected function withMarketingConsent(Builder $query): Builder { return $query->whereNotNull('marketing_consent_at')->whereNull('anonymized_at'); } /** * Removes all personal data (GDPR). Bookings stay for accounting, without personal messages. */ public function anonymize(): void { DB::transaction(function (): void { $this->bookings()->update([ 'message' => null, 'internal_notes' => null, 'offer_message' => null, 'referral_source' => null, 'utm_source' => null, 'utm_medium' => null, 'utm_campaign' => null, ]); Review::query()->whereIn('booking_id', $this->bookings()->select('id'))->update([ 'display_name' => 'Gast', 'private_feedback' => null, ]); $this->forceFill([ 'first_name' => 'Anonymisiert', 'last_name' => "#{$this->id}", 'email' => "anonymisiert-{$this->id}@invalid.local", 'phone' => null, 'street' => null, 'postal_code' => null, 'city' => null, 'notes' => null, 'tags' => null, 'marketing_consent_at' => null, 'anonymized_at' => now(), ])->save(); }); } }