Files
portal/app/app/Models/ImpersonationToken.php
T
Дмитрий a4601fe84b phase2(notifications-stage1): NotificationService + new_lead email (P0 этап 1)
Старт closing «Notification delivery» из карты P0. Этап 1/6 плана:
NotificationService + Mailable + интеграция в ProcessWebhookJob::chargeNewLead.

- App\Services\NotificationService — диспетчер 8 событий × 3 каналов
  (inapp/push/email) согласно schema.sql:699 users.notification_preferences.
  Этап 1 реализует только email-канал для new_lead.
- App\Mail\NewLeadNotification + emails/new_lead.blade.php — HTML-письмо
  в Forest-палитре с таблицей phone/contact_name/received_at/deal_id.
- ProcessWebhookJob::chargeNewLead — после ActivityLog вызывает
  notifyNewLead. Throwable от Mail::send проглатывается + Log::warning
  (отказ канала не должен валить транзакцию).
- Pest 11/11 в tests/Feature/Notifications/NewLeadNotificationTest.php:
  email=true получает / email=false не получает / schema-default не шлёт /
  inactive не получает / soft-deleted не получает / другой тенант не
  получает / Биз-19 дубль не дублирует / повторный vid не дублирует /
  balance=0 не шлёт / subject содержит project_name.
- IDE-helper регенерирован (4 модели получили @mixin docblocks).
- PHPStan baseline регенерирован (138 ignore.unmatched схлопнулись).

Pest 280/280 за 31.27 сек (+11 от 269, 1029 assertions).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-09 11:03:43 +03:00

90 lines
2.4 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Support\Carbon;
/**
* Токен impersonation (по ТЗ §22.7 / Ю-1).
*
* SaaS-admin запрашивает «войти как клиент» с reason (≥30 chars). Генерируется
* 6-значный код, bcrypt-хеш сохраняется тут, plain отправляется на
* `tenant.contact_email`. TTL 15 мин, 5 попыток ввода, далее invalidate.
*
* Без RLS — таблица доступна только из crm_admin_user (BYPASSRLS).
*
* @property int $id
* @property int $tenant_id
* @property int $requested_by
* @property string $code_hash
* @property string $reason
* @property string $sent_to_email
* @property Carbon $expires_at
* @property Carbon|null $used_at
* @property int|null $session_id
* @property Carbon|null $session_ended_at
* @property int $failed_attempts
* @property Carbon|null $invalidated_at
* @property int|null $second_approver_id
* @property Carbon|null $second_approval_at
* @property Carbon $created_at
*
* @mixin IdeHelperImpersonationToken
*/
class ImpersonationToken extends Model
{
/** schema-таблица не имеет updated_at. */
public const UPDATED_AT = null;
protected $fillable = [
'tenant_id',
'requested_by',
'code_hash',
'reason',
'sent_to_email',
'expires_at',
'used_at',
'session_id',
'session_ended_at',
'failed_attempts',
'invalidated_at',
'second_approver_id',
'second_approval_at',
];
protected function casts(): array
{
return [
'expires_at' => 'datetime',
'used_at' => 'datetime',
'session_ended_at' => 'datetime',
'invalidated_at' => 'datetime',
'second_approval_at' => 'datetime',
'created_at' => 'datetime',
];
}
public function isExpired(): bool
{
return $this->expires_at->isPast();
}
public function isUsable(): bool
{
return $this->used_at === null
&& $this->invalidated_at === null
&& $this->failed_attempts < 5
&& ! $this->isExpired();
}
/** @return BelongsTo<Tenant, $this> */
public function tenant(): BelongsTo
{
return $this->belongsTo(Tenant::class);
}
}