Files
preregister/app/Models/Subscriber.php
bert.hausmans d3abdb7ed9 feat: add Weeztix OAuth, coupon codes, and Mailwizz mapping
Implement Weeztix integration per documentation: database config and
subscriber coupon_code, OAuth redirect/callback, admin setup UI with
company/coupon selection via AJAX, synchronous coupon creation on public
subscribe with duplicate and rate-limit handling, Mailwizz field mapping
for coupon codes, subscriber table and CSV export, and connection hint
on the pages list.

Made-with: Cursor
2026-04-04 14:52:41 +02:00

104 lines
2.4 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Models;
use App\Services\PhoneNumberNormalizer;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
class Subscriber extends Model
{
use HasFactory;
protected $fillable = [
'preregistration_page_id',
'first_name',
'last_name',
'email',
'phone',
'synced_to_mailwizz',
'synced_at',
'coupon_code',
];
protected function casts(): array
{
return [
'synced_to_mailwizz' => 'boolean',
'synced_at' => 'datetime',
];
}
public function preregistrationPage(): BelongsTo
{
return $this->belongsTo(PreregistrationPage::class);
}
/**
* @param string|null $value
*/
public function setPhoneAttribute(mixed $value): void
{
if ($value === null) {
$this->attributes['phone'] = null;
return;
}
if (! is_string($value)) {
$this->attributes['phone'] = null;
return;
}
$trimmed = trim($value);
if ($trimmed === '') {
$this->attributes['phone'] = null;
return;
}
$normalized = app(PhoneNumberNormalizer::class)->normalizeToE164($trimmed);
$this->attributes['phone'] = $normalized;
}
/**
* Phones are stored as E.164 (e.g. +31612345678). Legacy rows may still be digits-only.
*/
public function phoneDisplay(): ?string
{
$phone = $this->phone;
if ($phone === null || $phone === '') {
return null;
}
$p = (string) $phone;
if (str_starts_with($p, '+')) {
return $p;
}
return preg_match('/^\d{8,15}$/', $p) === 1 ? '+'.$p : $p;
}
public function scopeSearch(Builder $query, ?string $term): Builder
{
if ($term === null || $term === '') {
return $query;
}
$like = '%'.$term.'%';
return $query->where(function (Builder $q) use ($like): void {
$q->where('first_name', 'like', $like)
->orWhere('last_name', 'like', $like)
->orWhere('email', 'like', $like)
->orWhere('phone', 'like', $like)
->orWhere('coupon_code', 'like', $like);
});
}
}