47 lines
2.5 KiB
PHP
47 lines
2.5 KiB
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
/** Payment facts stay in desk_payments; the existing encrypted events preserve documentary uses. */
|
|
class DeskPaymentEvidence
|
|
{
|
|
/** @param array<string,mixed> $input
|
|
* @return array<string,mixed>|null
|
|
*/
|
|
public function snapshot(string $caseId, string $requestId, array $input): ?array
|
|
{
|
|
if (! ($input['document_link_id'] ?? null) && ! ($input['document_id'] ?? null)) {
|
|
return null;
|
|
}
|
|
$document = DB::table('desk_documents as l')->join('documents as d', 'd.id', '=', 'l.document_id')
|
|
->where('l.case_id', $caseId)->whereNull('d.deleted_at')
|
|
->where(fn ($q) => $q->whereNull('l.request_id')->orWhere('l.request_id', $requestId))
|
|
->when($input['document_link_id'] ?? null, fn ($q, $id) => $q->where('l.id', $id))
|
|
->when($input['document_id'] ?? null, fn ($q, $id) => $q->where('d.id', $id))
|
|
->orderBy('l.created_at')->orderBy('l.id')->first(['d.*', 'l.id as link_id', 'l.source_reference', 'l.category', 'l.stage', 'l.valid_until as declared_valid_until']);
|
|
abort_unless($document !== null, 422, 'Seleccione um comprovativo associado a este pedido ou ao seu dossier.');
|
|
app(VerifiedDocumentBinary::class)->authorizeOpportunityAttachment($document);
|
|
|
|
return ['link_id' => $document->link_id, 'document_id' => $document->id, 'file_name' => $document->file_name, 'document_version' => (int) $document->version_no,
|
|
'sha256' => trim($document->sha256), 'source_reference' => $document->source_reference, 'category' => $document->category, 'stage' => $document->stage,
|
|
'valid_until' => $document->declared_valid_until, 'status_at_association' => $document->status, 'scan_at_association' => $document->malware_scan_status,
|
|
'scope' => 'registered_document_metadata_not_binary_or_payment_confirmation'];
|
|
}
|
|
|
|
/** @return array<string,array<string,mixed>> */
|
|
public function forRequest(string $requestId): array
|
|
{
|
|
$current = [];
|
|
foreach (DB::table('desk_events')->where('request_id', $requestId)->whereIn('action', ['payment_declared', 'payment_document_linked'])->orderBy('event_order')->get(['payload']) as $event) {
|
|
$payload = app(VirtualDesk::class)->decode($event->payload);
|
|
if (is_string($payload['payment_id'] ?? null) && is_array($payload['document_evidence'] ?? null)) {
|
|
$current[$payload['payment_id']] = $payload['document_evidence'];
|
|
}
|
|
}
|
|
|
|
return $current;
|
|
}
|
|
}
|