151 lines
5.7 KiB
PHP
151 lines
5.7 KiB
PHP
<?php
|
|
|
|
namespace App\Infrastructure\Reports\Adapters;
|
|
|
|
use App\Application\Reports\DTO\GenerateReportInput;
|
|
use App\Domain\Reports\Models\ReportSnapshot;
|
|
use App\Domain\Reports\ValueObjects\MetrikaConfig;
|
|
use App\Infrastructure\Reports\Services\AutoFillReportPayloadBuilder;
|
|
use App\Infrastructure\Reports\Services\ReportRuntimeService;
|
|
use App\Infrastructure\Reports\Services\ReportSaveOrchestrator;
|
|
use App\Models\Department;
|
|
use App\Models\Report;
|
|
use App\Models\User;
|
|
use App\Services\DateRange;
|
|
use App\Services\DateRangeService;
|
|
use App\Services\SnapshotService;
|
|
use DateTimeImmutable;
|
|
|
|
class LegacyReportServiceAdapter
|
|
{
|
|
public function __construct(
|
|
private readonly SnapshotService $snapshotService,
|
|
private readonly DateRangeService $dateRangeService,
|
|
private readonly AutoFillReportPayloadBuilder $autoFillReportPayloadBuilder,
|
|
private readonly ReportRuntimeService $reportRuntimeService,
|
|
private readonly ReportSaveOrchestrator $reportSaveOrchestrator,
|
|
) {}
|
|
|
|
public function prepareMemoryForHeavySave(): void
|
|
{
|
|
$this->reportRuntimeService->prepareForHeavySave();
|
|
}
|
|
|
|
/**
|
|
* @param array<string, mixed> $payload
|
|
* @throws \Exception
|
|
*/
|
|
public function buildSnapshotFromPayload(array $payload, string $reportType = 'daily'): ReportSnapshot
|
|
{
|
|
$start = $this->dateRangeService->toCarbon($payload['dates'][0] ?? $payload['period_start'] ?? null);
|
|
$end = $this->dateRangeService->toCarbon($payload['dates'][1] ?? $payload['period_end'] ?? null);
|
|
|
|
return new ReportSnapshot(
|
|
departmentId: (int) $payload['departmentId'],
|
|
userId: (int) $payload['userId'],
|
|
actorUserId: isset($payload['actorUserId']) ? (int) $payload['actorUserId'] : null,
|
|
periodStart: new DateTimeImmutable($start->format('Y-m-d H:i:s')),
|
|
periodEnd: new DateTimeImmutable($end->format('Y-m-d H:i:s')),
|
|
status: (string) ($payload['status'] ?? 'draft'),
|
|
autoFill: (bool) ($payload['autoFill'] ?? false),
|
|
metrics: MetrikaConfig::normalizeMetrics((array) ($payload['metrics'] ?? [])),
|
|
observationPatients: $this->normalizeObservationPatients((array) ($payload['observationPatients'] ?? [])),
|
|
unwantedEvents: $this->normalizeUnwantedEvents((array) ($payload['unwantedEvents'] ?? [])),
|
|
reportId: isset($payload['reportId']) ? (int) $payload['reportId'] : null,
|
|
createdAt: isset($payload['created_at']) ? new DateTimeImmutable((string) $payload['created_at']) : null,
|
|
sentAt: isset($payload['sent_at']) ? new DateTimeImmutable((string) $payload['sent_at']) : null,
|
|
reportType: $reportType,
|
|
);
|
|
}
|
|
|
|
public function buildSnapshotFromInput(GenerateReportInput $input): ReportSnapshot
|
|
{
|
|
if ($input->rawPayload !== null) {
|
|
return $this->buildSnapshotFromPayload([
|
|
...$input->rawPayload,
|
|
'actorUserId' => $input->actorUserId,
|
|
'autoFill' => $input->autoFill,
|
|
], $input->reportType);
|
|
}
|
|
|
|
return $input->toSnapshot();
|
|
}
|
|
|
|
public function createPatientSnapshots(Report $report, User $user, ReportSnapshot $snapshot, bool $autoFill): void
|
|
{
|
|
$this->snapshotService->createPatientSnapshots(
|
|
report: $report,
|
|
user: $user,
|
|
dates: [
|
|
$snapshot->periodStart->getTimestamp(),
|
|
$snapshot->periodEnd->getTimestamp(),
|
|
],
|
|
fillableAuto: $autoFill,
|
|
);
|
|
}
|
|
|
|
public function syncCalculatedMetrics(Report $report, User $user, ReportSnapshot $snapshot): void
|
|
{
|
|
$this->reportSaveOrchestrator->syncCalculatedMetrics(
|
|
$report,
|
|
$user,
|
|
[
|
|
'dates' => [
|
|
$snapshot->periodStart->getTimestamp(),
|
|
$snapshot->periodEnd->getTimestamp(),
|
|
],
|
|
],
|
|
);
|
|
}
|
|
|
|
public function finalizeStoredReport(Report $report): void
|
|
{
|
|
$this->reportSaveOrchestrator->finalizeStoredReport($report);
|
|
}
|
|
|
|
public function saveLethalMetricFromSnapshots(Report $report): void
|
|
{
|
|
$this->reportSaveOrchestrator->saveLethalMetricFromSnapshots($report);
|
|
}
|
|
|
|
public function clearCacheAfterReportCreation(User $user, Report $report): void
|
|
{
|
|
$this->reportRuntimeService->clearCacheAfterReportCreation($user, $report);
|
|
}
|
|
|
|
public function buildAutoFillPayload(User $user, Department $department, DateRange $dateRange): array
|
|
{
|
|
return $this->autoFillReportPayloadBuilder->build($user, $department, $dateRange);
|
|
}
|
|
|
|
/**
|
|
* @param array<int, array<string, mixed>> $patients
|
|
* @return array<int, array<string, mixed>>
|
|
*/
|
|
private function normalizeObservationPatients(array $patients): array
|
|
{
|
|
return array_values(array_map(static function (array $patient): array {
|
|
return [
|
|
'medical_history_id' => $patient['medical_history_id'] ?? $patient['id'] ?? null,
|
|
'department_patient_id' => $patient['department_patient_id'] ?? null,
|
|
'comment' => $patient['comment'] ?? null,
|
|
];
|
|
}, $patients));
|
|
}
|
|
|
|
/**
|
|
* @param array<int, array<string, mixed>> $events
|
|
* @return array<int, array<string, mixed>>
|
|
*/
|
|
private function normalizeUnwantedEvents(array $events): array
|
|
{
|
|
return array_values(array_map(static function (array $event): array {
|
|
return [
|
|
'title' => $event['title'] ?? '',
|
|
'comment' => $event['comment'] ?? '',
|
|
'is_visible' => (bool) ($event['is_visible'] ?? true),
|
|
];
|
|
}, $events));
|
|
}
|
|
}
|