Files
fewo-stommer/app/Process/Tester/SandboxSnapshot.php
T
LarsandClaude Sonnet 5.5 e95ba4f986 Schablonenkomposition: Basisprozess external.booking mit Platz für Finanz- und Dokumentenschablonen
Vier Schablonen (Eigenes/Portal-Inkasso, Eigene/Externe Vertragsdokumentation) in
app/Process/Templates, TemplateComposer prüft Ergebnisse, Verantwortlichkeiten, Ausschlüsse
und harte Modulabhängigkeiten. Der Plan friert die Schablonenversionen ein. A3–A5 bleiben
als Voreinstellungen; die vierte Kombination (eigenes Inkasso + externe Unterlagen) ist
darstellbar. Konfigurator-Logik und ConfigureChannelProcess kennen die Schablonenwahl.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-05 10:48:49 +01:00

143 lines
6.8 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
namespace App\Process\Tester;
use App\Enums\IntakeKind;
use App\Models\Channel;
use App\Models\Tenant;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use Throwable;
/**
* What a tester run starts from (docs/Prozessarchitektur/Architektur/04 §3.2): read – never written – from the real
* database by the landlord's request, as plain values. Only the landlord's own setup: the tenant, its websites,
* properties with seasons, offers, extras, templates and texts, the channels (the tested one with the unsaved draft)
* and the calendar as anonymous blocks. Never guests, bookings, messages, facts, access data or keys: the run creates
* its cases synthetically. Encrypted values are decrypted here and encrypted again with the run's own key.
*/
final class SandboxSnapshot
{
/** Tables copied, in insert order. */
public const TABLES = ['tenants', 'sites', 'site_domains', 'properties', 'seasons', 'special_offers', 'extras', 'cancellation_policies', 'contract_templates', 'contract_annexes', 'content_blocks', 'channels', 'blocked_periods'];
/** Columns never copied (access data, tokens, raw portal data). */
public const DROPPED = [
'tenants' => ['mailbox_password'],
'channels' => ['previous_export_token', 'export_snapshot', 'ical_analysis'],
'blocked_periods' => ['ical_properties', 'ical_raw', 'ical_interpretation', 'linked_booking_id', 'guest_name'],
];
/** Encrypted columns that are carried as plain values and encrypted again in the run. */
public const ENCRYPTED = ['tenants' => ['bank_account_holder', 'bank_iban', 'bank_bic', 'bank_name', 'tax_note']];
/**
* @param array<string, mixed> $draft model, variant, steps, settings of the tested channel and intake kind
* @return array{tenant_id: int, channel_id: int, intake: string, tables: array<string, list<array<string, mixed>>>, encrypted: array<string, list<string>>}
*/
public static function build(Channel $channel, IntakeKind $intake, array $draft): array
{
$tenant = Tenant::query()->findOrFail($channel->tenant_id);
$propertyIds = DB::table('properties')->where('tenant_id', $tenant->id)->pluck('id')->all();
$tables = [];
foreach (self::TABLES as $table) {
if (! Schema::hasTable($table)) {
continue;
}
$query = DB::table($table);
$query = match (true) {
$table === 'tenants' => $query->where('id', $tenant->id),
Schema::hasColumn($table, 'tenant_id') => $query->where('tenant_id', $tenant->id),
Schema::hasColumn($table, 'property_id') => $query->whereIn('property_id', $propertyIds),
default => null,
};
if ($query === null) {
continue;
}
$tables[$table] = $query->orderBy('id')->get()->map(fn (object $row): array => self::clean($table, (array) $row))->all();
}
// The tested channel carries the draft (also unsaved) – and creates cases from portal mails in the run.
foreach ($tables['channels'] ?? [] as $index => $row) {
if ((int) $row['id'] === $channel->id) {
$overrides = (array) json_decode((string) ($row['process_overrides'] ?? '[]'), true);
$overrides[$intake->value] = [...array_filter((array) ($draft['settings'] ?? []), fn (mixed $value): bool => $value !== null && $value !== ''), 'model' => $draft['model'] ?? null, 'variant' => $draft['variant'] ?? 'standard', 'steps' => (array) ($draft['steps'] ?? []), 'templates' => (array) ($draft['templates'] ?? [])];
$tables['channels'][$index]['process_overrides'] = json_encode($overrides);
$tables['channels'][$index]['case_intake'] = 'create';
// In the run the tested portal is active, so its (synthetic) booking mails are recognised.
$tables['channels'][$index]['is_active'] = 1;
$tables['channels'][$index]['status'] = 'active';
$settings = (array) json_decode((string) ($row['settings'] ?? '[]'), true);
$settings['email_senders'] = filled($settings['email_senders'] ?? null) ? $settings['email_senders'] : 'portal.invalid';
$tables['channels'][$index]['settings'] = json_encode($settings);
}
}
return ['tenant_id' => $tenant->id, 'channel_id' => $channel->id, 'intake' => $intake->value, 'tables' => $tables, 'encrypted' => self::ENCRYPTED];
}
/**
* @param array<string, mixed> $row
* @return array<string, mixed>
*/
private static function clean(string $table, array $row): array
{
foreach (self::DROPPED[$table] ?? [] as $column) {
if (array_key_exists($column, $row)) {
$row[$column] = null;
}
}
foreach (self::ENCRYPTED[$table] ?? [] as $column) {
if (filled($row[$column] ?? null)) {
try {
$row[$column] = Crypt::decryptString((string) $row[$column]);
} catch (Throwable) {
$row[$column] = null;
}
}
}
if ($table === 'channels' && array_key_exists('export_token', $row)) {
// The real export link is never part of a run: a random one of no use outside it.
$row['export_token'] = bin2hex(random_bytes(20));
}
if ($table === 'tenants') {
// The run is always in test operation and writes only to its own test address.
$row['operating_mode'] = 'test';
$row['test_recipient'] = 'tester@sandbox.invalid';
}
return $row;
}
/**
* Writes the snapshot into the run's own database (inside the tester process only).
*
* @param array{tables: array<string, list<array<string, mixed>>>, encrypted: array<string, list<string>>} $snapshot
*/
public static function seed(array $snapshot): void
{
if (config('database.default') !== 'sandbox') {
throw SandboxViolation::record('database', 'Einspielen nur in die Testdatenbank.');
}
Schema::disableForeignKeyConstraints();
foreach ($snapshot['tables'] as $table => $rows) {
if (! Schema::hasTable($table)) {
continue;
}
$columns = array_flip(Schema::getColumnListing($table));
foreach ($rows as $row) {
foreach ($snapshot['encrypted'][$table] ?? [] as $column) {
if (filled($row[$column] ?? null)) {
$row[$column] = Crypt::encryptString((string) $row[$column]);
}
}
DB::table($table)->insert(array_intersect_key($row, $columns));
}
}
Schema::enableForeignKeyConstraints();
}
}