Skip to content

Commit

Permalink
Provide DocumentValidated event for error interception
Browse files Browse the repository at this point in the history
  • Loading branch information
eudj1n committed Dec 5, 2023
1 parent 1b25f08 commit 99c9298
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,7 @@ class TestCmsController extends Controller
- `Mitwork\Kalkan\Events\DocumentRejected` - документ отклонен;
- `Mitwork\Kalkan\Events\DocumentRequested` - документ запрошен;
- `Mitwork\Kalkan\Events\DocumentSaved` - документ сохранен;
- `Mitwork\Kalkan\Events\DocumentValidated` - подпись документа проверена;
- `Mitwork\Kalkan\Events\DocumentSigned` - документ подписан;
- `Mitwork\Kalkan\Events\RequestProcessed` - запрос обработан;
- `Mitwork\Kalkan\Events\RequestRejected` - запрос отклонен;
Expand Down
30 changes: 30 additions & 0 deletions src/Events/DocumentValidated.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Mitwork\Kalkan\Events;

use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Foundation\Events\Dispatchable;

class DocumentValidated
{
use Dispatchable, InteractsWithSockets;

public int|string $id;

public string $content;

public string $signature;

public array $result;

public int|string|null $requestId;

public function __construct($id, $content, $signature, $result, $requestId = null)
{
$this->id = $id;
$this->content = $content;
$this->signature = $signature;
$this->result = $result;
$this->requestId = $requestId;
}
}
5 changes: 4 additions & 1 deletion src/Http/Actions/ProcessContent.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Mitwork\Kalkan\Events\AuthRejected;
use Mitwork\Kalkan\Events\DocumentRejected;
use Mitwork\Kalkan\Events\DocumentSigned;
use Mitwork\Kalkan\Events\DocumentValidated;
use Mitwork\Kalkan\Events\RequestProcessed;
use Mitwork\Kalkan\Events\RequestRejected;
use Mitwork\Kalkan\Http\Requests\ProcessDocumentRequest;
Expand Down Expand Up @@ -103,9 +104,11 @@ public function process(ProcessDocumentRequest $request): JsonResponse
return response()->json(['error' => $message, 'result' => $result], 500);
}

DocumentValidated::dispatch($signedDocument['id'], $original['data'], $signature, $result, $id);

if (! $this->documentService->process($signedDocument['id'], $result)) {

$message = __('kalkan::messages.unable_to_process_document');
$message = $this->documentService->message ?: __('kalkan::messages.unable_to_process_document');

if ($this->documentService->reject($signedDocument['id'], $message)) {
DocumentRejected::dispatch($signedDocument['id'], $message, $result, $id);
Expand Down
16 changes: 16 additions & 0 deletions src/Services/CacheDocumentService.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@

class CacheDocumentService implements DocumentService
{
/**
* Ошибка
*
* @var string|null
*/
public string|null $message = null;

/**
* Добавление документа в кэш
*
Expand Down Expand Up @@ -81,6 +88,15 @@ public function process(string|int $id, array $result = [], DocumentStatus $stat
return false;
}

if ($document['status'] === DocumentStatus::REJECTED) {

if (isset($document['message']) && $document['message']) {
$this->message = $document['message'];
}

return false;
}

$document['status'] = $status;
$document['result'] = $result;
$document['message'] = null;
Expand Down

0 comments on commit 99c9298

Please sign in to comment.