U-D/U-E/O5: Prozess-Tester im eigenen Prozess (standardmäßig gesperrt, 15 Negativtests, statischer Wächter), Szenarien und Bericht, Kanalereignisse (API-Eingang ohne Umbau von iCal/Mail), Abschluss nach finanzieller Klärung, Löschfrist 30 Tage
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
c255783cec
commit
0689c0a591
68 files changed
+3350
-35
No files matched your search
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
namespace App\Channels\Contracts;
|
||||
|
||||
use App\Channels\Events\ChannelEvent;
|
||||
use App\Models\Channel;
|
||||
use Carbon\CarbonImmutable;
|
||||
|
||||
/**
|
||||
* Capability of a connector whose portal offers an interface for bookings, cancellations, changes and amounts
|
||||
* (Architektur V2, 02 §8.4). The events go to ChannelEventHandler; the connector itself decides nothing. Network access
|
||||
* only through the Http facade (SideEffectPathsTest), so the process tester can replace it.
|
||||
*/
|
||||
interface DeliversEvents
|
||||
{
|
||||
/**
|
||||
* @return list<ChannelEvent>
|
||||
*/
|
||||
public function fetchEvents(Channel $channel, ?CarbonImmutable $since): array;
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace App\Channels\Contracts;
|
||||
|
||||
use App\Channels\Events\ChannelEvent;
|
||||
use App\Models\Channel;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
/**
|
||||
* Capability of a connector whose portal pushes events (webhook). The connector verifies the signature (authenticity
|
||||
* of the origin) and translates the payload; the business decision stays with ChannelEventHandler.
|
||||
*/
|
||||
interface ReceivesWebhooks
|
||||
{
|
||||
/**
|
||||
* @return list<ChannelEvent> empty when the signature is not valid
|
||||
*/
|
||||
public function receive(Channel $channel, Request $request): array;
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
<?php
|
||||
|
||||
namespace App\Channels\Events;
|
||||
|
||||
/**
|
||||
* The portal reports a cancellation of a known booking: a task on the case, never a cancellation, refund or fee (E-019).
|
||||
*/
|
||||
final readonly class BookingCancelledByChannel extends ChannelEvent
|
||||
{
|
||||
public function __construct(Provenance $provenance, public ?string $note = null)
|
||||
{
|
||||
parent::__construct($provenance);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
namespace App\Channels\Events;
|
||||
|
||||
use Carbon\CarbonImmutable;
|
||||
|
||||
/**
|
||||
* The portal reports other dates or guests of a known booking: a task with the comparison, never a change of the booking.
|
||||
*/
|
||||
final readonly class BookingChangedByChannel extends ChannelEvent
|
||||
{
|
||||
public function __construct(Provenance $provenance, public ?CarbonImmutable $arrival = null, public ?CarbonImmutable $departure = null, public ?int $guests = null)
|
||||
{
|
||||
parent::__construct($provenance);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
namespace App\Channels\Events;
|
||||
|
||||
/**
|
||||
* A normalised business event of a portal, independent of its transport (Architektur V2, K4/K10). Future API or
|
||||
* webhook adapters deliver these; ChannelEventHandler maps them onto the existing intake, so duplicates, origin and the
|
||||
* external binding are judged in one place for every transport.
|
||||
*/
|
||||
abstract readonly class ChannelEvent
|
||||
{
|
||||
public function __construct(public Provenance $provenance) {}
|
||||
}
|
||||
@@ -0,0 +1,113 @@
|
||||
<?php
|
||||
|
||||
namespace App\Channels\Events;
|
||||
|
||||
use App\Actions\RecordPortalAmounts;
|
||||
use App\Channels\Intake\IntakeDecision;
|
||||
use App\Channels\Intake\PortalIntake;
|
||||
use App\Enums\IntakeOutcome;
|
||||
use App\Models\BlockedPeriod;
|
||||
use App\Models\Booking;
|
||||
use App\Models\IntakeObservation;
|
||||
use App\Process\CaseLog;
|
||||
use App\Support\Money;
|
||||
use App\Tenancy\TenantContext;
|
||||
use InvalidArgumentException;
|
||||
|
||||
/**
|
||||
* The one entry of channel events (docs/Prozessarchitektur/Architektur/02 §8.3–8.4): maps each normalised event onto
|
||||
* the existing intake, so iCal, mail and future APIs are judged by the same rules – duplicates and certainty in
|
||||
* PortalIntake, amounts in RecordPortalAmounts, reports about known cases as tasks. Contains no decision rules of its
|
||||
* own and never changes a binding booking (stage M4 needs the landlord's action).
|
||||
*/
|
||||
final class ChannelEventHandler
|
||||
{
|
||||
public function __construct(private PortalIntake $intake, private RecordPortalAmounts $amounts) {}
|
||||
|
||||
public function handle(ChannelEvent $event): ?IntakeDecision
|
||||
{
|
||||
$channel = $event->provenance->channel;
|
||||
if (! $channel->isPortal() || $channel->tenant_id === null) {
|
||||
throw new InvalidArgumentException('Kanalereignisse kommen nur von Portalen.');
|
||||
}
|
||||
|
||||
return app(TenantContext::class)->run($channel->tenant, fn (): ?IntakeDecision => match (true) {
|
||||
$event instanceof IncomingBooking => $this->booking($event),
|
||||
$event instanceof BookingCancelledByChannel => $this->report($event, IntakeOutcome::CancellationSignal, $channel->name.' meldet über die Schnittstelle eine Stornierung'.($event->note ? ' ('.$event->note.')' : '').'. Bitte prüfen Sie die Buchung und die nächsten Schritte.'),
|
||||
$event instanceof BookingChangedByChannel => $this->report($event, IntakeOutcome::ChangeSignal, $channel->name.' meldet über die Schnittstelle eine Änderung'.($event->arrival && $event->departure ? ': '.$event->arrival->format('d.m.').'–'.$event->departure->format('d.m.Y') : '').($event->guests ? ', '.$event->guests.' Personen' : '').'. Die Buchung bleibt unverändert, bis Sie entscheiden.'),
|
||||
$event instanceof PaymentReported => $this->payment($event),
|
||||
$event instanceof PayoutReported => $this->payout($event),
|
||||
default => throw new InvalidArgumentException('Unbekanntes Kanalereignis '.$event::class.'.'),
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* A booking binding at the portal: blocked like a booking e-mail – source api, reference in the identifier – and judged
|
||||
* by PortalIntake – the same case, never a second one; a case only when certain.
|
||||
*/
|
||||
private function booking(IncomingBooking $event): IntakeDecision
|
||||
{
|
||||
$provenance = $event->provenance;
|
||||
$reference = $provenance->externalReference;
|
||||
// The reference is the identity of the block (as for booking mails); without one it is never taken as a reference.
|
||||
$uid = $reference !== null ? 'api:'.$reference : 'api-event:'.$provenance->evidenceRef;
|
||||
$block = $provenance->channel->blockedPeriods()->withTrashed()->where('source', 'api')->where('external_uid', $uid)->first()
|
||||
?? new BlockedPeriod(['property_id' => $provenance->channel->property_id, 'source' => 'api', 'reason' => $provenance->channel->name, 'external_uid' => $uid]);
|
||||
$block->fill(['starts_on' => $event->arrival->toDateString(), 'ends_on' => $event->departure->toDateString()]);
|
||||
if ($block->trashed()) {
|
||||
$block->restore();
|
||||
}
|
||||
$provenance->channel->blockedPeriods()->save($block);
|
||||
|
||||
return $this->intake->process($block->fresh(), IntakeObservation::SOURCE_API);
|
||||
}
|
||||
|
||||
private function report(BookingCancelledByChannel|BookingChangedByChannel $event, IntakeOutcome $outcome, string $message): ?IntakeDecision
|
||||
{
|
||||
$case = $this->case($event);
|
||||
if ($case === null) {
|
||||
return new IntakeDecision(IntakeOutcome::Clarify, ['Keinem gebuchten Vorgang sicher zuzuordnen.'], reference: $event->provenance->externalReference);
|
||||
}
|
||||
$this->intake->reportToCase($event->provenance->channel, $case, $outcome, $message, 'api:'.$event->provenance->evidenceRef, IntakeObservation::SOURCE_API);
|
||||
|
||||
return new IntakeDecision($outcome, [$message], $case, reference: $event->provenance->externalReference);
|
||||
}
|
||||
|
||||
private function payment(PaymentReported $event): ?IntakeDecision
|
||||
{
|
||||
$case = $this->case($event);
|
||||
if ($case === null) {
|
||||
return new IntakeDecision(IntakeOutcome::Clarify, ['Zahlungsmeldung keinem gebuchten Vorgang sicher zuzuordnen.'], reference: $event->provenance->externalReference);
|
||||
}
|
||||
$this->amounts->handle($case, null, [], [['amount' => $event->amount, 'paid_on' => $event->paidOn->toDateString()]], $event->provenance->channel->name);
|
||||
|
||||
return new IntakeDecision(IntakeOutcome::Link, ['Zahlung des Gastes laut Portal erfasst (kein Eingang auf unserem Konto).'], $case);
|
||||
}
|
||||
|
||||
private function payout(PayoutReported $event): ?IntakeDecision
|
||||
{
|
||||
$case = $this->case($event);
|
||||
if ($case === null) {
|
||||
return new IntakeDecision(IntakeOutcome::Clarify, ['Auszahlungsmeldung keinem gebuchten Vorgang sicher zuzuordnen.'], reference: $event->provenance->externalReference);
|
||||
}
|
||||
if ($event->executed) {
|
||||
// Evidence level "laut Portal erledigt" – the receipt on our account is recorded by the landlord (O5).
|
||||
CaseLog::record($case, 'evidence', 'Auszahlung laut '.$event->provenance->channel->name.' ausgeführt: '.Money::format($event->amount).' am '.$event->paidOn->format('d.m.Y').' – Eingang auf unserem Konto noch nicht erfasst.', data: ['module' => 'portal.payout', 'level' => 'stated_by_portal', 'source' => $event->provenance->transport, 'ref' => $event->provenance->evidenceRef], actor: $event->provenance->channel->name);
|
||||
} else {
|
||||
$this->amounts->handle($case, null, [['kind' => 'payout_announced', 'amount' => $event->amount, 'due_on' => $event->paidOn->toDateString()]], [], $event->provenance->channel->name);
|
||||
}
|
||||
|
||||
return new IntakeDecision(IntakeOutcome::Link, ['Auszahlung laut Portal vermerkt.'], $case);
|
||||
}
|
||||
|
||||
/**
|
||||
* The binding case of the event: by the portal's booking reference only (no merging by dates for reports).
|
||||
*/
|
||||
private function case(ChannelEvent $event): ?Booking
|
||||
{
|
||||
$reference = $event->provenance->externalReference;
|
||||
$case = $reference === null ? null : $this->intake->caseByReference($event->provenance->channel, $reference);
|
||||
|
||||
return $case !== null && $case->isBinding() ? $case : null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
namespace App\Channels\Events;
|
||||
|
||||
use Carbon\CarbonImmutable;
|
||||
|
||||
/**
|
||||
* A booking binding at the portal (M1/M2): its dates and reference; becomes a case only through PortalIntake's certainty rules.
|
||||
*/
|
||||
final readonly class IncomingBooking extends ChannelEvent
|
||||
{
|
||||
public function __construct(Provenance $provenance, public CarbonImmutable $arrival, public CarbonImmutable $departure, public ?int $guests = null, public ?int $total = null)
|
||||
{
|
||||
parent::__construct($provenance);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
namespace App\Channels\Events;
|
||||
|
||||
use Carbon\CarbonImmutable;
|
||||
|
||||
/**
|
||||
* The portal reports a payment of the guest to the portal: recorded "laut Portal" (GuestViaPortal), never as money on our account.
|
||||
*/
|
||||
final readonly class PaymentReported extends ChannelEvent
|
||||
{
|
||||
public function __construct(Provenance $provenance, public int $amount, public CarbonImmutable $paidOn)
|
||||
{
|
||||
parent::__construct($provenance);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
namespace App\Channels\Events;
|
||||
|
||||
use Carbon\CarbonImmutable;
|
||||
|
||||
/**
|
||||
* The portal announces or reports a payout to us: "laut Portal" only – a received payout is recorded by the landlord (decision O5).
|
||||
*/
|
||||
final readonly class PayoutReported extends ChannelEvent
|
||||
{
|
||||
public function __construct(Provenance $provenance, public int $amount, public CarbonImmutable $paidOn, public bool $executed = false)
|
||||
{
|
||||
parent::__construct($provenance);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
namespace App\Channels\Events;
|
||||
|
||||
use App\Models\Channel;
|
||||
use Carbon\CarbonImmutable;
|
||||
|
||||
/**
|
||||
* Where a channel event came from (docs/Prozessarchitektur/Architektur/02 §8.2): channel, transport and whether its
|
||||
* origin is technically authenticated (a signed webhook, an API call with credentials). It says nothing about whether a
|
||||
* business action really happened – that is evidence, which only the landlord or a checked receipt provides (M3).
|
||||
*/
|
||||
final readonly class Provenance
|
||||
{
|
||||
public const AUTHENTICATED = 'authenticated';
|
||||
|
||||
public const UNAUTHENTICATED = 'unauthenticated';
|
||||
|
||||
public function __construct(
|
||||
public Channel $channel,
|
||||
public string $transport,
|
||||
public string $authenticity,
|
||||
public ?string $externalReference,
|
||||
public string $evidenceRef,
|
||||
public CarbonImmutable $receivedAt,
|
||||
) {}
|
||||
|
||||
public function isAuthenticated(): bool
|
||||
{
|
||||
return $this->authenticity === self::AUTHENTICATED;
|
||||
}
|
||||
}
|
||||
@@ -65,7 +65,7 @@ public function evaluate(BlockedPeriod $block): IntakeDecision
|
||||
$channel = $block->channel;
|
||||
$reference = self::reference($block);
|
||||
|
||||
if ($channel === null || ! in_array($block->source, ['channel', 'email'], true)) {
|
||||
if ($channel === null || ! in_array($block->source, ['channel', 'email', 'api'], true)) {
|
||||
return new IntakeDecision(IntakeOutcome::Ignore, ['Kein importierter Portaleintrag.'], reference: $reference);
|
||||
}
|
||||
if ($block->trashed()) {
|
||||
@@ -246,6 +246,15 @@ public function cancellationMail(Channel $channel, ParsedPortalMail $parsed, Por
|
||||
return $case;
|
||||
}
|
||||
|
||||
/**
|
||||
* A report of the portal about a known case (channel events, Architektur V2: cancellation or change via an API):
|
||||
* the case gets a task and a history entry, nothing about the booking changes (E-019, stage M2 of 02 §8.2).
|
||||
*/
|
||||
public function reportToCase(Channel $channel, Booking $case, IntakeOutcome $outcome, string $message, string $key, string $source): void
|
||||
{
|
||||
$this->report($channel, $channel->case_intake ?? CaseIntakeMode::Off, $case, $outcome, $message, $key, $source);
|
||||
}
|
||||
|
||||
/**
|
||||
* A request e-mail of a portal: a request case with the dates and the reference (A2), when the channel creates
|
||||
* cases; otherwise only recorded.
|
||||
@@ -292,6 +301,11 @@ public static function reference(BlockedPeriod $block): ?string
|
||||
return trim((string) $fromRules);
|
||||
}
|
||||
|
||||
// A block of a portal's API (channel events, Architektur V2) carries the reference like a booking mail.
|
||||
if ($block->source === 'api' && str_starts_with((string) $block->external_uid, 'api:')) {
|
||||
return substr((string) $block->external_uid, 4);
|
||||
}
|
||||
|
||||
if ($block->source === 'email' && str_starts_with((string) $block->external_uid, 'email:')) {
|
||||
$reference = substr((string) $block->external_uid, 6);
|
||||
|
||||
@@ -315,7 +329,7 @@ public function caseByReference(Channel $channel, string $reference): ?Booking
|
||||
}
|
||||
|
||||
$linked = $channel->blockedPeriods()->withTrashed()->whereNotNull('linked_booking_id')
|
||||
->whereIn('external_uid', [$reference, 'email:'.$reference])->value('linked_booking_id');
|
||||
->whereIn('external_uid', [$reference, 'email:'.$reference, 'api:'.$reference])->value('linked_booking_id');
|
||||
|
||||
return $linked !== null ? Booking::query()->find($linked) : null;
|
||||
}
|
||||
@@ -346,6 +360,9 @@ private function certainty(BlockedPeriod $block, ?string $reference): ?string
|
||||
if ($block->source === 'email') {
|
||||
return $reference === null ? 'Buchungsmail ohne Buchungsnummer – ohne Nummer lässt sich ein späterer Kalendereintrag nicht sicher zuordnen.' : null;
|
||||
}
|
||||
if ($block->source === 'api') {
|
||||
return $reference === null ? 'Portalmeldung ohne Buchungsnummer – keinem Vorgang sicher zuzuordnen.' : null;
|
||||
}
|
||||
|
||||
// Classified by the landlord as a reservation booked at this portal.
|
||||
if ($block->occupation_kind === OccupationKind::Reservation && $block->reviewed_at !== null && $block->origin_channel_id === $block->channel_id) {
|
||||
|
||||
Reference in new issue
Block a user